LivoxLaser.cpp 12 KB

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