Robotsystem of the Robot Group Leipzig: replaycontroller.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages
- Examples

- Search for |

- File List
- File Members

# replaycontroller.h

Go to the documentation of this file.

    00001 /***************************************************************************
    00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
    00003  *    martius@informatik.uni-leipzig.de                                    *
    00004  *    fhesse@informatik.uni-leipzig.de                                     *
    00005  *    der@informatik.uni-leipzig.de                                        *
    00006  *                                                                         *
    00007  *   This program is free software; you can redistribute it and/or modify  *
    00008  *   it under the terms of the GNU General Public License as published by  *
    00009  *   the Free Software Foundation; either version 2 of the License, or     *
    00010  *   (at your option) any later version.                                   *
    00011  *                                                                         *
    00012  *   This program is distributed in the hope that it will be useful,       *
    00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
    00015  *   GNU General Public License for more details.                          *
    00016  *                                                                         *
    00017  *   You should have received a copy of the GNU General Public License     *
    00018  *   along with this program; if not, write to the                         *
    00019  *   Free Software Foundation, Inc.,                                       *
    00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
    00021  *                                                                         *
    00022  *   $Log: replaycontroller.h,v $
    00023  *   Revision 1.4  2008/04/17 14:54:45  martius
    00024  *   randomGen added, which is a random generator with long period and an
    00025  *    internal state. Each Agent has an instance and passed it to the controller
    00026  *    and the wiring. This is good for
    00027  *   a) repeatability on agent basis,
    00028  *   b) parallel execution as done in ode_robots
    00029  *
    00030  *   Revision 1.3  2007/07/19 15:57:25  martius
    00031  *   nicer output
    00032  *
    00033  *   Revision 1.2  2007/06/21 16:28:22  martius
    00034  *   *** empty log message ***
    00035  *
    00036  *   Revision 1.1  2007/06/14 13:52:54  martius
    00037  *   controller, that just replays a logfile
    00038  *
    00039  *                                                                         *
    00040  *                                                                         *
    00041  ***************************************************************************/
    00042 #ifndef __REPLAYCONTROLLER_H
    00043 #define __REPLAYCONTROLLER_H
    00044
    00045 #include "abstractcontroller.h"
    00046
    00047 /**
    00048  * Controller that replays a file
    00049  */
    00050 class ReplayController : public AbstractController {
    00051 public:
    00052   ReplayController(const char* filename, bool repeat=false)
    00053     : AbstractController("ReplayController", "$Id: replaycontroller.h,v 1.4 2008/04/17 14:54:45 martius Exp $"),
    00054       filename(filename), repeat(repeat) {
    00055
    00056     f=fopen(filename,"r");
    00057     if(!f){
    00058       std::cerr<< "ReplayController: error while opening file " << filename << std::endl;
    00059       exit(1);
    00060     }
    00061     if(!parseDataFileForHeader(f)){
    00062       std::cerr<< "ReplayController: error while seaching for header in file " << filename << std::endl;
    00063       exit(1);
    00064     }
    00065     printf("ReplayController: columns: Senors [%i, %i], Motors [%i, %i]\n",
    00066            sensorStart, sensorEnd, motorStart, motorEnd);
    00067   }
    00068
    00069   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
    00070     //assert(sensornumber == sensorEnd - sensorStart + 1);
    00071     assert(motornumber  == motorEnd - motorStart + 1);
    00072   }
    00073
    00074   virtual int getSensorNumber() const { return sensorEnd - sensorStart + 1;};
    00075
    00076   virtual int getMotorNumber() const { return motorEnd - motorStart + 1; };
    00077
    00078   virtual void step(const sensor* sensors, int sensornumber,
    00079                     motor* motors, int motornumber){
    00080     stepNoLearning(sensors,sensornumber, motors, motornumber);
    00081   }
    00082
    00083   virtual void stepNoLearning(const sensor* , int number_sensors,
    00084                               motor* motors, int number_motors){
    00085
    00086     if(!parseDataLine(m,f)){
    00087       if(repeat){
    00088         std::cout << "ReplayController: rewind" << std::endl;
    00089         rewind(f);
    00090       }else
    00091         std::cout << "ReplayController: no datafile in file" << filename << std::endl;
    00092     }else{
    00093       m=m.rows(motorStart, motorEnd);
    00094     }
    00095     m.convertToBuffer(motors, sensorEnd-sensorStart + 1);
    00096   }
    00097
    00098   /**** STOREABLE ****/
    00099   /** stores the controller values to a given file (binary).  */
    00100   virtual bool store(FILE* f) const {return false;}
    00101   /** loads the controller values from a given file (binary). */
    00102   virtual bool restore(FILE* f) {return false;}
    00103
    00104   // inspectable interface
    00105   virtual std::list<iparamkey> getInternalParamNames()const  { return std::list<iparamkey>(); }
    00106   virtual std::list<iparamval> getInternalParams() const { return std::list<iparamval>(); }
    00107
    00108 protected:
    00109
    00110   bool parseDataFileForHeader(FILE* f){
    00111     char buffer[1024];
    00112     int i;
    00113     sensorStart=-1;
    00114     sensorEnd=-1;
    00115     motorStart=-1;
    00116     motorEnd=-1;
    00117
    00118     while(fgets(buffer, 1024, f)) {
    00119       if(buffer[0]=='#' && buffer[1]=='C'){
    00120         // scan line and return
    00121         i=0;
    00122         char* p;
    00123         p=strtok(buffer," ");
    00124         if(!p) return false; // frist one is #C
    00125         while((p=strtok(NULL," "))!=NULL )  {
    00126           if(p[0]=='x' && p[1]=='['){
    00127             if(sensorStart==-1) sensorStart=i;
    00128             sensorEnd=i;
    00129           }
    00130           if(p[0]=='y' && p[1]=='['){
    00131             if(motorStart==-1) motorStart=i;
    00132             motorEnd=i;
    00133           }
    00134           i++;
    00135         }
    00136         return true;
    00137       }
    00138     }
    00139     return false;
    00140   }
    00141
    00142   static bool isEmpty(const char* c){
    00143     const char* p = c;
    00144     bool foundsomething = false;
    00145     while(*p != 0){
    00146       if(*p > ' ') foundsomething = true;
    00147       p++;
    00148     }
    00149     return !foundsomething;
    00150   }
    00151
    00152
    00153   static bool check4Number(const char* c){
    00154     const char* p = c;
    00155     while(*p != 0){
    00156       if(*p >= '0' && *p <= '9') return true;
    00157       p++;
    00158     }
    00159     return false;
    00160   }
    00161
    00162   static bool parseDataLine(matrix::Matrix& data, FILE* f){
    00163     char buffer[1024];
    00164     int i;
    00165     double dat[1024];
    00166     while(fgets(buffer, 1024, f)){
    00167       if(buffer[0]=='#' || isEmpty(buffer)){
    00168         continue;
    00169       }else{
    00170         i=0;
    00171         char* p;
    00172         p=strtok(buffer," ");
    00173         if(!p) return false;
    00174         dat[i] = atof(p);
    00175         i++;
    00176         while((p=strtok(NULL," "))!=NULL )  {
    00177           if(!check4Number(p)) continue;
    00178           dat[i] = atof(p);
    00179           i++;
    00180         };
    00181         data.set(i,1,dat);
    00182         return true;
    00183       }
    00184     };
    00185     return false;
    00186   }
    00187
    00188
    00189
    00190 protected:
    00191   int sensorStart;
    00192   int sensorEnd;
    00193   int motorStart;
    00194   int motorEnd;
    00195   matrix::Matrix m;
    00196   const char* filename;
    00197   FILE* f;
    00198   bool repeat;
    00199
    00200 };
    00201
    00202 #endif