Pose2SLAMExample_g2o.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file Pose2SLAMExample_g2o.cpp
  10. * @brief A 2D Pose SLAM example that reads input from g2o, converts it to a factor graph and does the
  11. * optimization. Output is written on a file, in g2o format
  12. * Syntax for the script is ./Pose2SLAMExample_g2o input.g2o output.g2o
  13. * @date May 15, 2014
  14. * @author Luca Carlone
  15. */
  16. #include <gtsam/slam/dataset.h>
  17. #include <gtsam/geometry/Pose2.h>
  18. #include <gtsam/nonlinear/GaussNewtonOptimizer.h>
  19. #include <fstream>
  20. using namespace std;
  21. using namespace gtsam;
  22. // HOWTO: ./Pose2SLAMExample_g2o inputFile outputFile (maxIterations) (tukey/huber)
  23. int main(const int argc, const char *argv[]) {
  24. string kernelType = "none";
  25. int maxIterations = 100; // default
  26. string g2oFile = findExampleDataFile("noisyToyGraph.txt"); // default
  27. // Parse user's inputs
  28. if (argc > 1) {
  29. g2oFile = argv[1]; // input dataset filename
  30. }
  31. if (argc > 3) {
  32. maxIterations = atoi(argv[3]); // user can specify either tukey or huber
  33. }
  34. if (argc > 4) {
  35. kernelType = argv[4]; // user can specify either tukey or huber
  36. }
  37. // reading file and creating factor graph
  38. NonlinearFactorGraph::shared_ptr graph;
  39. Values::shared_ptr initial;
  40. bool is3D = false;
  41. if (kernelType.compare("none") == 0) {
  42. boost::tie(graph, initial) = readG2o(g2oFile, is3D);
  43. }
  44. if (kernelType.compare("huber") == 0) {
  45. std::cout << "Using robust kernel: huber " << std::endl;
  46. boost::tie(graph, initial) =
  47. readG2o(g2oFile, is3D, KernelFunctionTypeHUBER);
  48. }
  49. if (kernelType.compare("tukey") == 0) {
  50. std::cout << "Using robust kernel: tukey " << std::endl;
  51. boost::tie(graph, initial) =
  52. readG2o(g2oFile, is3D, KernelFunctionTypeTUKEY);
  53. }
  54. // Add prior on the pose having index (key) = 0
  55. auto priorModel = //
  56. noiseModel::Diagonal::Variances(Vector3(1e-6, 1e-6, 1e-8));
  57. graph->addPrior(0, Pose2(), priorModel);
  58. std::cout << "Adding prior on pose 0 " << std::endl;
  59. GaussNewtonParams params;
  60. params.setVerbosity("TERMINATION");
  61. if (argc > 3) {
  62. params.maxIterations = maxIterations;
  63. std::cout << "User required to perform maximum " << params.maxIterations
  64. << " iterations " << std::endl;
  65. }
  66. std::cout << "Optimizing the factor graph" << std::endl;
  67. GaussNewtonOptimizer optimizer(*graph, *initial, params);
  68. Values result = optimizer.optimize();
  69. std::cout << "Optimization complete" << std::endl;
  70. std::cout << "initial error=" << graph->error(*initial) << std::endl;
  71. std::cout << "final error=" << graph->error(result) << std::endl;
  72. if (argc < 3) {
  73. result.print("result");
  74. } else {
  75. const string outputFile = argv[2];
  76. std::cout << "Writing results to file: " << outputFile << std::endl;
  77. NonlinearFactorGraph::shared_ptr graphNoKernel;
  78. Values::shared_ptr initial2;
  79. boost::tie(graphNoKernel, initial2) = readG2o(g2oFile);
  80. writeG2o(*graphNoKernel, result, outputFile);
  81. std::cout << "done! " << std::endl;
  82. }
  83. return 0;
  84. }