g2o
Loading...
Searching...
No Matches
Functions | Variables
sclam_laser_calib.cpp File Reference
#include <csignal>
#include <fstream>
#include <iostream>
#include <map>
#include "g2o/core/factory.h"
#include "g2o/core/hyper_dijkstra.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/stuff/color_macros.h"
#include "g2o/stuff/command_args.h"
#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/string_tools.h"
#include "g2o/stuff/timeutil.h"
#include "g2o/types/data/types_data.h"
#include "g2o/types/sclam2d/types_sclam2d.h"
#include "gm2dl_io.h"
Include dependency graph for sclam_laser_calib.cpp:

Go to the source code of this file.

Functions

 G2O_USE_OPTIMIZATION_LIBRARY (eigen)
 
 G2O_USE_TYPE_GROUP (slam2d)
 
void sigquit_handler (int sig)
 
int main (int argc, char **argv)
 

Variables

static bool hasToStop = false
 

Function Documentation

◆ G2O_USE_OPTIMIZATION_LIBRARY()

G2O_USE_OPTIMIZATION_LIBRARY ( eigen  )

◆ G2O_USE_TYPE_GROUP()

G2O_USE_TYPE_GROUP ( slam2d  )

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 65 of file sclam_laser_calib.cpp.

65 {
66 int maxIterations;
67 bool verbose;
68 string inputFilename;
69 string gnudump;
70 string outputfilename;
71 bool initialGuess;
72 // command line parsing
73 CommandArgs commandLineArguments;
74 commandLineArguments.param("i", maxIterations, 10, "perform n iterations");
75 commandLineArguments.param("v", verbose, false,
76 "verbose output of the optimization process");
77 commandLineArguments.param("guess", initialGuess, false,
78 "initial guess based on spanning tree");
79 commandLineArguments.param("gnudump", gnudump, "",
80 "dump to gnuplot data file");
81 commandLineArguments.param("o", outputfilename, "",
82 "output final version of the graph");
83 commandLineArguments.paramLeftOver("gm2dl-input", inputFilename, "",
84 "gm2dl file which will be processed");
85
86 commandLineArguments.parseArgs(argc, argv);
87
88 OptimizationAlgorithmFactory* solverFactory =
90
91 SparseOptimizer optimizer;
92 optimizer.setVerbose(verbose);
93 optimizer.setForceStopFlag(&hasToStop);
94
95 OptimizationAlgorithmProperty solverProperty;
96 optimizer.setAlgorithm(solverFactory->construct("lm_var", solverProperty));
97
98 // loading
99 if (!Gm2dlIO::readGm2dl(inputFilename, optimizer, false)) {
100 cerr << "Error while loading gm2dl file" << endl;
101 }
102
103 VertexSE2* laserOffset =
104 dynamic_cast<VertexSE2*>(optimizer.vertex(numeric_limits<int>::max()));
105 // laserOffset->setEstimate(SE2()); // set to Identity
106 if (laserOffset) {
107 cerr << "Initial laser offset "
108 << laserOffset->estimate().toVector().transpose() << endl;
109 }
110 bool gaugeFreedom = optimizer.gaugeFreedom();
111
112 OptimizableGraph::Vertex* gauge = optimizer.findGauge();
113 if (gaugeFreedom) {
114 if (!gauge) {
115 cerr << "# cannot find a vertex to fix in this thing" << endl;
116 return 2;
117 } else {
118 cerr << "# graph is fixed by node " << gauge->id() << endl;
119 gauge->setFixed(true);
120 }
121 } else {
122 cerr << "# graph is fixed by priors" << endl;
123 }
124
125 // sanity check
126 HyperDijkstra d(&optimizer);
128 d.shortestPaths(gauge, &f);
129 // cerr << PVAR(d.visited().size()) << endl;
130
131 if (d.visited().size() != optimizer.vertices().size()) {
132 cerr << CL_RED("Warning: d.visited().size() != optimizer.vertices().size()")
133 << endl;
134 cerr << "visited: " << d.visited().size() << endl;
135 cerr << "vertices: " << optimizer.vertices().size() << endl;
136 if (1)
137 for (SparseOptimizer::VertexIDMap::const_iterator it =
138 optimizer.vertices().begin();
139 it != optimizer.vertices().end(); ++it) {
141 static_cast<OptimizableGraph::Vertex*>(it->second);
142 if (d.visited().count(v) == 0) {
143 cerr << "\t unvisited vertex " << it->first << " "
144 << static_cast<void*>(v) << endl;
145 v->setFixed(true);
146 }
147 }
148 }
149
150 optimizer.initializeOptimization();
151 optimizer.computeActiveErrors();
152 cerr << "Initial chi2 = " << FIXED(optimizer.chi2()) << endl;
153
154 // if (guessCostFunction)
155 // optimizer.computeInitialGuess(guessCostFunction);
156
157 signal(SIGINT, sigquit_handler);
158
159 int i = optimizer.optimize(maxIterations);
160 if (maxIterations > 0 && !i) {
161 cerr << "optimize failed, result might be invalid" << endl;
162 }
163
164 if (laserOffset) {
165 cerr << "Calibrated laser offset "
166 << laserOffset->estimate().toVector().transpose() << endl;
167 }
168
169 if (outputfilename.size() > 0) {
170 Gm2dlIO::updateLaserData(optimizer);
171 cerr << "Writing " << outputfilename << " ... ";
172 bool writeStatus = Gm2dlIO::writeGm2dl(outputfilename, optimizer);
173 cerr << (writeStatus ? "done." : "failed") << endl;
174 }
175
176 if (gnudump.size() > 0) {
177 ofstream fout(gnudump.c_str());
178 for (SparseOptimizer::VertexIDMap::const_iterator it =
179 optimizer.vertices().begin();
180 it != optimizer.vertices().end(); ++it) {
181 VertexSE2* v = dynamic_cast<VertexSE2*>(it->second);
182 fout << v->estimate().toVector().transpose() << endl;
183 }
184 }
185
186 return 0;
187}
const EstimateType & estimate() const
return the current estimate of the vertex
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)
static bool readGm2dl(const std::string &filename, SparseOptimizer &optimizer, bool overrideCovariances=false)
Definition gm2dl_io.cpp:46
static bool updateLaserData(SparseOptimizer &optimizer)
Definition gm2dl_io.cpp:227
static bool writeGm2dl(const std::string &filename, const SparseOptimizer &optimizer)
Definition gm2dl_io.cpp:167
int id() const
returns the id
const VertexIDMap & vertices() const
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
Vector3 toVector() const
convert to a 3D vector (x, y, theta)
Definition se2.h:105
int optimize(int iterations, bool online=false)
void setForceStopFlag(bool *flag)
void setVerbose(bool verbose)
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
void setAlgorithm(OptimizationAlgorithm *algorithm)
virtual Vertex * findGauge()
finds a gauge in the graph to remove the undefined dof.
2D pose Vertex, (x,y,theta)
Definition vertex_se2.h:41
#define CL_RED(s)
void sigquit_handler(int sig)
static bool hasToStop
double chi2() const
returns the chi2 of the current configuration
Vertex * vertex(int id)
returns the vertex number id appropriately casted

References g2o::OptimizableGraph::chi2(), CL_RED, g2o::SparseOptimizer::computeActiveErrors(), g2o::OptimizationAlgorithmFactory::construct(), g2o::BaseVertex< D, T >::estimate(), g2o::SparseOptimizer::findGauge(), g2o::SparseOptimizer::gaugeFreedom(), hasToStop, g2o::HyperGraph::Vertex::id(), g2o::SparseOptimizer::initializeOptimization(), g2o::OptimizationAlgorithmFactory::instance(), g2o::SparseOptimizer::optimize(), g2o::CommandArgs::param(), g2o::CommandArgs::paramLeftOver(), g2o::CommandArgs::parseArgs(), g2o::Gm2dlIO::readGm2dl(), g2o::SparseOptimizer::setAlgorithm(), g2o::OptimizableGraph::Vertex::setFixed(), g2o::SparseOptimizer::setForceStopFlag(), g2o::SparseOptimizer::setVerbose(), g2o::HyperDijkstra::shortestPaths(), sigquit_handler(), g2o::SE2::toVector(), g2o::Gm2dlIO::updateLaserData(), g2o::OptimizableGraph::vertex(), g2o::HyperGraph::vertices(), g2o::HyperDijkstra::visited(), and g2o::Gm2dlIO::writeGm2dl().

◆ sigquit_handler()

void sigquit_handler ( int  sig)

Definition at line 54 of file sclam_laser_calib.cpp.

54 {
55 if (sig == SIGINT) {
56 hasToStop = 1;
57 static int cnt = 0;
58 if (cnt++ == 2) {
59 cerr << __PRETTY_FUNCTION__ << " forcing exit" << endl;
60 exit(1);
61 }
62 }
63}
#define __PRETTY_FUNCTION__
Definition macros.h:90

References __PRETTY_FUNCTION__, and hasToStop.

Referenced by main().

Variable Documentation

◆ hasToStop

bool hasToStop = false
static

Definition at line 49 of file sclam_laser_calib.cpp.

Referenced by main(), and sigquit_handler().