LivoxLaser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "LivoxLaser.h"
  2. RegisterLaser(Livox)
  3. CLivoxLaser::DeviceItem CLivoxLaser::g_devices[kMaxLidarCount] = { 0 };
  4. std::map<uint8_t, std::string> CLivoxLaser::g_handle_sn= std::map<uint8_t, std::string>();
  5. std::map<std::string, uint8_t> CLivoxLaser::g_sn_handle = std::map<std::string, uint8_t>();
  6. std::map<std::string, CLivoxLaser*> CLivoxLaser::g_sn_laser= std::map<std::string, CLivoxLaser*>();
  7. CLivoxLaser* CLivoxLaser::g_all_laser[kMaxLidarCount] = { 0 };
  8. unsigned int CLivoxLaser::g_count[kMaxLidarCount] = { 0 };
  9. CLivoxLaser::CLivoxLaser(int id, Laser_proto::laser_parameter laser_param)
  10. :Laser_base(id, laser_param)
  11. {
  12. //设备livox扫描最大帧数
  13. m_frame_maxnum = laser_param.frame_num();
  14. //判断参数类型,
  15. if(laser_param.type()=="Livox")
  16. {
  17. //填充雷达设备的广播码
  18. g_sn_laser.insert(std::make_pair(laser_param.sn(), this));
  19. //初始化livox
  20. InitLivox();
  21. }
  22. }
  23. CLivoxLaser::~CLivoxLaser()
  24. {
  25. }
  26. //雷达链接设备,为3个线程添加线程执行函数。
  27. Error_manager CLivoxLaser::connect_laser()
  28. {
  29. return Laser_base::connect_laser();
  30. }
  31. //雷达断开链接,释放3个线程
  32. Error_manager CLivoxLaser::disconnect_laser()
  33. {
  34. return Laser_base::disconnect_laser();
  35. }
  36. //对外的接口函数,负责接受并处理任务单,
  37. //input:p_laser_task 雷达任务单,基类的指针,指向子类的实例,(多态)
  38. //注:这个函数为虚函数,实际的处理任务的代码由子类重载并实现。
  39. Error_manager CLivoxLaser::execute_task(Task_Base* p_laser_task)
  40. {
  41. LOG(INFO) << " CLivoxLaser::execute_task start "<< this;
  42. Error_manager t_error;
  43. Error_manager t_result;
  44. //检查指针
  45. if (p_laser_task == NULL) {
  46. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  47. "Laser_base::porform_task failed, POINTER_IS_NULL");
  48. }
  49. //检查任务类型,
  50. if (p_laser_task->get_task_type() != LASER_TASK)
  51. {
  52. return Error_manager(Error_code::LIVOX_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  53. "laser task type error != LASER_TASK");
  54. }
  55. //检查雷达状态
  56. t_error = check_laser();
  57. if ( t_error != SUCCESS )
  58. {
  59. t_result.compare_and_cover_error(t_error);
  60. return t_result;
  61. }
  62. else
  63. {
  64. //接受任务,并将任务的状态改为TASK_SIGNED已签收
  65. mp_laser_task = (Laser_task *) p_laser_task;
  66. mp_laser_task->set_task_statu(TASK_SIGNED);
  67. //检查消息内容是否正确,
  68. //检查三维点云指针
  69. if (mp_laser_task->get_task_point_cloud().get() == NULL)
  70. {
  71. t_result.error_manager_reset(Error_code::POINTER_IS_NULL,
  72. Error_level::MINOR_ERROR,
  73. "execute_task mp_task_point_cloud is null");
  74. t_result.compare_and_cover_error(t_error);
  75. }
  76. else if (mp_laser_task->get_task_cloud_lock() == NULL)
  77. {
  78. t_error.error_manager_reset(Error_code::POINTER_IS_NULL,
  79. Error_level::MINOR_ERROR,
  80. "execute_task mp_task_cloud_lock is null");
  81. t_result.compare_and_cover_error(t_error);
  82. }
  83. else
  84. {
  85. m_frame_maxnum=mp_laser_task->get_task_frame_maxnum();
  86. //设置保存文件的路径
  87. std::string save_path = mp_laser_task->get_task_save_path();
  88. t_error = set_open_save_path(save_path,m_laser_param.is_save_txt());
  89. if ( t_error != Error_code::SUCCESS )
  90. {
  91. //文件保存文件的路径的设置 允许失败。继续后面的动作
  92. t_result.compare_and_cover_error(t_error);
  93. }
  94. //启动雷达扫描
  95. t_error = start_scan();
  96. if ( t_error != Error_code::SUCCESS )
  97. {
  98. t_result.compare_and_cover_error(t_error);
  99. }
  100. else
  101. {
  102. //将任务的状态改为 TASK_WORKING 处理中
  103. mp_laser_task->set_task_statu(TASK_WORKING);
  104. }
  105. }
  106. }
  107. //返回错误码
  108. if (t_result != Error_code::SUCCESS)
  109. {
  110. //将任务的状态改为 TASK_OVER 结束任务
  111. mp_laser_task->set_task_statu(TASK_OVER);
  112. //返回错误码
  113. mp_laser_task->set_task_error_manager(t_result);
  114. return t_result;
  115. }
  116. return t_result;
  117. }
  118. //检查雷达状态,是否正常运行
  119. Error_manager CLivoxLaser::check_laser()
  120. {
  121. if ( is_ready() )
  122. {
  123. return Error_code::SUCCESS;
  124. }
  125. else
  126. {
  127. return Error_manager(Error_code::lIVOX_STATUS_ERROR, Error_level::MINOR_ERROR,
  128. " CLivoxLaser::check_status error ");
  129. }
  130. return Error_code::SUCCESS;
  131. }
  132. //雷达的启动接口函数, 让雷达进行扫描,一般需要子类重载,不同的雷达开始方式不同。
  133. Error_manager CLivoxLaser::start_scan()
  134. {
  135. LOG(INFO) << " livox start :"<<this;
  136. //清空livox子类的队列,
  137. m_queue_livox_data.clear_and_delete();
  138. g_count[m_handle] = 0;
  139. return Laser_base::start_scan();
  140. }
  141. //雷达的停止接口函数, 让雷达停止扫描,一般需要子类重载,不同的雷达结束方式不同。
  142. Error_manager CLivoxLaser::stop_scan()
  143. {
  144. return Laser_base::stop_scan();
  145. }
  146. //结束任务单,stop之后,要检查线程状态和数据结果,然后才能 end_task
  147. Error_manager CLivoxLaser::end_task()
  148. {
  149. return Laser_base::end_task();
  150. }
  151. //判断雷达状态是否为待机,如果已经准备好,则可以执行任务。
  152. //子类重载 is_ready(),里面增加livox sdk后台线程状态的判断。
  153. bool CLivoxLaser::is_ready()
  154. {
  155. //livox雷达设备的状态,livox sdk后台线程的状态
  156. if ( m_laser_statu == LASER_READY &&
  157. g_devices[m_handle].device_state != kDeviceStateDisconnect )
  158. {
  159. true;
  160. }
  161. else
  162. {
  163. false;
  164. }
  165. }
  166. //接受二进制消息的功能函数,每次只接受一个CBinaryData
  167. // 纯虚函数,必须由子类重载,
  168. bool CLivoxLaser::receive_buf_to_queue(Binary_buf& binary_buf)
  169. {
  170. Binary_buf* tp_livox_buf;
  171. if (m_queue_livox_data.try_pop(tp_livox_buf))
  172. {
  173. binary_buf = *tp_livox_buf;
  174. delete tp_livox_buf;
  175. std::cout << "huli m_queue_livox_data.try_pop" << std::endl;
  176. return true;
  177. }
  178. return false;
  179. }
  180. //将二进制消息转化为三维点云的功能函数,每次只转化一个CBinaryData,
  181. // 纯虚函数,必须由子类重载,
  182. Buf_type CLivoxLaser::transform_buf_to_points(Binary_buf* p_binary_buf, std::vector<CPoint3D>& point3D_cloud)
  183. {
  184. if ( p_binary_buf ==NULL )
  185. {
  186. return BUF_UNKNOW;
  187. }
  188. else
  189. {
  190. //livox的二进制数据格式就是三维点坐标。直接强行转化指针类型即可
  191. LivoxRawPoint *tp_Livox_data = (LivoxRawPoint *)p_binary_buf->get_buf();
  192. //计算这一帧数据有多少个三维点。
  193. int t_count = p_binary_buf->get_length() / (sizeof(LivoxRawPoint));
  194. if (t_count <= 0)
  195. {
  196. return BUF_UNKNOW;
  197. }
  198. else
  199. {
  200. //转变三维点格式,并存入vector。
  201. for (int i = 0; i < t_count; ++i)
  202. {
  203. //LivoxRawPoint 转为 CPoint3D
  204. //int32_t 转 double。不要信号强度
  205. LivoxRawPoint t_livox_point = tp_Livox_data[i];
  206. CPoint3D t_point3D;
  207. t_point3D.x = t_livox_point.x;
  208. t_point3D.y = t_livox_point.y;
  209. t_point3D.z = t_livox_point.z;
  210. point3D_cloud.push_back(t_point3D);
  211. // std::cout << "huli points:" << t_point3D.x << " : " << t_point3D.y << " : " << t_point3D.z<< std::endl;
  212. }
  213. return BUF_DATA;
  214. }
  215. }
  216. }
  217. void CLivoxLaser::InitLivox()
  218. {
  219. static bool g_init = false;
  220. if (g_init == false)
  221. {
  222. if (!Init()) {
  223. LOG(INFO) << "livox sdk init failed...";
  224. }
  225. else
  226. {
  227. LivoxSdkVersion _sdkversion;
  228. GetLivoxSdkVersion(&_sdkversion);
  229. char buf[255] = { 0 };
  230. sprintf(buf, "Livox SDK version %d.%d.%d .\n", _sdkversion.major, _sdkversion.minor, _sdkversion.patch);
  231. LOG(INFO) << buf;
  232. SetBroadcastCallback(OnDeviceBroadcast);
  233. SetDeviceStateUpdateCallback(OnDeviceChange);
  234. g_init = true;
  235. }
  236. }
  237. }
  238. bool CLivoxLaser::IsScanComplete()
  239. {
  240. //雷达的采集帧数判断,直接比较任务单里面的帧数最大值
  241. if(mp_laser_task!=NULL)
  242. return g_count[m_handle] >= mp_laser_task->get_task_frame_maxnum();
  243. else
  244. return false;
  245. }
  246. void CLivoxLaser::UpdataHandle()
  247. {
  248. std::string sn = m_laser_param.sn();
  249. if (g_sn_handle.find(sn) != g_sn_handle.end())
  250. {
  251. m_handle = g_sn_handle[sn];
  252. }
  253. }
  254. void CLivoxLaser::OnSampleCallback(uint8_t status, uint8_t handle, uint8_t response, void *data) {
  255. CLivoxLaser* laser = (CLivoxLaser*)data;
  256. if (status == kStatusSuccess) {
  257. if (response != 0) {
  258. g_devices[handle].device_state = kDeviceStateConnect;
  259. }
  260. }
  261. else if (status == kStatusTimeout) {
  262. g_devices[handle].device_state = kDeviceStateConnect;
  263. }
  264. }
  265. void CLivoxLaser::OnDeviceChange(const DeviceInfo *info, DeviceEvent type)
  266. {
  267. if (info == NULL) {
  268. return;
  269. }
  270. uint8_t handle = info->handle;
  271. if (handle >= kMaxLidarCount) {
  272. return;
  273. }
  274. /*std::cout<<"-----------------------------------------------------------------"<<std::endl;
  275. std::cout<<" OnDeviceChange handle : "<<info->broadcast_code<<" type : "<<type <<" device_state : "
  276. <<g_devices[handle].device_state<<std::endl;*/
  277. if (type == kEventConnect) {
  278. //QueryDeviceInformation(handle, OnDeviceInformation, NULL);
  279. if (g_devices[handle].device_state == kDeviceStateDisconnect)
  280. {
  281. g_devices[handle].device_state = kDeviceStateConnect;
  282. g_devices[handle].info = *info;
  283. }
  284. }
  285. else if (type == kEventDisconnect) {
  286. g_devices[handle].device_state = kDeviceStateDisconnect;
  287. }
  288. else if (type == kEventStateChange) {
  289. g_devices[handle].info = *info;
  290. }
  291. if (g_devices[handle].device_state == kDeviceStateConnect)
  292. {
  293. if (g_devices[handle].info.state == kLidarStateNormal)
  294. {
  295. if (g_devices[handle].info.type == kDeviceTypeHub)
  296. {
  297. HubStartSampling(OnSampleCallback, NULL);
  298. }
  299. else
  300. {
  301. LidarStartSampling(handle, OnSampleCallback, NULL);
  302. }
  303. g_devices[handle].device_state = kDeviceStateSampling;
  304. }
  305. }
  306. }
  307. void CLivoxLaser::OnDeviceBroadcast(const BroadcastDeviceInfo *info)
  308. {
  309. if (info == NULL) {
  310. return;
  311. }
  312. //printf("Receive Broadcast Code %s\n", info->broadcast_code);
  313. LOG(INFO) << " broadcast sn : " << info->broadcast_code;
  314. bool result = false;
  315. uint8_t handle = 0;
  316. result = AddLidarToConnect(info->broadcast_code, &handle);
  317. if (result == kStatusSuccess) {
  318. /** Set the point cloud data for a specific Livox LiDAR. */
  319. SetDataCallback(handle, LidarDataCallback, NULL);
  320. g_devices[handle].handle = handle;
  321. g_devices[handle].device_state = kDeviceStateDisconnect;
  322. std::string sn = info->broadcast_code;
  323. if (g_handle_sn.find(handle) != g_handle_sn.end())
  324. g_handle_sn[handle] = sn;
  325. else
  326. g_handle_sn.insert(std::make_pair(handle,sn));
  327. if (g_sn_handle.find(sn) != g_sn_handle.end())
  328. g_sn_handle[sn] = handle;
  329. else
  330. g_sn_handle.insert(std::make_pair(sn,handle));
  331. }
  332. }
  333. void CLivoxLaser::LidarDataCallback(uint8_t handle, LivoxEthPacket *data, uint32_t data_num, void *client_data)
  334. {
  335. // std::cout << "huli LidarDataCallback" << std::endl;
  336. CLivoxLaser* livox = g_all_laser[handle];
  337. if (livox == NULL)
  338. {
  339. if (g_handle_sn.find(handle) != g_handle_sn.end())
  340. {
  341. std::string sn = g_handle_sn[handle];
  342. if (g_sn_laser.find(sn) != g_sn_laser.end())
  343. {
  344. livox = g_sn_laser[sn];
  345. g_all_laser[handle] = livox;
  346. if (livox)
  347. livox->UpdataHandle();
  348. }
  349. }
  350. }
  351. // std::cout << "huli LidarDataCallback " << std::endl;
  352. if (data && livox)
  353. {
  354. // std::cout << "huli m_laser_scan_flag = " << livox->m_laser_scan_flag << std::endl;
  355. //判断雷达扫描标志位
  356. if (livox->m_laser_scan_flag)
  357. {
  358. if (livox->IsScanComplete())
  359. {
  360. std::cout << "huli qwe point count = " << livox->g_count[livox->m_handle] << std::endl;
  361. livox->stop_scan();
  362. return;
  363. }
  364. else
  365. {
  366. std::cout << "huli asd point count = " << livox->g_count[livox->m_handle] << std::endl;
  367. }
  368. // std::cout << "huli LidarDataCallback " << std::endl;
  369. if (g_count[handle] >= livox->m_frame_maxnum)
  370. return;
  371. uint64_t cur_timestamp = *((uint64_t *)(data->timestamp));
  372. LivoxRawPoint *p_point_data = (LivoxRawPoint *)data->data;
  373. Binary_buf* data_bin = new Binary_buf((char*)p_point_data, data_num * sizeof(LivoxRawPoint));
  374. livox->m_queue_livox_data.push(data_bin);
  375. std::cout << "huli m_queue_livox_data.push " << std::endl;
  376. g_count[handle]++;
  377. }
  378. /* else
  379. {
  380. std::cout << "huli 1z error " << "data = "<< data << std::endl;
  381. std::cout << "huli 1z error " << "livox = "<< livox << std::endl;
  382. //?????????????????
  383. livox->m_laser_statu = LASER_READY;
  384. usleep(10*1000);
  385. }*/
  386. }
  387. else
  388. {
  389. std::cout << "huli 2z error " << "data = "<< data << std::endl;
  390. std::cout << "huli 2z error " << "livox = "<< livox << std::endl;
  391. }
  392. }