LivoxLaser.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. void CLivoxLaser::UpdataHandle()
  10. {
  11. std::string sn = m_laser_param.sn();
  12. if (g_sn_handle.find(sn) != g_sn_handle.end())
  13. {
  14. m_handle = g_sn_handle[sn];
  15. }
  16. }
  17. bool CLivoxLaser::IsScanComplete()
  18. {
  19. if(mp_laser_task!=NULL)
  20. return g_count[m_handle] >= mp_laser_task->get_task_points_number();
  21. else
  22. return false;
  23. }
  24. void CLivoxLaser::InitLivox()
  25. {
  26. static bool g_init = false;
  27. if (g_init == false)
  28. {
  29. if (!Init()) {
  30. LOG(INFO) << "livox sdk init failed...";
  31. }
  32. else
  33. {
  34. LivoxSdkVersion _sdkversion;
  35. GetLivoxSdkVersion(&_sdkversion);
  36. char buf[255] = { 0 };
  37. sprintf(buf, "Livox SDK version %d.%d.%d .\n", _sdkversion.major, _sdkversion.minor, _sdkversion.patch);
  38. LOG(INFO) << buf;
  39. SetBroadcastCallback(OnDeviceBroadcast);
  40. SetDeviceStateUpdateCallback(OnDeviceChange);
  41. g_init = true;
  42. }
  43. }
  44. }
  45. CLivoxLaser::CLivoxLaser(int id, Laser_proto::laser_parameter laser_param)
  46. :Laser_base(id, laser_param)
  47. {
  48. m_frame_maxnum = laser_param.frame_num();
  49. if(laser_param.type()=="Livox")
  50. g_sn_laser.insert(std::make_pair(laser_param.sn(), this));
  51. InitLivox();
  52. }
  53. CLivoxLaser::~CLivoxLaser()
  54. {
  55. }
  56. void CLivoxLaser::OnSampleCallback(uint8_t status, uint8_t handle, uint8_t response, void *data) {
  57. CLivoxLaser* laser = (CLivoxLaser*)data;
  58. if (status == kStatusSuccess) {
  59. if (response != 0) {
  60. g_devices[handle].device_state = kDeviceStateConnect;
  61. }
  62. }
  63. else if (status == kStatusTimeout) {
  64. g_devices[handle].device_state = kDeviceStateConnect;
  65. }
  66. }
  67. void CLivoxLaser::OnDeviceChange(const DeviceInfo *info, DeviceEvent type)
  68. {
  69. if (info == NULL) {
  70. return;
  71. }
  72. uint8_t handle = info->handle;
  73. if (handle >= kMaxLidarCount) {
  74. return;
  75. }
  76. /*std::cout<<"-----------------------------------------------------------------"<<std::endl;
  77. std::cout<<" OnDeviceChange handle : "<<info->broadcast_code<<" type : "<<type <<" device_state : "
  78. <<g_devices[handle].device_state<<std::endl;*/
  79. if (type == kEventConnect) {
  80. //QueryDeviceInformation(handle, OnDeviceInformation, NULL);
  81. if (g_devices[handle].device_state == kDeviceStateDisconnect)
  82. {
  83. g_devices[handle].device_state = kDeviceStateConnect;
  84. g_devices[handle].info = *info;
  85. }
  86. }
  87. else if (type == kEventDisconnect) {
  88. g_devices[handle].device_state = kDeviceStateDisconnect;
  89. }
  90. else if (type == kEventStateChange) {
  91. g_devices[handle].info = *info;
  92. }
  93. if (g_devices[handle].device_state == kDeviceStateConnect)
  94. {
  95. if (g_devices[handle].info.state == kLidarStateNormal) {
  96. if (g_devices[handle].info.type == kDeviceTypeHub) {
  97. HubStartSampling(OnSampleCallback, NULL);
  98. }
  99. else {
  100. LidarStartSampling(handle, OnSampleCallback, NULL);
  101. }
  102. g_devices[handle].device_state = kDeviceStateSampling;
  103. }
  104. }
  105. }
  106. void CLivoxLaser::OnDeviceBroadcast(const BroadcastDeviceInfo *info)
  107. {
  108. if (info == NULL) {
  109. return;
  110. }
  111. //printf("Receive Broadcast Code %s\n", info->broadcast_code);
  112. LOG(INFO) << " broadcast sn : " << info->broadcast_code;
  113. bool result = false;
  114. uint8_t handle = 0;
  115. result = AddLidarToConnect(info->broadcast_code, &handle);
  116. if (result == kStatusSuccess) {
  117. /** Set the point cloud data for a specific Livox LiDAR. */
  118. SetDataCallback(handle, LidarDataCallback, NULL);
  119. g_devices[handle].handle = handle;
  120. g_devices[handle].device_state = kDeviceStateDisconnect;
  121. std::string sn = info->broadcast_code;
  122. if (g_handle_sn.find(handle) != g_handle_sn.end())
  123. g_handle_sn[handle] = sn;
  124. else
  125. g_handle_sn.insert(std::make_pair(handle,sn));
  126. if (g_sn_handle.find(sn) != g_sn_handle.end())
  127. g_sn_handle[sn] = handle;
  128. else
  129. g_sn_handle.insert(std::make_pair(sn,handle));
  130. }
  131. }
  132. void CLivoxLaser::LidarDataCallback(uint8_t handle, LivoxEthPacket *data, uint32_t data_num, void *client_data)
  133. {
  134. ///?????????????? ?????????handle
  135. CLivoxLaser* livox = g_all_laser[handle];
  136. if (livox == 0)
  137. {
  138. if (g_handle_sn.find(handle) != g_handle_sn.end())
  139. {
  140. std::string sn = g_handle_sn[handle];
  141. if (g_sn_laser.find(sn) != g_sn_laser.end())
  142. {
  143. livox = g_sn_laser[sn];
  144. g_all_laser[handle] = livox;
  145. if (livox)
  146. livox->UpdataHandle();
  147. }
  148. }
  149. }
  150. if (data && livox)
  151. {
  152. if (livox->m_bStart_capture.WaitFor(1))
  153. {
  154. if (livox->IsScanComplete())
  155. {
  156. livox->Stop();
  157. return;
  158. }
  159. if (g_count[handle] >= livox->m_frame_maxnum)
  160. return;
  161. uint64_t cur_timestamp = *((uint64_t *)(data->timestamp));
  162. LivoxRawPoint *p_point_data = (LivoxRawPoint *)data->data;
  163. CBinaryData* data_bin = new CBinaryData((char*)p_point_data, data_num * sizeof(LivoxRawPoint));
  164. livox->m_queue_livox_data.push(data_bin);
  165. g_count[handle]++;
  166. }
  167. else if(livox->m_statu!=eLaser_ready)
  168. {
  169. //?????????????????
  170. livox->m_statu = eLaser_ready;
  171. usleep(10*1000);
  172. }
  173. }
  174. }
  175. eLaserStatu CLivoxLaser::GetStatu()
  176. {
  177. if(g_devices[m_handle].device_state == kDeviceStateConnect||
  178. g_devices[m_handle].device_state == kDeviceStateSampling)
  179. {
  180. m_statu=eLaser_ready;
  181. }
  182. if(g_devices[m_handle].device_state == kDeviceStateDisconnect)
  183. {
  184. m_statu=eLaser_disconnect;
  185. }
  186. return m_statu;
  187. }
  188. bool CLivoxLaser::Connect()
  189. {
  190. return Laser_base::Connect();
  191. }
  192. void CLivoxLaser::Disconnect()
  193. {
  194. Laser_base::Disconnect();
  195. }
  196. bool CLivoxLaser::Start()
  197. {
  198. LOG(INFO) << " livox start :"<<this;
  199. m_queue_livox_data.clear();
  200. g_count[m_handle] = 0;
  201. return Laser_base::Start();
  202. }
  203. bool CLivoxLaser::Stop()
  204. {
  205. return Laser_base::Stop();
  206. }
  207. bool CLivoxLaser::RecvData(CBinaryData& data)
  208. {
  209. CBinaryData* livox_data;
  210. if (m_queue_livox_data.try_pop(livox_data))
  211. {
  212. data = *livox_data;
  213. delete livox_data;
  214. return true;
  215. }
  216. return false;
  217. }
  218. DATA_type CLivoxLaser::Data2PointXYZ(CBinaryData* pData, std::vector<CPoint3D>& points)
  219. {
  220. LivoxRawPoint *p_point_data = (LivoxRawPoint *)pData->Data();
  221. int count = pData->Length() / (sizeof(LivoxRawPoint));
  222. if (count <= 0)
  223. return eUnknow;
  224. for (int i = 0; i < count; ++i)
  225. {
  226. LivoxRawPoint point = p_point_data[i];
  227. CPoint3D pt;
  228. pt.x = point.x;
  229. pt.y = point.y;
  230. pt.z = point.z;
  231. points.push_back(pt);
  232. }
  233. return eData;
  234. }