wj_716N_lidar_protocol.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. #include <iostream>
  3. #include <mutex>
  4. #include <chrono>
  5. #include <math.h>
  6. #include "glog/logging.h"
  7. #include "string.h"
  8. #include "error_code/error_code.hpp"
  9. #include "tool/binary_buf.h"
  10. #include "tool/thread_safe_queue.hpp"
  11. #include "tool/thread_condition.h"
  12. #include "tool/point2D_tool.h"
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/asio.hpp>
  15. #include <boost/asio/placeholders.hpp>
  16. #include <boost/system/error_code.hpp>
  17. #include <boost/bind/bind.hpp>
  18. #include "pcl/point_types.h"
  19. #include "pcl/point_cloud.h"
  20. #include "pcl/conversions.h"
  21. #include "pcl/common/common.h"
  22. typedef struct {
  23. std::string frame_id = "laser";
  24. double min_ang = -2.35619449;
  25. double max_ang = 2.35619449;
  26. double range_min = 0.0;
  27. double range_max = 30.0;
  28. int frequency_scan = 1;
  29. } wj_716_lidarConfig;
  30. #define MAX_LENGTH_DATA_PROCESS 200000
  31. typedef struct TagDataCache {
  32. unsigned char m_acdata[MAX_LENGTH_DATA_PROCESS];
  33. unsigned int m_u32in;
  34. unsigned int m_u32out;
  35. } DataCache;
  36. struct Stamp // Laser内参
  37. {
  38. long msecs;
  39. };
  40. struct Header // Laser内参
  41. {
  42. int seq;
  43. //替换成只用ms表示
  44. Stamp stamp;
  45. std::string frame_id;
  46. };
  47. struct Laser // 雷达数据及雷达参数
  48. {
  49. Header header;
  50. double angle_min;
  51. double angle_max;
  52. double angle_increment;
  53. double range_min;
  54. double range_max;
  55. double time_increment;
  56. long long scan_time;
  57. std::vector<double> ranges;
  58. std::vector<double> x;
  59. std::vector<double> y;
  60. std::vector<double> angle_to_point;
  61. std::vector<double> intensities;
  62. };
  63. class wj_716N_lidar_protocol {
  64. public:
  65. wj_716N_lidar_protocol();
  66. // 初始化
  67. Error_manager init(Thread_safe_queue<Binary_buf *> *p_communication_data_queue,
  68. Point2D_tool::Polar_coordinates_box polar_coordinates_box,
  69. Point2D_tool::Point2D_box point2D_box,
  70. Point2D_tool::Point2D_transform point2D_transform);
  71. //反初始化
  72. Error_manager uninit();
  73. //获取扫描周期
  74. int get_scan_cycle() const;
  75. // 获取扫描点云
  76. Error_manager get_scan_points(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_out);
  77. // 获取扫描点云更新时间点
  78. std::chrono::system_clock::time_point get_scan_time() const;
  79. bool setConfig(wj_716_lidarConfig &new_config, uint32_t level);
  80. void update_data(int index);
  81. private:
  82. //解析线程函数
  83. void thread_analysis();
  84. void movedata(DataCache &sdata);
  85. bool dataProcess(unsigned char *data, int reclen);
  86. bool OnRecvProcess(unsigned char *data, int len);
  87. bool checkXor(unsigned char *recvbuf, int recvlen);
  88. bool protocl(unsigned char *data, int len);
  89. public:
  90. Laser scan;
  91. int freq_scan;
  92. int resizeNum;
  93. int index_start;
  94. int index_end;
  95. bool heartstate;
  96. int total_point;
  97. std::mutex m_read_write_mtx; // 点云获取互斥锁
  98. std::mutex m_scan_mutex; // 扫描点的锁
  99. Thread_safe_queue<Binary_buf *> *mp_communication_data_queue; //通信数据队列, 内存由上级管理
  100. // 雷达扫描点平面坐标
  101. pcl::PointCloud<pcl::PointXYZ>::Ptr mp_scan_points; //扫描点的点云, 内存由本类管理
  102. // 控制参数
  103. Point2D_tool::Polar_coordinates_box m_polar_coordinates_box; //极坐标的限定范围
  104. Point2D_tool::Point2D_box m_point2D_box; //平面坐标的限定范围
  105. Point2D_tool::Point2D_transform m_point2D_transform; //平面坐标的转换矩阵
  106. // 解析数据的线程, 自动从队列取出原始数据, 然后解析为点云
  107. std::thread *mp_thread_analysis; // 解析线程, 内存由本模块管理
  108. Thread_condition m_condition_analysis; //解析线程的条件变量
  109. private:
  110. DataCache m_sdata;
  111. wj_716_lidarConfig config_;
  112. unsigned int m_u32PreFrameNo;
  113. unsigned int m_u32ExpectedPackageNo;
  114. int m_n32currentDataNo;
  115. float scandata[1081];
  116. float scanintensity[1081];
  117. };