g2o
Classes | Typedefs | Functions
simulator_3d_plane.cpp File Reference
#include <fstream>
#include "g2o/core/sparse_optimizer.h"
#include "g2o/solvers/pcg/linear_solver_pcg.h"
#include "g2o/core/block_solver.h"
#include "g2o/core/linear_solver.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/types/slam3d/types_slam3d.h"
#include "g2o/types/slam3d_addons/types_slam3d_addons.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/command_args.h"
#include <iostream>
Include dependency graph for simulator_3d_plane.cpp:

Go to the source code of this file.

Classes

struct  SimulatorItem
 
struct  WorldItem
 
struct  Sensor
 
struct  Robot
 
struct  Simulator
 
struct  PlaneItem
 
struct  PlaneSensor
 

Typedefs

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

Functions

double uniform_rand (double lowerBndr, double upperBndr)
 
double gauss_rand (double sigma)
 
Eigen::Isometry3d sample_noise_from_se3 (const Vector6d &cov)
 
Vector3d sample_noise_from_plane (const Vector3d &cov)
 
int main (int argc, char **argv)
 

Typedef Documentation

typedef std::vector<Robot*> RobotVector

Definition at line 158 of file simulator_3d_plane.cpp.

typedef std::vector<Sensor*> SensorVector

Definition at line 92 of file simulator_3d_plane.cpp.

typedef std::set<WorldItem*> WorldItemSet

Definition at line 78 of file simulator_3d_plane.cpp.

Function Documentation

double gauss_rand ( double  sigma)

Definition at line 27 of file simulator_3d_plane.cpp.

References uniform_rand().

Referenced by sample_noise_from_plane(), sample_noise_from_se3(), g2o::tutorial::Simulator::sampleTransformation(), and g2o::tutorial::Simulator::simulate().

28 {
29  double x, y, r2;
30  do {
31  x = -1.0 + 2.0 * uniform_rand(0.0, 1.0);
32  y = -1.0 + 2.0 * uniform_rand(0.0, 1.0);
33  r2 = x * x + y * y;
34  } while (r2 > 1.0 || r2 == 0.0);
35  return sigma * y * std::sqrt(-2.0 * log(r2) / r2);
36 }
double uniform_rand(double lowerBndr, double upperBndr)
int main ( int  argc,
char **  argv 
)

Definition at line 252 of file simulator_3d_plane.cpp.

References Simulator::_lastVertexId, PlaneSensor::_nplane, PlaneSensor::_offsetVertex, Simulator::_robots, g2o::BaseRobot::_sensors, Simulator::_world, g2o::OptimizableGraph::addParameter(), g2o::SparseBlockMatrix< MatrixType >::block(), g2o::SparseOptimizer::computeMarginals(), g2o::OptimizationAlgorithmFactory::construct(), g2o::BaseVertex< D, T >::estimate(), g2o::Plane3D::fromVector(), g2o::OptimizableGraph::Vertex::hessianIndex(), g2o::SparseOptimizer::initializeOptimization(), g2o::OptimizationAlgorithmFactory::instance(), g2o::OptimizationAlgorithmFactory::listSolvers(), M_PI, Simulator::move(), g2o::SparseOptimizer::optimize(), g2o::CommandArgs::param(), g2o::CommandArgs::parseArgs(), Simulator::relativeMove(), sample_noise_from_se3(), g2o::OptimizableGraph::save(), Simulator::sense(), g2o::SparseOptimizer::setAlgorithm(), g2o::BaseVertex< D, T >::setEstimate(), g2o::OptimizableGraph::Vertex::setFixed(), g2o::Parameter::setId(), g2o::SparseOptimizer::setVerbose(), g2o::SparseOptimizer::solver(), g2o::SparseOptimizer, WorldItem::vertex(), g2o::OptimizableGraph::vertex(), and g2o::VertexSE3::write().

252  {
253  int maxIterations;
254  bool verbose;
255  bool robustKernel;
256  double lambdaInit;
257  CommandArgs arg;
258  bool fixSensor;
259  bool fixPlanes;
260  bool fixFirstPose;
261  bool fixTrajectory;
262  bool planarMotion;
263  bool listSolvers;
264  string strSolver;
265  cerr << "graph" << endl;
266  arg.param("i", maxIterations, 5, "perform n iterations");
267  arg.param("v", verbose, false, "verbose output of the optimization process");
268  arg.param("solver", strSolver, "lm_var", "select one specific solver");
269  arg.param("lambdaInit", lambdaInit, 0, "user specified lambda init for levenberg");
270  arg.param("robustKernel", robustKernel, false, "use robust error functions");
271  arg.param("fixSensor", fixSensor, false, "fix the sensor position on the robot");
272  arg.param("fixTrajectory", fixTrajectory, false, "fix the trajectory");
273  arg.param("fixFirstPose", fixFirstPose, false, "fix the first robot pose");
274  arg.param("fixPlanes", fixPlanes, false, "fix the planes (do localization only)");
275  arg.param("planarMotion", planarMotion, false, "robot moves on a plane");
276  arg.param("listSolvers", listSolvers, false, "list the solvers");
277  arg.parseArgs(argc, argv);
278 
279 
280 
282  ParameterSE3Offset* odomOffset=new ParameterSE3Offset();
283  odomOffset->setId(0);
284  g->addParameter(odomOffset);
285 
286  OptimizationAlgorithmFactory* solverFactory = OptimizationAlgorithmFactory::instance();
287  OptimizationAlgorithmProperty solverProperty;
288  OptimizationAlgorithm* solver = solverFactory->construct(strSolver, solverProperty);
289  g->setAlgorithm(solver);
290  if (listSolvers){
291  solverFactory->listSolvers(cerr);
292  return 0;
293  }
294 
295  if (! g->solver()){
296  cerr << "Error allocating solver. Allocating \"" << strSolver << "\" failed!" << endl;
297  cerr << "available solvers: " << endl;
298  solverFactory->listSolvers(cerr);
299  cerr << "--------------" << endl;
300  return 0;
301  }
302 
303  cerr << "sim" << endl;
304  Simulator* sim = new Simulator(g);
305 
306  cerr << "robot" << endl;
307  Robot* r=new Robot(g);
308 
309 
310  cerr << "planeSensor" << endl;
311  Matrix3d R=Matrix3d::Identity();
312  R <<
313  0, 0, 1,
314  -1, 0, 0,
315  0, -1, 0;
316 
317  Isometry3d sensorPose=Isometry3d::Identity();
318  sensorPose.matrix().block<3,3>(0,0) = R;
319  sensorPose.translation()= Vector3d(.3 , 0.5 , 1.2);
320  PlaneSensor* ps = new PlaneSensor(r, 0, sensorPose);
321  ps->_nplane << 0.03, 0.03, 0.005;
322  r->_sensors.push_back(ps);
323  sim->_robots.push_back(r);
324 
325  cerr << "p1" << endl;
326  Plane3D plane;
327  PlaneItem* pi =new PlaneItem(g,1);
328  plane.fromVector(Eigen::Vector4d(0.,0.,1.,5.));
329  static_cast<VertexPlane*>(pi->vertex())->setEstimate(plane);
330  pi->vertex()->setFixed(fixPlanes);
331  sim->_world.insert(pi);
332 
333  plane.fromVector(Eigen::Vector4d(1.,0.,0.,5.));
334  pi =new PlaneItem(g,2);
335  static_cast<VertexPlane*>(pi->vertex())->setEstimate(plane);
336  pi->vertex()->setFixed(fixPlanes);
337  sim->_world.insert(pi);
338 
339  cerr << "p2" << endl;
340  pi =new PlaneItem(g,3);
341  plane.fromVector(Eigen::Vector4d(0.,1.,0.,5.));
342  static_cast<VertexPlane*>(pi->vertex())->setEstimate(plane);
343  pi->vertex()->setFixed(fixPlanes);
344  sim->_world.insert(pi);
345 
346 
347  Quaterniond q, iq;
348  if (planarMotion) {
349  r->_planarMotion = true;
350  r->_nmovecov << 0.01, 0.0025, 1e-9, 0.001, 0.001, 0.025;
351  q = Quaterniond(AngleAxisd(0.2, Vector3d::UnitZ()).toRotationMatrix());
352  iq = Quaterniond(AngleAxisd(-0.2, Vector3d::UnitZ()).toRotationMatrix());
353  } else {
354  r->_planarMotion = false;
355  //r->_nmovecov << 0.1, 0.005, 1e-9, 0.05, 0.001, 0.001;
356  r->_nmovecov << 0.1, 0.005, 1e-9, 0.001, 0.001, 0.05;
357  q = Quaterniond((AngleAxisd(M_PI/10, Vector3d::UnitZ()) * AngleAxisd(0.1, Vector3d::UnitY())).toRotationMatrix());
358  iq = Quaterniond((AngleAxisd(-M_PI/10, Vector3d::UnitZ()) * AngleAxisd(0.1, Vector3d::UnitY())).toRotationMatrix());
359  }
360 
361  Isometry3d delta=Isometry3d::Identity();
362  sim->_lastVertexId=4;
363 
364  Isometry3d startPose=Isometry3d::Identity();
365  startPose.matrix().block<3,3>(0,0) = AngleAxisd(-0.75*M_PI, Vector3d::UnitZ()).toRotationMatrix();
366  sim->move(0,startPose);
367 
368  int k =20;
369  int l = 2;
370  double delta_t = 0.2;
371  for (int j=0; j<l; j++) {
372  Vector3d tr(1.,0.,0.);
373  delta.matrix().block<3,3>(0,0) = q.toRotationMatrix();
374  if (j==(l-1)){
375  delta.matrix().block<3,3>(0,0) = Matrix3d::Identity();
376  }
377  delta.translation()=tr*(delta_t*j);
378  Isometry3d iDelta = delta.inverse();
379  for (int a=0; a<2; a++){
380  for (int i=0; i<k; i++){
381  cerr << "m";
382  if (a==0)
383  sim->relativeMove(0,delta);
384  else
385  sim->relativeMove(0,iDelta);
386  cerr << "s";
387  sim->sense(0);
388  }
389  }
390  }
391 
392  for (int j=0; j<l; j++) {
393  Vector3d tr(1.,0.,0.);
394  delta.matrix().block<3,3>(0,0) = iq.toRotationMatrix();
395  if (j==l-1){
396  delta.matrix().block<3,3>(0,0) = Matrix3d::Identity();
397  }
398  delta.translation()=tr*(delta_t*j);
399  Isometry3d iDelta = delta.inverse();
400  for (int a=0; a<2; a++){
401  for (int i=0; i<k; i++){
402  cerr << "m";
403  if (a==0)
404  sim->relativeMove(0,delta);
405  else
406  sim->relativeMove(0,iDelta);
407  cerr << "s";
408  sim->sense(0);
409  }
410  }
411  }
412 
413  ofstream os("test_gt.g2o");
414  g->save(os);
415 
416  if (fixSensor) {
417  ps->_offsetVertex->setFixed(true);
418  } else {
419  Vector6d noffcov;
420  noffcov << 0.1,0.1,0.1,0.5, 0.5, 0.5;
422  ps->_offsetVertex->setFixed(false);
423  }
424 
425  if (fixFirstPose){
426  OptimizableGraph::Vertex* gauge = g->vertex(4);
427  if (gauge)
428  gauge->setFixed(true);
429  } // else {
430  // // multiply all vertices of the robot by this standard quantity
431  // Quaterniond q(AngleAxisd(1, Vector3d::UnitZ()).toRotationMatrix());
432  // Vector3d tr(1,0,0);
433  // Isometry3d delta;
434  // delta.matrix().block<3,3>(0,0)=q.toRotationMatrix();
435  // delta.translation()=tr;
436  // for (size_t i=0; i< g->vertices().size(); i++){
437  // VertexSE3 *v = dynamic_cast<VertexSE3 *>(g->vertex(i));
438  // if (v && v->id()>0){
439  // v->setEstimate (v->estimate()*delta);
440  // }
441  // }
442  // }
443 
444  ofstream osp("test_preopt.g2o");
445  g->save(osp);
446  //g->setMethod(SparseOptimizer::LevenbergMarquardt);
448  g->setVerbose(verbose);
449  g->optimize(maxIterations);
450  if (! fixSensor ){
452  std::pair<int, int> indexParams;
453  indexParams.first = ps->_offsetVertex->hessianIndex();
454  indexParams.second = ps->_offsetVertex->hessianIndex();
455  std::vector<std::pair <int, int> > blockIndices;
456  blockIndices.push_back(indexParams);
457  if (!g->computeMarginals(spinv, blockIndices)){
458  cerr << "error in computing the covariance" << endl;
459  } else {
460 
461  MatrixXd m = *spinv.block(ps->_offsetVertex->hessianIndex(), ps->_offsetVertex->hessianIndex());
462 
463  cerr << "Param covariance" << endl;
464  cerr << m << endl;
465  cerr << "OffsetVertex: " << endl;
466  ps->_offsetVertex->write(cerr);
467  cerr << endl;
468  cerr << "rotationDeterminant: " << m.block<3,3>(0,0).determinant() << endl;
469  cerr << "translationDeterminant: " << m.block<3,3>(3,3).determinant() << endl;
470  cerr << endl;
471  }
472  }
473  ofstream os1("test_postOpt.g2o");
474  g->save(os1);
475 
476 }
Command line parsing of argc and argv.
Definition: command_args.h:46
VertexSE3 * _offsetVertex
describe the properties of a solver
int optimize(int iterations, bool online=false)
SparseMatrixBlock * block(int r, int c, bool alloc=false)
returns the block at location r,c. if alloc=true he block is created if it does not exist ...
OptimizationAlgorithm * solver()
void move(int robotIndex, const Isometry3d &newRobotPose)
int hessianIndex() const
temporary index of this node in the parameter vector obtained from linearization
create solvers based on their short name
void setId(int id_)
Definition: parameter.cpp:35
Vertex * vertex(int id)
returns the vertex number id appropriately casted
virtual bool write(std::ostream &os) const
write the vertex to a stream
Definition: vertex_se3.cpp:58
std::set< BaseSensor * > _sensors
Definition: simulator.h:90
WorldItemSet _world
bool parseArgs(int argc, char **argv, bool exitOnError=true)
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
void setVerbose(bool verbose)
class G2O_CORE_API SparseOptimizer
void setAlgorithm(OptimizationAlgorithm *algorithm)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
void fromVector(const Eigen::Vector4d &coeffs_)
Definition: plane3d.h:59
bool addParameter(Parameter *p)
void relativeMove(int robotIndex, const Isometry3d &delta)
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
Definition: base_vertex.h:101
void listSolvers(std::ostream &os) const
list the known solvers into a stream
A general case Vertex for optimization.
void sense(int robotIndex)
const EstimateType & estimate() const
return the current estimate of the vertex
Definition: base_vertex.h:99
Eigen::Matrix< double, 6, 1 > Vector6d
Eigen::Isometry3d sample_noise_from_se3(const Vector6d &cov)
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
Generic interface for a non-linear solver operating on a graph.
OptimizableGraph::Vertex * vertex()
bool computeMarginals(SparseBlockMatrix< MatrixXD > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
#define M_PI
Definition: misc.h:34
Sparse matrix which uses blocks.
OptimizationAlgorithm * construct(const std::string &tag, OptimizationAlgorithmProperty &solverProperty) const
RobotVector _robots
Vector3d sample_noise_from_plane ( const Vector3d &  cov)

Definition at line 57 of file simulator_3d_plane.cpp.

References gauss_rand().

Referenced by PlaneSensor::sense().

57  {
58  return Vector3d(gauss_rand(cov(0)), gauss_rand(cov(1)), gauss_rand(cov(2)));
59 }
double gauss_rand(double sigma)
Eigen::Isometry3d sample_noise_from_se3 ( const Vector6d cov)

Definition at line 38 of file simulator_3d_plane.cpp.

References gauss_rand().

Referenced by main(), and Robot::move().

38  {
39  double nx=gauss_rand(cov(0));
40  double ny=gauss_rand(cov(1));
41  double nz=gauss_rand(cov(2));
42 
43  double nroll=gauss_rand(cov(3));
44  double npitch=gauss_rand(cov(4));
45  double nyaw=gauss_rand(cov(5));
46 
47  AngleAxisd aa(AngleAxisd(nyaw, Vector3d::UnitZ())*
48  AngleAxisd(nroll, Vector3d::UnitX())*
49  AngleAxisd(npitch, Vector3d::UnitY()));
50 
51  Eigen::Isometry3d retval=Isometry3d::Identity();
52  retval.matrix().block<3,3>(0,0)= aa.toRotationMatrix();
53  retval.translation()=Vector3d(nx,ny,nz);
54  return retval;
55 }
double gauss_rand(double sigma)
double uniform_rand ( double  lowerBndr,
double  upperBndr 
)

Definition at line 22 of file simulator_3d_plane.cpp.

Referenced by gauss_rand(), g2o::tutorial::Rand::gauss_rand(), and g2o::tutorial::Simulator::simulate().

23 {
24  return lowerBndr + ((double) std::rand() / (RAND_MAX + 1.0)) * (upperBndr - lowerBndr);
25 }