Klamp't  0.8.1
Sensor.h
1 #ifndef CONTROL_SENSORS_H
2 #define CONTROL_SENSORS_H
3 
4 #include <KrisLibrary/math/vector.h>
5 #include <map>
6 #include <vector>
7 #include <memory>
8 #include <string>
9 #include <typeinfo>
10 using namespace std;
11 
16 class Robot;
17 class RobotWorld;
19 class WorldSimulation;
20 class TiXmlElement;
21 
52 {
53  public:
54  SensorBase();
55  virtual ~SensorBase() {}
56  virtual const char* Type() const { return "SensorBase"; }
58  virtual void Simulate(ControlledRobotSimulator* robot,WorldSimulation* sim) {}
60  virtual void SimulateKinematic(Robot& robot,RobotWorld& world) {}
62  virtual void Advance(double dt) {}
64  virtual void Reset() {}
65  virtual bool ReadState(File& f);
66  virtual bool WriteState(File& f) const;
68  virtual void MeasurementNames(vector<string>& names) const { names.resize(0); }
70  virtual void GetMeasurements(vector<double>& values) const { values.resize(0); }
74  virtual void SetMeasurements(const vector<double>& values) { }
76  virtual void GetInternalState(vector<double>& state) const { }
78  virtual void SetInternalState(const vector<double>& state) { }
80  virtual map<string,string> Settings() const;
82  virtual bool GetSetting(const string& name,string& str) const;
85  virtual bool SetSetting(const string& name,const string& str);
88  virtual void DrawGL(const Robot& robot,const vector<double>& measurements) {}
89 
90  string name;
91  double rate;
92 };
93 
94 
95 
96 
107 {
108  public:
109  void MakeDefault(Robot* robot);
110  bool LoadSettings(const char* fn);
111  bool SaveSettings(const char* fn);
112  bool LoadSettings(TiXmlElement* in);
113  void SaveSettings(TiXmlElement* out);
114  bool LoadMeasurements(TiXmlElement* in);
115  void SaveMeasurements(TiXmlElement* out);
116  bool ReadState(File& f);
117  bool WriteState(File& f) const;
118  shared_ptr<SensorBase> GetNamedSensor(const string& name);
119  template <class T>
120  void GetTypedSensors(vector<T*>& sensors);
121  template <class T>
122  T* GetTypedSensor(int index=0);
123 
124  vector<shared_ptr<SensorBase> > sensors;
125 };
126 
127 
128 
129 template <class T>
130 void RobotSensors::GetTypedSensors(vector<T*>& _sensors)
131 {
132  _sensors.resize(0);
133  for(size_t i=0;i<sensors.size();i++)
134  if(typeid(T) == typeid(*sensors[i])) _sensors.push_back(dynamic_cast<T*>(sensors[i].get()));
135 }
136 
137 
138 template <class T>
139 T* RobotSensors::GetTypedSensor(int index)
140 {
141  for(size_t i=0;i<sensors.size();i++) {
142  if(typeid(T) == typeid(*sensors[i])) {
143  if(index==0) return dynamic_cast<T*>(sensors[i].get());
144  index--;
145  }
146  }
147  return NULL;
148 }
149 
150 
151 
152 //these macros will help you read in / write out settings
153 #define FILL_SENSOR_SETTING(res,membername) \
154  { \
155  stringstream ss; \
156  ss<<membername; \
157  res[#membername] = ss.str(); \
158  }
159 #define GET_SENSOR_SETTING(membername) \
160  if(name == #membername) { \
161  stringstream ss; \
162  ss << membername; \
163  str = ss.str(); \
164  return true; \
165  }
166 #define SET_SENSOR_SETTING(membername) \
167  if(name == #membername) { \
168  stringstream ss(str); \
169  ss >> membername; \
170  return bool(ss); \
171  }
172 
173 #define FILL_ARRAY_SENSOR_SETTING(res,membername,count) \
174  { \
175  stringstream ss; \
176  for(int _i=0;_i<count;_i++) \
177  ss<<membername[_i]<<" "; \
178  settings[#membername] = ss.str(); \
179  }
180 #define GET_ARRAY_SENSOR_SETTING(membername,count) \
181  if(name == #membername) { \
182  stringstream ss; \
183  for(int _i=0;_i<count;_i++) \
184  ss << membername[_i]<<" "; \
185  str = ss.str(); \
186  return true; \
187  }
188 #define SET_ARRAY_SENSOR_SETTING(membername,count) \
189  if(name == #membername) { \
190  stringstream ss(str); \
191  for(int _i=0;_i<count;_i++) \
192  ss >> membername[_i]; \
193  return bool(ss); \
194  }
195 
196 #define FILL_VECTOR_SENSOR_SETTING(res,membername) \
197  { \
198  stringstream ss; \
199  for(size_t _i=0;_i<membername.size();_i++) \
200  ss<<membername[_i]<<" "; \
201  settings[#membername] = ss.str(); \
202  }
203 #define GET_VECTOR_SENSOR_SETTING(membername) \
204  if(name == #membername) { \
205  stringstream ss; \
206  for(size_t _i=0;_i<membername.size();_i++) \
207  ss << membername[_i]<<" "; \
208  str = ss.str(); \
209  return true; \
210  }
211 #define SET_VECTOR_SENSOR_SETTING(membername) \
212  if(name == #membername) { \
213  stringstream ss(str); \
214  membername.resize(0); \
215  while(ss) { \
216  membername.resize(membername.size()+1); \
217  ss >> membername.back(); \
218  if(ss.fail()) { \
219  membername.resize(membername.size()-1); \
220  return true; \
221  } \
222  if(ss.bad()) return false; \
223  } \
224  return true; \
225  }
226 
227 #endif
virtual void DrawGL(const Robot &robot, const vector< double > &measurements)
Definition: Sensor.h:88
The main robot type used in RobotSim.
Definition: Robot.h:79
A class containing information about an ODE-simulated and controlled robot.
Definition: ControlledSimulator.h:17
The main world class containing multiple robots, objects, and static geometries (terrains). Lights and other viewport information may also be stored here.
Definition: World.h:20
virtual void GetMeasurements(vector< double > &values) const
Must be overridden to returns a list of all measurements.
Definition: Sensor.h:70
virtual void SetInternalState(const vector< double > &state)
Any other state besides measurements/settings that you might want to store. Used in WriteState...
Definition: Sensor.h:78
virtual void Simulate(ControlledRobotSimulator *robot, WorldSimulation *sim)
Called whenever the sensor is updated from the simulaton.
Definition: Sensor.h:58
A sensor base class. A SensorBase should allow a Controller to both connect to a simulation as well a...
Definition: Sensor.h:51
virtual void Reset()
Should be overridden if the sensor is stateful to reset to an initial state.
Definition: Sensor.h:64
virtual void GetInternalState(vector< double > &state) const
Any other state besides measurements/settings that you might want to store. Used in ReadState...
Definition: Sensor.h:76
A set of sensors for the robot.
Definition: Sensor.h:106
virtual void Advance(double dt)
Advances to the next time step with duration dt elapsed.
Definition: Sensor.h:62
virtual void SimulateKinematic(Robot &robot, RobotWorld &world)
Updates the sensor for a kinematic world. Useful for non-simulation debugging.
Definition: Sensor.h:60
A physical simulator for a RobotWorld.
Definition: WorldSimulation.h:67
virtual void MeasurementNames(vector< string > &names) const
Must be overridden to produce a list of names of each measurement.
Definition: Sensor.h:68
virtual void SetMeasurements(const vector< double > &values)
Definition: Sensor.h:74