wj_716N_lidar_protocol.h 4.3 KB

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