g2o
Loading...
Searching...
No Matches
sclam_laser_calib.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G.Grisetti, 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 <csignal>
28#include <fstream>
29#include <iostream>
30#include <map>
31
32#include "g2o/core/factory.h"
39#include "g2o/stuff/macros.h"
41#include "g2o/stuff/timeutil.h"
44#include "gm2dl_io.h"
45
46using namespace std;
47using namespace g2o;
48
49static bool hasToStop = false;
50
53
54void sigquit_handler(int sig) {
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}
64
65int main(int argc, char** argv) {
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)
#define G2O_USE_TYPE_GROUP(typeGroupName)
Definition factory.h:159
int main()
Definition gicp_demo.cpp:44
#define __PRETTY_FUNCTION__
Definition macros.h:90
Definition jet.h:876
#define G2O_USE_OPTIMIZATION_LIBRARY(libraryname)
void sigquit_handler(int sig)
static bool hasToStop
HyperGraph::VertexSet & visited()
void shortestPaths(HyperGraph::Vertex *v, HyperDijkstra::CostFunction *cost, double maxDistance=std::numeric_limits< double >::max(), double comparisonConditioner=1e-3, bool directed=false, double maxEdgeCost=std::numeric_limits< double >::max())
double chi2() const
returns the chi2 of the current configuration
Vertex * vertex(int id)
returns the vertex number id appropriately casted
utility functions for handling time related stuff