Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
replaycontroller.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __REPLAYCONTROLLER_H
25 #define __REPLAYCONTROLLER_H
26 
27 #include "abstractcontroller.h"
28 
29 /**
30  * Controller that replays a file
31  */
33 public:
34  ReplayController(const char* filename, bool repeat=false)
35  : AbstractController("ReplayController", "1.0"),
36  filename(filename), repeat(repeat) {
37 
38  f=fopen(filename,"r");
39  if(!f){
40  std::cerr<< "ReplayController: error while opening file " << filename << std::endl;
41  exit(1);
42  }
44  std::cerr<< "ReplayController: error while seaching for header in file " << filename << std::endl;
45  exit(1);
46  }
47  printf("ReplayController: columns: Senors [%i, %i], Motors [%i, %i]\n",
49  }
50 
51  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
52  //assert(sensornumber == sensorEnd - sensorStart + 1);
53  assert(motornumber == motorEnd - motorStart + 1);
54  }
55 
56  virtual int getSensorNumber() const { return sensorEnd - sensorStart + 1;};
57 
58  virtual int getMotorNumber() const { return motorEnd - motorStart + 1; };
59 
60  virtual void step(const sensor* sensors, int sensornumber,
61  motor* motors, int motornumber){
62  stepNoLearning(sensors,sensornumber, motors, motornumber);
63  }
64 
65  virtual void stepNoLearning(const sensor* , int number_sensors,
66  motor* motors, int number_motors){
67 
68  if(!parseDataLine(m,f)){
69  if(repeat){
70  std::cout << "ReplayController: rewind" << std::endl;
71  rewind(f);
72  }else
73  std::cout << "ReplayController: no datafile in file" << filename << std::endl;
74  }else{
76  }
77  m.convertToBuffer(motors, motorEnd-motorStart + 1);
78  }
79 
80  /**** STOREABLE ****/
81  /** stores the controller values to a given file (binary). */
82  virtual bool store(FILE* f) const {return false;}
83  /** loads the controller values from a given file (binary). */
84  virtual bool restore(FILE* f) {return false;}
85 
86  // inspectable interface
87  virtual std::list<iparamkey> getInternalParamNames()const { return std::list<iparamkey>(); }
88  virtual std::list<iparamval> getInternalParams() const { return std::list<iparamval>(); }
89 
90 protected:
91 
93  char buffer[1024];
94  int i;
95  sensorStart=-1;
96  sensorEnd=-1;
97  motorStart=-1;
98  motorEnd=-1;
99 
100  while(fgets(buffer, 1024, f)) {
101  if(buffer[0]=='#' && buffer[1]=='C'){
102  // scan line and return
103  i=0;
104  char* p;
105  p=strtok(buffer," ");
106  if(!p) return false; // frist one is #C
107  while((p=strtok(NULL," "))!=NULL ) {
108  if(p[0]=='x' && p[1]=='['){
109  if(sensorStart==-1) sensorStart=i;
110  sensorEnd=i;
111  }
112  if(p[0]=='y' && p[1]=='['){
113  if(motorStart==-1) motorStart=i;
114  motorEnd=i;
115  }
116  i++;
117  }
118  return true;
119  }
120  }
121  return false;
122  }
123 
124  static bool isEmpty(const char* c){
125  const char* p = c;
126  bool foundsomething = false;
127  while(*p != 0){
128  if(*p > ' ') foundsomething = true;
129  p++;
130  }
131  return !foundsomething;
132  }
133 
134 
135  static bool check4Number(const char* c){
136  const char* p = c;
137  while(*p != 0){
138  if(*p >= '0' && *p <= '9') return true;
139  p++;
140  }
141  return false;
142  }
143 
144  static bool parseDataLine(matrix::Matrix& data, FILE* f){
145  char buffer[1024];
146  int i;
147  double dat[1024];
148  while(fgets(buffer, 1024, f)){
149  if(buffer[0]=='#' || isEmpty(buffer)){
150  continue;
151  }else{
152  i=0;
153  char* p;
154  p=strtok(buffer," ");
155  if(!p) return false;
156  dat[i] = atof(p);
157  i++;
158  while((p=strtok(NULL," "))!=NULL ) {
159  if(!check4Number(p)) continue;
160  dat[i] = atof(p);
161  i++;
162  };
163  data.set(i,1,dat);
164  return true;
165  }
166  };
167  return false;
168  }
169 
170 
171 
172 protected:
176  int motorEnd;
178  const char* filename;
179  FILE* f;
180  bool repeat;
181 
182 };
183 
184 #endif
Matrix type.
Definition: matrix.h:65
virtual int getMotorNumber() const
Definition: replaycontroller.h:58
ReplayController(const char *filename, bool repeat=false)
Definition: replaycontroller.h:34
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
matrix::Matrix m
Definition: replaycontroller.h:177
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual bool store(FILE *f) const
stores the controller values to a given file (binary).
Definition: replaycontroller.h:82
int convertToBuffer(D *buffer, I len) const
stores the content of the matrix (row-wise) in the given buffer
Definition: matrix.cpp:160
double sensor
Definition: abstractcontroller.h:48
FILE * f
Definition: replaycontroller.h:179
int sensorStart
Definition: replaycontroller.h:173
virtual bool restore(FILE *f)
loads the controller values from a given file (binary).
Definition: replaycontroller.h:84
Controller that replays a file.
Definition: replaycontroller.h:32
bool repeat
Definition: replaycontroller.h:180
int motorStart
Definition: replaycontroller.h:175
Matrix rows(I startindex, I endindex) const
Definition: matrix.cpp:116
virtual void stepNoLearning(const sensor *, int number_sensors, motor *motors, int number_motors)
performs one step without learning.
Definition: replaycontroller.h:65
static bool parseDataLine(matrix::Matrix &data, FILE *f)
Definition: replaycontroller.h:144
bool parseDataFileForHeader(FILE *f)
Definition: replaycontroller.h:92
virtual std::list< iparamval > getInternalParams() const
Definition: replaycontroller.h:88
int sensorEnd
Definition: replaycontroller.h:174
virtual std::list< iparamkey > getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: replaycontroller.h:87
void set(I _m, I _n, const D *_data=0)
sets the size of the matrix and maybe the data if given (row-wise).
Definition: matrix.cpp:147
virtual int getSensorNumber() const
Definition: replaycontroller.h:56
static bool check4Number(const char *c)
Definition: replaycontroller.h:135
int motorEnd
Definition: replaycontroller.h:176
static bool isEmpty(const char *c)
Definition: replaycontroller.h:124
double motor
Definition: abstractcontroller.h:49
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)
initialisation of the controller with the given sensor/ motornumber Must be called before use...
Definition: replaycontroller.h:51
virtual void step(const sensor *sensors, int sensornumber, motor *motors, int motornumber)
performs one step (includes learning).
Definition: replaycontroller.h:60
int c
Definition: hexapod.cpp:56
const char * filename
Definition: replaycontroller.h:178
double atof(const char *)