当前位置:   article > 正文

cartographer-ros阅读梳理(六)后端部分-关于回环检测搜索算法SubmapScanMatcher与后端优化器optimization_problem__cartographer回环检测

cartographer回环检测

一、前言

        在前述constraint_builder_2d的建立过程中,建立了新节点与旧子图之间的约束,并将这些约束加入了因子图中,这种约束可以认为是cartgrapher的回环检测,在约束建立后,具体的搜索方法使用的是constraint_builder_2d中私有类建立的SubmapScanMatcher,本文将对其中涉及到的分支定界搜索方法进行分析,并对后端优化器optimization_problem_进行分析,至此整个后端优化过程也就完整结束了

二、SubmapScanMatcher方法

        constraint_builder_2d中私有类建立的SubmapScanMatcher如下

  1. private:
  2. struct SubmapScanMatcher {
  3. const Grid2D* grid = nullptr;
  4. std::unique_ptr<scan_matching::FastCorrelativeScanMatcher2D>
  5. fast_correlative_scan_matcher;
  6. std::weak_ptr<common::Task> creation_task_handle;
  7. };

        fast_correlative_scan_matcher的构造来源于fast_correlative_scan_matcher_2d.h中的方法,其构造函数如下

  1. FastCorrelativeScanMatcher2D::FastCorrelativeScanMatcher2D(
  2. //子图占据的栅格
  3. const Grid2D& grid,
  4. //子图匹配配置
  5. const proto::FastCorrelativeScanMatcherOptions2D& options)
  6. : options_(options),
  7. //子图地图的作用范围
  8. limits_(grid.limits()),
  9. //存储预算图的容器
  10. precomputation_grid_stack_(
  11. absl::make_unique<PrecomputationGridStack2D>(grid, options)) {}
  12. FastCorrelativeScanMatcher2D::~FastCorrelativeScanMatcher2D() {}

        约束器构建时,通过选项matchfullmap来选择位姿是否与所有地图进行匹配,然后传入FastCorrelativeScanMatcher2D的不同函数,分为定点匹配与全地图匹配两个函数

  1. //定点匹配
  2. bool FastCorrelativeScanMatcher2D::Match(
  3. const transform::Rigid2d& initial_pose_estimate,
  4. const sensor::PointCloud& point_cloud, const float min_score, float* score,
  5. transform::Rigid2d* pose_estimate) const {
  6. const SearchParameters search_parameters(options_.linear_search_window(),
  7. options_.angular_search_window(),
  8. point_cloud, limits_.resolution());
  9. return MatchWithSearchParameters(search_parameters, initial_pose_estimate,
  10. point_cloud, min_score, score,
  11. pose_estimate);
  12. }
  13. //全地图匹配
  14. bool FastCorrelativeScanMatcher2D::MatchFullSubmap(
  15. const sensor::PointCloud& point_cloud, float min_score, float* score,
  16. transform::Rigid2d* pose_estimate) const {
  17. // Compute a search window around the center of the submap that includes it
  18. // fully.
  19. const SearchParameters search_parameters(
  20. 1e6 * limits_.resolution(), // Linear search window, 1e6 cells/direction.
  21. M_PI, // Angular search window, 180 degrees in both directions.
  22. point_cloud, limits_.resolution());
  23. const transform::Rigid2d center = transform::Rigid2d::Translation(
  24. limits_.max() - 0.5 * limits_.resolution() *
  25. Eigen::Vector2d(limits_.cell_limits().num_y_cells,
  26. limits_.cell_limits().num_x_cells));
  27. return MatchWithSearchParameters(search_parameters, center, point_cloud,
  28. min_score, score, pose_estimate);
  29. }

        两种匹配方法均是输入路径节点初始位姿(全地图匹配不需要)、路径节点的点云、以及匹配参数,调用深度优先分支界定搜索MatchWithSearchParameters函数,匹配完成的返回结果装入pose_estimate中

        

  1. bool FastCorrelativeScanMatcher2D::MatchWithSearchParameters(
  2. SearchParameters search_parameters,
  3. const transform::Rigid2d& initial_pose_estimate,
  4. const sensor::PointCloud& point_cloud, float min_score, float* score,
  5. transform::Rigid2d* pose_estimate) const {
  6. CHECK(score != nullptr);
  7. CHECK(pose_estimate != nullptr);
  8. //将激光点云转化到世界坐标系下
  9. const Eigen::Rotation2Dd initial_rotation = initial_pose_estimate.rotation();
  10. const sensor::PointCloud rotated_point_cloud = sensor::TransformPointCloud(
  11. point_cloud,
  12. transform::Rigid3f::Rotation(Eigen::AngleAxisf(
  13. initial_rotation.cast<float>().angle(), Eigen::Vector3f::UnitZ())));
  14. //调用correlative_scan_matcher_2d.cc函数,将点云经过角分辨率旋转,获得点云组
  15. const std::vector<sensor::PointCloud> rotated_scans =
  16. GenerateRotatedScans(rotated_point_cloud, search_parameters);
  17. //调用correlative_scan_matcher_2d.cc函数,完成点云离散化,转化为整型栅格单元索引
  18. const std::vector<DiscreteScan2D> discrete_scans = DiscretizeScans(
  19. limits_, rotated_scans,
  20. Eigen::Translation2f(initial_pose_estimate.translation().x(),
  21. initial_pose_estimate.translation().y()));
  22. //缩小搜索窗口的大小,提高搜索效率
  23. search_parameters.ShrinkToFit(discrete_scans, limits_.cell_limits());
  24. //对搜索空间分割,得到初始子空间点集合
  25. const std::vector<Candidate2D> lowest_resolution_candidates =
  26. ComputeLowestResolutionCandidates(discrete_scans, search_parameters);
  27. //分支定界搜索
  28. const Candidate2D best_candidate = BranchAndBound(
  29. discrete_scans, search_parameters, lowest_resolution_candidates,
  30. precomputation_grid_stack_->max_depth(), min_score);
  31. if (best_candidate.score > min_score) {
  32. *score = best_candidate.score;
  33. *pose_estimate = transform::Rigid2d(
  34. {initial_pose_estimate.translation().x() + best_candidate.x,
  35. initial_pose_estimate.translation().y() + best_candidate.y},
  36. initial_rotation * Eigen::Rotation2Dd(best_candidate.orientation));
  37. return true;
  38. }
  39. return false;
  40. }

        整个搜索过程也是比较直观的,把点云和位姿扔进去直接利用深度优先分支定界搜索,关于搜索方法的原理有很多大佬已经总结的很好,我就不再献丑了,这里只分析一下算法

  1. std::vector<Candidate2D>
  2. FastCorrelativeScanMatcher2D::ComputeLowestResolutionCandidates(
  3. //离散化后的点云
  4. const std::vector<DiscreteScan2D>& discrete_scans,
  5. const SearchParameters& search_parameters) const {
  6. //对搜索空间进行初始分割,构建候选位置点
  7. std::vector<Candidate2D> lowest_resolution_candidates =
  8. GenerateLowestResolutionCandidates(search_parameters);
  9. //计算各个估计位姿点的分数
  10. ScoreCandidates(
  11. precomputation_grid_stack_->Get(precomputation_grid_stack_->max_depth()),
  12. discrete_scans, search_parameters, &lowest_resolution_candidates);
  13. return lowest_resolution_candidates;
  14. }
  15. //构建候选位置点
  16. std::vector<Candidate2D>
  17. FastCorrelativeScanMatcher2D::GenerateLowestResolutionCandidates(
  18. const SearchParameters& search_parameters) const {
  19. //步长
  20. const int linear_step_size = 1 << precomputation_grid_stack_->max_depth();
  21. int num_candidates = 0;
  22. //遍历所有搜索方向
  23. for (int scan_index = 0; scan_index != search_parameters.num_scans;
  24. ++scan_index) {
  25. const int num_lowest_resolution_linear_x_candidates =
  26. (search_parameters.linear_bounds[scan_index].max_x -
  27. search_parameters.linear_bounds[scan_index].min_x + linear_step_size) /
  28. linear_step_size;
  29. const int num_lowest_resolution_linear_y_candidates =
  30. (search_parameters.linear_bounds[scan_index].max_y -
  31. search_parameters.linear_bounds[scan_index].min_y + linear_step_size) /
  32. linear_step_size;
  33. num_candidates += num_lowest_resolution_linear_x_candidates *
  34. num_lowest_resolution_linear_y_candidates;
  35. }
  36. //遍历不同角度的scan的x方向与y方向的所有可行解
  37. std::vector<Candidate2D> candidates;
  38. candidates.reserve(num_candidates);
  39. for (int scan_index = 0; scan_index != search_parameters.num_scans;
  40. ++scan_index) {
  41. for (int x_index_offset = search_parameters.linear_bounds[scan_index].min_x;
  42. x_index_offset <= search_parameters.linear_bounds[scan_index].max_x;
  43. x_index_offset += linear_step_size) {
  44. for (int y_index_offset =
  45. search_parameters.linear_bounds[scan_index].min_y;
  46. y_index_offset <= search_parameters.linear_bounds[scan_index].max_y;
  47. y_index_offset += linear_step_size) {
  48. candidates.emplace_back(scan_index, x_index_offset, y_index_offset,
  49. search_parameters);
  50. }
  51. }
  52. }
  53. CHECK_EQ(candidates.size(), num_candidates);
  54. return candidates;
  55. }
  56. void FastCorrelativeScanMatcher2D::ScoreCandidates(
  57. const PrecomputationGrid2D& precomputation_grid,
  58. const std::vector<DiscreteScan2D>& discrete_scans,
  59. const SearchParameters& search_parameters,
  60. std::vector<Candidate2D>* const candidates) const {
  61. //遍历所有候选点
  62. for (Candidate2D& candidate : *candidates) {
  63. int sum = 0;
  64. for (const Eigen::Array2i& xy_index :
  65. discrete_scans[candidate.scan_index]) {
  66. const Eigen::Array2i proposed_xy_index(
  67. xy_index.x() + candidate.x_index_offset,
  68. xy_index.y() + candidate.y_index_offset);
  69. sum += precomputation_grid.GetValue(proposed_xy_index);
  70. }
  71. //一个scan的sum除以这个scan中点的个数,即为得分
  72. candidate.score = precomputation_grid.ToScore(
  73. sum / static_cast<float>(discrete_scans[candidate.scan_index].size()));
  74. }
  75. std::sort(candidates->begin(), candidates->end(),
  76. std::greater<Candidate2D>());
  77. }
  78. Candidate2D FastCorrelativeScanMatcher2D::BranchAndBound(
  79. const std::vector<DiscreteScan2D>& discrete_scans,
  80. const SearchParameters& search_parameters,
  81. const std::vector<Candidate2D>& candidates, const int candidate_depth,
  82. float min_score) const {
  83. //如果搜索树高为0,也就是找到了一个叶子结点,把这个结果返回
  84. if (candidate_depth == 0) {
  85. // Return the best candidate.
  86. return *candidates.begin();
  87. }
  88. //候选点容器
  89. Candidate2D best_high_resolution_candidate(0, 0, 0, search_parameters);
  90. best_high_resolution_candidate.score = min_score;
  91. //遍历所有的候选点
  92. for (const Candidate2D& candidate : candidates) {
  93. //如果有一个候选点的分数很低,后面的候选点也不会有更好的结果了
  94. if (candidate.score <= min_score) {
  95. break;
  96. }
  97. //如果当前点的分数比较高,需要进行分支
  98. std::vector<Candidate2D> higher_resolution_candidates;
  99. const int half_width = 1 << (candidate_depth - 1);
  100. //对x、y偏移进行遍历,求出这一个candidate的四个子节点候选
  101. for (int x_offset : {0, half_width}) {
  102. if (candidate.x_index_offset + x_offset >
  103. search_parameters.linear_bounds[candidate.scan_index].max_x) {
  104. break;
  105. }
  106. for (int y_offset : {0, half_width}) {
  107. if (candidate.y_index_offset + y_offset >
  108. search_parameters.linear_bounds[candidate.scan_index].max_y) {
  109. break;
  110. }
  111. higher_resolution_candidates.emplace_back(
  112. candidate.scan_index, candidate.x_index_offset + x_offset,
  113. candidate.y_index_offset + y_offset, search_parameters);
  114. }
  115. }
  116. //对新扩展的候选点定界并排序
  117. ScoreCandidates(precomputation_grid_stack_->Get(candidate_depth - 1),
  118. discrete_scans, search_parameters,
  119. &higher_resolution_candidates);
  120. best_high_resolution_candidate = std::max(
  121. best_high_resolution_candidate,
  122. //递归调用
  123. BranchAndBound(discrete_scans, search_parameters,
  124. higher_resolution_candidates, candidate_depth - 1,
  125. best_high_resolution_candidate.score));
  126. }
  127. return best_high_resolution_candidate;
  128. }

        上面是整个分支定界法搜索的流程,我将其理解为一种二维上的二分法,也就是四分法,在对搜索空间进行切割处理后,构建候选的位置点,递归调用分支定界法不停地四分搜索,将预想的xy坐标一直加上半个偏置,逐步缩小搜索范围,直到找到最终的叶子结点,作为位姿估计的结果并返回

三、optimization_problem_后端优化器

        前面构建的约束器,以及SubmapScanMatcher方法,实现新旧节点与新旧子图之间的约束关系,仍旧需要根据后端优化问题,考察所有约束,调整子图和路径节点的位姿,来最小化全局估计与局部估计之间的偏差,这就需要std::unique_ptr<optimization::OptimizationProblem2D> optimization_problem_后端优化器来起作用

        在pose_graph_2d中,私有类optimization_problem_的建立依赖于optimization_problem_2d.h,继承自接口类optimization_problem_interface.h后端优化器是通过一种称为SPA(Sparse Pose Adjustment)的技术,根据节点与子图之间的约束关系,优化路径节点与子图的世界坐标,本质上还是通过列文伯格-马尔夸特(LM)法来寻找最优解

        优化动作的触发是由posd_graph_2d.cc中RunOptimization函数执行的

  1. void PoseGraph2D::RunOptimization() {
  2. //没有要优化的对象
  3. if (optimization_problem_->submap_data().empty()) {
  4. return;
  5. }
  6. // No other thread is accessing the optimization_problem_,
  7. // data_.constraints, data_.frozen_trajectories and data_.landmark_nodes
  8. // when executing the Solve. Solve is time consuming, so not taking the mutex
  9. // before Solve to avoid blocking foreground processing.
  10. //程序运行这部分的时候,没有其他待优化问题要解决了,这部分程序比较耗时
  11. //保证constraints约束器、GetTrajectoryStates路径生成、landmark_nodes地标程序都完成了
  12. optimization_problem_->Solve(data_.constraints, GetTrajectoryStates(),
  13. data_.landmark_nodes);
  14. //加线程锁
  15. absl::MutexLock locker(&mutex_);
  16. const auto& submap_data = optimization_problem_->submap_data();
  17. const auto& node_data = optimization_problem_->node_data();
  18. //对每一条轨迹遍历
  19. for (const int trajectory_id : node_data.trajectory_ids()) {
  20. //对每一个节点遍历
  21. for (const auto& node : node_data.trajectory(trajectory_id)) {
  22. //用优化后的位姿来更新轨迹点的世界坐标
  23. auto& mutable_trajectory_node = data_.trajectory_nodes.at(node.id);
  24. mutable_trajectory_node.global_pose =
  25. transform::Embed3D(node.data.global_pose_2d) *
  26. transform::Rigid3d::Rotation(
  27. mutable_trajectory_node.constant_data->gravity_alignment);
  28. }
  29. // Extrapolate all point cloud poses that were not included in the
  30. // 'optimization_problem_' yet.
  31. //计算SPA优化后的世界坐标变换关系
  32. const auto local_to_new_global =
  33. ComputeLocalToGlobalTransform(submap_data, trajectory_id);
  34. //计算SPA优化前的世界坐标变换关系
  35. const auto local_to_old_global = ComputeLocalToGlobalTransform(
  36. data_.global_submap_poses_2d, trajectory_id);
  37. //计算旧坐标系到新坐标系的变换关系
  38. const transform::Rigid3d old_global_to_new_global =
  39. local_to_new_global * local_to_old_global.inverse();
  40. const NodeId last_optimized_node_id =
  41. std::prev(node_data.EndOfTrajectory(trajectory_id))->id;
  42. auto node_it =
  43. std::next(data_.trajectory_nodes.find(last_optimized_node_id));
  44. for (; node_it != data_.trajectory_nodes.EndOfTrajectory(trajectory_id);
  45. ++node_it) {
  46. auto& mutable_trajectory_node = data_.trajectory_nodes.at(node_it->id);
  47. //旧坐标系到新坐标系的变换关系,左乘到global_pose上,进行修正
  48. mutable_trajectory_node.global_pose =
  49. old_global_to_new_global * mutable_trajectory_node.global_pose;
  50. }
  51. }
  52. for (const auto& landmark : optimization_problem_->landmark_data()) {
  53. //landmark也一样的处理
  54. data_.landmark_nodes[landmark.first].global_landmark_pose = landmark.second;
  55. }
  56. //记录下当前位姿
  57. data_.global_submap_poses_2d = submap_data;
  58. }

        optimization_problem_2d.cc中,后端优化的核心就是类OptimizationProblem2D的成员函数Solve,通过Ceres库进行优化,调整子图和路径节点的世界位姿

  1. void OptimizationProblem2D::Solve(
  2. const std::vector<Constraint>& constraints,
  3. const std::map<int, PoseGraphInterface::TrajectoryState>&
  4. trajectories_state,
  5. const std::map<std::string, LandmarkNode>& landmark_nodes) {
  6. //没有需要优化的对象,返回
  7. if (node_data_.empty()) {
  8. // Nothing to optimize.
  9. return;
  10. }
  11. //已经冻结的轨迹
  12. std::set<int> frozen_trajectories;
  13. for (const auto& it : trajectories_state) {
  14. if (it.second == PoseGraphInterface::TrajectoryState::FROZEN) {
  15. frozen_trajectories.insert(it.first);
  16. }
  17. }
  18. //建立ceres问题的优化对象
  19. ceres::Problem::Options problem_options;
  20. ceres::Problem problem(problem_options);
  21. // Set the starting point.
  22. // TODO(hrapp): Move ceres data into SubmapSpec.
  23. MapById<SubmapId, std::array<double, 3>> C_submaps;
  24. MapById<NodeId, std::array<double, 3>> C_nodes;
  25. std::map<std::string, CeresPose> C_landmarks;
  26. bool first_submap = true;
  27. //遍历所有的子图,将子图的全局位姿放置到刚刚构建的临时容器C_submaps中
  28. for (const auto& submap_id_data : submap_data_) {
  29. const bool frozen =
  30. frozen_trajectories.count(submap_id_data.id.trajectory_id) != 0;
  31. C_submaps.Insert(submap_id_data.id,
  32. FromPose(submap_id_data.data.global_pose));
  33. //把要优化的位子问题添加进去
  34. problem.AddParameterBlock(C_submaps.at(submap_id_data.id).data(), 3);
  35. if (first_submap || frozen) {
  36. first_submap = false;
  37. // Fix the pose of the first submap or all submaps of a frozen
  38. // trajectory.
  39. //第一幅子图或者已经冻结的子图,把C_submaps相关数值设成常量
  40. problem.SetParameterBlockConstant(C_submaps.at(submap_id_data.id).data());
  41. }
  42. }
  43. //遍历所有的路径节点,将他们的全局位姿作为优化参数告知ceres对象problem
  44. for (const auto& node_id_data : node_data_) {
  45. //已经冻结的节点就不用优化了,和上面子图道理一样
  46. const bool frozen =
  47. frozen_trajectories.count(node_id_data.id.trajectory_id) != 0;
  48. C_nodes.Insert(node_id_data.id, FromPose(node_id_data.data.global_pose_2d));
  49. //需要优化的节点加进去
  50. problem.AddParameterBlock(C_nodes.at(node_id_data.id).data(), 3);
  51. if (frozen) {
  52. problem.SetParameterBlockConstant(C_nodes.at(node_id_data.id).data());
  53. }
  54. }
  55. // Add cost functions for intra- and inter-submap constraints.
  56. //遍历所有的约束,描述优化问题的残差块
  57. for (const Constraint& constraint : constraints) {
  58. //构建子图位姿与路径位姿的残差方程,ceres传入数据是指针的方式,所以最后优化结果保存在C_submaps和C_nodes里
  59. problem.AddResidualBlock(
  60. CreateAutoDiffSpaCostFunction(constraint.pose),
  61. // Loop closure constraints should have a loss function.
  62. constraint.tag == Constraint::INTER_SUBMAP
  63. ? new ceres::HuberLoss(options_.huber_scale())
  64. : nullptr,
  65. C_submaps.at(constraint.submap_id).data(),
  66. C_nodes.at(constraint.node_id).data());
  67. }
  68. // Add cost functions for landmarks.
  69. //根据路标点添加残差项
  70. AddLandmarkCostFunctions(landmark_nodes, node_data_, &C_nodes, &C_landmarks,
  71. &problem, options_.huber_scale());
  72. // Add penalties for violating odometry or changes between consecutive nodes
  73. // if odometry is not available.
  74. //遍历所有的路径节点,根据Local SLAM以及里程计等局部定位的信息建立相邻的路径节点之间的位姿变换关系
  75. for (auto node_it = node_data_.begin(); node_it != node_data_.end();) {
  76. const int trajectory_id = node_it->id.trajectory_id;
  77. const auto trajectory_end = node_data_.EndOfTrajectory(trajectory_id);
  78. if (frozen_trajectories.count(trajectory_id) != 0) {
  79. node_it = trajectory_end;
  80. continue;
  81. }
  82. auto prev_node_it = node_it;
  83. for (++node_it; node_it != trajectory_end; ++node_it) {
  84. const NodeId first_node_id = prev_node_it->id;
  85. const NodeSpec2D& first_node_data = prev_node_it->data;
  86. prev_node_it = node_it;
  87. const NodeId second_node_id = node_it->id;
  88. const NodeSpec2D& second_node_data = node_it->data;
  89. if (second_node_id.node_index != first_node_id.node_index + 1) {
  90. continue;
  91. }
  92. // Add a relative pose constraint based on the odometry (if available).
  93. std::unique_ptr<transform::Rigid3d> relative_odometry =
  94. CalculateOdometryBetweenNodes(trajectory_id, first_node_data,
  95. second_node_data);
  96. if (relative_odometry != nullptr) {
  97. problem.AddResidualBlock(
  98. CreateAutoDiffSpaCostFunction(Constraint::Pose{
  99. *relative_odometry, options_.odometry_translation_weight(),
  100. options_.odometry_rotation_weight()}),
  101. nullptr /* loss function */, C_nodes.at(first_node_id).data(),
  102. C_nodes.at(second_node_id).data());
  103. }
  104. // Add a relative pose constraint based on consecutive local SLAM poses.
  105. const transform::Rigid3d relative_local_slam_pose =
  106. transform::Embed3D(first_node_data.local_pose_2d.inverse() *
  107. second_node_data.local_pose_2d);
  108. problem.AddResidualBlock(
  109. CreateAutoDiffSpaCostFunction(
  110. Constraint::Pose{relative_local_slam_pose,
  111. options_.local_slam_pose_translation_weight(),
  112. options_.local_slam_pose_rotation_weight()}),
  113. nullptr /* loss function */, C_nodes.at(first_node_id).data(),
  114. C_nodes.at(second_node_id).data());
  115. }
  116. }
  117. //看到一种说法说C_fixed_frames一般指gps信号这样的全局信息,感觉这块可以改编
  118. std::map<int, std::array<double, 3>> C_fixed_frames;
  119. for (auto node_it = node_data_.begin(); node_it != node_data_.end();) {
  120. const int trajectory_id = node_it->id.trajectory_id;
  121. const auto trajectory_end = node_data_.EndOfTrajectory(trajectory_id);
  122. if (!fixed_frame_pose_data_.HasTrajectory(trajectory_id)) {
  123. node_it = trajectory_end;
  124. continue;
  125. }
  126. const TrajectoryData& trajectory_data = trajectory_data_.at(trajectory_id);
  127. bool fixed_frame_pose_initialized = false;
  128. for (; node_it != trajectory_end; ++node_it) {
  129. const NodeId node_id = node_it->id;
  130. const NodeSpec2D& node_data = node_it->data;
  131. const std::unique_ptr<transform::Rigid3d> fixed_frame_pose =
  132. Interpolate(fixed_frame_pose_data_, trajectory_id, node_data.time);
  133. if (fixed_frame_pose == nullptr) {
  134. continue;
  135. }
  136. const Constraint::Pose constraint_pose{
  137. *fixed_frame_pose, options_.fixed_frame_pose_translation_weight(),
  138. options_.fixed_frame_pose_rotation_weight()};
  139. if (!fixed_frame_pose_initialized) {
  140. transform::Rigid2d fixed_frame_pose_in_map;
  141. if (trajectory_data.fixed_frame_origin_in_map.has_value()) {
  142. fixed_frame_pose_in_map = transform::Project2D(
  143. trajectory_data.fixed_frame_origin_in_map.value());
  144. } else {
  145. fixed_frame_pose_in_map =
  146. node_data.global_pose_2d *
  147. transform::Project2D(constraint_pose.zbar_ij).inverse();
  148. }
  149. C_fixed_frames.emplace(trajectory_id,
  150. FromPose(fixed_frame_pose_in_map));
  151. fixed_frame_pose_initialized = true;
  152. }
  153. problem.AddResidualBlock(
  154. CreateAutoDiffSpaCostFunction(constraint_pose),
  155. options_.fixed_frame_pose_use_tolerant_loss()
  156. ? new ceres::TolerantLoss(
  157. options_.fixed_frame_pose_tolerant_loss_param_a(),
  158. options_.fixed_frame_pose_tolerant_loss_param_b())
  159. : nullptr,
  160. C_fixed_frames.at(trajectory_id).data(), C_nodes.at(node_id).data());
  161. }
  162. }
  163. // Solve.
  164. //描述完优化参数和残差计算方式之后,通过ceres求解优化问题
  165. ceres::Solver::Summary summary;
  166. ceres::Solve(
  167. common::CreateCeresSolverOptions(options_.ceres_solver_options()),
  168. &problem, &summary);
  169. if (options_.log_solver_summary()) {
  170. LOG(INFO) << summary.FullReport();
  171. }
  172. // Store the result.
  173. //存储优化结果
  174. for (const auto& C_submap_id_data : C_submaps) {
  175. submap_data_.at(C_submap_id_data.id).global_pose =
  176. ToPose(C_submap_id_data.data);
  177. }
  178. for (const auto& C_node_id_data : C_nodes) {
  179. node_data_.at(C_node_id_data.id).global_pose_2d =
  180. ToPose(C_node_id_data.data);
  181. }
  182. for (const auto& C_fixed_frame : C_fixed_frames) {
  183. trajectory_data_.at(C_fixed_frame.first).fixed_frame_origin_in_map =
  184. transform::Embed3D(ToPose(C_fixed_frame.second));
  185. }
  186. for (const auto& C_landmark : C_landmarks) {
  187. landmark_data_[C_landmark.first] = C_landmark.second.ToRigid();
  188. }
  189. }

        Solve函数在接受子图、节点、轨迹等的位姿信息,确定优化目标后,对他们的各自的位姿信息做出调整,完成全局优化,整个optimization_problem也就完成了

四、结语

        整个cartographer_2d的主题流程基本梳理完了,历时半个月,感觉也只是把整个工程理了一遍,里面其实有好多具体的问题和原理笔者并没有展开分析,后续可能会根据这套工程做一些改编和利用,如果有机会的话,再把遇到的问题和思路发上来,欢迎大家多多交流

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/557939
推荐阅读
相关标签
  

闽ICP备14008679号