Klamp't  0.9.0
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 
11 class TiXmlElement;
12 
13 namespace Klampt {
14  using namespace std;
15 
20 class RobotModel;
21 class WorldModel;
22 class SimRobotController;
23 class Simulator;
24 
56 {
57  public:
58  SensorBase();
59  virtual ~SensorBase() {}
60  virtual const char* Type() const { return "SensorBase"; }
62  virtual void Simulate(SimRobotController* robot,Simulator* sim) {}
64  virtual void SimulateKinematic(RobotModel& robot,WorldModel& world) {}
66  virtual void Advance(double dt) {}
68  virtual void Reset() {}
69  virtual bool ReadState(File& f);
70  virtual bool WriteState(File& f) const;
72  virtual void MeasurementNames(vector<string>& names) const { names.resize(0); }
74  virtual void GetMeasurements(vector<double>& values) const { values.resize(0); }
78  virtual void SetMeasurements(const vector<double>& values) { }
80  virtual void GetInternalState(vector<double>& state) const { }
82  virtual void SetInternalState(const vector<double>& state) { }
84  virtual map<string,string> Settings() const;
86  virtual bool GetSetting(const string& name,string& str) const;
89  virtual bool SetSetting(const string& name,const string& str);
92  virtual void DrawGL(const RobotModel& robot,const vector<double>& measurements) {}
93 
94  string name;
95  double rate;
96  bool enabled;
97 };
98 
99 
100 
101 
112 {
113  public:
114  void MakeDefault(RobotModel* robot);
115  bool LoadSettings(const char* fn);
116  bool SaveSettings(const char* fn);
117  bool LoadSettings(TiXmlElement* in);
118  void SaveSettings(TiXmlElement* out);
119  bool LoadMeasurements(TiXmlElement* in);
120  void SaveMeasurements(TiXmlElement* out);
121  bool ReadState(File& f);
122  bool WriteState(File& f) const;
123  shared_ptr<SensorBase> GetNamedSensor(const string& name);
124  template <class T>
125  void GetTypedSensors(vector<T*>& sensors);
126  template <class T>
127  T* GetTypedSensor(int index=0);
128  shared_ptr<SensorBase> CreateByType(const char* type) const;
129 
130  vector<shared_ptr<SensorBase> > sensors;
131 };
132 
133 
134 
135 template <class T>
136 void RobotSensors::GetTypedSensors(vector<T*>& _sensors)
137 {
138  _sensors.resize(0);
139  for(size_t i=0;i<sensors.size();i++)
140  if(typeid(T) == typeid(*sensors[i])) _sensors.push_back(dynamic_cast<T*>(sensors[i].get()));
141 }
142 
143 
144 template <class T>
145 T* RobotSensors::GetTypedSensor(int index)
146 {
147  for(size_t i=0;i<sensors.size();i++) {
148  if(typeid(T) == typeid(*sensors[i])) {
149  if(index==0) return dynamic_cast<T*>(sensors[i].get());
150  index--;
151  }
152  }
153  return NULL;
154 }
155 
156 
157 
158 //these macros will help you read in / write out settings
159 #define FILL_SENSOR_SETTING(res,membername) \
160  { \
161  stringstream ss; \
162  ss<<membername; \
163  res[#membername] = ss.str(); \
164  }
165 #define GET_SENSOR_SETTING(membername) \
166  if(name == #membername) { \
167  stringstream ss; \
168  ss << membername; \
169  str = ss.str(); \
170  return true; \
171  }
172 #define SET_SENSOR_SETTING(membername) \
173  if(name == #membername) { \
174  stringstream ss(str); \
175  ss >> membername; \
176  return bool(ss); \
177  }
178 
179 #define FILL_ARRAY_SENSOR_SETTING(res,membername,count) \
180  { \
181  stringstream ss; \
182  for(int _i=0;_i<count;_i++) \
183  ss<<membername[_i]<<" "; \
184  settings[#membername] = ss.str(); \
185  }
186 #define GET_ARRAY_SENSOR_SETTING(membername,count) \
187  if(name == #membername) { \
188  stringstream ss; \
189  for(int _i=0;_i<count;_i++) \
190  ss << membername[_i]<<" "; \
191  str = ss.str(); \
192  return true; \
193  }
194 #define SET_ARRAY_SENSOR_SETTING(membername,count) \
195  if(name == #membername) { \
196  stringstream ss(str); \
197  for(int _i=0;_i<count;_i++) \
198  ss >> membername[_i]; \
199  return bool(ss); \
200  }
201 
202 #define FILL_VECTOR_SENSOR_SETTING(res,membername) \
203  { \
204  stringstream ss; \
205  for(size_t _i=0;_i<membername.size();_i++) \
206  ss<<membername[_i]<<" "; \
207  settings[#membername] = ss.str(); \
208  }
209 #define GET_VECTOR_SENSOR_SETTING(membername) \
210  if(name == #membername) { \
211  stringstream ss; \
212  for(size_t _i=0;_i<membername.size();_i++) \
213  ss << membername[_i]<<" "; \
214  str = ss.str(); \
215  return true; \
216  }
217 #define SET_VECTOR_SENSOR_SETTING(membername) \
218  if(name == #membername) { \
219  stringstream ss(str); \
220  membername.resize(0); \
221  while(ss) { \
222  membername.resize(membername.size()+1); \
223  ss >> membername.back(); \
224  if(ss.fail()) { \
225  membername.resize(membername.size()-1); \
226  return true; \
227  } \
228  if(ss.bad()) return false; \
229  } \
230  return true; \
231  }
232 
233 } //namespace Klampt
234 
235 #endif
virtual void SetMeasurements(const vector< double > &values)
Definition: Sensor.h:78
virtual void Simulate(SimRobotController *robot, Simulator *sim)
Called whenever the sensor is updated from the simulaton.
Definition: Sensor.h:62
A set of sensors for the robot.
Definition: Sensor.h:111
A physical simulator for a WorldModel.
Definition: Simulator.h:69
virtual void Advance(double dt)
Advances to the next time step with duration dt elapsed.
Definition: Sensor.h:66
A class containing information about an ODE-simulated and controlled robot.
Definition: SimRobotController.h:19
A sensor base class. A SensorBase should allow a Controller to both connect to a simulation as well a...
Definition: Sensor.h:55
virtual void GetMeasurements(vector< double > &values) const
Must be overridden to returns a list of all measurements.
Definition: Sensor.h:74
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:82
virtual void Reset()
Should be overridden if the sensor is stateful to reset to an initial state.
Definition: Sensor.h:68
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:80
virtual void MeasurementNames(vector< string > &names) const
Must be overridden to produce a list of names of each measurement.
Definition: Sensor.h:72
The main robot type used in RobotSim.
Definition: Robot.h:83
virtual void SimulateKinematic(RobotModel &robot, WorldModel &world)
Updates the sensor for a kinematic world. Useful for non-simulation debugging.
Definition: Sensor.h:64
virtual void DrawGL(const RobotModel &robot, const vector< double > &measurements)
Definition: Sensor.h:92
Definition: ContactDistance.h:6
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:24