wj_716N_lidar_protocol.h 4.3 KB

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