2 GTSAM Copyright 2010-2018, Georgia Tech Research Corporation, 3 Atlanta, Georgia 30332-0415 5 Authors: Frank Dellaert, et al. (see THANKS for the full author list) 7 See LICENSE for the license information 9 A 2D Pose SLAM example that reads input from g2o, converts it to a factor graph 10 and does the optimization. Output is written on a file, in g2o format 14 from __future__
import print_function
18 import matplotlib.pyplot
as plt
25 """Create 3d double numpy array.""" 26 return np.array([x, y, z], dtype=float)
29 parser = argparse.ArgumentParser(
30 description=
"A 2D Pose SLAM example that reads input from g2o, " 31 "converts it to a factor graph and does the optimization. " 32 "Output is written on a file, in g2o format")
33 parser.add_argument(
'-i',
'--input', help=
'input file g2o format')
34 parser.add_argument(
'-o',
'--output',
35 help=
"the path to the output file with optimized graph")
36 parser.add_argument(
'-m',
'--maxiter', type=int,
37 help=
"maximum number of iterations for optimizer")
38 parser.add_argument(
'-k',
'--kernel', choices=[
'none',
'huber',
'tukey'],
39 default=
"none", help=
"Type of kernel used")
40 parser.add_argument(
"-p",
"--plot", action=
"store_true",
41 help=
"Flag to plot results")
42 args = parser.parse_args()
47 maxIterations = 100
if args.maxiter
is None else args.maxiter
53 assert args.kernel ==
"none",
"Supplied kernel type is not yet implemented" 57 graph.add(gtsam.PriorFactorPose2(0,
gtsam.Pose2(), priorModel))
60 params.setVerbosity(
"Termination")
61 params.setMaxIterations(maxIterations)
66 result = optimizer.optimize()
68 print(
"Optimization complete")
69 print(
"initial error = ", graph.error(initial))
70 print(
"final error = ", graph.error(result))
73 if args.output
is None:
74 print(
"\nFactor Graph:\n{}".format(graph))
75 print(
"\nInitial Estimate:\n{}".format(initial))
76 print(
"Final Result:\n{}".format(result))
78 outputFile = args.output
79 print(
"Writing results to file: ", outputFile)
86 for i
in range(resultPoses.shape[0]):
void print(const Matrix &A, const string &s, ostream &stream)
Matrix extractPose2(const Values &values)
Extract all Pose2 values into a single matrix [x y theta].
GTSAM_EXPORT GraphAndValues readG2o(const std::string &g2oFile, const bool is3D=false, KernelFunctionType kernelFunctionType=KernelFunctionTypeNONE)
This function parses a g2o file and stores the measurements into a NonlinearFactorGraph and the initi...
GTSAM_EXPORT void writeG2o(const NonlinearFactorGraph &graph, const Values &estimate, const std::string &filename)
This function writes a g2o file from NonlinearFactorGraph and a Values structure. ...
static shared_ptr Variances(const Vector &variances, bool smart=true)
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)