slam.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <thread>
  2. #include <mutex>
  3. #include <deque>
  4. #include "map/map.h"
  5. #include "odom/odom.h"
  6. #include "ceres_matcher/ceres_scan_matcher_2d.h"
  7. #include "real_match_2d/real_match_2d.h"
  8. namespace hlzn_slam {
  9. namespace slam {
  10. using namespace ceres_matching;
  11. typedef struct __con con;
  12. struct SubMap {
  13. SubMap(float size, float resolution) : next(nullptr)
  14. {
  15. map = std::shared_ptr<Map>(new Map(size, size, resolution));
  16. }
  17. ~SubMap()
  18. {
  19. map = nullptr;
  20. next = nullptr;
  21. }
  22. std::shared_ptr<Map> map;
  23. common::PointCloud point_cloud;
  24. common::Rigid2f track_pose;
  25. common::Rigid2f global_pose;
  26. std::shared_ptr<con> next;
  27. };
  28. struct __con {
  29. __con() : submap(nullptr)
  30. {}
  31. ~__con()
  32. {
  33. submap = nullptr;
  34. }
  35. std::shared_ptr<SubMap> submap;
  36. common::Rigid2f transform;
  37. };
  38. class Slam {
  39. public:
  40. Slam(std::unique_ptr<Map> global_map);
  41. ~Slam();
  42. /**
  43. * 说明:初始化slam模块,定义初始位置
  44. * @param [in] init:初始位置
  45. * @param [in] is_fast: 是否快速定位; true: 是 false: 否
  46. */
  47. void init(const common::Rigid2f& init, bool is_fast, float first_search_range = 5.0, float first_search_angle = M_PI);
  48. /**
  49. * 说明:设置定位参数
  50. * @param [in] search_range: 线性搜索范围
  51. * @param [in] search_angle: 角度搜索范围
  52. * @param [in] use_towermatch: false, true
  53. * @param [in] probability_plus: 概率步长
  54. * @param [in] probability_up: 概率上限
  55. * @param [in] window_size: 滑动窗口数量
  56. * @param [in] achieve_range: 窗口分离距离值
  57. * @param [in] achieve_angle: 窗口分离角度值
  58. */
  59. void setParam(float search_range = 0.3, float search_angle = 0.1, bool use_towermatch = true, float probability_plus = 0.2, float probability_up = 0.8,
  60. int window_size = 20, float achieve_range = 0.5, float achieve_angle = 0.4, float submap_size = 15.0, float submap_resolution = 0.05,
  61. bool use_odom = true);
  62. void addPointCloud(common::PointCloud point_cloud, common::laserScan scan, Eigen::Matrix<float, 4, 4> tf);
  63. void addOriginScan(common::laserScan& scan, Eigen::Matrix<float, 4, 4>& tf);
  64. void addOdom2DVelocity(float& v, float& w);
  65. void addOdom2D(float& x, float& y, float& theta);
  66. bool isInitialize();
  67. common::TimeRigid2f getCurrentPose();
  68. private:
  69. common::Rigid2f __Match(common::Rigid2f& pose,
  70. common::PointCloud& point_cloud, Map& map,
  71. const float& range,
  72. const float& rad,
  73. const float occupied_space_weight = 1.0,
  74. const float translation_weight = 2.0,
  75. const float rotation_weight = 3.0,
  76. const float self_stab_weight = 1.0);
  77. common::PointCloud __transformPointCloud(
  78. const common::PointCloud& point_cloud, const common::Rigid2f& transform);
  79. void __poseGraph(common::Rigid2f& global_pose, common::Rigid2f& track_pose);
  80. void __poseGraph2(common::Rigid2f& global_pose);
  81. void __createNewSubmap(common::PointCloud& point_cloud, common::Rigid2f& global_pose, common::Rigid2f& track_pose);
  82. void __slam(common::PointCloud point_cloud);
  83. void __Rigid2f2Double7(common::Rigid2f& rigid2f, double* d7);
  84. void __double7ToRigid2f(double* d7, common::Rigid2f& rigid2f);
  85. void __writeTimePose(common::TimeRigid2f time_pose);
  86. void __buildSubmap(common::Rigid2f pose);
  87. void __growGlobalmap(common::Rigid2f pose);
  88. void __Rigid2fTranslation2Double3(common::Rigid2f& rigid2f, double* d3);
  89. void __Rigid2fQuaterniond2Double4(common::Rigid2f& rigid2f, double* d4);
  90. void __double34ToRigid2f(double* d3, double* d4, common::Rigid2f& rigid2f);
  91. void __optimization(common::PointCloud& point_cloud, common::Rigid2f& track_pose, common::Rigid2f& global_pose);
  92. void __globalMapBeamMode(common::Rigid2f& global_tf);
  93. bool __frameParallaxAchieve(common::Rigid2f& pose, float achieve_range, float achieve_angle);
  94. common::PointCloud __transformScanData2PointCloudError(float error, const float angle_increment = 0.0);
  95. common::TimeRigid2f time_pose_;
  96. common::TimeRigid2f odom_pose_;
  97. std::deque<std::shared_ptr<SubMap>> submaps_;
  98. std::unique_ptr<Map> global_map_;
  99. std::unique_ptr<odom::odom> odom_;
  100. bool is_initialize_;
  101. std::thread matchThread_;
  102. common::laserScan scan_;
  103. Eigen::Matrix<float, 4, 4> laser_tf_;
  104. std::mutex time_pose_lock_;
  105. #define TIME_POSE_LOCK() std::lock_guard<std::mutex> lockGuard(time_pose_lock_);
  106. private:
  107. bool use_towermatch_;
  108. bool use_odom_;
  109. float probability_plus_;
  110. float probability_up_;
  111. float search_range_;
  112. float search_angle_;
  113. float window_size_;
  114. float achieve_range_;
  115. float achieve_angle_;
  116. float submap_size_;
  117. float submap_resolution_;
  118. /**/
  119. float first_search_range_;
  120. float first_search_angle_;
  121. /**/
  122. float v_, w_;
  123. };
  124. }
  125. }