LivoxLaser.cpp 11 KB

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