LivoxLaser.cpp 5.8 KB

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