ceres_scan_matcher_2d.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "ceres_matcher/ceres_scan_matcher_2d.h"
  2. namespace hlzn_slam {
  3. namespace ceres_matching {
  4. CeresScanMatcher2D::CeresScanMatcher2D(const double& occupied_space_weight, const double& translation_weight,
  5. const double& rotation_weight, const double& self_stab_weight)
  6. {
  7. occupied_space_weight_ = occupied_space_weight;
  8. translation_weight_ = translation_weight;
  9. rotation_weight_ = rotation_weight;
  10. self_stab_weight_ = self_stab_weight;
  11. ceres_solver_options_.linear_solver_type = ceres::DENSE_QR;
  12. ceres_solver_options_.use_nonmonotonic_steps = true;
  13. ceres_solver_options_.max_num_iterations = 20;
  14. ceres_solver_options_.num_threads = 1;
  15. ceres_solver_options_.minimizer_progress_to_stdout = false;
  16. }
  17. CeresScanMatcher2D::~CeresScanMatcher2D() {}
  18. void CeresScanMatcher2D::Match(const common::Rigid2f& target_pose,
  19. const common::Rigid2f& initial_pose_estimate,
  20. const common::PointCloud& point_cloud,
  21. Map& map,
  22. common::Rigid2f* const pose_estimate,
  23. ceres::Solver::Summary* const summary) const {
  24. double ceres_pose_estimate[3] = {initial_pose_estimate.translation()(0),
  25. initial_pose_estimate.translation()(1),
  26. initial_pose_estimate.rotation().angle()};
  27. Eigen::Vector2d target_translation = Eigen::Vector2d(target_pose.translation()(0), target_pose.translation()(1));
  28. ceres::Problem problem;
  29. problem.AddResidualBlock(
  30. CreateOccupiedSpaceCostFunction2D(
  31. occupied_space_weight_ / std::sqrt(static_cast<double>(point_cloud.data.size())),
  32. point_cloud, map),
  33. nullptr /* loss function */, ceres_pose_estimate);
  34. problem.AddResidualBlock(
  35. TranslationDeltaCostFunctor2D::CreateAutoDiffCostFunction(
  36. translation_weight_, target_translation), //10.0
  37. nullptr /* loss function */, ceres_pose_estimate);
  38. problem.AddResidualBlock(
  39. RotationDeltaCostFunctor2D::CreateAutoDiffCostFunction(
  40. rotation_weight_, ceres_pose_estimate[2]), // 40.0
  41. nullptr /* loss function */, ceres_pose_estimate);
  42. // double angle = target_pose.rotation().angle();
  43. // if ((ceres_pose_estimate[2] - angle) > M_PI) {
  44. // angle += M_PI * 2.0;
  45. // }
  46. // else if ((ceres_pose_estimate[2] - angle) < -M_PI){
  47. // angle -= M_PI * 2.0;
  48. // }
  49. // problem.AddResidualBlock(
  50. // RotationDeltaCostFunctor2D::CreateAutoDiffCostFunction(
  51. // self_stab_weight_, target_pose.rotation().angle()), // 6.0
  52. // nullptr /* loss function */, ceres_pose_estimate);
  53. ceres::Solve(ceres_solver_options_, &problem, summary);
  54. *pose_estimate = common::Rigid2f(
  55. {ceres_pose_estimate[0], ceres_pose_estimate[1]}, ceres_pose_estimate[2]);
  56. }
  57. } // namespace scan_matching
  58. } // namespace cartographer