g2o
simulator.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 G. Grisetti, R. Kuemmerle, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_SIMULATOR_
28 #define G2O_SIMULATOR_
29 
30 #include <string>
31 #include <set>
32 #include "g2o/config.h"
34 #include "g2o/stuff/sampler.h"
35 #include "g2o_simulator_api.h"
36 
37 namespace g2o {
38 
39 class World;
40 class BaseSensor;
41 
43  public:
44  BaseWorldObject(World* world_=0) {_world = world_; _vertex=0;}
45  virtual ~BaseWorldObject();
46  void setWorld(World* world_) {_world = world_;}
47  World* world() {return _world;}
49  OptimizableGraph::Vertex* vertex() {return _vertex;}
50  virtual void setVertex(OptimizableGraph::Vertex* vertex_);
51  protected:
55 };
56 
57 template <class VertexType_>
58 class WorldObject: public BaseWorldObject, VertexType_{
59  public:
61  typedef VertexType_ VertexType;
62  typedef typename VertexType_::EstimateType EstimateType;
63  WorldObject(World* world_=0): BaseWorldObject(world_){
64  _vertex = new VertexType();
65  }
66  virtual void setVertex(OptimizableGraph::Vertex* vertex_){
67  if(! dynamic_cast<VertexType*>(vertex_))
68  return;
69  _vertex = vertex_;
70  }
71 
72  VertexType* vertex() {
73  if (! _vertex) return 0;
74  return dynamic_cast<VertexType*>(_vertex);
75  }
76 };
77 
79  public:
80  BaseRobot(World* world_, const std::string& name_){_world = world_; _name = name_; }
81  void setWorld(World* world_) {_world = world_;}
82  World* world() {return _world;}
83  const std::string& name() const {return _name;}
85  bool addSensor(BaseSensor* sensor);
86  const std::set<BaseSensor*> sensors() {return _sensors;}
87  virtual void sense();
88  protected:
90  std::set<BaseSensor*> _sensors;
91  std::string _name;
92 };
93 
94 template <class RobotPoseObject>
95 class Robot: public BaseRobot{
96  public:
98  typedef RobotPoseObject PoseObject;
99  typedef std::list<PoseObject*> TrajectoryType;
100  typedef typename PoseObject::VertexType VertexType;
101  typedef typename PoseObject::EstimateType PoseType;
102 
103  Robot(World* world_, const std::string& name_): BaseRobot(world_, name_){}
104  virtual void relativeMove(const PoseType& movement_) {
105  _pose=_pose*movement_;
106  move(_pose);
107  }
108 
109  virtual void move(const PoseType& pose_) {
110  _pose=pose_;
111  if (world()) {
112  PoseObject* po=new PoseObject();
113  po->vertex()->setEstimate(_pose);
114  world()->addWorldObject(po);
115  _trajectory.push_back(po);
116  }
117  }
118 
119  TrajectoryType& trajectory() {return _trajectory;}
120  const PoseType& pose() const {return _pose;}
121  protected:
122  TrajectoryType _trajectory;
123  PoseType _pose;
124 };
125 
127  public:
128  BaseSensor(const std::string& name_){ _name = name_;}
129  inline BaseRobot* robot() {return _robot;}
130  inline void setRobot(BaseRobot* robot_) {_robot = robot_;}
131  World* world();
133  std::vector<Parameter*> parameters() {return _parameters;}
134  virtual void sense() = 0;
135  virtual void addParameters() {}
136  protected:
137  std::string _name;
138  std::vector<Parameter*> _parameters;
140 };
141 
142 template <class RobotType_, class EdgeType_>
143 class UnarySensor: public BaseSensor {
144  public:
146  typedef RobotType_ RobotType;
150  typedef EdgeType_ EdgeType;
151  typedef typename EdgeType::InformationType InformationType;
152 
153  UnarySensor(const std::string& name): BaseSensor(name) {
154  _information.setIdentity();
155  }
156 
157  void setInformation(const InformationType& information_ ) {
158  _information = information_ ;
159  _sampler.setDistribution(_information.inverse());
160  }
161 
162  const InformationType& information() {return _information; }
163 
164  virtual void sense() {
165  _robotPoseObject = 0;
166  // set the robot pose
167  if (! robot())
168  return;
169 
170  RobotType* r =dynamic_cast<RobotType*>(robot());
171  if (!r)
172  return;
173 
174  if(! r->trajectory().empty())
175  _robotPoseObject = *(r->trajectory().rbegin());
176 
177  if (! world() || ! graph())
178  return;
179 
180  EdgeType* e=mkEdge();
181  if (e) {
183  addNoise(e);
184  graph()->addEdge(e);
185  }
186  }
187 
188 
189  protected:
190  PoseObject* _robotPoseObject;
191  InformationType _information;
192 
193  EdgeType* mkEdge(){
194  PoseVertexType* robotVertex = (PoseVertexType*)_robotPoseObject->vertex();
195  EdgeType* e = new EdgeType();
196  e->vertices()[0]=robotVertex;
197  e->information().setIdentity();
198  return e;
199  }
201  virtual void addNoise(EdgeType*){};
202 };
203 
204 template <class RobotType_, class EdgeType_, class WorldObjectType_>
205 class BinarySensor: public BaseSensor {
206  public:
208  typedef RobotType_ RobotType;
212  typedef EdgeType_ EdgeType;
213  typedef WorldObjectType_ WorldObjectType;
215  typedef typename EdgeType::InformationType InformationType;
216 
217  BinarySensor(const std::string& name): BaseSensor(name) {
218  _information.setIdentity();
219  }
220 
221  void setInformation(const InformationType& information_ ) {
222  _information = information_ ;
223  _sampler.setDistribution(_information.inverse());
224  }
225 
226  const InformationType& information() {return _information; }
227 
228  virtual void sense() {
229  _robotPoseObject = 0;
230  // set the robot pose
231  if (! robot())
232  return;
233 
234  RobotType* r =dynamic_cast<RobotType*>(robot());
235  if (!r)
236  return;
237 
238  if(! r->trajectory().empty())
239  _robotPoseObject = *(r->trajectory().rbegin());
240 
241  if (! world() || ! graph())
242  return;
243 
244  // naive search. just for initial testing
245  for(std::set<BaseWorldObject*>::iterator it=world()->objects().begin(); it!=world()->objects().end(); it++){
246  WorldObjectType * wo = dynamic_cast<WorldObjectType*>(*it);
247  if (wo){
248  EdgeType* e=mkEdge(wo);
249  if (e) {
251  addNoise(e);
252  graph()->addEdge(e);
253  }
254  }
255  }
256  }
257 
258 
259  protected:
260  PoseObject* _robotPoseObject;
261  InformationType _information;
262 
263  EdgeType* mkEdge(WorldObjectType* object){
264  PoseVertexType* robotVertex = (PoseVertexType*)_robotPoseObject->vertex();
265  EdgeType* e = new EdgeType();
266  e->vertices()[0]=robotVertex;
267  e->vertices()[1]=object->vertex();
268  e->information().setIdentity();
269  return e;
270  }
272  virtual void addNoise(EdgeType*){};
273 };
274 
276 {
277  public:
278  World(OptimizableGraph* graph_) {_graph = graph_; _runningId=0; _paramId=0;}
279  OptimizableGraph* graph() {return _graph;}
280  bool addRobot(BaseRobot* robot);
281  bool addWorldObject(BaseWorldObject* worldObject);
282  bool addParameter(Parameter* p);
283 
284  std::set<BaseWorldObject*>& objects() {return _objects;}
285  std::set<BaseRobot*>& robots() {return _robots; }
286  protected:
287  std::set<BaseWorldObject*> _objects;
288  std::set<BaseRobot*> _robots;
291  int _paramId;
292 };
293 
294 } // end namespace
295 
296 #endif
RobotType::PoseObject::VertexType PoseVertexType
Definition: simulator.h:149
RobotType_ RobotType
Definition: simulator.h:146
WorldObjectType::VertexType VertexType
Definition: simulator.h:214
const Vertex * vertex(size_t i) const
Definition: hyper_graph.h:186
World(OptimizableGraph *graph_)
Definition: simulator.h:278
virtual void sense()
Definition: simulator.h:164
RobotType::PoseObject::VertexType PoseVertexType
Definition: simulator.h:211
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: simulator.h:97
const std::set< BaseSensor * > sensors()
Definition: simulator.h:86
std::string _name
Definition: simulator.h:91
std::set< BaseWorldObject * > _objects
Definition: simulator.h:287
RobotPoseObject PoseObject
Definition: simulator.h:98
std::set< BaseRobot * > _robots
Definition: simulator.h:288
InformationType _information
Definition: simulator.h:261
virtual void addParameters()
Definition: simulator.h:135
virtual bool setMeasurementFromState()
std::vector< Parameter * > parameters()
Definition: simulator.h:133
BaseWorldObject(World *world_=0)
Definition: simulator.h:44
void setWorld(World *world_)
Definition: simulator.h:46
virtual bool setMeasurementFromState()
GaussianSampler< typename EdgeType::ErrorVector, InformationType > _sampler
Definition: simulator.h:200
OptimizableGraph * graph()
Definition: simulator.h:279
GaussianSampler< typename EdgeType::ErrorVector, InformationType > _sampler
Definition: simulator.h:271
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: simulator.h:60
RobotType::TrajectoryType TrajectoryType
Definition: simulator.h:210
EdgeType * mkEdge()
Definition: simulator.h:193
RobotType::PoseObject PoseObject
Definition: simulator.h:209
BaseRobot(World *world_, const std::string &name_)
Definition: simulator.h:80
World * _world
Definition: simulator.h:89
EdgeType::InformationType InformationType
Definition: simulator.h:215
std::list< PoseObject * > TrajectoryType
Definition: simulator.h:99
WorldObjectType_ WorldObjectType
Definition: simulator.h:213
PoseType _pose
Definition: simulator.h:123
std::set< BaseSensor * > _sensors
Definition: simulator.h:90
BaseSensor(const std::string &name_)
Definition: simulator.h:128
OptimizableGraph * _graph
Definition: simulator.h:52
OptimizableGraph::Vertex * _vertex
Definition: simulator.h:53
const std::string & name() const
Definition: simulator.h:83
BinarySensor(const std::string &name)
Definition: simulator.h:217
const VertexContainer & vertices() const
Definition: hyper_graph.h:178
virtual void sense()
Definition: simulator.h:228
void setInformation(const InformationType &information_)
Definition: simulator.h:157
const InformationType & information()
Definition: simulator.h:226
VertexType_::EstimateType EstimateType
Definition: simulator.h:62
World * world()
Definition: simulator.h:82
TrajectoryType & trajectory()
Definition: simulator.h:119
PoseObject * _robotPoseObject
Definition: simulator.h:260
#define G2O_SIMULATOR_API
std::string _name
Definition: simulator.h:137
virtual void addNoise(EdgeType *)
Definition: simulator.h:201
EdgeType_ EdgeType
Definition: simulator.h:212
InformationType _information
Definition: simulator.h:191
BaseRobot * _robot
Definition: simulator.h:139
VertexType_ VertexType
Definition: simulator.h:61
virtual void addNoise(EdgeType *)
Definition: simulator.h:272
UnarySensor(const std::string &name)
Definition: simulator.h:153
PoseObject::EstimateType PoseType
Definition: simulator.h:101
RobotType_ RobotType
Definition: simulator.h:208
EdgeType::InformationType InformationType
Definition: simulator.h:151
RobotType::PoseObject PoseObject
Definition: simulator.h:147
A general case Vertex for optimization.
TrajectoryType _trajectory
Definition: simulator.h:122
const PoseType & pose() const
Definition: simulator.h:120
virtual void relativeMove(const PoseType &movement_)
Definition: simulator.h:104
PoseObject * _robotPoseObject
Definition: simulator.h:190
Robot(World *world_, const std::string &name_)
Definition: simulator.h:103
BaseRobot * robot()
Definition: simulator.h:129
EIGEN_STRONG_INLINE const InformationType & information() const
information matrix of the constraint
Definition: base_edge.h:67
RobotType::TrajectoryType TrajectoryType
Definition: simulator.h:148
std::set< BaseWorldObject * > & objects()
Definition: simulator.h:284
std::set< BaseRobot * > & robots()
Definition: simulator.h:285
void setWorld(World *world_)
Definition: simulator.h:81
virtual void setVertex(OptimizableGraph::Vertex *vertex_)
Definition: simulator.h:66
std::vector< Parameter * > _parameters
Definition: simulator.h:138
PoseObject::VertexType VertexType
Definition: simulator.h:100
EdgeType_ EdgeType
Definition: simulator.h:150
WorldObject(World *world_=0)
Definition: simulator.h:63
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: simulator.h:145
VertexType * vertex()
Definition: simulator.h:72
virtual void move(const PoseType &pose_)
Definition: simulator.h:109
int _paramId
Definition: simulator.h:291
void setRobot(BaseRobot *robot_)
Definition: simulator.h:130
OptimizableGraph::Vertex * vertex()
Definition: simulator.h:49
int _runningId
Definition: simulator.h:290
void setInformation(const InformationType &information_)
Definition: simulator.h:221
EdgeType * mkEdge(WorldObjectType *object)
Definition: simulator.h:263
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the graph
Definition: protocol.txt:7
const InformationType & information()
Definition: simulator.h:162
OptimizableGraph * _graph
Definition: simulator.h:289