LivoxMid100Laser.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "LivoxMid100Laser.h"
  2. #include <glog/logging.h>
  3. RegisterLaser(LivoxMid100)
  4. CLivoxMid100Laser::CLivoxMid100Laser(int id, Laser_proto::laser_parameter laser_param)
  5. :CLivoxLaser(id, laser_param)
  6. {
  7. //设备livox扫描最大帧数
  8. m_frame_maxnum = laser_param.frame_num();
  9. //判断参数类型,
  10. if (laser_param.type() == "LivoxMid100")
  11. {
  12. std::string sn = laser_param.sn();
  13. std::string sn1 = sn, sn2 = sn, sn3 = sn;
  14. sn1 += "1";
  15. sn2 += "2";
  16. sn3 += "3";
  17. g_sn_laser.insert(std::make_pair(sn1, this));
  18. g_sn_laser.insert(std::make_pair(sn2, this));
  19. g_sn_laser.insert(std::make_pair(sn3, this));
  20. //初始化livox
  21. InitLivox();
  22. }
  23. }
  24. CLivoxMid100Laser::~CLivoxMid100Laser()
  25. {
  26. }
  27. //雷达链接设备,为3个线程添加线程执行函数。
  28. Error_manager CLivoxMid100Laser::connect_laser()
  29. {
  30. Error_manager t_error;
  31. //设置点云变换矩阵
  32. double matrix[12]={0};
  33. matrix[0]=m_laser_param.mat_r00();
  34. matrix[1]=m_laser_param.mat_r01();
  35. matrix[2]=m_laser_param.mat_r02();
  36. matrix[3]=m_laser_param.mat_r03();
  37. matrix[4]=m_laser_param.mat_r10();
  38. matrix[5]=m_laser_param.mat_r11();
  39. matrix[6]=m_laser_param.mat_r12();
  40. matrix[7]=m_laser_param.mat_r13();
  41. matrix[8]=m_laser_param.mat_r20();
  42. matrix[9]=m_laser_param.mat_r21();
  43. matrix[10]=m_laser_param.mat_r22();
  44. matrix[11]=m_laser_param.mat_r23();
  45. t_error = set_laser_matrix(matrix, 12);
  46. if ( t_error != Error_code::SUCCESS )
  47. {
  48. return t_error;
  49. }
  50. return CLivoxLaser::connect_laser();
  51. }
  52. //雷达断开链接,释放3个线程
  53. Error_manager CLivoxMid100Laser::disconnect_laser()
  54. {
  55. return CLivoxLaser::disconnect_laser();
  56. }
  57. //对外的接口函数,负责接受并处理任务单,
  58. //input:p_laser_task 雷达任务单,基类的指针,指向子类的实例,(多态)
  59. //注:这个函数为虚函数,实际的处理任务的代码由子类重载并实现。
  60. Error_manager CLivoxMid100Laser::execute_task(Task_Base* p_laser_task)
  61. {
  62. LOG(INFO) << " CLivoxMid100Laser::execute_task start "<< this;
  63. Error_manager t_error;
  64. Error_manager t_result;
  65. //检查指针
  66. if (p_laser_task == NULL) {
  67. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  68. "Laser_base::porform_task failed, POINTER_IS_NULL");
  69. }
  70. //检查任务类型,
  71. if (p_laser_task->get_task_type() != LASER_TASK)
  72. {
  73. return Error_manager(Error_code::LIVOX_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  74. "laser task type error != LASER_TASK");
  75. }
  76. //检查当前状态
  77. t_error=check_laser();
  78. if(t_error!=SUCCESS)
  79. {
  80. return t_error;
  81. }
  82. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  83. mp_laser_task = (Laser_task *) p_laser_task;
  84. mp_laser_task->set_task_statu(TASK_SIGNED);
  85. //检查消息内容是否正确,
  86. //检查三维点云指针
  87. if (mp_laser_task->get_task_point_cloud() == NULL)
  88. {
  89. t_result.error_manager_reset(Error_code::POINTER_IS_NULL,
  90. Error_level::MINOR_ERROR,
  91. "execute_task mp_task_point_cloud is null");
  92. //将任务的状态改为 TASK_OVER 结束任务
  93. mp_laser_task->set_task_statu(TASK_OVER);
  94. //返回错误码
  95. mp_laser_task->set_task_error_manager(t_result);
  96. return t_result;
  97. }
  98. else
  99. {
  100. m_frame_maxnum=mp_laser_task->get_task_frame_maxnum();
  101. //设置保存文件的路径
  102. std::string save_path = mp_laser_task->get_save_path();
  103. t_error = set_open_save_path(save_path);
  104. if ( t_error != Error_code::SUCCESS )
  105. {
  106. //文件保存文件的路径的设置 允许失败。继续后面的动作
  107. t_result.compare_and_cover_error(t_error);
  108. }
  109. //启动雷达扫描
  110. t_error = start_scan();
  111. if ( t_error != Error_code::SUCCESS )
  112. {
  113. t_result.compare_and_cover_error(t_error);
  114. //将任务的状态改为 TASK_OVER 结束任务
  115. mp_laser_task->set_task_statu(TASK_OVER);
  116. //返回错误码
  117. mp_laser_task->set_task_error_manager(t_result);
  118. return t_result;
  119. }
  120. else
  121. {
  122. //将任务的状态改为 TASK_WORKING 处理中
  123. mp_laser_task->set_task_statu(TASK_WORKING);
  124. }
  125. }
  126. //返回错误码
  127. if (t_result != Error_code::SUCCESS)
  128. {
  129. mp_laser_task->set_task_error_manager(t_result);
  130. }
  131. return t_result;
  132. }
  133. //检查雷达状态,是否正常运行
  134. Error_manager CLivoxMid100Laser::check_laser()
  135. {
  136. return CLivoxLaser::check_laser();
  137. }
  138. //雷达的启动接口函数, 让雷达进行扫描,一般需要子类重载,不同的雷达开始方式不同。
  139. Error_manager CLivoxMid100Laser::start_scan()
  140. {
  141. LOG(INFO) << " livoxMid100 start :" << m_laser_param.sn();
  142. //清空livox子类的队列,
  143. m_queue_livox_data.clear_and_delete();
  144. g_count[m_handle1] = 0;
  145. g_count[m_handle2] = 0;
  146. g_count[m_handle3] = 0;
  147. return CLivoxLaser::start_scan();
  148. }
  149. //雷达的停止接口函数, 让雷达停止扫描,一般需要子类重载,不同的雷达结束方式不同。
  150. Error_manager CLivoxMid100Laser::stop_scan()
  151. {
  152. LOG(INFO)<<" livoxmid100 stoped !!!"<<m_laser_param.sn();
  153. return CLivoxLaser::stop_scan();
  154. }
  155. //结束任务单,stop之后,要检查线程状态和数据结果,然后才能 end_task
  156. Error_manager CLivoxMid100Laser::end_task()
  157. {
  158. return CLivoxLaser::end_task();
  159. }
  160. //判断雷达状态是否为待机,如果已经准备好,则可以执行任务。
  161. //子类重载 is_ready(),里面增加livox sdk后台线程状态的判断。
  162. bool CLivoxMid100Laser::is_ready()
  163. {
  164. //3个livox雷达设备的状态,livox sdk后台线程的状态
  165. bool cond1=g_devices[m_handle1].device_state == kDeviceStateConnect ||
  166. g_devices[m_handle1].device_state == kDeviceStateSampling;
  167. bool cond2=g_devices[m_handle2].device_state == kDeviceStateConnect ||
  168. g_devices[m_handle2].device_state == kDeviceStateSampling;
  169. bool cond3=g_devices[m_handle3].device_state == kDeviceStateConnect ||
  170. g_devices[m_handle3].device_state == kDeviceStateSampling;
  171. if ( cond1 && cond2 && cond3 && m_laser_statu == LASER_READY )
  172. {
  173. true;
  174. }
  175. else
  176. {
  177. false;
  178. }
  179. }
  180. void CLivoxMid100Laser::UpdataHandle()
  181. {
  182. std::string sn = m_laser_param.sn();
  183. std::string sn1 = sn, sn2 = sn, sn3 = sn;
  184. sn1 += "1";
  185. sn2 += "2";
  186. sn3 += "3";
  187. if (g_sn_handle.find(sn1) != g_sn_handle.end())
  188. {
  189. m_handle1 = g_sn_handle[sn1];
  190. }
  191. if (g_sn_handle.find(sn2) != g_sn_handle.end())
  192. {
  193. m_handle2 = g_sn_handle[sn2];
  194. }
  195. if (g_sn_handle.find(sn3) != g_sn_handle.end())
  196. {
  197. m_handle3 = g_sn_handle[sn3];
  198. }
  199. }
  200. bool CLivoxMid100Laser::IsScanComplete()
  201. {
  202. //雷达的采集帧数判断,直接比较任务单里面的帧数最大值
  203. if(mp_laser_task==NULL)
  204. {
  205. return false;
  206. }
  207. else
  208. {
  209. int frame_count=mp_laser_task->get_task_frame_maxnum();
  210. return g_count[m_handle1] >= frame_count && g_count[m_handle2] >= frame_count
  211. && g_count[m_handle2] >= frame_count;
  212. }
  213. }