velodyne_lidar_device.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #include "velodyne_lidar_device.h"
  2. // 万集雷达封装类构造函数
  3. Velodyne_lidar_device::Velodyne_lidar_device()
  4. {
  5. m_velodyne_lidar_device_status = E_UNKNOWN;
  6. mp_velodyne_lidar_scan_task = NULL;
  7. }
  8. // 万集雷达封装类析构函数
  9. Velodyne_lidar_device::~Velodyne_lidar_device()
  10. {
  11. uninit();
  12. }
  13. void Velodyne_lidar_device::dev_info_config(std::string model, int rpm)
  14. {
  15. double packet_rate; // packet frequency (Hz)
  16. std::string model_full_name;
  17. if ((model == "VLS128"))
  18. {
  19. packet_rate = 6253.9; // 3 firing cycles in a data packet. 3 x 53.3 μs = 0.1599 ms is the accumulation delay per packet.
  20. // 1 packet/0.1599 ms = 6253.9 packets/second
  21. model_full_name = model;
  22. }
  23. else if ((model == "64E_S2") ||
  24. (model == "64E_S2.1")) // generates 1333312 points per second
  25. { // 1 packet holds 384 points
  26. packet_rate = 3472.17; // 1333312 / 384
  27. model_full_name = std::string("HDL-") + model;
  28. }
  29. else if (model == "64E")
  30. {
  31. packet_rate = 2600.0;
  32. model_full_name = std::string("HDL-") + model;
  33. }
  34. else if (model == "64E_S3") // generates 2222220 points per second (half for strongest and half for lastest)
  35. { // 1 packet holds 384 points
  36. packet_rate = 5787.03; // 2222220 / 384
  37. model_full_name = std::string("HDL-") + model;
  38. }
  39. else if (model == "32E")
  40. {
  41. packet_rate = 1808.0;
  42. model_full_name = std::string("HDL-") + model;
  43. }
  44. else if (model == "32C")
  45. {
  46. packet_rate = 1507.0;
  47. model_full_name = std::string("VLP-") + model;
  48. }
  49. else if (model == "VLP16")
  50. {
  51. packet_rate = 754; // 754 Packets/Second for Last or Strongest mode 1508 for dual (VLP-16 User Manual)
  52. model_full_name = "VLP-16";
  53. }
  54. else
  55. {
  56. LOG(WARNING) << "unknown Velodyne LIDAR model: " << model;
  57. packet_rate = 2600.0;
  58. }
  59. std::string deviceName(std::string("Velodyne ") + model_full_name);
  60. LOG(INFO) << deviceName << " rotating at " << rpm << " RPM";
  61. double frequency = (rpm / 60.0); // expected Hz rate
  62. // default number of packets for each scan is a single revolution
  63. // (fractions rounded up)
  64. m_num_packet_per_scan = (int)ceil(packet_rate / frequency);
  65. LOG(INFO) << "publishing " << m_num_packet_per_scan << " packets per scan";
  66. }
  67. // 初始化
  68. Error_manager Velodyne_lidar_device::init(velodyne::velodyneLidarParams params)
  69. {
  70. m_params = params;
  71. Error_manager t_error;
  72. LOG(INFO) << " Velodyne_lidar_device::init " << this;
  73. m_num_packet_inside = 0;
  74. dev_info_config(params.model(), params.rpm());
  75. m_velodyne_lidar_device_status = E_UNKNOWN;
  76. m_velodyne_socket_status = E_UNKNOWN;
  77. m_velodyne_protocol_status = E_UNKNOWN;
  78. m_lidar_id = params.lidar_id();
  79. // 设置雷达内参矩阵
  80. Eigen::AngleAxisd rot_x = Eigen::AngleAxisd(params.calib().r(), Eigen::Vector3d::UnitX());
  81. Eigen::AngleAxisd rot_y = Eigen::AngleAxisd(params.calib().p(), Eigen::Vector3d::UnitY());
  82. Eigen::AngleAxisd rot_z = Eigen::AngleAxisd(params.calib().y(), Eigen::Vector3d::UnitZ());
  83. Eigen::Vector3d trans(params.calib().cx(), params.calib().cy(), params.calib().cz());
  84. m_calib_matrix << rot_x.toRotationMatrix() * rot_y.toRotationMatrix() * rot_z.toRotationMatrix(), trans, 0.0, 0.0, 0.0, 1.0;
  85. //唤醒队列
  86. m_communication_data_queue.wake_queue();
  87. //初始化通信协议
  88. t_error = m_protocol.setup(params.model(), params.calibrationfile(), params.max_range(), params.min_range(), params.min_angle(), params.max_angle()) == true ? SUCCESS : Error_manager(PARAMETER_ERROR, MINOR_ERROR, "protocol parameter error");
  89. if (t_error != SUCCESS)
  90. {
  91. return t_error;
  92. }
  93. m_velodyne_protocol_status = E_READY;
  94. //初始化雷达通信模块
  95. std::string t_ip = params.ip();
  96. int t_port = params.port();
  97. t_error = m_input_socket.init(t_ip, t_port) == true ? SUCCESS : Error_manager(VELODYNE_LIDAR_CONNECT_FAILED, MINOR_ERROR, "socket connect failed");
  98. if (t_error != SUCCESS)
  99. {
  100. return t_error;
  101. }
  102. m_velodyne_socket_status = E_READY;
  103. m_capture_condition.reset(false, false, true);
  104. mp_capture_thread = new std::thread(&Velodyne_lidar_device::capture_thread_fun, this);
  105. m_parse_condition.reset(false, false, false);
  106. mp_parse_thread = new std::thread(&Velodyne_lidar_device::parse_thread_fun, this);
  107. //启动 内部线程。默认wait。
  108. m_execute_condition.reset(false, false, false);
  109. mp_execute_thread = new std::thread(&Velodyne_lidar_device::execute_thread_fun, this);
  110. m_velodyne_lidar_device_status = E_READY;
  111. return t_error;
  112. }
  113. //反初始化
  114. Error_manager Velodyne_lidar_device::uninit()
  115. {
  116. if (mp_capture_thread)
  117. {
  118. m_capture_condition.kill_all();
  119. mp_capture_thread->join();
  120. delete mp_capture_thread;
  121. mp_capture_thread = NULL;
  122. }
  123. // //终止队列,防止 wait_and_pop 阻塞线程。
  124. m_communication_data_queue.termination_queue();
  125. if (mp_parse_thread)
  126. {
  127. m_parse_condition.kill_all();
  128. mp_parse_thread->join();
  129. delete mp_parse_thread;
  130. mp_parse_thread = NULL;
  131. }
  132. //关闭线程
  133. //回收线程的资源
  134. if (mp_execute_thread)
  135. {
  136. m_execute_condition.kill_all();
  137. mp_execute_thread->join();
  138. delete mp_execute_thread;
  139. mp_execute_thread = NULL;
  140. }
  141. m_input_socket.uninit();
  142. //清空队列
  143. m_communication_data_queue.clear_and_delete();
  144. if (m_velodyne_lidar_device_status != E_FAULT)
  145. {
  146. m_velodyne_lidar_device_status = E_UNKNOWN;
  147. }
  148. return Error_code::SUCCESS;
  149. }
  150. //对外的接口函数,负责接受并处理任务单,
  151. Error_manager Velodyne_lidar_device::execute_task(Task_Base *p_velodyne_lidar_scan_task)
  152. {
  153. LOG(INFO) << " ---Laser_manager::execute_task run--- " << this;
  154. Error_manager t_error;
  155. Error_manager t_result;
  156. //检查指针
  157. if (p_velodyne_lidar_scan_task == NULL)
  158. {
  159. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  160. "Velodyne_lidar_device::execute_task failed, POINTER_IS_NULL");
  161. }
  162. //检查任务类型,
  163. if (p_velodyne_lidar_scan_task->get_task_type() != VELODYNE_LIDAR_SCAN)
  164. {
  165. return Error_manager(Error_code::VELODYNE_LIDAR_DEVICE_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  166. " task type error != VELODYNE_LIDAR_SCAN ");
  167. }
  168. //检查接收方的状态
  169. t_error = check_status();
  170. if (t_error != SUCCESS)
  171. {
  172. t_result.compare_and_cover_error(t_error);
  173. }
  174. else
  175. {
  176. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  177. mp_velodyne_lidar_scan_task = (Velodyne_lidar_scan_task *)p_velodyne_lidar_scan_task;
  178. mp_velodyne_lidar_scan_task->set_task_statu(TASK_SIGNED);
  179. //检查消息内容是否正确,
  180. //检查三维点云指针
  181. if (mp_velodyne_lidar_scan_task->get_task_point_cloud().get() == NULL)
  182. {
  183. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  184. Error_level::MINOR_ERROR,
  185. "execute_task mp_task_point_cloud is null");
  186. t_result.compare_and_cover_error(t_error);
  187. }
  188. else if (mp_velodyne_lidar_scan_task->get_task_cloud_lock() == NULL)
  189. {
  190. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  191. Error_level::MINOR_ERROR,
  192. "execute_task mp_task_cloud_lock is null");
  193. t_result.compare_and_cover_error(t_error);
  194. }
  195. else
  196. {
  197. m_velodyne_lidar_device_status = E_BUSY;
  198. //启动雷达管理模块,的核心工作线程
  199. m_execute_condition.notify_all(true);
  200. //通知 thread_work 子线程启动。
  201. //将任务的状态改为 TASK_WORKING 处理中
  202. mp_velodyne_lidar_scan_task->set_task_statu(TASK_WORKING);
  203. }
  204. //return 之前要填充任务单里面的错误码.
  205. if (t_result != Error_code::SUCCESS)
  206. {
  207. //忽略轻微故障
  208. if (t_result.get_error_level() >= Error_level::MINOR_ERROR)
  209. {
  210. //将任务的状态改为 TASK_ERROR 结束错误
  211. mp_velodyne_lidar_scan_task->set_task_statu(TASK_ERROR);
  212. }
  213. //返回错误码
  214. mp_velodyne_lidar_scan_task->compare_and_cover_task_error_manager(t_result);
  215. }
  216. }
  217. return t_result;
  218. }
  219. //检查雷达状态,是否正常运行
  220. Error_manager Velodyne_lidar_device::check_status()
  221. {
  222. updata_status();
  223. if (m_velodyne_lidar_device_status == E_READY)
  224. {
  225. return Error_code::SUCCESS;
  226. }
  227. else if (m_velodyne_lidar_device_status == E_BUSY)
  228. {
  229. return Error_manager(Error_code::VELODYNE_LIDAR_DEVICE_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  230. " Velodyne_lidar_device::check_status() error ");
  231. }
  232. else
  233. {
  234. return Error_manager(Error_code::VELODYNE_LIDAR_DEVICE_STATUS_ERROR, Error_level::MINOR_ERROR,
  235. " Velodyne_lidar_device::check_status() error ");
  236. }
  237. return Error_code::SUCCESS;
  238. }
  239. //结束任务单,里面会根据任务的故障等级修正雷达管理模块的状态和任务单的状态
  240. Error_manager Velodyne_lidar_device::end_task()
  241. {
  242. m_execute_condition.notify_all(false);
  243. if (m_velodyne_lidar_device_status == E_BUSY)
  244. {
  245. m_velodyne_lidar_device_status = E_READY;
  246. }
  247. //else 状态不变
  248. //在结束任务单时,将雷达任务状态改为 TASK_OVER 已结束
  249. if (mp_velodyne_lidar_scan_task != NULL)
  250. {
  251. //判断任务单的错误等级,
  252. if (mp_velodyne_lidar_scan_task->get_task_error_manager().get_error_level() < Error_level::MINOR_ERROR)
  253. {
  254. //强制改为TASK_OVER,不管它当前在做什么。
  255. mp_velodyne_lidar_scan_task->set_task_statu(TASK_OVER);
  256. }
  257. else
  258. {
  259. //强制改为 TASK_ERROR,不管它当前在做什么。
  260. mp_velodyne_lidar_scan_task->set_task_statu(TASK_ERROR);
  261. }
  262. }
  263. return Error_code::SUCCESS;
  264. }
  265. //取消任务单,由发送方提前取消任务单
  266. Error_manager Velodyne_lidar_device::cancel_task(Task_Base *p_wanji_lidar_scan_task)
  267. {
  268. if (p_wanji_lidar_scan_task == mp_velodyne_lidar_scan_task)
  269. {
  270. m_execute_condition.notify_all(false);
  271. if (m_velodyne_lidar_device_status == E_BUSY)
  272. {
  273. m_velodyne_lidar_device_status = E_READY;
  274. }
  275. //else 状态不变
  276. //强制改为 TASK_DEAD,不管它当前在做什么。
  277. mp_velodyne_lidar_scan_task->set_task_statu(TASK_DEAD);
  278. return Error_code::SUCCESS;
  279. }
  280. else
  281. {
  282. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  283. " Velodyne_lidar_device::cancel_task PARAMRTER ERROR ");
  284. }
  285. }
  286. //判断是否为待机,如果已经准备好,则可以执行任务。
  287. bool Velodyne_lidar_device::is_ready()
  288. {
  289. updata_status();
  290. return m_velodyne_lidar_device_status == E_READY;
  291. }
  292. //外部调用获取新的点云, 获取指令时间之后的点云, 如果没有就会等待点云刷新, 直到超时, (内置while)
  293. Error_manager Velodyne_lidar_device::get_new_cloud_and_wait(std::mutex *p_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud,
  294. std::chrono::steady_clock::time_point command_time, int timeout_ms)
  295. {
  296. if (p_mutex == NULL || p_cloud.get() == NULL)
  297. {
  298. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  299. " POINTER IS NULL ");
  300. }
  301. //判断是否超时
  302. while (std::chrono::steady_clock::now() - command_time < std::chrono::milliseconds(timeout_ms))
  303. {
  304. //获取指令时间之后的点云, 如果没有就会循环
  305. if (m_parse_ring_time > command_time)
  306. {
  307. std::unique_lock<std::mutex> lck(*p_mutex);
  308. {
  309. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  310. if (m_ring_cloud.size() <= 0)
  311. return VELODYNE_LIDAR_DEVICE_NO_CLOUD;
  312. for (size_t i = 0; i < m_ring_cloud.size(); i++)
  313. {
  314. p_cloud->push_back(pcl::PointXYZ(m_ring_cloud[i].x, m_ring_cloud[i].y, m_ring_cloud[i].z));
  315. }
  316. }
  317. return SUCCESS;
  318. }
  319. //else 等待下一次数据更新
  320. //等1ms
  321. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  322. }
  323. //超时退出就报错
  324. return Error_manager(Error_code::WJ_LIDAR_GET_CLOUD_TIMEOUT, Error_level::MINOR_ERROR,
  325. " Velodyne_lidar_device::get_new_cloud_and_wait error ");
  326. }
  327. //外部调用获取当前点云, 获取指令时间之后的点云, 如果没有就会报错, 不会等待
  328. Error_manager Velodyne_lidar_device::get_current_cloud(std::mutex *p_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud,
  329. std::chrono::steady_clock::time_point command_time)
  330. {
  331. if (p_mutex == NULL || p_cloud.get() == NULL)
  332. {
  333. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  334. " POINTER IS NULL ");
  335. }
  336. //获取指令时间之后的点云, 如果没有就会报错, 不会等待
  337. if (m_parse_time > command_time)
  338. {
  339. std::unique_lock<std::mutex> lck(*p_mutex);
  340. {
  341. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  342. if (m_cloud_buf.size() <= 0)
  343. return VELODYNE_LIDAR_DEVICE_NO_CLOUD;
  344. for (size_t i = 0; i < m_cloud_buf.size(); i++)
  345. {
  346. p_cloud->push_back(pcl::PointXYZ(m_cloud_buf[i].x, m_cloud_buf[i].y, m_cloud_buf[i].z));
  347. }
  348. }
  349. return SUCCESS;
  350. }
  351. else
  352. {
  353. return Error_manager(Error_code::VELODYNE_LIDAR_DEVICE_NO_CLOUD, Error_level::MINOR_ERROR,
  354. " Velodyne_lidar_device::get_current_cloud error ");
  355. }
  356. }
  357. //外部调用获取最新的点云, 如果没有就会报错, 不会等待
  358. Error_manager Velodyne_lidar_device::get_last_cloud(std::mutex *p_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud,
  359. std::chrono::steady_clock::time_point command_time)
  360. {
  361. if (p_mutex == NULL || p_cloud.get() == NULL)
  362. {
  363. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  364. " POINTER IS NULL ");
  365. }
  366. if (m_parse_ring_time > command_time - std::chrono::milliseconds(int(60000.0/m_params.rpm())))
  367. {
  368. std::unique_lock<std::mutex> lck(*p_mutex);
  369. {
  370. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  371. if (m_ring_cloud.size() <= 0)
  372. return VELODYNE_LIDAR_DEVICE_NO_CLOUD;
  373. for (size_t i = 0; i < m_ring_cloud.size(); i++)
  374. {
  375. p_cloud->push_back(pcl::PointXYZ(m_ring_cloud[i].x, m_ring_cloud[i].y, m_ring_cloud[i].z));
  376. }
  377. }
  378. return SUCCESS;
  379. }
  380. else
  381. {
  382. return Error_manager(Error_code::VELODYNE_LIDAR_DEVICE_NO_CLOUD, Error_level::MINOR_ERROR,
  383. " Velodyne_lidar_device::get_current_cloud error ");
  384. }
  385. }
  386. //外部调用获取最新的点云, 获取指令时间往前一个周期内的点云, 如果没有就会报错, 不会等待
  387. //注:函数内部不加锁, 由外部统一加锁.
  388. Error_manager Velodyne_lidar_device::get_last_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud,
  389. std::chrono::steady_clock::time_point command_time)
  390. {
  391. if (p_cloud.get() == NULL)
  392. {
  393. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  394. " POINTER IS NULL ");
  395. }
  396. if (m_parse_ring_time > command_time - std::chrono::milliseconds(int(60000.0/m_params.rpm())))
  397. {
  398. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  399. if (m_ring_cloud.size() <= 0)
  400. return VELODYNE_LIDAR_DEVICE_NO_CLOUD;
  401. for (size_t i = 0; i < m_ring_cloud.size(); i++)
  402. {
  403. p_cloud->push_back(pcl::PointXYZ(m_ring_cloud[i].x, m_ring_cloud[i].y, m_ring_cloud[i].z));
  404. }
  405. return SUCCESS;
  406. }
  407. else
  408. {
  409. return Error_manager(Error_code::VELODYNE_LIDAR_DEVICE_NO_CLOUD, Error_level::MINOR_ERROR,
  410. " Velodyne_lidar_device::get_current_cloud error ");
  411. }
  412. }
  413. Velodyne_lidar_device::Velodyne_lidar_device_status Velodyne_lidar_device::get_status()
  414. {
  415. updata_status();
  416. return m_velodyne_lidar_device_status;
  417. }
  418. void Velodyne_lidar_device::updata_status()
  419. {
  420. if (m_velodyne_socket_status == E_FAULT || m_velodyne_protocol_status == E_FAULT)
  421. {
  422. m_velodyne_lidar_device_status = E_FAULT;
  423. }else if (m_velodyne_socket_status == E_DISCONNECT)
  424. {
  425. m_velodyne_lidar_device_status = E_DISCONNECT;
  426. }
  427. else if(m_velodyne_socket_status == E_BUSY || m_velodyne_protocol_status == E_BUSY)
  428. {
  429. m_velodyne_lidar_device_status = E_BUSY;
  430. }
  431. else if (m_velodyne_socket_status == E_READY && m_velodyne_protocol_status == E_READY)
  432. {
  433. if (m_execute_condition.get_pass_ever())
  434. {
  435. m_velodyne_lidar_device_status = E_BUSY;
  436. }
  437. else
  438. {
  439. m_velodyne_lidar_device_status = E_READY;
  440. }
  441. }
  442. else
  443. {
  444. // LOG(WARNING)<<"unknown????? "<<m_velodyne_socket_status<<", "<<m_velodyne_protocol_status;
  445. m_velodyne_lidar_device_status = E_UNKNOWN;
  446. }
  447. }
  448. void Velodyne_lidar_device::capture_thread_fun()
  449. {
  450. LOG(INFO) << " Velodyne_lidar_device::capture_thread_fun() start " << this;
  451. Error_manager t_error;
  452. while (m_capture_condition.is_alive())
  453. {
  454. m_capture_condition.wait_for_millisecond(1);
  455. if (m_capture_condition.is_alive())
  456. {
  457. // socket状态正常才更新状态
  458. if (m_velodyne_socket_status == E_READY)
  459. m_velodyne_socket_status = E_BUSY;
  460. if (m_input_socket.getPacket(m_buf) == 0)
  461. {
  462. Binary_buf *tp_binary_buf = new Binary_buf(reinterpret_cast<char *>(m_buf), PACKET_SIZE);
  463. //将数据添加到队列中, 然后内存权限转交给队列.
  464. m_communication_data_queue.push(tp_binary_buf);
  465. m_capture_time = std::chrono::steady_clock::now();
  466. m_velodyne_socket_status = E_READY;
  467. }
  468. else
  469. {
  470. m_velodyne_socket_status = E_DISCONNECT;
  471. }
  472. // 队列有数据,激活解析线程
  473. if (m_communication_data_queue.size() > 0)
  474. m_parse_condition.notify_one(false, true);
  475. }
  476. }
  477. LOG(INFO) << " Velodyne_lidar_device::capture_thread_fun() end :" << this;
  478. return;
  479. }
  480. void Velodyne_lidar_device::parse_thread_fun()
  481. {
  482. LOG(INFO) << " Velodyne_lidar_device::parse_thread_fun() start " << this;
  483. Error_manager t_error;
  484. while (m_parse_condition.is_alive())
  485. {
  486. m_parse_condition.wait();
  487. if (m_parse_condition.is_alive())
  488. {
  489. if (m_velodyne_protocol_status == E_READY)
  490. m_velodyne_protocol_status = E_BUSY;
  491. if (m_protocol.isInitialized())
  492. {
  493. // LOG(INFO) << "11111";
  494. Binary_buf *buf = *m_communication_data_queue.wait_and_pop();
  495. // LOG(INFO) << "22222";
  496. if (buf != nullptr)
  497. {
  498. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  499. m_protocol.unpack(reinterpret_cast<unsigned char *>(buf->get_buf()), m_cloud_buf);
  500. m_num_packet_inside++;
  501. m_parse_time = std::chrono::steady_clock::now();
  502. // 雷达掉线,清除不完整数据,不更新时间戳
  503. if (m_velodyne_socket_status != E_READY && m_velodyne_socket_status != E_BUSY)
  504. {
  505. m_num_packet_inside = 0;
  506. m_cloud_buf.clear();
  507. m_ring_cloud.clear();
  508. }
  509. else if (m_num_packet_inside >= m_num_packet_per_scan) // 一环数据填满,同时更新时间戳
  510. {
  511. m_num_packet_inside = 0;
  512. m_ring_cloud.clear();
  513. m_ring_cloud = m_cloud_buf;
  514. m_cloud_buf.clear();
  515. // 对环点云进行内参变换
  516. for (size_t i = 0; i < m_ring_cloud.size(); i++)
  517. {
  518. m_ring_cloud[i].transform(m_calib_matrix);
  519. }
  520. m_parse_ring_time = std::chrono::steady_clock::now();
  521. }
  522. }
  523. m_velodyne_protocol_status = E_READY;
  524. }
  525. else
  526. {
  527. m_velodyne_protocol_status = E_FAULT;
  528. }
  529. }
  530. }
  531. LOG(INFO) << " Velodyne_lidar_device::parse_thread_fun() end " << this;
  532. return;
  533. }
  534. //mp_laser_manager_thread 线程执行函数,
  535. //thread_work 内部线程负责获取点云结果
  536. void Velodyne_lidar_device::execute_thread_fun()
  537. {
  538. LOG(INFO) << " Velodyne_lidar_device::execute_thread_fun() start " << this;
  539. Error_manager t_error;
  540. //雷达管理的独立线程,负责控制管理所有的雷达。
  541. while (m_execute_condition.is_alive())
  542. {
  543. m_execute_condition.wait();
  544. if (m_execute_condition.is_alive())
  545. {
  546. std::this_thread::yield();
  547. if (mp_velodyne_lidar_scan_task == NULL)
  548. {
  549. //没有任务就忽略, 直接关闭线程
  550. m_velodyne_lidar_device_status = E_READY;
  551. m_execute_condition.notify_all(false);
  552. }
  553. else
  554. {
  555. if (mp_velodyne_lidar_scan_task->is_over_time())
  556. {
  557. t_error.error_manager_reset(TASK_TIMEOVER, MINOR_ERROR, "Velodyne_lidar_device::get_cloud_thread() TASK_TIMEOVER");
  558. mp_velodyne_lidar_scan_task->compare_and_cover_task_error_manager(t_error);
  559. //故障结束任务
  560. end_task();
  561. }
  562. else
  563. {
  564. //获取这个时间后面的点云.
  565. if (m_parse_ring_time > mp_velodyne_lidar_scan_task->get_command_time())
  566. {
  567. std::mutex *tp_mutex = mp_velodyne_lidar_scan_task->get_task_cloud_lock();
  568. pcl::PointCloud<pcl::PointXYZ>::Ptr tp_cloud = mp_velodyne_lidar_scan_task->get_task_point_cloud();
  569. {
  570. std::unique_lock<std::mutex> lck(*tp_mutex);
  571. {
  572. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  573. // 转换lidar points为pcl点云
  574. for (size_t i = 0; i < m_ring_cloud.size(); i++)
  575. {
  576. tp_cloud->push_back(pcl::PointXYZ(m_ring_cloud[i].x, m_ring_cloud[i].y, m_ring_cloud[i].z));
  577. }
  578. }
  579. }
  580. mp_velodyne_lidar_scan_task->compare_and_cover_task_error_manager(t_error);
  581. //正常结束任务
  582. end_task();
  583. }
  584. else
  585. {
  586. //等1ms
  587. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  588. }
  589. }
  590. }
  591. }
  592. }
  593. LOG(INFO) << " Velodyne_lidar_device::execute_thread_fun() end :" << this;
  594. return;
  595. }