Laser.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #include "Laser.h"
  2. #include "laser_message.pb.h"
  3. Laser_base::Laser_base(int laser_id, Laser_proto::laser_parameter laser_param)
  4. :m_laser_id(laser_id)
  5. ,m_laser_param(laser_param)
  6. ,m_laser_scan_flag(false)
  7. ,m_laser_statu(LASER_DISCONNECT)
  8. ,m_points_count(0)
  9. ,m_save_flag(false)
  10. {
  11. mp_thread_receive = NULL;
  12. mp_thread_transform = NULL;
  13. mp_thread_publish = NULL;
  14. init_laser_matrix();
  15. mp_laser_task = NULL;
  16. }
  17. Laser_base::~Laser_base()
  18. {
  19. disconnect_laser();
  20. close_save_path();
  21. }
  22. //雷达链接设备,为3个线程添加线程执行函数。
  23. Error_manager Laser_base::connect_laser()
  24. {
  25. //检查雷达状态
  26. if ( m_laser_statu != LASER_DISCONNECT )
  27. {
  28. return Error_manager(Error_code::LASER_STATUS_ERROR, Error_level::MINOR_ERROR,
  29. " Laser_base::connect_laser m_laser_statu != LASER_DISCONNECT ");
  30. }
  31. else
  32. {
  33. //这里不建议用detach,而是应该在disconnect里面使用join
  34. //创建接受缓存的线程,不允许重复创建
  35. if (mp_thread_receive != NULL)
  36. {
  37. return Error_manager(Error_code::LASER_CONNECT_FAILED, Error_level::MINOR_ERROR,
  38. " mp_thread_receive is alive ");
  39. }
  40. else
  41. {
  42. //接受缓存的线程,线程存活,暂停运行
  43. m_condition_receive.reset(false, false, false);
  44. mp_thread_receive = new std::thread(&Laser_base::thread_receive, this);
  45. }
  46. //转化数据的线程,不允许重复创建
  47. if (mp_thread_transform != NULL)
  48. {
  49. return Error_manager(Error_code::LASER_CONNECT_FAILED, Error_level::MINOR_ERROR,
  50. " mp_thread_transform is alive ");
  51. }
  52. else
  53. {
  54. //转化数据的线程,线程存活,暂停运行
  55. m_condition_transform.reset(false, false, false);
  56. mp_thread_transform = new std::thread(&Laser_base::thread_transform, this);
  57. }
  58. //发布信息的线程,不允许重复创建
  59. if (mp_thread_publish != NULL)
  60. {
  61. return Error_manager(Error_code::LASER_CONNECT_FAILED, Error_level::MINOR_ERROR,
  62. " mp_thread_publish is alive ");
  63. }
  64. else
  65. {
  66. //发布信息的线程,线程存活,循环运行,
  67. //注:mp_thread_publish 需要线程持续不断的往上位机发送信息,不受任务的影响。
  68. m_condition_publish.reset(false, true, false);
  69. mp_thread_publish = new std::thread(&Laser_base::thread_publish, this);
  70. }
  71. LOG(INFO)<<"laser connected success";
  72. m_laser_statu = LASER_READY;
  73. }
  74. return Error_code::SUCCESS;
  75. }
  76. //雷达断开链接,释放3个线程
  77. Error_manager Laser_base::disconnect_laser()
  78. {
  79. //终止队列,防止 wait_and_pop 阻塞线程。
  80. m_queue_laser_data.termination_queue();
  81. //杀死3个线程,强制退出
  82. if (mp_thread_receive)
  83. {
  84. m_condition_receive.kill_all();
  85. }
  86. if (mp_thread_transform)
  87. {
  88. m_condition_transform.kill_all();
  89. }
  90. if (mp_thread_publish)
  91. {
  92. m_condition_publish.kill_all();
  93. }
  94. //回收3个线程的资源
  95. if (mp_thread_receive)
  96. {
  97. mp_thread_receive->join();
  98. delete mp_thread_receive;
  99. mp_thread_receive = NULL;
  100. }
  101. if (mp_thread_transform)
  102. {
  103. mp_thread_transform->join();
  104. delete mp_thread_transform;
  105. mp_thread_transform = 0;
  106. }
  107. if (mp_thread_publish)
  108. {
  109. mp_thread_publish->join();
  110. delete mp_thread_publish;
  111. mp_thread_publish = NULL;
  112. }
  113. //清空队列
  114. m_queue_laser_data.clear_and_delete();
  115. if ( m_laser_statu != LASER_FAULT )
  116. {
  117. m_laser_statu = LASER_DISCONNECT;
  118. }
  119. return Error_code::SUCCESS;
  120. }
  121. //对外的接口函数,负责接受并处理任务单,
  122. //input:p_laser_task 雷达任务单,基类的指针,指向子类的实例,(多态)
  123. //注:这个函数为虚函数,实际的处理任务的代码由子类重载并实现。
  124. Error_manager Laser_base::execute_task(Task_Base* p_laser_task)
  125. {
  126. //检查指针
  127. if(p_laser_task == NULL)
  128. {
  129. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  130. "Laser_base::execute_task failed, POINTER_IS_NULL");
  131. }
  132. else
  133. {
  134. return Error_manager(Error_code::CLASS_BASE_FUNCTION_CANNOT_USE, Error_level::NEGLIGIBLE_ERROR,
  135. "Laser_base::execute_task cannot use");
  136. }
  137. }
  138. //检查雷达状态,是否正常运行
  139. Error_manager Laser_base::check_laser()
  140. {
  141. if ( is_ready() )
  142. {
  143. return Error_code::SUCCESS;
  144. }
  145. return Error_manager(Error_code::LASER_CHECK_FAILED, Error_level::MINOR_ERROR,
  146. " check_laser failed ");
  147. }
  148. //雷达的启动接口函数, 让雷达进行扫描,一般需要子类重载,不同的雷达开始方式不同。
  149. Error_manager Laser_base::start_scan()
  150. {
  151. //LOG(INFO) << " Laser_base::start_scan "<< this;
  152. if ( is_ready() )
  153. {
  154. //启动雷达扫描
  155. m_laser_scan_flag=true; //启动雷达扫描
  156. m_laser_statu = LASER_BUSY; //雷达状态 繁忙
  157. m_condition_receive.notify_all(true); //唤醒接受线程
  158. m_condition_transform.notify_all(true); //唤醒转化线程
  159. //重置数据
  160. m_points_count=0;
  161. m_queue_laser_data.clear_and_delete();
  162. m_last_data.clear();
  163. return Error_code::SUCCESS;
  164. }
  165. else
  166. {
  167. return Error_manager(Error_code::LASER_START_FAILED, Error_level::MINOR_ERROR,
  168. "Laser_base::start_scan() is not ready ");
  169. }
  170. }
  171. //雷达的停止接口函数, 让雷达停止扫描,一般需要子类重载,不同的雷达结束方式不同。
  172. Error_manager Laser_base::stop_scan()
  173. {
  174. //LOG(INFO) << " Laser_base::stop_scan "<< this;
  175. //stop_scan 只是将 m_laser_scan_flag 改为 stop。
  176. //然后多线程内部判断 m_laser_scan_flag,然后自动停止线程
  177. m_laser_scan_flag=false;
  178. return Error_code::SUCCESS;
  179. }
  180. //结束任务单,stop之后,要检查线程状态和数据结果,然后才能 end_task
  181. Error_manager Laser_base::end_task()
  182. {
  183. //LOG(INFO) << " Laser_base::end_task "<< this;
  184. m_laser_scan_flag=false;
  185. m_condition_receive.notify_all(false);
  186. m_condition_transform.notify_all(false);
  187. m_points_count=0;
  188. m_queue_laser_data.clear_and_delete();
  189. m_last_data.clear();
  190. close_save_path();
  191. //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
  192. if(mp_laser_task !=NULL)
  193. {
  194. //判断任务单的错误等级,
  195. if ( mp_laser_task->get_task_error_manager().get_error_level() <= Error_level::MINOR_ERROR)
  196. {
  197. if ( m_laser_statu == LASER_BUSY )
  198. {
  199. //故障等级为 NORMAL NEGLIGIBLE_ERROR MINOR_ERROR,则恢复待机,可以接受下一次的任务单
  200. m_laser_statu = LASER_READY;
  201. }
  202. }
  203. else
  204. {
  205. //故障等级为 MAJOR_ERROR CRITICAL_ERROR,则雷达故障,不可以再接受任务单。
  206. m_laser_statu = LASER_FAULT;
  207. }
  208. //强制改为TASK_OVER,不管它当前在做什么。
  209. mp_laser_task->set_task_statu(TASK_OVER);
  210. }
  211. return Error_code::SUCCESS;
  212. }
  213. //设置保存文件的路径,并打开文件,
  214. Error_manager Laser_base::set_open_save_path(std::string save_path,bool is_save)
  215. {
  216. m_save_flag = is_save;
  217. m_points_log_tool.close();
  218. m_binary_log_tool.close();
  219. if (is_save == false)
  220. {
  221. return Error_code::SUCCESS;
  222. }
  223. else
  224. {
  225. m_save_path = save_path;
  226. char pts_file[255] = { 0 };
  227. sprintf(pts_file, "%s/pts%d.txt", save_path.c_str(), m_laser_id+1);
  228. m_points_log_tool.open(pts_file);
  229. //std::cout << "huli m_points_log_tool path "<< pts_file << std::endl;
  230. char bin_file[255] = { 0 };
  231. sprintf(bin_file, "%s/laser%d.data", save_path.c_str(), m_laser_id+1);
  232. m_binary_log_tool.open(bin_file,true);
  233. return Error_code::SUCCESS;
  234. }
  235. }
  236. //关闭保存文件,推出前一定要执行
  237. Error_manager Laser_base::close_save_path()
  238. {
  239. m_save_flag = false;
  240. m_points_log_tool.close();
  241. m_binary_log_tool.close();
  242. return Error_code::SUCCESS;
  243. }
  244. //判断雷达状态是否为待机,如果已经准备好,则可以执行任务。
  245. //子类重载 is_ready(),里面增加livox sdk后台线程状态的判断。
  246. bool Laser_base::is_ready()
  247. {
  248. return ( get_laser_statu() == LASER_READY );
  249. }
  250. //获取雷达状态
  251. Laser_statu Laser_base::get_laser_statu()
  252. {
  253. return m_laser_statu;
  254. }
  255. //获取雷达id
  256. int Laser_base::get_laser_id()
  257. {
  258. return m_laser_id;
  259. }
  260. //线程执行函数,将二进制消息存入队列缓存,
  261. void Laser_base::thread_receive()
  262. {
  263. LOG(INFO) << " thread_receive start "<< this;
  264. //接受雷达消息,每次循环只接受一个 Binary_buf
  265. while (m_condition_receive.is_alive())
  266. {
  267. m_condition_receive.wait();
  268. if ( m_condition_receive.is_alive() )
  269. {
  270. //tp_binaty_buf的内存 需要手动分配,然后存入链表。
  271. //m_queue_laser_data的内存由 Laser_base 基类管理。
  272. Binary_buf* tp_binaty_buf = new Binary_buf();
  273. //获取雷达的通信消息缓存
  274. if (this->receive_buf_to_queue(*tp_binaty_buf))
  275. {
  276. //将缓存 存入队列。
  277. //thread_receive存入前要手动分配内存,thread_transform取出后要记得释放。
  278. m_queue_laser_data.push(tp_binaty_buf);
  279. }
  280. else
  281. {
  282. delete tp_binaty_buf;
  283. tp_binaty_buf=NULL;
  284. //接受线程无法接受到数据的同时,也收到了雷达停止扫描的信息,那么线程停止
  285. //如果m_laser_scan_flag停止扫描,但是receive_buf_to_queue仍然能够接受到数据。那么线程继续运行。
  286. //m_laser_scan_flag停止的瞬间,可能还有残留的数据,需要继续执行。
  287. if ( !m_laser_scan_flag )
  288. {
  289. //停止线程,m_condition_receive.wait() 函数将会阻塞。
  290. m_condition_receive.set_pass_ever(false);
  291. }
  292. }
  293. }
  294. }
  295. LOG(INFO) << " thread_receive end :"<<this;
  296. return;
  297. }
  298. //线程执行函数,转化并处理三维点云。
  299. void Laser_base::thread_transform()
  300. {
  301. LOG(INFO) << " thread_transform start "<< this;
  302. //转化雷达数据,每次循环只转化一个 Binary_buf,然后得到 t_point3D_cloud.size() 个三维点,
  303. while (m_condition_transform.is_alive())
  304. {
  305. m_condition_transform.wait();
  306. if ( m_condition_transform.is_alive() )
  307. {
  308. //tp_binaty_buf的内存 从链表取出时,就自带内存了。需要手动释放。
  309. //m_queue_laser_data的内存由 Laser_base 基类管理。
  310. Binary_buf* tp_binaty_buf=NULL;
  311. //第1步,从队列中取出缓存。
  312. int huli_test = m_queue_laser_data.size();
  313. //thread_receive存入前要手动分配内存,thread_transform取出后要记得释放。
  314. bool t_pop_flag = m_queue_laser_data.try_pop(tp_binaty_buf);
  315. /*if ( t_pop_flag )
  316. {
  317. std::cout << "huli m_queue_laser_data.size() last = " << huli_test << std::endl;
  318. std::cout << "huli m_queue_laser_data.size() now = " << m_queue_laser_data.size() << std::endl;
  319. }*/
  320. //第2步,处理数据,缓存转化缓存为三维点。
  321. //如果获取到了 Binary_buf,或者上次有未完成的 m_last_data,那么就执行数据转化。
  322. //注注注注注意了,扫描停止后,m_last_data一定要保证清空,不能有垃圾数据残留,这会阻塞线程的暂停
  323. if (t_pop_flag || m_last_data.get_length() > 0)
  324. {
  325. //std::cout << "huli thread_transform " << m_queue_laser_data.size() << std::endl;
  326. //std::cout << "huli t_pop_flag = "<< t_pop_flag << std::endl;
  327. //缓存类型
  328. Buf_type t_buf_type = BUF_UNKNOW;
  329. //雷达扫描结果,三维点云(雷达自身坐标系)
  330. std::vector<CPoint3D> t_point3D_cloud;
  331. //第2.1步,缓存转化为三维点。
  332. if (t_pop_flag)
  333. {
  334. if (tp_binaty_buf == NULL)
  335. {
  336. //try_pop返回true,但是tp_binaty_buf没有数据,直接跳过。这个一般不可能出现。
  337. continue;
  338. }
  339. else
  340. {
  341. //保存雷达通信 二进制的源文件。
  342. if(m_save_flag)
  343. {
  344. m_binary_log_tool.write(tp_binaty_buf->get_buf(), tp_binaty_buf->get_length());
  345. }
  346. //缓存转化为三维点。传入新的缓存,
  347. t_buf_type= this->transform_buf_to_points(tp_binaty_buf, t_point3D_cloud);
  348. }
  349. }
  350. else if(m_last_data.get_length() > 0)
  351. {
  352. //缓存转化为三维点。没有新消息,就传null,这将处理 m_last_data 上一次遗留的缓存。
  353. t_buf_type = this->transform_buf_to_points(NULL, t_point3D_cloud);
  354. }
  355. //第2.2步,判断t_buf_type,处理雷达数据。并存入任务单
  356. if (t_buf_type == BUF_UNKNOW || t_buf_type == BUF_READY)
  357. {
  358. //跳过这次循环。
  359. continue;
  360. }
  361. else if (t_buf_type == BUF_DATA || t_buf_type == BUF_START || t_buf_type == BUF_STOP)
  362. {
  363. //循环处理雷达数据
  364. for (int i = 0; i < t_point3D_cloud.size(); ++i)
  365. {
  366. //三维点的坐标变换,从雷达自己的坐标系,转化到公共坐标系,(目前以plc为公共坐标系)
  367. CPoint3D t_point = transform_by_matrix(t_point3D_cloud[i]);
  368. //保存雷达扫描 三维点云的最终结果。
  369. if(m_save_flag) {
  370. char buf[64] = {0};
  371. sprintf(buf, "%f %f %f\n", t_point.x, t_point.y, t_point.z);
  372. m_points_log_tool.write(buf, strlen(buf));
  373. //std::cout << "huli m_points_log_tool.write" << std::endl;
  374. }
  375. //此时 t_point 为雷达采集数据的结果,转换后的三维点云。
  376. //将每个点存入任务单里面的点云容器。
  377. if(mp_laser_task!=NULL)
  378. {
  379. Error_manager t_error= mp_laser_task->task_push_point(pcl::PointXYZ(t_point.x,t_point.y,t_point.z));
  380. if ( t_error != Error_code::SUCCESS )
  381. {
  382. mp_laser_task->get_task_error_manager().compare_and_cover_error(t_error);
  383. stop_scan();
  384. }
  385. }
  386. }
  387. //统计扫描点个数。
  388. m_points_count += t_point3D_cloud.size();
  389. //思科雷达停止扫描。
  390. if (t_buf_type == BUF_STOP)
  391. {
  392. //注:这个只是思科的雷达停止处,不同的雷达在不同的地方调用 stop_scan
  393. stop_scan();
  394. }
  395. }
  396. }
  397. else
  398. {
  399. //转化线程 不需要数据的同时,也收到了雷达停止扫描的信息并且接受线程已经停止,那么停止转化线程,
  400. //注:必须先停止接受线程,再停止转化线程。转化线程停止之后即可结束任务。
  401. //如果m_laser_scan_flag停止扫描,接受线程和转化线程还有未完成的数据。那么线程继续运行。
  402. //m_laser_scan_flag停止的瞬间,可能还有残留的数据,需要继续执行。防止数据遗留。
  403. if ( !m_laser_scan_flag && m_condition_receive.get_pass_ever() == false)
  404. {
  405. //停止线程,m_condition_transform.wait() 函数将会阻塞。
  406. m_condition_transform.set_pass_ever(false);
  407. //mp_thread_transform 停止时,雷达扫描任务彻底完成,此时结束任务
  408. end_task();
  409. }
  410. }
  411. //第3步,手动释放缓存
  412. if ( tp_binaty_buf != NULL )
  413. {
  414. delete tp_binaty_buf;
  415. }
  416. }
  417. }
  418. LOG(INFO) << " thread_transform end "<< this;
  419. return;
  420. }
  421. Error_manager Laser_base::publish_laser_to_message()
  422. {
  423. return SUCCESS;
  424. }
  425. //线程执行函数,公开发布雷达的相关信息,用作上位机的监视。
  426. void Laser_base::thread_publish(Laser_base *p_laser)
  427. {
  428. LOG(INFO) << " thread_publish start ";
  429. if(p_laser==NULL)
  430. {
  431. return;
  432. }
  433. while (p_laser->m_condition_publish.is_alive())
  434. {
  435. p_laser->m_condition_publish.wait();
  436. if ( p_laser->m_condition_publish.is_alive() )
  437. {
  438. p_laser->publish_laser_to_message();
  439. //每隔300ms,发送一次雷达信息状态。
  440. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  441. }
  442. }
  443. LOG(INFO) << " thread_publish end ";
  444. return;
  445. }
  446. //初始化变换矩阵,设置默认值
  447. Error_manager Laser_base::init_laser_matrix()
  448. {
  449. if ( LASER_MATRIX_ARRAY_SIZE == 12 )
  450. {
  451. //详见转化的算法transfor。保证转化前后坐标一致。
  452. mp_laser_matrix[0] = 1;
  453. mp_laser_matrix[1] = 0;
  454. mp_laser_matrix[2] = 0;
  455. mp_laser_matrix[3] = 0;
  456. mp_laser_matrix[4] = 0;
  457. mp_laser_matrix[5] = 1;
  458. mp_laser_matrix[6] = 0;
  459. mp_laser_matrix[7] = 0;
  460. mp_laser_matrix[8] = 0;
  461. mp_laser_matrix[9] = 0;
  462. mp_laser_matrix[10] = 1;
  463. mp_laser_matrix[11] = 0;
  464. }
  465. else
  466. {
  467. for (int i = 0; i < LASER_MATRIX_ARRAY_SIZE; ++i)
  468. {
  469. //设为0之后,变换之后,新的点坐标全部为0
  470. mp_laser_matrix[i] = 0;
  471. }
  472. }
  473. return Error_code::SUCCESS;
  474. }
  475. //设置变换矩阵,用作三维点的坐标变换,
  476. Error_manager Laser_base::set_laser_matrix(double* p_matrix, int size)
  477. {
  478. if ( p_matrix == NULL )
  479. {
  480. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  481. " set_laser_matrix p_matrix IS_NULL ");
  482. }
  483. else if ( size != LASER_MATRIX_ARRAY_SIZE )
  484. {
  485. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  486. " set_laser_matrix size is not Match");
  487. }
  488. else
  489. {
  490. if(m_laser_id==5)
  491. std::cout<<"---------------------- horizon matrix";
  492. memcpy(mp_laser_matrix, p_matrix, LASER_MATRIX_ARRAY_SIZE * sizeof(double));
  493. return Error_code::SUCCESS;
  494. }
  495. }
  496. //三维点的坐标变换的功能函数,从雷达自己的坐标系,转化到公共坐标系,(目前以plc为公共坐标系)
  497. CPoint3D Laser_base::transform_by_matrix(CPoint3D point)
  498. {
  499. CPoint3D result;
  500. double x = point.x;
  501. double y = point.y;
  502. double z = point.z;
  503. result.x = x * mp_laser_matrix[0] + y*mp_laser_matrix[1] + z*mp_laser_matrix[2] + mp_laser_matrix[3];
  504. result.y = x * mp_laser_matrix[4] + y*mp_laser_matrix[5] + z*mp_laser_matrix[6] + mp_laser_matrix[7];
  505. result.z = x * mp_laser_matrix[8] + y*mp_laser_matrix[9] + z*mp_laser_matrix[10] + mp_laser_matrix[11];
  506. return result;
  507. }