g2o
Loading...
Searching...
No Matches
Functions
test_simulator3d.cpp File Reference
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "g2o/core/optimizable_graph.h"
#include "g2o/stuff/command_args.h"
#include "g2o/stuff/sampler.h"
#include "simulator3d.h"
Include dependency graph for test_simulator3d.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 43 of file test_simulator3d.cpp.

43 {
44 CommandArgs arg;
45 int nlandmarks;
46 int simSteps;
47 double worldSize;
48 bool hasOdom;
49 bool hasPoseSensor;
50 bool hasPointSensor;
51 bool hasPointDepthSensor;
52 bool hasPointDisparitySensor;
53 bool hasCompass;
54 bool hasGPS;
55
56 std::string outputFilename;
57 arg.param("simSteps", simSteps, 100, "number of simulation steps");
58 arg.param("nLandmarks", nlandmarks, 1000, "number of landmarks");
59 arg.param("worldSize", worldSize, 25.0, "size of the world");
60 arg.param("hasOdom", hasOdom, false, "the robot has an odometry");
61 arg.param("hasPointSensor", hasPointSensor, false,
62 "the robot has a point sensor");
63 arg.param("hasPointDepthSensor", hasPointDepthSensor, false,
64 "the robot has a point sensor");
65 arg.param("hasPointDisparitySensor", hasPointDisparitySensor, false,
66 "the robot has a point sensor");
67 arg.param("hasPoseSensor", hasPoseSensor, false,
68 "the robot has a pose sensor");
69 arg.param("hasCompass", hasCompass, false, "the robot has a compass");
70 arg.param("hasGPS", hasGPS, false, "the robot has a GPS");
71 arg.paramLeftOver("graph-output", outputFilename, "simulator_out.g2o",
72 "graph file which will be written", true);
73
74 arg.parseArgs(argc, argv);
75
76 std::mt19937 generator;
77 OptimizableGraph graph;
78 World world(&graph);
79 for (int i = 0; i < nlandmarks; i++) {
81 double x = sampleUniform(-.5, .5, &generator) * worldSize;
82 double y = sampleUniform(-.5, .5, &generator) * worldSize;
83 double z = sampleUniform(-.5, .5);
84 landmark->vertex()->setEstimate(Vector3d(x, y, z));
85 world.addWorldObject(landmark);
86 }
87 Robot3D robot(&world, "myRobot");
88 world.addRobot(&robot);
89
90 stringstream ss;
91 ss << "-ws" << worldSize;
92 ss << "-nl" << nlandmarks;
93 ss << "-steps" << simSteps;
94
95 if (hasOdom) {
96 SensorOdometry3D* odometrySensor = new SensorOdometry3D("odometry");
97 robot.addSensor(odometrySensor);
98 ss << "-odom";
99 }
100
101 if (hasPointSensor) {
102 SensorPointXYZ* pointSensor = new SensorPointXYZ("pointSensor");
103 pointSensor->setFov(M_PI / 4);
104 robot.addSensor(pointSensor);
105 Eigen::Isometry3d cameraPose;
106 Eigen::Matrix3d R;
107 R << 0, 0, 1, -1, 0, 0, 0, -1, 0;
108 pointSensor->setMaxRange(2.);
109 cameraPose = R;
110 cameraPose.translation() = Vector3d(0., 0., 0.3);
111 pointSensor->offsetParam()->setOffset(cameraPose);
112 ss << "-pointXYZ";
113 }
114
115 if (hasPointDisparitySensor) {
116 SensorPointXYZDisparity* disparitySensor =
117 new SensorPointXYZDisparity("disparitySensor");
118 disparitySensor->setFov(M_PI / 4);
119 disparitySensor->setMinRange(0.5);
120 disparitySensor->setMaxRange(2.);
121 robot.addSensor(disparitySensor);
122 Eigen::Isometry3d cameraPose;
123 Eigen::Matrix3d R;
124 R << 0, 0, 1, -1, 0, 0, 0, -1, 0;
125 cameraPose = R;
126 cameraPose.translation() = Vector3d(0., 0., 0.3);
127 disparitySensor->offsetParam()->setOffset(cameraPose);
128 ss << "-disparity";
129 }
130
131 if (hasPointDepthSensor) {
132 SensorPointXYZDepth* depthSensor = new SensorPointXYZDepth("depthSensor");
133 depthSensor->setFov(M_PI / 4);
134 depthSensor->setMinRange(0.5);
135 depthSensor->setMaxRange(2.);
136 robot.addSensor(depthSensor);
137 Eigen::Isometry3d cameraPose;
138 Eigen::Matrix3d R;
139 R << 0, 0, 1, -1, 0, 0, 0, -1, 0;
140 cameraPose = R;
141 cameraPose.translation() = Vector3d(0., 0., 0.3);
142 depthSensor->offsetParam()->setOffset(cameraPose);
143 ss << "-depth";
144 }
145
146 if (hasPoseSensor) {
147 SensorPose3D* poseSensor = new SensorPose3D("poseSensor");
148 robot.addSensor(poseSensor);
149 poseSensor->setMaxRange(5);
150 ss << "-pose";
151 }
152
153#ifdef _POSE_PRIOR_SENSOR
154 SensorSE3Prior posePriorSensor("posePriorSensor");
155 robot.addSensor(&posePriorSensor);
156 {
157 Eigen::Isometry3d cameraPose;
158 Eigen::Matrix3d R;
159 R << 0, 0, 1, -1, 0, 0, 0, -1, 0;
160 cameraPose = R;
161 cameraPose.translation() = Vector3d(0., 0., 0.3);
162 posePriorSensor.offsetParam()->setOffset(cameraPose);
163 }
164#endif
165
166#ifdef _POSE_SENSOR_OFFSET
167 SensorPose3DOffset poseSensor("poseSensor");
168 poseSensor.setFov(M_PI / 4);
169 poseSensor.setMinRange(0.5);
170 poseSensor.setMaxRange(5);
171 robot.addSensor(&poseSensor);
172 if (0) {
173 Eigen::Isometry3d cameraPose;
174 Eigen::Matrix3d R;
175 R << 0, 0, 1, -1, 0, 0, 0, -1, 0;
176 cameraPose = R;
177 cameraPose.translation() = Vector3d(0., 0., 0.3);
178 poseSensor.offsetParam1()->setOffset(cameraPose);
179 poseSensor.offsetParam2()->setOffset(cameraPose);
180 }
181#endif
182
183 robot.move(Eigen::Isometry3d::Identity());
184 double pStraight = 0.7;
185 Eigen::Isometry3d moveStraight = Eigen::Isometry3d::Identity();
186 moveStraight.translation() = Vector3d(1., 0., 0.);
187 double pLeft = 0.15;
188 Eigen::Isometry3d moveLeft = Eigen::Isometry3d::Identity();
189 moveLeft = AngleAxisd(M_PI / 2, Vector3d::UnitZ());
190 // double pRight=0.15;
191 Eigen::Isometry3d moveRight = Eigen::Isometry3d::Identity();
192 moveRight = AngleAxisd(-M_PI / 2, Vector3d::UnitZ());
193
194 Eigen::Matrix3d dtheta = Eigen::Matrix3d::Identity();
195 for (int i = 0; i < simSteps; i++) {
196 bool boundariesReached = true;
197 cerr << "m";
198 Vector3d dt;
199 const Eigen::Isometry3d& pose = robot.pose();
200 if (pose.translation().x() < -.5 * worldSize) {
201 dtheta = AngleAxisd(0, Vector3d::UnitZ());
202 } else if (pose.translation().x() > .5 * worldSize) {
203 dtheta = AngleAxisd(-M_PI, Vector3d::UnitZ());
204 } else if (pose.translation().y() < -.5 * worldSize) {
205 dtheta = AngleAxisd(M_PI / 2, Vector3d::UnitZ());
206 } else if (pose.translation().y() > .5 * worldSize) {
207 dtheta = AngleAxisd(-M_PI / 2, Vector3d::UnitZ());
208 } else {
209 boundariesReached = false;
210 }
211
212 Eigen::Isometry3d move = Eigen::Isometry3d::Identity();
213 if (boundariesReached) {
214 Eigen::Matrix3d mTheta = pose.rotation().inverse() * dtheta;
215 move = mTheta;
216 AngleAxisd aa(mTheta);
217 if (aa.angle() < std::numeric_limits<double>::epsilon()) {
218 move.translation() = Vector3d(1., 0., 0.);
219 }
220 } else {
221 double sampled = sampleUniform();
222 if (sampled < pStraight)
223 move = moveStraight;
224 else if (sampled < pStraight + pLeft)
225 move = moveLeft;
226 else
227 move = moveRight;
228 }
229
230 // select a random move of the robot
231 robot.relativeMove(move);
232 // do a sense
233 cerr << "s";
234 robot.sense();
235 }
236 // string fname=outputFilename + ss.str() + ".g2o";
237 // ofstream testStream(fname.c_str());
238 ofstream testStream(outputFilename.c_str());
239 graph.save(testStream);
240}
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 setOffset(const Isometry3 &offset_=Isometry3::Identity())
void setMinRange(double minRange_)
void setMaxRange(double maxRange_)
ParameterSE3Offset * offsetParam()
ParameterSE3Offset * offsetParam()
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition sampler.cpp:35
WorldObject< VertexPointXYZ > WorldObjectTrackXYZ
Robot< WorldObjectSE3 > Robot3D
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.

References g2o::World::addRobot(), g2o::World::addWorldObject(), g2o::SensorPointXYZ::offsetParam(), g2o::SensorPointXYZDepth::offsetParam(), g2o::SensorPointXYZDisparity::offsetParam(), g2o::SensorSE3Prior::offsetParam(), g2o::SensorPose3DOffset::offsetParam1(), g2o::SensorPose3DOffset::offsetParam2(), g2o::CommandArgs::param(), g2o::CommandArgs::paramLeftOver(), g2o::CommandArgs::parseArgs(), g2o::sampleUniform(), g2o::OptimizableGraph::save(), g2o::PointSensorParameters::setFov(), g2o::PointSensorParameters::setMaxRange(), g2o::PointSensorParameters::setMinRange(), and g2o::ParameterSE3Offset::setOffset().