velodyne_manager.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * @Description: velodyne多线雷达管理类
  3. * 非分布式模式下,将采集所有雷达点云,并下发给所有区域
  4. * 分布式模式下,程序各自独立,分别只实例化一个设定的区域,以及相应雷达
  5. * @Author: yct
  6. * @Date: 2021-07-23 16:37:33
  7. * @LastEditTime: 2021-09-03 16:20:26
  8. * @LastEditors: yct
  9. */
  10. #ifndef VELODYNE_MANAGER_HH
  11. #define VELODYNE_MANAGER_HH
  12. #include <thread>
  13. #include <map>
  14. #include <memory>
  15. #include "tool/singleton.h"
  16. #include "tool/point_tool.h"
  17. #include "error_code/error_code.hpp"
  18. #include "velodyne_driver/velodyne_lidar_device.h"
  19. #include "ground_region.h"
  20. #include "velodyne_config.pb.h"
  21. #include "velodyne_manager_task.h"
  22. #include "rslidar_mqtt_async.h"
  23. struct Timestamped_cloud {
  24. public:
  25. Timestamped_cloud(velodyne::CalibParameter calib) {
  26. m_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  27. m_time_collected = std::chrono::system_clock::now();
  28. m_extrinsic.CopyFrom(calib);
  29. updateMatrix();
  30. }
  31. Timestamped_cloud(velodyne::CalibParameter calib, pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud) {
  32. m_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  33. m_cloud->operator+=(*input_cloud);
  34. m_time_collected = std::chrono::system_clock::now();
  35. m_extrinsic.CopyFrom(calib);
  36. updateMatrix();
  37. }
  38. ~Timestamped_cloud() {}
  39. // 设置点云,覆盖
  40. void setCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud) {
  41. m_cloud->clear();
  42. m_cloud->operator+=(*input_cloud);
  43. m_time_collected = std::chrono::system_clock::now();
  44. }
  45. // 添加点云至现有点云中
  46. void addCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud) {
  47. m_cloud->operator+=(*input_cloud);
  48. m_time_collected = std::chrono::system_clock::now();
  49. }
  50. void clearCloud() {
  51. m_cloud->clear();
  52. m_time_collected = std::chrono::system_clock::now();
  53. }
  54. // 更新外参矩阵
  55. void updateMatrix() {
  56. Eigen::AngleAxisd rot_x = Eigen::AngleAxisd(m_extrinsic.r() * M_PI / 180.0, Eigen::Vector3d::UnitX());
  57. Eigen::AngleAxisd rot_y = Eigen::AngleAxisd(m_extrinsic.p() * M_PI / 180.0, Eigen::Vector3d::UnitY());
  58. Eigen::AngleAxisd rot_z = Eigen::AngleAxisd(m_extrinsic.y() * M_PI / 180.0, Eigen::Vector3d::UnitZ());
  59. Eigen::Vector3d trans(m_extrinsic.cx(), m_extrinsic.cy(), m_extrinsic.cz());
  60. m_extrin_matrix << rot_x.toRotationMatrix() * rot_y.toRotationMatrix() *
  61. rot_z.toRotationMatrix(), trans, 0.0, 0.0, 0.0, 1.0;
  62. }
  63. // 使用外参返回变换后点云
  64. pcl::PointCloud<pcl::PointXYZ>::Ptr transCloud() {
  65. pcl::PointCloud<pcl::PointXYZ>::Ptr t_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  66. if (m_cloud->size() > 0) {
  67. for (size_t i = 0; i < m_cloud->size(); i++) {
  68. Eigen::Vector4d t_point(m_cloud->points[i].x, m_cloud->points[i].y, m_cloud->points[i].z, 1.0);
  69. t_point.x() = m_extrin_matrix(0, 0) * t_point.x() + m_extrin_matrix(0, 1) * t_point.y() +
  70. m_extrin_matrix(0, 2) * t_point.z() + m_extrin_matrix(0, 3) * t_point.w();
  71. t_point.y() = m_extrin_matrix(1, 0) * t_point.x() + m_extrin_matrix(1, 1) * t_point.y() +
  72. m_extrin_matrix(1, 2) * t_point.z() + m_extrin_matrix(1, 3) * t_point.w();
  73. t_point.z() = m_extrin_matrix(2, 0) * t_point.x() + m_extrin_matrix(2, 1) * t_point.y() +
  74. m_extrin_matrix(2, 2) * t_point.z() + m_extrin_matrix(2, 3) * t_point.w();
  75. t_point.w() = m_extrin_matrix(3, 0) * t_point.x() + m_extrin_matrix(3, 1) * t_point.y() +
  76. m_extrin_matrix(3, 2) * t_point.z() + m_extrin_matrix(3, 3) * t_point.w();
  77. pcl::PointXYZ t_pcl_point;
  78. t_pcl_point.x = t_point.x() / t_point.w();
  79. t_pcl_point.y = t_point.y() / t_point.w();
  80. t_pcl_point.z = t_point.z() / t_point.w();
  81. t_cloud->push_back(t_pcl_point);
  82. }
  83. }
  84. // t_cloud->operator+=(*m_cloud);
  85. return t_cloud;
  86. }
  87. // 时间戳
  88. std::chrono::system_clock::time_point m_time_collected;
  89. // 点云
  90. pcl::PointCloud<pcl::PointXYZ>::Ptr m_cloud;
  91. // 外参数据
  92. velodyne::CalibParameter m_extrinsic;
  93. // 外参齐次矩阵
  94. Eigen::Matrix4d m_extrin_matrix;
  95. };
  96. class Velodyne_manager : public Singleton<Velodyne_manager> {
  97. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  98. friend class Singleton<Velodyne_manager>;
  99. public:
  100. //velodyne管理任务超时时间1000ms,
  101. #define VELODYNE_MANAGER_EXECUTE_TIMEOUT_MS 1000
  102. //velodyne配置参数的默认路径
  103. const char *VELODYNE_MANAGER_PARAMETER_PATH = ETC_PATH"MeasureNode/velodyne_manager.prototxt";
  104. const char *LEFT_WHEEL_MODEL = ETC_PATH"MeasureNode/left_model.txt";
  105. const char *RIGHT_WHEEL_MODEL = ETC_PATH"MeasureNode/right_model.txt";
  106. //雷达管理的状态
  107. enum Velodyne_manager_status {
  108. E_UNKNOWN = 0, //未知
  109. E_READY = 1, //准备,待机
  110. E_BUSY = 2, //工作正忙
  111. E_ISSUED_SCAN = 3, //下发任务, 获取点云
  112. E_WAIT_SCAN = 4, //等待任务, 扫描点云
  113. E_ISSUED_DETECT = 5, //下发任务, 算法预测
  114. E_WAIT_DETECT = 6, //等待任务, 算法预测
  115. E_FAULT = 10, //故障
  116. };
  117. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  118. Velodyne_manager(const Velodyne_manager &) = delete;
  119. Velodyne_manager &operator=(const Velodyne_manager &) = delete;
  120. ~Velodyne_manager() = default;
  121. //初始化 雷达 管理模块。如下三选一
  122. Error_manager velodyne_manager_init(int terminal = 0);
  123. //初始化 雷达 管理模块。从文件读取
  124. Error_manager velodyne_manager_init_from_protobuf(std::string prototxt_path, int terminal = 0);
  125. //初始化 雷达 管理模块。从protobuf读取
  126. Error_manager
  127. velodyne_manager_init_from_protobuf(velodyne::velodyneManagerParams &velodyne_parameters, int terminal = 0);
  128. // 反初始化
  129. Error_manager Velodyne_manager_uninit();
  130. //对外的接口函数,负责接受并处理任务单,
  131. Error_manager execute_task(Task_Base *p_velodyne_manager_task);
  132. //结束任务单,里面会根据任务的故障等级修正雷达管理模块的状态和任务单的状态
  133. Error_manager end_task();
  134. //取消任务单,由发送方提前取消任务单
  135. Error_manager cancel_task(Task_Base *p_velodyne_manager_task);
  136. //检查雷达状态,是否正常运行
  137. Error_manager check_status();
  138. //判断是否为待机,如果已经准备好,则可以执行任务。
  139. bool is_ready() { return m_velodyne_manager_status == E_READY; }
  140. public://get or set member variable
  141. Velodyne_manager_status get_status() { return m_velodyne_manager_status; }
  142. std::map<int, Velodyne_lidar_device *> &get_velodyne_lidar_device_map() { return m_velodyne_lidar_device_map; }
  143. std::map<int, Ground_region *> &get_ground_region_map() { return m_ground_region_map; }
  144. protected://member functions
  145. //自动收集点云的线程函数
  146. void collect_cloud_thread_fun();
  147. //更新点云,自行计算
  148. Error_manager update_region_cloud();
  149. //执行外界任务的执行函数
  150. void execute_thread_fun();
  151. // 根据区域与激光id生成唯一string作为key
  152. std::string generate_region_lidar_key(int region_id, int lidar_id) {
  153. return std::to_string(region_id) + std::string("-") + std::to_string(lidar_id);
  154. }
  155. private:
  156. // 父类的构造函数必须保护,子类的构造函数必须私有。
  157. Velodyne_manager() = default;
  158. int m_terminal_id;
  159. Velodyne_manager_status m_velodyne_manager_status; //velodyne管理状态
  160. std::map<int, Velodyne_lidar_device *> m_velodyne_lidar_device_map; // velodyne雷达实例指针数组, 内存由本类管理
  161. CloudDataMqttAsync *m_mqtt_cloud_data;
  162. std::map<int, Ground_region *> m_ground_region_map; // 区域功能实例指针数组, 内存由本类管理
  163. //velodyne雷达的自动功能, 定时收集所有点云, 然后通知detect去计算车轮信息.
  164. std::mutex m_cloud_collection_mutex; // 点云更新互斥锁
  165. std::map<std::string, std::shared_ptr<Timestamped_cloud>> m_region_laser_to_cloud_collection_map; //扫描点的点云集合, 内存由本类管理
  166. // std::chrono::system_clock::time_point m_cloud_update_time; //扫描点的更新时间. 已被时间戳点云替换
  167. std::thread *mp_collect_cloud_thread; //收集点云的线程指针,内存由本类管理
  168. Thread_condition m_collect_cloud_condition; //收集点云的条件变量
  169. //任务执行线程
  170. std::thread *mp_execute_thread; //执行的线程指针,内存由本类管理
  171. Thread_condition m_execute_condition; //执行的条件变量
  172. Velodyne_manager_task *mp_velodyne_manager_task; //velodyne管理模块的任务单的指针,内存由发送方管理。
  173. };
  174. #endif // !VELODYNE_MANAGER_HH