g2o
Loading...
Searching...
No Matches
simulator2d_segment.cpp
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#include <cstdlib>
28#include <fstream>
29#include <iostream>
30#include <sstream>
31
34#include "g2o/stuff/sampler.h"
35#include "simulator2d.h"
36
37using namespace g2o;
38using namespace std;
39
40int main(int argc, char** argv) {
41 CommandArgs arg;
42 int nlandmarks;
43 int nSegments;
44 int simSteps;
45 double worldSize;
46 bool hasOdom;
47 bool hasPoseSensor;
48 bool hasPointSensor;
49 bool hasPointBearingSensor;
50 bool hasSegmentSensor;
51 bool hasCompass;
52 bool hasGPS;
53
54 int segmentGridSize;
55 double minSegmentLength, maxSegmentLength;
56
57 std::string outputFilename;
58 arg.param("simSteps", simSteps, 100, "number of simulation steps");
59 arg.param("nLandmarks", nlandmarks, 1000, "number of landmarks");
60 arg.param("nSegments", nSegments, 1000, "number of segments");
61 arg.param("segmentGridSize", segmentGridSize, 50,
62 "number of cells of the grid where to align the segments");
63 arg.param("minSegmentLength", minSegmentLength, 0.5,
64 "minimal Length of a segment in the world");
65 arg.param("maxSegmentLength", maxSegmentLength, 3,
66 "maximal Length of a segment in the world");
67 arg.param("worldSize", worldSize, 25.0, "size of the world");
68 arg.param("hasOdom", hasOdom, false, "the robot has an odometry");
69 arg.param("hasPointSensor", hasPointSensor, false,
70 "the robot has a point sensor");
71 arg.param("hasPointBearingSensor", hasPointBearingSensor, false,
72 "the robot has a point bearing sensor");
73 arg.param("hasPoseSensor", hasPoseSensor, false,
74 "the robot has a pose sensor");
75 arg.param("hasCompass", hasCompass, false, "the robot has a compass");
76 arg.param("hasGPS", hasGPS, false, "the robot has a GPS");
77 arg.param("hasSegmentSensor", hasSegmentSensor, false,
78 "the robot has a segment sensor");
79 arg.paramLeftOver("graph-output", outputFilename, "simulator_out.g2o",
80 "graph file which will be written", true);
81
82 arg.parseArgs(argc, argv);
83
84 std::mt19937 generator;
85 OptimizableGraph graph;
86 World world(&graph);
87 for (int i = 0; i < nlandmarks; i++) {
89 double x = sampleUniform(-.5, .5, &generator) * worldSize;
90 double y = sampleUniform(-.5, .5, &generator) * worldSize;
91 landmark->vertex()->setEstimate(Vector2d(x, y));
92 world.addWorldObject(landmark);
93 }
94
95 cerr << "nSegments = " << nSegments << endl;
96
97 for (int i = 0; i < nSegments; i++) {
99 int ix = sampleUniform(-segmentGridSize, segmentGridSize, &generator);
100 int iy = sampleUniform(-segmentGridSize, segmentGridSize, &generator);
101 int ith = sampleUniform(0, 3, &generator);
102 double th = (M_PI / 2) * ith;
103 th = atan2(sin(th), cos(th));
104 double xc = ix * (worldSize / segmentGridSize);
105 double yc = iy * (worldSize / segmentGridSize);
106
107 double l2 = sampleUniform(minSegmentLength, maxSegmentLength, &generator);
108
109 double x1 = xc + cos(th) * l2;
110 double y1 = yc + sin(th) * l2;
111 double x2 = xc - cos(th) * l2;
112 double y2 = yc - sin(th) * l2;
113
114 segment->vertex()->setEstimateP1(Vector2d(x1, y1));
115 segment->vertex()->setEstimateP2(Vector2d(x2, y2));
116 world.addWorldObject(segment);
117 }
118
119 Robot2D robot(&world, "myRobot");
120 world.addRobot(&robot);
121
122 stringstream ss;
123 ss << "-ws" << worldSize;
124 ss << "-nl" << nlandmarks;
125 ss << "-steps" << simSteps;
126
127 if (hasOdom) {
128 SensorOdometry2D* odometrySensor = new SensorOdometry2D("odometry");
129 robot.addSensor(odometrySensor);
130 Matrix3d odomInfo = odometrySensor->information();
131 odomInfo.setIdentity();
132 odomInfo *= 100;
133 odomInfo(2, 2) = 1000;
134 odometrySensor->setInformation(odomInfo);
135 ss << "-odom";
136 }
137
138 if (hasPoseSensor) {
139 SensorPose2D* poseSensor = new SensorPose2D("poseSensor");
140 robot.addSensor(poseSensor);
141 Matrix3d poseInfo = poseSensor->information();
142 poseInfo.setIdentity();
143 poseInfo *= 100;
144 poseInfo(2, 2) = 1000;
145 poseSensor->setInformation(poseInfo);
146 ss << "-pose";
147 }
148
149 if (hasPointSensor) {
150 SensorPointXY* pointSensor = new SensorPointXY("pointSensor");
151 robot.addSensor(pointSensor);
152 Matrix2d pointInfo = pointSensor->information();
153 pointInfo.setIdentity();
154 pointInfo *= 100;
155 pointSensor->setInformation(pointInfo);
156 ss << "-pointXY";
157 }
158
159 if (hasPointBearingSensor) {
160 SensorPointXYBearing* bearingSensor =
161 new SensorPointXYBearing("bearingSensor");
162 robot.addSensor(bearingSensor);
163 bearingSensor->setInformation(bearingSensor->information() * 1000);
164 ss << "-pointBearing";
165 }
166
167 if (hasSegmentSensor) {
168 SensorSegment2D* segmentSensor = new SensorSegment2D("segmentSensorSensor");
169 segmentSensor->setMaxRange(3);
170 segmentSensor->setMinRange(.1);
171 robot.addSensor(segmentSensor);
172 segmentSensor->setInformation(segmentSensor->information() * 1000);
173
174 SensorSegment2DLine* segmentSensorLine =
175 new SensorSegment2DLine("segmentSensorSensorLine");
176 segmentSensorLine->setMaxRange(3);
177 segmentSensorLine->setMinRange(.1);
178 robot.addSensor(segmentSensorLine);
179 Matrix2d m = segmentSensorLine->information();
180 m = m * 1000;
181 m(0, 0) *= 10;
182 segmentSensorLine->setInformation(m);
183
184 SensorSegment2DPointLine* segmentSensorPointLine =
185 new SensorSegment2DPointLine("segmentSensorSensorPointLine");
186 segmentSensorPointLine->setMaxRange(3);
187 segmentSensorPointLine->setMinRange(.1);
188 robot.addSensor(segmentSensorPointLine);
189 Matrix3d m3 = segmentSensorPointLine->information();
190 m3 = m3 * 1000;
191 m3(3, 3) *= 10;
192 segmentSensorPointLine->setInformation(m3);
193
194 ss << "-segment2d";
195 }
196
197 robot.move(SE2());
198 double pStraight = 0.7;
199 SE2 moveStraight;
200 moveStraight.setTranslation(Vector2d(1., 0.));
201 double pLeft = 0.15;
202 SE2 moveLeft;
203 moveLeft.setRotation(Rotation2Dd(M_PI / 2));
204 // double pRight=0.15;
205 SE2 moveRight;
206 moveRight.setRotation(Rotation2Dd(-M_PI / 2));
207
208 for (int i = 0; i < simSteps; i++) {
209 cerr << "m";
210 SE2 move;
211 SE2 pose = robot.pose();
212 double dtheta = -100;
213 Vector2d dt;
214 if (pose.translation().x() < -.5 * worldSize) {
215 dtheta = 0;
216 }
217
218 if (pose.translation().x() > .5 * worldSize) {
219 dtheta = -M_PI;
220 }
221
222 if (pose.translation().y() < -.5 * worldSize) {
223 dtheta = M_PI / 2;
224 }
225
226 if (pose.translation().y() > .5 * worldSize) {
227 dtheta = -M_PI / 2;
228 }
229 if (dtheta < -M_PI) {
230 // select a random move of the robot
231 double sampled = sampleUniform(0., 1., &generator);
232 if (sampled < pStraight)
233 move = moveStraight;
234 else if (sampled < pStraight + pLeft)
235 move = moveLeft;
236 else
237 move = moveRight;
238 } else {
239 double mTheta = dtheta - pose.rotation().angle();
240 move.setRotation(Rotation2Dd(mTheta));
241 if (move.rotation().angle() < std::numeric_limits<double>::epsilon()) {
242 move.setTranslation(Vector2d(1., 0.));
243 }
244 }
245 robot.relativeMove(move);
246 // do a sense
247 cerr << "s";
248 robot.sense();
249 }
250 // string fname=outputFilename + ss.str() + ".g2o";
251 ofstream testStream(outputFilename.c_str());
252 graph.save(testStream);
253
254 return 0;
255}
const InformationType & information()
Definition simulator.h:256
void setInformation(const InformationType &information_)
Definition simulator.h:251
Command line parsing of argc and argv.
bool parseArgs(int argc, char **argv, bool exitOnError=true)
void paramLeftOver(const std::string &name, std::string &p, const std::string &defValue, const std::string &desc, bool optional=false)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
void setMinRange(double minRange_)
void setMaxRange(double maxRange_)
represent SE2
Definition se2.h:43
const Vector2 & translation() const
translational component
Definition se2.h:57
void setRotation(const Rotation2D &R_)
Definition se2.h:62
void setTranslation(const Vector2 &t_)
Definition se2.h:58
const Rotation2D & rotation() const
rotational component
Definition se2.h:61
bool addRobot(BaseRobot *robot)
Definition simulator.cpp:82
bool addWorldObject(BaseWorldObject *worldObject)
Definition simulator.cpp:91
int main()
Definition gicp_demo.cpp:44
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition sampler.cpp:35
WorldObject< VertexPointXY > WorldObjectPointXY
WorldObject< VertexSegment2D > WorldObjectSegment2D
Robot< WorldObjectSE2 > Robot2D
Definition jet.h:876
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.