Laser.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef __LASER__HH__
  2. #define __LASER__HH__
  3. #include "Point2D.h"
  4. #include "Point3D.h"
  5. #include "LogFiles.h"
  6. #include "src/CalibParam.pb.h"
  7. #include "src/StdCondition.h"
  8. enum DATA_type
  9. {
  10. eStart=0
  11. ,eReady
  12. ,eData
  13. ,eStop
  14. ,eHerror
  15. ,eUnknow
  16. };
  17. class CBinaryData
  18. {
  19. public:
  20. CBinaryData();
  21. CBinaryData(const CBinaryData& data);
  22. ~CBinaryData();
  23. CBinaryData(const char* buf, int len, DATA_type type= eUnknow);
  24. CBinaryData& operator=(const CBinaryData& data);
  25. bool operator==(const char* str);
  26. const char* operator+(int n);
  27. CBinaryData& operator+(CBinaryData& data);
  28. char& operator[](int n);
  29. char* Data()const;
  30. int Length()const;
  31. protected:
  32. char* m_buf;
  33. int m_length;
  34. };
  35. #include <queue>
  36. #include <memory>
  37. #include <mutex>
  38. #include <condition_variable>
  39. template<typename T>
  40. class threadsafe_queue
  41. {
  42. private:
  43. mutable std::mutex mut;
  44. std::queue<T> data_queue;
  45. std::condition_variable data_cond;
  46. public:
  47. threadsafe_queue() {}
  48. threadsafe_queue(threadsafe_queue const& other)
  49. {
  50. std::lock_guard<std::mutex> lk(other.mut);
  51. data_queue = other.data_queue;
  52. }
  53. ~threadsafe_queue()
  54. {
  55. while (!empty())
  56. {
  57. try_pop();
  58. }
  59. }
  60. size_t size()
  61. {
  62. return data_queue.size();
  63. }
  64. void push(T new_value)//入队操作
  65. {
  66. std::lock_guard<std::mutex> lk(mut);
  67. data_queue.push(new_value);
  68. data_cond.notify_one();
  69. }
  70. void wait_and_pop(T& value)//直到有元素可以删除为止
  71. {
  72. std::unique_lock<std::mutex> lk(mut);
  73. data_cond.wait(lk, [this] {return !data_queue.empty(); });
  74. value = data_queue.front();
  75. data_queue.pop();
  76. }
  77. std::shared_ptr<T> wait_and_pop()
  78. {
  79. std::unique_lock<std::mutex> lk(mut);
  80. data_cond.wait(lk, [this] {return !data_queue.empty(); });
  81. std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
  82. data_queue.pop();
  83. return res;
  84. }
  85. //只访问 不 pop
  86. bool front(T& value)
  87. {
  88. std::lock_guard<std::mutex> lk(mut);
  89. if (data_queue.empty())
  90. return false;
  91. value = data_queue.front();
  92. return true;
  93. }
  94. bool try_pop(T& value)//不管有没有队首元素直接返回
  95. {
  96. if (data_queue.empty())
  97. return false;
  98. std::lock_guard<std::mutex> lk(mut);
  99. value = data_queue.front();
  100. data_queue.pop();
  101. return true;
  102. }
  103. std::shared_ptr<T> try_pop()
  104. {
  105. std::lock_guard<std::mutex> lk(mut);
  106. if (data_queue.empty())
  107. return std::shared_ptr<T>();
  108. std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
  109. data_queue.pop();
  110. return res;
  111. }
  112. bool empty() const
  113. {
  114. std::lock_guard<std::mutex> lk(mut);
  115. return data_queue.empty();
  116. }
  117. void clear()
  118. {
  119. while (!empty()) {
  120. try_pop();
  121. }
  122. }
  123. };
  124. //////////////
  125. enum eLaserStatu
  126. {
  127. eLaser_connect=0,
  128. eLaser_ready , //空闲状态
  129. eLaser_busy //扫描中
  130. , eLaser_disconnect //断线
  131. };
  132. typedef void (*PointCallBack)(CPoint3D , void* );
  133. class CLaser
  134. {
  135. public:
  136. CLaser(int id,Automatic::stLaserCalibParam laser_param);
  137. virtual ~CLaser();
  138. ///连接数据源
  139. virtual bool Connect();
  140. virtual void Disconnect();
  141. ///开始、停止采集
  142. virtual bool Start();
  143. virtual bool Stop();
  144. ///设置点云变换矩阵(标定参数)
  145. void SetMetrix(double* data);
  146. ///设置获取三维点回调函数
  147. void SetPointCallBack(PointCallBack fnc,void* pointer);
  148. public:
  149. ///设置数据存储路径
  150. void SetSaveDir(std::string strDir,bool bSave=true);
  151. ///查询雷达是否空闲
  152. bool IsReady() { return m_statu == eLaser_ready || m_statu==eLaser_connect; }
  153. eLaserStatu GetStatu(){return m_statu;}
  154. int ID() { return m_id; }
  155. protected:
  156. ////获取原始数据包
  157. virtual bool RecvData(CBinaryData& data) = 0;
  158. ////解析原始数据包成点云
  159. virtual DATA_type Data2PointXYZ(CBinaryData* pData, std::vector<CPoint3D>& points)=0;
  160. void thread_recv();
  161. void thread_toXYZ();
  162. ////点云变换
  163. virtual CPoint3D transfor(CPoint3D point);
  164. protected:
  165. std::thread* m_ThreadRcv;
  166. std::thread* m_ThreadPro;
  167. StdCondition m_bThreadRcvRun;
  168. StdCondition m_bThreadProRun;
  169. int m_id;
  170. eLaserStatu m_statu;
  171. bool m_bSave_file;
  172. Automatic::stLaserCalibParam m_laser_param;////配置参数
  173. //数据处理相关
  174. threadsafe_queue<CBinaryData*> m_queue_laser_data; // 数据队列
  175. CBinaryData m_last_data; //上一报数据中未解析完的
  176. double* m_dMatrix;
  177. PointCallBack m_point_callback_fnc;
  178. void* m_point_callback_pointer;
  179. //数据存储
  180. CLogFile m_binary_log_tool; //存储二进制
  181. CLogFile m_pts_log_tool; //存储点云
  182. std::string m_pts_save_path;
  183. StdCondition m_bStart_capture;
  184. };
  185. class LaserRegistory
  186. {
  187. typedef CLaser* (*CreateLaserFunc)(int id, Automatic::stLaserCalibParam laser_param);
  188. public:
  189. LaserRegistory(std::string name, CreateLaserFunc pFun) {
  190. AddCreator(name, pFun);
  191. }
  192. static CLaser* CreateLaser(std::string name, int id,Automatic::stLaserCalibParam laser_param) {
  193. if (GetFuncMap().count(name) == 0)
  194. return 0;
  195. return GetFuncMap()[name](id,laser_param);
  196. }
  197. private:
  198. static std::map<std::string, CreateLaserFunc>& GetFuncMap() {
  199. static std::map<std::string, CreateLaserFunc>* g_map = new std::map<std::string, CreateLaserFunc>;
  200. return *g_map;
  201. }
  202. void AddCreator(std::string name, CreateLaserFunc pFun) {
  203. GetFuncMap()[name] = pFun;
  204. }
  205. };
  206. #define RegisterLaser(NAME) \
  207. static CLaser* Create_##NAME##_Laser(int id, Automatic::stLaserCalibParam param) \
  208. { \
  209. return new C##NAME##Laser(id,param); \
  210. } \
  211. LaserRegistory g_##NAME##_Laser(#NAME,Create_##NAME##_Laser);
  212. #endif