g2o
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
simulator_3d_line.cpp File Reference
#include <fstream>
#include <iostream>
#include "g2o/core/block_solver.h"
#include "g2o/core/linear_solver.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/stuff/command_args.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/sampler.h"
#include "g2o/types/slam3d/types_slam3d.h"
#include "g2o/types/slam3d_addons/types_slam3d_addons.h"
Include dependency graph for simulator_3d_line.cpp:

Go to the source code of this file.

Classes

struct  SimulatorItem
 
struct  WorldItem
 
struct  Sensor
 
struct  Robot
 
struct  Simulator
 
struct  LineItem
 
struct  LineSensor
 

Typedefs

typedef std::set< WorldItem * > WorldItemSet
 
typedef std::vector< Sensor * > SensorVector
 
typedef std::vector< Robot * > RobotVector
 

Functions

Eigen::Isometry3d sample_noise_from_se3 (const Vector6 &cov)
 
Vector4d sample_noise_from_line (const Vector4d &cov)
 
int main (int argc, char **argv)
 

Typedef Documentation

◆ RobotVector

typedef std::vector<Robot*> RobotVector

Definition at line 146 of file simulator_3d_line.cpp.

◆ SensorVector

typedef std::vector<Sensor*> SensorVector

Definition at line 79 of file simulator_3d_line.cpp.

◆ WorldItemSet

typedef std::set<WorldItem*> WorldItemSet

Definition at line 64 of file simulator_3d_line.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 248 of file simulator_3d_line.cpp.

248 {
249 bool verbose, robustKernel, fixLines, planarMotion, listSolvers;
250 int maxIterations;
251 double lambdaInit;
252 string strSolver;
253 CommandArgs arg;
254 arg.param("i", maxIterations, 10, "perform n iterations");
255 arg.param("v", verbose, false, "verbose output of the optimization process");
256 arg.param("solver", strSolver, "lm_var", "select one specific solver");
257 arg.param("lambdaInit", lambdaInit, 0,
258 "user specified lambda init for levenberg");
259 arg.param("robustKernel", robustKernel, false, "use robust error functions");
260 arg.param("fixLines", fixLines, false,
261 "fix the lines (do localization only)");
262 arg.param("planarMotion", planarMotion, false, "robot moves on a plane");
263 arg.param("listSolvers", listSolvers, false, "list the solvers");
264 arg.parseArgs(argc, argv);
265
267 ParameterSE3Offset* odomOffset = new ParameterSE3Offset();
268 odomOffset->setId(0);
269 g->addParameter(odomOffset);
270
271 OptimizationAlgorithmFactory* solverFactory =
273 OptimizationAlgorithmProperty solverProperty;
274 OptimizationAlgorithm* solver =
275 solverFactory->construct(strSolver, solverProperty);
276 g->setAlgorithm(solver);
277 if (listSolvers) {
278 solverFactory->listSolvers(std::cout);
279 return 0;
280 }
281
282 if (!g->solver()) {
283 std::cout << "Error allocating solver. Allocating \"" << strSolver
284 << "\" failed!" << std::endl;
285 std::cout << "Available solvers: " << std::endl;
286 solverFactory->listSolvers(std::cout);
287 std::cout << "--------------" << std::endl;
288 return 0;
289 }
290
291 std::cout << "Creating simulator" << std::endl;
292 Simulator* sim = new Simulator(g);
293
294 std::cout << "Creating robot" << std::endl;
295 Robot* r = new Robot(g);
296
297 std::cout << "Creating line sensor" << std::endl;
298 Isometry3d sensorPose = Isometry3d::Identity();
299 LineSensor* ls = new LineSensor(r, 0, sensorPose);
300 ls->_nline << 0.001, 0.001, 0.001, 0.0001;
301 // ls->_nline << 1e-9, 1e-9, 1e-9, 1e-9;
302 r->_sensors.push_back(ls);
303 sim->_robots.push_back(r);
304
305 Line3D line;
306 std::cout << "Creating landmark line 1" << std::endl;
307 LineItem* li = new LineItem(g, 1);
308 Vector6 liv;
309 liv << 0.0, 0.0, 5.0, 0.0, 1.0, 0.0;
310 line = Line3D::fromCartesian(liv);
311 static_cast<VertexLine3D*>(li->vertex())->setEstimate(line);
312 li->vertex()->setFixed(fixLines);
313 sim->_world.insert(li);
314
315 std::cout << "Creating landmark line 2" << std::endl;
316 liv << 5.0, 0.0, 0.0, 0.0, 0.0, 1.0;
317 line = Line3D::fromCartesian(liv);
318 li = new LineItem(g, 2);
319 static_cast<VertexLine3D*>(li->vertex())->setEstimate(line);
320 li->vertex()->setFixed(fixLines);
321 sim->_world.insert(li);
322
323 std::cout << "Creating landmark line 3" << std::endl;
324 liv << 0.0, 5.0, 0.0, 1.0, 0.0, 0.0;
325 line = Line3D::fromCartesian(liv);
326 li = new LineItem(g, 3);
327 static_cast<VertexLine3D*>(li->vertex())->setEstimate(line);
328 li->vertex()->setFixed(fixLines);
329 sim->_world.insert(li);
330
331 Quaterniond q, iq;
332 if (planarMotion) {
333 r->_planarMotion = true;
334 r->_nmovecov << 0.01, 0.0025, 1e-9, 0.001, 0.001, 0.025;
335 q = Quaterniond(AngleAxisd(0.2, Vector3d::UnitZ()).toRotationMatrix());
336 iq = Quaterniond(AngleAxisd(-0.2, Vector3d::UnitZ()).toRotationMatrix());
337 } else {
338 r->_planarMotion = false;
339 r->_nmovecov << 0.1, 0.005, 1e-9, 0.001, 0.001, 0.05;
340 q = Quaterniond((AngleAxisd(M_PI / 10, Vector3d::UnitZ()) *
341 AngleAxisd(0.1, Vector3d::UnitY()))
342 .toRotationMatrix());
343 iq = Quaterniond((AngleAxisd(-M_PI / 10, Vector3d::UnitZ()) *
344 AngleAxisd(0.1, Vector3d::UnitY()))
345 .toRotationMatrix());
346 }
347
348 Isometry3d delta = Isometry3d::Identity();
349 sim->_lastVertexId = 4;
350
351 Isometry3d startPose = Isometry3d::Identity();
352 startPose.matrix().block<3, 3>(0, 0) =
353 AngleAxisd(-0.75 * M_PI, Vector3d::UnitZ()).toRotationMatrix();
354 sim->move(0, startPose);
355
356 int k = 20;
357 int l = 2;
358 double delta_t = 0.2;
359 for (int j = 0; j < l; ++j) {
360 Vector3d tr(1.0, 0.0, 0.0);
361 delta.matrix().block<3, 3>(0, 0) = q.toRotationMatrix();
362 if (j == l - 1) {
363 delta.matrix().block<3, 3>(0, 0) = Matrix3d::Identity();
364 }
365 delta.translation() = tr * (delta_t * j);
366 Isometry3d iDelta = delta.inverse();
367 for (int a = 0; a < 2; ++a) {
368 for (int i = 0; i < k; ++i) {
369 std::cout << "m";
370 if (a == 0) {
371 sim->relativeMove(0, delta);
372 } else {
373 sim->relativeMove(0, iDelta);
374 }
375 std::cout << "s";
376 sim->sense(0);
377 }
378 }
379 }
380 for (int j = 0; j < l; ++j) {
381 Vector3d tr(1.0, 0.0, 0.0);
382 delta.matrix().block<3, 3>(0, 0) = iq.toRotationMatrix();
383 if (j == l - 1) {
384 delta.matrix().block<3, 3>(0, 0) = Matrix3d::Identity();
385 }
386 delta.translation() = tr * (delta_t * j);
387 Isometry3d iDelta = delta.inverse();
388 for (int a = 0; a < 2; ++a) {
389 for (int i = 0; i < k; ++i) {
390 std::cout << "m";
391 if (a == 0) {
392 sim->relativeMove(0, delta);
393 } else {
394 sim->relativeMove(0, iDelta);
395 }
396 std::cout << "s";
397 sim->sense(0);
398 }
399 }
400 }
401 std::cout << std::endl;
402
403 ls->_offsetVertex->setFixed(true);
404 OptimizableGraph::Vertex* gauge = g->vertex(4);
405 if (gauge) {
406 gauge->setFixed(true);
407 }
408
409 ofstream osp("line3d.g2o");
410 g->save(osp);
411 std::cout << "Saved graph on file line3d.g2o, use g2o_viewer to work with it."
412 << std::endl;
413
414 return 0;
415}
std::set< BaseSensor * > _sensors
Definition simulator.h:99
Command line parsing of argc and argv.
bool parseArgs(int argc, char **argv, bool exitOnError=true)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
static G2O_TYPES_SLAM3D_ADDONS_API Line3D fromCartesian(const Vector6 &cart)
Definition line3d.h:80
A general case Vertex for optimization.
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
create solvers based on their short name
OptimizationAlgorithm * construct(const std::string &tag, OptimizationAlgorithmProperty &solverProperty) const
static OptimizationAlgorithmFactory * instance()
return the instance
void listSolvers(std::ostream &os) const
list the known solvers into a stream
Generic interface for a non-linear solver operating on a graph.
void setId(int id_)
Definition parameter.cpp:33
void setAlgorithm(OptimizationAlgorithm *algorithm)
OptimizationAlgorithm * solver()
VectorN< 6 > Vector6
Definition eigen_types.h:53
VertexSE3 * _offsetVertex
void sense(int robotIndex)
WorldItemSet _world
void relativeMove(int robotIndex, const Isometry3d &delta)
RobotVector _robots
void move(int robotIndex, const Isometry3d &newRobotPose)
OptimizableGraph::Vertex * vertex()
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
bool addParameter(Parameter *p)
Vertex * vertex(int id)
returns the vertex number id appropriately casted

References Simulator::_lastVertexId, LineSensor::_nline, LineSensor::_offsetVertex, Simulator::_robots, g2o::BaseRobot::_sensors, Simulator::_world, g2o::OptimizableGraph::addParameter(), g2o::OptimizationAlgorithmFactory::construct(), g2o::Line3D::fromCartesian(), g2o::OptimizationAlgorithmFactory::instance(), g2o::OptimizationAlgorithmFactory::listSolvers(), Simulator::move(), g2o::CommandArgs::param(), g2o::CommandArgs::parseArgs(), Simulator::relativeMove(), g2o::OptimizableGraph::save(), Simulator::sense(), g2o::SparseOptimizer::setAlgorithm(), g2o::OptimizableGraph::Vertex::setFixed(), g2o::Parameter::setId(), g2o::SparseOptimizer::solver(), WorldItem::vertex(), and g2o::OptimizableGraph::vertex().

◆ sample_noise_from_line()

Vector4d sample_noise_from_line ( const Vector4d &  cov)

Definition at line 39 of file simulator_3d_line.cpp.

39 {
40 return Vector4d(
41 Sampler::gaussRand(0., cov(0)), Sampler::gaussRand(0., cov(1)),
42 Sampler::gaussRand(0., cov(2)), Sampler::gaussRand(0., cov(3)));
43}
static double gaussRand(double mean, double sigma)
Definition sampler.h:89

References g2o::Sampler::gaussRand().

Referenced by LineSensor::sense().

◆ sample_noise_from_se3()

Eigen::Isometry3d sample_noise_from_se3 ( const Vector6 cov)

Definition at line 20 of file simulator_3d_line.cpp.

20 {
21 double nx = Sampler::gaussRand(0., cov(0));
22 double ny = Sampler::gaussRand(0., cov(1));
23 double nz = Sampler::gaussRand(0., cov(2));
24
25 double nroll = Sampler::gaussRand(0., cov(3));
26 double npitch = Sampler::gaussRand(0., cov(4));
27 double nyaw = Sampler::gaussRand(0., cov(5));
28
29 AngleAxisd aa(AngleAxisd(nyaw, Vector3d::UnitZ()) *
30 AngleAxisd(nroll, Vector3d::UnitX()) *
31 AngleAxisd(npitch, Vector3d::UnitY()));
32
33 Eigen::Isometry3d retval = Isometry3d::Identity();
34 retval.matrix().block<3, 3>(0, 0) = aa.toRotationMatrix();
35 retval.translation() = Vector3d(nx, ny, nz);
36 return retval;
37}

References g2o::Sampler::gaussRand().

Referenced by Robot::move().