g2o
Loading...
Searching...
No Matches
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 <list>
31#include <set>
32#include <string>
33
34#include "g2o/config.h"
35#include "g2o/stuff/sampler.h"
37#include "g2o_simulator_api.h"
38
39namespace g2o {
40
41class World;
42class BaseSensor;
43
45 public:
46 BaseWorldObject(World* world_ = 0) {
47 _world = world_;
48 _vertex = 0;
49 }
50 virtual ~BaseWorldObject();
51 void setWorld(World* world_) { _world = world_; }
52 World* world() const { return _world; }
53 OptimizableGraph* graph();
54 OptimizableGraph::Vertex* vertex() { return _vertex; }
55 virtual void setVertex(OptimizableGraph::Vertex* vertex_);
56
57 protected:
61};
62
63template <class VertexType_>
64class WorldObject : public BaseWorldObject, VertexType_ {
65 public:
67 typedef VertexType_ VertexType;
68 typedef typename VertexType_::EstimateType EstimateType;
69 WorldObject(World* world_ = 0) : BaseWorldObject(world_) {
70 _vertex = new VertexType();
71 }
72 virtual void setVertex(OptimizableGraph::Vertex* vertex_) {
73 if (!dynamic_cast<VertexType*>(vertex_)) return;
74 _vertex = vertex_;
75 }
76
78 if (!_vertex) return 0;
79 return dynamic_cast<VertexType*>(_vertex);
80 }
81};
82
84 public:
85 BaseRobot(World* world_, const std::string& name_) {
86 _world = world_;
87 _name = name_;
88 }
89 void setWorld(World* world_) { _world = world_; }
90 World* world() const { return _world; }
91 const std::string& name() const { return _name; }
92 OptimizableGraph* graph() const;
93 bool addSensor(BaseSensor* sensor);
94 const std::set<BaseSensor*> sensors() { return _sensors; }
95 virtual void sense();
96
97 protected:
99 std::set<BaseSensor*> _sensors;
100 std::string _name;
101};
102
104 public:
106 _graph = graph_;
107 _runningId = 0;
108 _paramId = 0;
109 }
110 OptimizableGraph* graph() { return _graph; }
111 bool addRobot(BaseRobot* robot);
112 bool addWorldObject(BaseWorldObject* worldObject);
113 bool addParameter(Parameter* p);
114
115 std::set<BaseWorldObject*>& objects() { return _objects; }
116 std::set<BaseRobot*>& robots() { return _robots; }
117
118 protected:
119 std::set<BaseWorldObject*> _objects;
120 std::set<BaseRobot*> _robots;
124};
125
126template <class RobotPoseObject>
127class Robot : public BaseRobot {
128 public:
130 typedef RobotPoseObject PoseObject;
131 typedef std::list<PoseObject*> TrajectoryType;
132 typedef typename PoseObject::VertexType VertexType;
133 typedef typename PoseObject::EstimateType PoseType;
134
135 Robot(World* world_, const std::string& name_) : BaseRobot(world_, name_) {}
136 virtual void relativeMove(const PoseType& movement_) {
137 _pose = _pose * movement_;
138 move(_pose);
139 }
140
141 virtual void move(const PoseType& pose_) {
142 _pose = pose_;
143 if (world()) {
144 PoseObject* po = new PoseObject();
145 po->vertex()->setEstimate(_pose);
146 world()->addWorldObject(po);
147 _trajectory.push_back(po);
148 }
149 }
150
152 const PoseType& pose() const { return _pose; }
153
154 protected:
157};
158
160 public:
161 BaseSensor(const std::string& name_) { _name = name_; }
162 inline BaseRobot* robot() { return _robot; }
163 inline void setRobot(BaseRobot* robot_) { _robot = robot_; }
164 World* world() const;
165 OptimizableGraph* graph() const;
166 const std::vector<Parameter*>& parameters() { return _parameters; }
167 virtual void sense() = 0;
168 virtual void addParameters() {}
169
170 protected:
171 std::string _name;
172 std::vector<Parameter*> _parameters;
174};
175
176template <class RobotType_, class EdgeType_>
177class UnarySensor : public BaseSensor {
178 public:
180 typedef RobotType_ RobotType;
181 typedef typename RobotType::PoseObject PoseObject;
182 typedef typename RobotType::TrajectoryType TrajectoryType;
183 typedef typename RobotType::PoseObject::VertexType PoseVertexType;
184 typedef EdgeType_ EdgeType;
185 typedef typename EdgeType::InformationType InformationType;
186
187 UnarySensor(const std::string& name) : BaseSensor(name) {
188 _information.setIdentity();
189 }
190
191 void setInformation(const InformationType& information_) {
192 _information = information_;
194 }
195
197
198 virtual void sense() {
200 // set the robot pose
201 if (!robot()) return;
202
203 RobotType* r = dynamic_cast<RobotType*>(robot());
204 if (!r) return;
205
206 if (!r->trajectory().empty())
207 _robotPoseObject = *(r->trajectory().rbegin());
208
209 if (!world() || !graph()) return;
210
211 EdgeType* e = mkEdge();
212 if (e) {
213 e->setMeasurementFromState();
214 addNoise(e);
215 graph()->addEdge(e);
216 }
217 }
218
219 protected:
222
224 PoseVertexType* robotVertex = (PoseVertexType*)_robotPoseObject->vertex();
225 EdgeType* e = new EdgeType();
226 e->vertices()[0] = robotVertex;
227 e->information().setIdentity();
228 return e;
229 }
231 virtual void addNoise(EdgeType*){};
232};
233
234template <class RobotType_, class EdgeType_, class WorldObjectType_>
235class BinarySensor : public BaseSensor {
236 public:
238 typedef RobotType_ RobotType;
239 typedef typename RobotType::PoseObject PoseObject;
240 typedef typename RobotType::TrajectoryType TrajectoryType;
241 typedef typename RobotType::PoseObject::VertexType PoseVertexType;
242 typedef EdgeType_ EdgeType;
243 typedef WorldObjectType_ WorldObjectType;
244 typedef typename WorldObjectType::VertexType VertexType;
245 typedef typename EdgeType::InformationType InformationType;
246
247 BinarySensor(const std::string& name) : BaseSensor(name) {
248 _information.setIdentity();
249 }
250
251 void setInformation(const InformationType& information_) {
252 _information = information_;
254 }
255
257
258 virtual void sense() {
260 // set the robot pose
261 if (!robot()) return;
262
263 RobotType* r = dynamic_cast<RobotType*>(robot());
264 if (!r) return;
265
266 if (!r->trajectory().empty())
267 _robotPoseObject = *(r->trajectory().rbegin());
268
269 if (!world() || !graph()) return;
270
271 // naive search. just for initial testing
272 for (std::set<BaseWorldObject*>::iterator it = world()->objects().begin();
273 it != world()->objects().end(); ++it) {
274 WorldObjectType* wo = dynamic_cast<WorldObjectType*>(*it);
275 if (wo) {
276 EdgeType* e = mkEdge(wo);
277 if (e) {
278 e->setMeasurementFromState();
279 addNoise(e);
280 graph()->addEdge(e);
281 }
282 }
283 }
284 }
285
286 protected:
289
291 PoseVertexType* robotVertex = (PoseVertexType*)_robotPoseObject->vertex();
292 EdgeType* e = new EdgeType();
293 e->vertices()[0] = robotVertex;
294 e->vertices()[1] = object->vertex();
295 e->information().setIdentity();
296 return e;
297 }
299 virtual void addNoise(EdgeType*){};
300};
301
302} // namespace g2o
303
304#endif
BaseRobot(World *world_, const std::string &name_)
Definition simulator.h:85
void setWorld(World *world_)
Definition simulator.h:89
World * world() const
Definition simulator.h:90
World * _world
Definition simulator.h:98
std::string _name
Definition simulator.h:100
const std::string & name() const
Definition simulator.h:91
const std::set< BaseSensor * > sensors()
Definition simulator.h:94
std::set< BaseSensor * > _sensors
Definition simulator.h:99
BaseRobot * _robot
Definition simulator.h:173
OptimizableGraph * graph() const
Definition simulator.cpp:76
std::vector< Parameter * > _parameters
Definition simulator.h:172
virtual void sense()=0
World * world() const
Definition simulator.cpp:71
BaseSensor(const std::string &name_)
Definition simulator.h:161
std::string _name
Definition simulator.h:171
const std::vector< Parameter * > & parameters()
Definition simulator.h:166
virtual void addParameters()
Definition simulator.h:168
BaseRobot * robot()
Definition simulator.h:162
void setRobot(BaseRobot *robot_)
Definition simulator.h:163
World * world() const
Definition simulator.h:52
BaseWorldObject(World *world_=0)
Definition simulator.h:46
OptimizableGraph::Vertex * _vertex
Definition simulator.h:59
OptimizableGraph::Vertex * vertex()
Definition simulator.h:54
OptimizableGraph * _graph
Definition simulator.h:58
void setWorld(World *world_)
Definition simulator.h:51
InformationType _information
Definition simulator.h:288
EdgeType::InformationType InformationType
Definition simulator.h:245
WorldObjectType::VertexType VertexType
Definition simulator.h:244
virtual void sense()
Definition simulator.h:258
WorldObjectType_ WorldObjectType
Definition simulator.h:243
RobotType::TrajectoryType TrajectoryType
Definition simulator.h:240
EdgeType * mkEdge(WorldObjectType *object)
Definition simulator.h:290
PoseObject * _robotPoseObject
Definition simulator.h:287
RobotType::PoseObject::VertexType PoseVertexType
Definition simulator.h:241
const InformationType & information()
Definition simulator.h:256
RobotType::PoseObject PoseObject
Definition simulator.h:239
RobotType_ RobotType
Definition simulator.h:238
void setInformation(const InformationType &information_)
Definition simulator.h:251
GaussianSampler< typename EdgeType::ErrorVector, InformationType > _sampler
Definition simulator.h:298
BinarySensor(const std::string &name)
Definition simulator.h:247
virtual void addNoise(EdgeType *)
Definition simulator.h:299
EdgeType_ EdgeType
Definition simulator.h:242
void setDistribution(const CovarianceType &cov)
Definition sampler.h:53
A general case Vertex for optimization.
Robot(World *world_, const std::string &name_)
Definition simulator.h:135
PoseType _pose
Definition simulator.h:156
TrajectoryType _trajectory
Definition simulator.h:155
PoseObject::VertexType VertexType
Definition simulator.h:132
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition simulator.h:129
virtual void move(const PoseType &pose_)
Definition simulator.h:141
const PoseType & pose() const
Definition simulator.h:152
TrajectoryType & trajectory()
Definition simulator.h:151
virtual void relativeMove(const PoseType &movement_)
Definition simulator.h:136
RobotPoseObject PoseObject
Definition simulator.h:130
std::list< PoseObject * > TrajectoryType
Definition simulator.h:131
PoseObject::EstimateType PoseType
Definition simulator.h:133
EdgeType * mkEdge()
Definition simulator.h:223
EdgeType::InformationType InformationType
Definition simulator.h:185
InformationType _information
Definition simulator.h:221
virtual void addNoise(EdgeType *)
Definition simulator.h:231
UnarySensor(const std::string &name)
Definition simulator.h:187
PoseObject * _robotPoseObject
Definition simulator.h:220
void setInformation(const InformationType &information_)
Definition simulator.h:191
RobotType::PoseObject PoseObject
Definition simulator.h:181
GaussianSampler< typename EdgeType::ErrorVector, InformationType > _sampler
Definition simulator.h:230
RobotType::PoseObject::VertexType PoseVertexType
Definition simulator.h:183
virtual void sense()
Definition simulator.h:198
RobotType::TrajectoryType TrajectoryType
Definition simulator.h:182
RobotType_ RobotType
Definition simulator.h:180
const InformationType & information()
Definition simulator.h:196
EdgeType_ EdgeType
Definition simulator.h:184
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition simulator.h:66
virtual void setVertex(OptimizableGraph::Vertex *vertex_)
Definition simulator.h:72
WorldObject(World *world_=0)
Definition simulator.h:69
VertexType_ VertexType
Definition simulator.h:67
VertexType_::EstimateType EstimateType
Definition simulator.h:68
VertexType * vertex()
Definition simulator.h:77
std::set< BaseWorldObject * > _objects
Definition simulator.h:119
std::set< BaseWorldObject * > & objects()
Definition simulator.h:115
OptimizableGraph * _graph
Definition simulator.h:121
std::set< BaseRobot * > _robots
Definition simulator.h:120
std::set< BaseRobot * > & robots()
Definition simulator.h:116
int _runningId
Definition simulator.h:122
bool addWorldObject(BaseWorldObject *worldObject)
Definition simulator.cpp:91
OptimizableGraph * graph()
Definition simulator.h:110
World(OptimizableGraph *graph_)
Definition simulator.h:105
#define G2O_SIMULATOR_API
virtual bool addEdge(HyperGraph::Edge *e)