lds_lidar.cpp
Go to the documentation of this file.
1 //
2 // The MIT License (MIT)
3 //
4 // Copyright (c) 2019 Livox. All rights reserved.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //
24 
25 #include "lds_lidar.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <memory>
30 #include <mutex>
31 #include <thread>
32 
33 #include "rapidjson/document.h"
35 #include "rapidjson/stringbuffer.h"
36 
37 using namespace std;
38 
39 namespace livox_ros {
40 
43 LdsLidar *g_lds_ldiar = nullptr;
44 
48 LdsLidar::LdsLidar(uint32_t interval_ms) : Lds(interval_ms, kSourceRawLidar) {
49  auto_connect_mode_ = true;
50  is_initialized_ = false;
51 
52  whitelist_count_ = 0;
54 
55  ResetLdsLidar();
56 }
57 
59 
61 
62 int LdsLidar::InitLdsLidar(std::vector<std::string> &broadcast_code_strs,
63  const char *user_config_path) {
64  if (is_initialized_) {
65  printf("LiDAR data source is already inited!\n");
66  return -1;
67  }
68 
69  if (!Init()) {
70  Uninit();
71  printf("Livox-SDK init fail!\n");
72  return -1;
73  }
74 
75  LivoxSdkVersion _sdkversion;
76  GetLivoxSdkVersion(&_sdkversion);
77  printf("Livox SDK version %d.%d.%d\n", _sdkversion.major, _sdkversion.minor,
78  _sdkversion.patch);
79 
80  SetBroadcastCallback(OnDeviceBroadcast);
81  SetDeviceStateUpdateCallback(OnDeviceChange);
82 
84  for (auto input_str : broadcast_code_strs) {
85  AddBroadcastCodeToWhitelist(input_str.c_str());
86  }
87 
88  ParseConfigFile(user_config_path);
89 
90  if (whitelist_count_) {
92  printf("Disable auto connect mode!\n");
93 
94  printf("List all broadcast code in whiltelist:\n");
95  for (uint32_t i = 0; i < whitelist_count_; i++) {
96  printf("%s\n", broadcast_code_whitelist_[i]);
97  }
98  } else {
100  printf(
101  "No broadcast code was added to whitelist, swith to automatic "
102  "connection mode!\n");
103  }
104 
105  if (enable_timesync_) {
108  printf("Timesync init fail\n");
109  return -1;
110  }
111 
113  printf("Set Timesync callback fail\n");
114  return -1;
115  }
116 
118  }
119 
121  if (!Start()) {
122  Uninit();
123  printf("Livox-SDK init fail!\n");
124  return -1;
125  }
126 
128  if (g_lds_ldiar == nullptr) {
129  g_lds_ldiar = this;
130  }
131  is_initialized_ = true;
132  printf("Livox-SDK init success!\n");
133 
134  return 0;
135 }
136 
138  if (!is_initialized_) {
139  printf("LiDAR data source is not exit");
140  return -1;
141  }
142 
143  Uninit();
144  printf("Livox SDK Deinit completely!\n");
145 
146  if (timesync_) {
148  }
149 
150  return 0;
151 }
152 
154 
158 void LdsLidar::OnLidarDataCb(uint8_t handle, LivoxEthPacket *data,
159  uint32_t data_num, void *client_data) {
160  using namespace std;
161 
162  LdsLidar *lds_lidar = static_cast<LdsLidar *>(client_data);
163  LivoxEthPacket *eth_packet = data;
164 
165  if (!data || !data_num || (handle >= kMaxLidarCount)) {
166  return;
167  }
168 
169  lds_lidar->StorageRawPacket(handle, eth_packet);
170 }
171 
172 void LdsLidar::OnDeviceBroadcast(const BroadcastDeviceInfo *info) {
173  if (info == nullptr) {
174  return;
175  }
176 
177  if (info->dev_type == kDeviceTypeHub) {
178  printf("In lidar mode, couldn't connect a hub : %s\n",
179  info->broadcast_code);
180  return;
181  }
182 
183  if (g_lds_ldiar->IsAutoConnectMode()) {
184  printf("In automatic connection mode, will connect %s\n",
185  info->broadcast_code);
186  } else {
187  if (!g_lds_ldiar->IsBroadcastCodeExistInWhitelist(info->broadcast_code)) {
188  printf("Not in the whitelist, please add %s to if want to connect!\n",
189  info->broadcast_code);
190  return;
191  }
192  }
193 
194  bool result = false;
195  uint8_t handle = 0;
196  result = AddLidarToConnect(info->broadcast_code, &handle);
197  if (result == kStatusSuccess && handle < kMaxLidarCount) {
198  SetDataCallback(handle, OnLidarDataCb, (void *)g_lds_ldiar);
199 
200  LidarDevice *p_lidar = &(g_lds_ldiar->lidars_[handle]);
201  p_lidar->handle = handle;
202  p_lidar->connect_state = kConnectStateOff;
203 
204  UserRawConfig config;
205  if (g_lds_ldiar->GetRawConfig(info->broadcast_code, config)) {
206  printf("Could not find raw config, set config to default!\n");
207  config.enable_fan = 1;
208  config.return_mode = kFirstReturn;
210  config.imu_rate = kImuFreq200Hz;
212  config.enable_high_sensitivity = false;
213  }
214 
215  p_lidar->config.enable_fan = config.enable_fan;
216  p_lidar->config.return_mode = config.return_mode;
217  p_lidar->config.coordinate = config.coordinate;
218  p_lidar->config.imu_rate = config.imu_rate;
222  } else {
223  printf("Add lidar to connect is failed : %d %d \n", result, handle);
224  }
225 }
226 
228 void LdsLidar::OnDeviceChange(const DeviceInfo *info, DeviceEvent type) {
229  if (info == nullptr) {
230  return;
231  }
232 
233  uint8_t handle = info->handle;
234  if (handle >= kMaxLidarCount) {
235  return;
236  }
237 
238  LidarDevice *p_lidar = &(g_lds_ldiar->lidars_[handle]);
239  if (type == kEventConnect) {
240  QueryDeviceInformation(handle, DeviceInformationCb, g_lds_ldiar);
241  if (p_lidar->connect_state == kConnectStateOff) {
242  p_lidar->connect_state = kConnectStateOn;
243  p_lidar->info = *info;
244  }
245  } else if (type == kEventDisconnect) {
246  printf("Lidar[%s] disconnect!\n", info->broadcast_code);
247  ResetLidar(p_lidar, kSourceRawLidar);
248  } else if (type == kEventStateChange) {
249  p_lidar->info = *info;
250  }
251 
252  if (p_lidar->connect_state == kConnectStateOn) {
253  printf("Lidar[%s] status_code[%d] working state[%d] feature[%d]\n",
254  p_lidar->info.broadcast_code,
255  p_lidar->info.status.status_code.error_code, p_lidar->info.state,
256  p_lidar->info.feature);
257  SetErrorMessageCallback(handle, LidarErrorStatusCb);
258 
260  if (p_lidar->info.state == kLidarStateNormal) {
262  lock_guard<mutex> lock(g_lds_ldiar->config_mutex_);
263 
264  if (p_lidar->config.coordinate != 0) {
265  SetSphericalCoordinate(handle, SetCoordinateCb, g_lds_ldiar);
266  } else {
267  SetCartesianCoordinate(handle, SetCoordinateCb, g_lds_ldiar);
268  }
269  p_lidar->config.set_bits |= kConfigCoordinate;
270 
271  if (kDeviceTypeLidarMid40 != info->type) {
272  LidarSetPointCloudReturnMode(
273  handle, (PointCloudReturnMode)(p_lidar->config.return_mode),
274  SetPointCloudReturnModeCb, g_lds_ldiar);
275  p_lidar->config.set_bits |= kConfigReturnMode;
276  }
277 
278  if ((kDeviceTypeLidarMid70 != info->type) &&
279  (kDeviceTypeLidarMid40 != info->type)) {
280  LidarSetImuPushFrequency(handle, (ImuFreq)(p_lidar->config.imu_rate),
281  SetImuRatePushFrequencyCb, g_lds_ldiar);
282  p_lidar->config.set_bits |= kConfigImuRate;
283  }
284 
285  if (p_lidar->config.extrinsic_parameter_source ==
287  LidarGetExtrinsicParameter(handle, GetLidarExtrinsicParameterCb,
288  g_lds_ldiar);
290  }
291 
292  if (kDeviceTypeLidarTele == info->type) {
293  if (p_lidar->config.enable_high_sensitivity) {
294  LidarEnableHighSensitivity(handle, SetHighSensitivityCb, g_lds_ldiar);
295  printf("Enable high sensitivity\n");
296  } else {
297  LidarDisableHighSensitivity(handle, SetHighSensitivityCb,
298  g_lds_ldiar);
299  printf("Disable high sensitivity\n");
300  }
302  }
303 
305  }
306  }
307 }
308 
310 void LdsLidar::DeviceInformationCb(livox_status status, uint8_t handle,
311  DeviceInformationResponse *ack,
312  void *clent_data) {
313  if (status != kStatusSuccess) {
314  printf("Device Query Informations Failed : %d\n", status);
315  }
316  if (ack) {
317  printf("firmware version: %d.%d.%d.%d\n", ack->firmware_version[0],
318  ack->firmware_version[1], ack->firmware_version[2],
319  ack->firmware_version[3]);
320  }
321 }
322 
324 void LdsLidar::LidarErrorStatusCb(livox_status status, uint8_t handle,
325  ErrorMessage *message) {
326  static uint32_t error_message_count = 0;
327  if (message != NULL) {
328  ++error_message_count;
329  if (0 == (error_message_count % 100)) {
330  printf("handle: %u\n", handle);
331  printf("temp_status : %u\n", message->lidar_error_code.temp_status);
332  printf("volt_status : %u\n", message->lidar_error_code.volt_status);
333  printf("motor_status : %u\n", message->lidar_error_code.motor_status);
334  printf("dirty_warn : %u\n", message->lidar_error_code.dirty_warn);
335  printf("firmware_err : %u\n", message->lidar_error_code.firmware_err);
336  printf("pps_status : %u\n", message->lidar_error_code.device_status);
337  printf("fan_status : %u\n", message->lidar_error_code.fan_status);
338  printf("self_heating : %u\n", message->lidar_error_code.self_heating);
339  printf("ptp_status : %u\n", message->lidar_error_code.ptp_status);
340  printf("time_sync_status : %u\n",
341  message->lidar_error_code.time_sync_status);
342  printf("system_status : %u\n", message->lidar_error_code.system_status);
343  }
344  }
345 }
346 
347 void LdsLidar::ControlFanCb(livox_status status, uint8_t handle,
348  uint8_t response, void *clent_data) {}
349 
350 void LdsLidar::SetPointCloudReturnModeCb(livox_status status, uint8_t handle,
351  uint8_t response, void *clent_data) {
352  LdsLidar *lds_lidar = static_cast<LdsLidar *>(clent_data);
353 
354  if (handle >= kMaxLidarCount) {
355  return;
356  }
357  LidarDevice *p_lidar = &(lds_lidar->lidars_[handle]);
358 
359  if (status == kStatusSuccess) {
360  printf("Set return mode success!\n");
361 
362  lock_guard<mutex> lock(lds_lidar->config_mutex_);
363  p_lidar->config.set_bits &= ~((uint32_t)(kConfigReturnMode));
364  if (!p_lidar->config.set_bits) {
365  LidarStartSampling(handle, StartSampleCb, lds_lidar);
367  }
368  } else {
369  LidarSetPointCloudReturnMode(
370  handle, (PointCloudReturnMode)(p_lidar->config.return_mode),
371  SetPointCloudReturnModeCb, lds_lidar);
372  printf("Set return mode fail, try again!\n");
373  }
374 }
375 
376 void LdsLidar::SetCoordinateCb(livox_status status, uint8_t handle,
377  uint8_t response, void *clent_data) {
378  LdsLidar *lds_lidar = static_cast<LdsLidar *>(clent_data);
379 
380  if (handle >= kMaxLidarCount) {
381  return;
382  }
383  LidarDevice *p_lidar = &(lds_lidar->lidars_[handle]);
384 
385  if (status == kStatusSuccess) {
386  printf("Set coordinate success!\n");
387 
388  lock_guard<mutex> lock(lds_lidar->config_mutex_);
389  p_lidar->config.set_bits &= ~((uint32_t)(kConfigCoordinate));
390  if (!p_lidar->config.set_bits) {
391  LidarStartSampling(handle, StartSampleCb, lds_lidar);
393  }
394  } else {
395  if (p_lidar->config.coordinate != 0) {
396  SetSphericalCoordinate(handle, SetCoordinateCb, lds_lidar);
397  } else {
398  SetCartesianCoordinate(handle, SetCoordinateCb, lds_lidar);
399  }
400 
401  printf("Set coordinate fail, try again!\n");
402  }
403 }
404 
405 void LdsLidar::SetImuRatePushFrequencyCb(livox_status status, uint8_t handle,
406  uint8_t response, void *clent_data) {
407  LdsLidar *lds_lidar = static_cast<LdsLidar *>(clent_data);
408 
409  if (handle >= kMaxLidarCount) {
410  return;
411  }
412  LidarDevice *p_lidar = &(lds_lidar->lidars_[handle]);
413 
414  if (status == kStatusSuccess) {
415  printf("Set imu rate success!\n");
416 
417  lock_guard<mutex> lock(lds_lidar->config_mutex_);
418  p_lidar->config.set_bits &= ~((uint32_t)(kConfigImuRate));
419  if (!p_lidar->config.set_bits) {
420  LidarStartSampling(handle, StartSampleCb, lds_lidar);
422  }
423  } else {
424  LidarSetImuPushFrequency(handle, (ImuFreq)(p_lidar->config.imu_rate),
425  SetImuRatePushFrequencyCb, g_lds_ldiar);
426  printf("Set imu rate fail, try again!\n");
427  }
428 }
429 
432  livox_status status, uint8_t handle,
433  LidarGetExtrinsicParameterResponse *response, void *clent_data) {
434  LdsLidar *lds_lidar = static_cast<LdsLidar *>(clent_data);
435  if (handle >= kMaxLidarCount) {
436  return;
437  }
438 
439  if (status == kStatusSuccess) {
440  if (response != nullptr) {
441  printf("Lidar[%d] get ExtrinsicParameter status[%d] response[%d]\n",
442  handle, status, response->ret_code);
443  LidarDevice *p_lidar = &(lds_lidar->lidars_[handle]);
444  ExtrinsicParameter *p_extrinsic = &p_lidar->extrinsic_parameter;
445  p_extrinsic->euler[0] = static_cast<float>(response->roll * PI / 180.0);
446  p_extrinsic->euler[1] = static_cast<float>(response->pitch * PI / 180.0);
447  p_extrinsic->euler[2] = static_cast<float>(response->yaw * PI / 180.0);
448  p_extrinsic->trans[0] = static_cast<float>(response->x / 1000.0);
449  p_extrinsic->trans[1] = static_cast<float>(response->y / 1000.0);
450  p_extrinsic->trans[2] = static_cast<float>(response->z / 1000.0);
451  EulerAnglesToRotationMatrix(p_extrinsic->euler, p_extrinsic->rotation);
452  if (p_lidar->config.extrinsic_parameter_source) {
453  p_extrinsic->enable = true;
454  }
455  printf("Lidar[%d] get ExtrinsicParameter success!\n", handle);
456 
457  lock_guard<mutex> lock(lds_lidar->config_mutex_);
459  if (!p_lidar->config.set_bits) {
460  LidarStartSampling(handle, StartSampleCb, lds_lidar);
462  }
463  } else {
464  printf("Lidar[%d] get ExtrinsicParameter fail!\n", handle);
465  }
466  } else if (status == kStatusTimeout) {
467  printf("Lidar[%d] get ExtrinsicParameter timeout!\n", handle);
468  }
469 }
470 
471 void LdsLidar::SetHighSensitivityCb(livox_status status, uint8_t handle,
472  DeviceParameterResponse *response,
473  void *clent_data) {
474  LdsLidar *lds_lidar = static_cast<LdsLidar *>(clent_data);
475 
476  if (handle >= kMaxLidarCount) {
477  return;
478  }
479  LidarDevice *p_lidar = &(lds_lidar->lidars_[handle]);
480 
481  if (status == kStatusSuccess) {
483  printf("Set high sensitivity success!\n");
484 
485  lock_guard<mutex> lock(lds_lidar->config_mutex_);
486  if (!p_lidar->config.set_bits) {
487  LidarStartSampling(handle, StartSampleCb, lds_lidar);
489  };
490  } else {
491  if (p_lidar->config.enable_high_sensitivity) {
492  LidarEnableHighSensitivity(handle, SetHighSensitivityCb, g_lds_ldiar);
493  } else {
494  LidarDisableHighSensitivity(handle, SetHighSensitivityCb, g_lds_ldiar);
495  }
496  printf("Set high sensitivity fail, try again!\n");
497  }
498 }
499 
501 void LdsLidar::StartSampleCb(livox_status status, uint8_t handle,
502  uint8_t response, void *clent_data) {
503  LdsLidar *lds_lidar = static_cast<LdsLidar *>(clent_data);
504 
505  if (handle >= kMaxLidarCount) {
506  return;
507  }
508 
509  LidarDevice *p_lidar = &(lds_lidar->lidars_[handle]);
510  if (status == kStatusSuccess) {
511  if (response != 0) {
512  p_lidar->connect_state = kConnectStateOn;
513  printf("Lidar start sample fail : state[%d] handle[%d] res[%d]\n", status,
514  handle, response);
515  } else {
516  printf("Lidar start sample success\n");
517  }
518  } else if (status == kStatusTimeout) {
519  p_lidar->connect_state = kConnectStateOn;
520  printf("Lidar start sample timeout : state[%d] handle[%d] res[%d]\n",
521  status, handle, response);
522  }
523 }
524 
526 void LdsLidar::StopSampleCb(livox_status status, uint8_t handle,
527  uint8_t response, void *clent_data) {}
528 
529 void LdsLidar::SetRmcSyncTimeCb(livox_status status, uint8_t handle,
530  uint8_t response, void *client_data) {
531  if (handle >= kMaxLidarCount) {
532  return;
533  }
534  printf("Set lidar[%d] sync time status[%d] response[%d]\n", handle, status,
535  response);
536 }
537 
538 void LdsLidar::ReceiveSyncTimeCallback(const char *rmc, uint32_t rmc_length,
539  void *client_data) {
540  LdsLidar *lds_lidar = static_cast<LdsLidar *>(client_data);
541  // std::unique_lock<std::mutex> lock(mtx);
542  LidarDevice *p_lidar = nullptr;
543  for (uint8_t handle = 0; handle < kMaxLidarCount; handle++) {
544  p_lidar = &(lds_lidar->lidars_[handle]);
545  if (p_lidar->connect_state == kConnectStateSampling &&
546  p_lidar->info.state == kLidarStateNormal) {
547  livox_status status = LidarSetRmcSyncTime(handle, rmc, rmc_length,
548  SetRmcSyncTimeCb, lds_lidar);
549  if (status != kStatusSuccess) {
550  printf("Set GPRMC synchronization time error code: %d.\n", status);
551  }
552  }
553  }
554 }
555 
557 int LdsLidar::AddBroadcastCodeToWhitelist(const char *broadcast_code) {
558  if (!broadcast_code || (strlen(broadcast_code) > kBroadcastCodeSize) ||
559  (whitelist_count_ >= kMaxLidarCount)) {
560  return -1;
561  }
562 
563  if (LdsLidar::IsBroadcastCodeExistInWhitelist(broadcast_code)) {
564  printf("%s is alrealy exist!\n", broadcast_code);
565  return -1;
566  }
567 
568  strcpy(broadcast_code_whitelist_[whitelist_count_], broadcast_code);
570 
571  return 0;
572 }
573 
574 bool LdsLidar::IsBroadcastCodeExistInWhitelist(const char *broadcast_code) {
575  if (!broadcast_code) {
576  return false;
577  }
578 
579  for (uint32_t i = 0; i < whitelist_count_; i++) {
580  if (strncmp(broadcast_code, broadcast_code_whitelist_[i],
581  kBroadcastCodeSize) == 0) {
582  return true;
583  }
584  }
585 
586  return false;
587 }
588 
590  do {
591  if (!doc.HasMember("timesync_config") || !doc["timesync_config"].IsObject())
592  break;
593 
594  const rapidjson::Value &object = doc["timesync_config"];
595  if (!object.IsObject()) break;
596 
597  if (!object.HasMember("enable_timesync") ||
598  !object["enable_timesync"].IsBool())
599  break;
600  enable_timesync_ = object["enable_timesync"].GetBool();
601 
602  if (!object.HasMember("device_name") || !object["device_name"].IsString())
603  break;
604  std::string device_name = object["device_name"].GetString();
605  std::strncpy(timesync_config_.dev_config.name, device_name.c_str(),
607 
608  if (!object.HasMember("comm_device_type") ||
609  !object["comm_device_type"].IsInt())
610  break;
611  timesync_config_.dev_config.type = object["comm_device_type"].GetInt();
612 
614  if (!object.HasMember("baudrate_index") ||
615  !object["baudrate_index"].IsInt())
616  break;
618  object["baudrate_index"].GetInt();
619 
620  if (!object.HasMember("parity_index") || !object["parity_index"].IsInt())
621  break;
623  object["parity_index"].GetInt();
624  }
625 
626  if (enable_timesync_) {
627  printf("Enable timesync : \n");
629  printf("Uart[%s],baudrate index[%d],parity index[%d]\n",
633  }
634  } else {
635  printf("Disable timesync\n");
636  }
637  return 0;
638  } while (0);
639 
640  return -1;
641 }
642 
644 int LdsLidar::ParseConfigFile(const char *pathname) {
645  FILE *raw_file = std::fopen(pathname, "rb");
646  if (!raw_file) {
647  printf("Open json config file fail!\n");
648  return -1;
649  }
650 
651  char read_buffer[32768];
652  rapidjson::FileReadStream config_file(raw_file, read_buffer,
653  sizeof(read_buffer));
654 
656  if (!doc.ParseStream(config_file).HasParseError()) {
657  if (doc.HasMember("lidar_config") && doc["lidar_config"].IsArray()) {
658  const rapidjson::Value &array = doc["lidar_config"];
659  size_t len = array.Size();
660  for (size_t i = 0; i < len; i++) {
661  const rapidjson::Value &object = array[i];
662  if (object.IsObject()) {
663  UserRawConfig config = {0};
664  memset(&config, 0, sizeof(config));
665  if (object.HasMember("broadcast_code") &&
666  object["broadcast_code"].IsString()) {
667  std::string broadcast_code = object["broadcast_code"].GetString();
668  std::strncpy(config.broadcast_code, broadcast_code.c_str(),
669  sizeof(config.broadcast_code));
670  } else {
671  printf("User config file parse error\n");
672  continue;
673  }
674 
675  if (object.HasMember("enable_connect") &&
676  object["enable_connect"].IsBool()) {
677  config.enable_connect = object["enable_connect"].GetBool();
678  }
679  if (object.HasMember("enable_fan") && object["enable_fan"].IsBool()) {
680  config.enable_fan = object["enable_fan"].GetBool();
681  }
682  if (object.HasMember("return_mode") &&
683  object["return_mode"].IsInt()) {
684  config.return_mode = object["return_mode"].GetInt();
685  }
686  if (object.HasMember("coordinate") && object["coordinate"].IsInt()) {
687  config.coordinate = object["coordinate"].GetInt();
688  }
689  if (object.HasMember("imu_rate") && object["imu_rate"].IsInt()) {
690  config.imu_rate = object["imu_rate"].GetInt();
691  }
692  if (object.HasMember("extrinsic_parameter_source") &&
693  object["extrinsic_parameter_source"].IsInt()) {
695  object["extrinsic_parameter_source"].GetInt();
696  }
697  if (object.HasMember("enable_high_sensitivity") &&
698  object["enable_high_sensitivity"].GetBool()) {
699  config.enable_high_sensitivity =
700  object["enable_high_sensitivity"].GetBool();
701  }
702 
703  printf("broadcast code[%s] : %d %d %d %d %d %d\n",
704  config.broadcast_code, config.enable_connect,
705  config.enable_fan, config.return_mode, config.coordinate,
706  config.imu_rate, config.extrinsic_parameter_source);
707  if (config.enable_connect) {
709  if (AddRawUserConfig(config)) {
710  printf("Raw config is already exist : %s \n",
711  config.broadcast_code);
712  }
713  }
714  }
715  }
716  }
717  }
718 
719  if (ParseTimesyncConfig(doc)) {
720  printf("Parse timesync config fail\n");
721  enable_timesync_ = false;
722  }
723  } else {
724  printf("User config file parse error[%d]\n",
725  doc.ParseStream(config_file).HasParseError());
726  }
727 
728  std::fclose(raw_file);
729 
730  return 0;
731 }
732 
734  if (IsExistInRawConfig(config.broadcast_code)) {
735  return -1;
736  }
737 
738  raw_config_.push_back(config);
739  printf("Add Raw user config : %s \n", config.broadcast_code);
740 
741  return 0;
742 }
743 
744 bool LdsLidar::IsExistInRawConfig(const char *broadcast_code) {
745  if (broadcast_code == nullptr) {
746  return false;
747  }
748 
749  for (auto ite_config : raw_config_) {
750  if (strncmp(ite_config.broadcast_code, broadcast_code,
751  kBroadcastCodeSize) == 0) {
752  return true;
753  }
754  }
755 
756  return false;
757 }
758 
759 int LdsLidar::GetRawConfig(const char *broadcast_code, UserRawConfig &config) {
760  if (broadcast_code == nullptr) {
761  return -1;
762  }
763 
764  for (auto ite_config : raw_config_) {
765  if (strncmp(ite_config.broadcast_code, broadcast_code,
766  kBroadcastCodeSize) == 0) {
767  config.enable_fan = ite_config.enable_fan;
768  config.return_mode = ite_config.return_mode;
769  config.coordinate = ite_config.coordinate;
770  config.imu_rate = ite_config.imu_rate;
771  config.extrinsic_parameter_source = ite_config.extrinsic_parameter_source;
772  config.enable_high_sensitivity = ite_config.enable_high_sensitivity;
773  return 0;
774  }
775  }
776 
777  return -1;
778 }
779 
780 } // namespace livox_ros
TranslationVector trans
Definition: lds.h:166
DeviceInfo info
Definition: lds.h:185
static void SetImuRatePushFrequencyCb(livox_status status, uint8_t handle, uint8_t response, void *clent_data)
Definition: lds_lidar.cpp:405
int32_t InitTimeSync(const TimeSyncConfig &config)
Definition: timesync.cpp:52
const double PI
Definition: lds.h:74
bool enable_high_sensitivity
Definition: lds.h:155
uint32_t imu_rate
Definition: lds.h:144
const uint32_t kDeviceTypeLidarMid70
Definition: lds.h:249
uint32_t coordinate
Definition: lds.h:152
static void SetRmcSyncTimeCb(livox_status status, uint8_t handle, uint8_t response, void *client_data)
Definition: lds_lidar.cpp:529
void StartTimesync()
Definition: timesync.h:55
void ResetLdsLidar(void)
Definition: lds_lidar.cpp:60
LdsLidar * g_lds_ldiar
Definition: lds_lidar.cpp:43
UserConfig config
Definition: lds.h:190
CommDevUartConfig uart
Definition: comm_device.h:64
std::vector< UserRawConfig > raw_config_
Definition: lds_lidar.h:111
int GetRawConfig(const char *broadcast_code, UserRawConfig &config)
Definition: lds_lidar.cpp:759
union livox_ros::CommDevConfig::@0 config
ExtrinsicParameter extrinsic_parameter
Definition: lds.h:191
TimeSync * timesync_
Definition: lds_lidar.h:114
static void SetCoordinateCb(livox_status status, uint8_t handle, uint8_t response, void *clent_data)
Definition: lds_lidar.cpp:376
static void OnDeviceChange(const DeviceInfo *info, DeviceEvent type)
Definition: lds_lidar.cpp:228
static TimeSync * GetInstance()
Definition: timesync.h:47
std::mutex config_mutex_
Definition: lds_lidar.h:116
static void SetPointCloudReturnModeCb(livox_status status, uint8_t handle, uint8_t response, void *clent_data)
Definition: lds_lidar.cpp:350
uint32_t imu_rate
Definition: lds.h:153
unsigned char uint8_t
Definition: stdint.h:125
int32_t DeInitTimeSync()
Definition: timesync.cpp:81
static void OnDeviceBroadcast(const BroadcastDeviceInfo *info)
Definition: lds_lidar.cpp:172
void EnableAutoConnectMode(void)
Definition: lds_lidar.h:98
static void ControlFanCb(livox_status status, uint8_t handle, uint8_t response, void *clent_data)
Definition: lds_lidar.cpp:347
uint32_t whitelist_count_
Definition: lds_lidar.h:108
int ParseConfigFile(const char *pathname)
Definition: lds_lidar.cpp:644
uint32_t return_mode
Definition: lds.h:142
char broadcast_code_whitelist_[kMaxLidarCount][kBroadcastCodeSize]
Definition: lds_lidar.h:110
bool enable_high_sensitivity
Definition: lds.h:146
uint32_t return_mode
Definition: lds.h:151
bool IsExistInRawConfig(const char *broadcast_code)
Definition: lds_lidar.cpp:744
int AddBroadcastCodeToWhitelist(const char *broadcast_code)
Definition: lds_lidar.cpp:557
uint32_t extrinsic_parameter_source
Definition: lds.h:154
TimeSyncConfig timesync_config_
Definition: lds_lidar.h:115
virtual void PrepareExit(void)
Definition: lds_lidar.cpp:153
int AddRawUserConfig(UserRawConfig &config)
Definition: lds_lidar.cpp:733
static void StartSampleCb(livox_status status, uint8_t handle, uint8_t response, void *clent_data)
Definition: lds_lidar.cpp:501
uint32_t extrinsic_parameter_source
Definition: lds.h:145
static void ReceiveSyncTimeCallback(const char *rmc, uint32_t rmc_length, void *client_data)
Definition: lds_lidar.cpp:538
static void ResetLidar(LidarDevice *lidar, uint8_t data_src)
Definition: lds.cpp:563
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2575
unsigned int uint32_t
Definition: stdint.h:127
static void OnLidarDataCb(uint8_t handle, LivoxEthPacket *data, uint32_t data_num, void *client_data)
Definition: lds_lidar.cpp:158
static void GetLidarExtrinsicParameterCb(livox_status status, uint8_t handle, LidarGetExtrinsicParameterResponse *response, void *clent_data)
Definition: lds_lidar.cpp:431
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:3029
volatile uint32_t set_bits
Definition: lds.h:156
int InitLdsLidar(std::vector< std::string > &broadcast_code_strs, const char *user_config_path)
Definition: lds_lidar.cpp:62
bool IsBroadcastCodeExistInWhitelist(const char *broadcast_code)
Definition: lds_lidar.cpp:574
RotationMatrix rotation
Definition: lds.h:167
CommDevConfig dev_config
Definition: timesync.h:41
char name[kDevNameLengthMax]
Definition: comm_device.h:62
void StorageRawPacket(uint8_t handle, LivoxEthPacket *eth_packet)
Definition: lds.cpp:637
volatile bool is_initialized_
Definition: lds_lidar.h:109
static void SetHighSensitivityCb(livox_status status, uint8_t handle, DeviceParameterResponse *response, void *clent_data)
Definition: lds_lidar.cpp:471
uint32_t coordinate
Definition: lds.h:143
static void DeviceInformationCb(livox_status status, uint8_t handle, DeviceInformationResponse *ack, void *clent_data)
Definition: lds_lidar.cpp:310
int DeInitLdsLidar(void)
Definition: lds_lidar.cpp:137
int32_t SetReceiveSyncTimeCb(FnReceiveSyncTimeCb cb, void *data)
Definition: timesync.h:60
void DisableAutoConnectMode(void)
Definition: lds_lidar.h:99
void EulerAnglesToRotationMatrix(EulerAngle euler, RotationMatrix matrix)
Definition: lds.cpp:150
bool IsAutoConnectMode(void)
Definition: lds_lidar.h:100
int ParseTimesyncConfig(rapidjson::Document &doc)
Definition: lds_lidar.cpp:589
void ResetLds(uint8_t data_src)
Definition: lds.cpp:581
char broadcast_code[16]
Definition: lds.h:139
LidarDevice lidars_[kMaxSourceLidar]
Definition: lds.h:446
static void StopSampleCb(livox_status status, uint8_t handle, uint8_t response, void *clent_data)
Definition: lds_lidar.cpp:526
volatile LidarConnectState connect_state
Definition: lds.h:184
static void LidarErrorStatusCb(livox_status status, uint8_t handle, ErrorMessage *message)
Definition: lds_lidar.cpp:324
uint8_t handle
Definition: lds.h:173


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46