62 #if defined(__cplusplus) && __cplusplus >= 201103L
63 #ifndef _GXX_NULLPTR_T
64 #define _GXX_NULLPTR_T
65 typedef decltype(
nullptr) nullptr_t;
72 fprintf(stderr,
"*WARN* YOU ARE USING DEPRECATED API: %s, PLEASE MOVE TO %s\n", fn, replacement);
91 static inline float getAngle(
const sl_lidar_response_measurement_node_t& node)
96 static inline void setAngle(sl_lidar_response_measurement_node_t& node,
float v)
112 static inline sl_u16
getDistanceQ2(
const sl_lidar_response_measurement_node_t& node)
114 return node.distance_q2;
122 template <
class TNode>
128 template <
class TNode >
131 float inc_origin_angle = 360.f / count;
135 for (i = 0; i < count; i++) {
142 float expect_angle =
getAngle(nodebuffer[i + 1]) - inc_origin_angle;
143 if (expect_angle < 0.0
f) expect_angle = 0.0f;
144 setAngle(nodebuffer[i], expect_angle);
154 for (i = count - 1; i < count; i--) {
160 while (i != (count - 1)) {
162 float expect_angle =
getAngle(nodebuffer[i - 1]) + inc_origin_angle;
163 if (expect_angle > 360.0
f) expect_angle -= 360.0f;
164 setAngle(nodebuffer[i], expect_angle);
171 float frontAngle =
getAngle(nodebuffer[0]);
172 for (i = 1; i < count; i++) {
174 float expect_angle = frontAngle + i * inc_origin_angle;
175 if (expect_angle > 360.0
f) expect_angle -= 360.0f;
176 setAngle(nodebuffer[i], expect_angle);
181 std::sort(nodebuffer, nodebuffer + count, &angleLessThan<TNode>);
218 size_t copiedCount = 0;
277 auto operationalBuf = &
_scanbuffer[operationBufID];
280 if (operationalBuf->size()) {
290 assert(operationalBuf->size() == 0);
296 if (operationalBuf->size() == 0) {
304 operationalBuf->at(operationalBuf->size() - 1) = *hqNode;
307 operationalBuf->push_back(*hqNode);
324 if (out_timestamp_uS) {
346 return newOperationalID;
408 _dataunpacker.reset(internal::LIDARSampleDataUnpacker::CreateInstance(*
this));
449 if (fetchAliasName) {
450 std::vector<_u8> replyData;
452 if (
IS_OK(ans) && replyData.size()) {
453 out_description.resize(replyData.size() + 1);
454 memcpy(&out_description[0], &replyData[0], replyData.size());
455 out_description[replyData.size()] =
'\0';
456 if (out_description !=
"") {
525 bool confProtocolSupported =
false;
529 if (confProtocolSupported) {
533 if (!ans)
return ans;
535 for (sl_u16 i = 0; i < modeCount; i++) {
537 memset(&scanModeInfoTmp, 0,
sizeof(scanModeInfoTmp));
538 scanModeInfoTmp.
id = i;
540 if (!ans)
return ans;
542 if (!ans)
return ans;
544 if (!ans)
return ans;
546 if (!ans)
return ans;
547 outModes.push_back(scanModeInfoTmp);
562 std::vector<sl_u8> answer;
563 bool lidarSupportConfigCmds =
false;
565 if (!ans)
return ans;
567 if (lidarSupportConfigCmds) {
569 if (!ans)
return ans;
570 if (answer.size() <
sizeof(sl_u16)) {
573 const sl_u16 *p_answer =
reinterpret_cast<const sl_u16*
>(&answer[0]);
592 bool ifSupportLidarConf =
false;
598 if (!outUsedScanMode) outUsedScanMode = &localMode;
602 if (!ans)
return ans;
606 if (!ans)
return ans;
625 if (ifSupportLidarConf) {
629 if (!ans)
return ans;
631 if (!ans)
return ans;
633 if (!ans)
return ans;
635 if (!ans)
return ans;
644 outUsedScanMode.
us_per_sample = sampleRateTmp.std_sample_duration_us;
647 strcpy(outUsedScanMode.
scan_mode,
"Standard");
671 bool ifSupportLidarConf;
677 if (!ans)
return ans;
694 if (!outUsedScanMode) outUsedScanMode = &localMode;
696 bool ifSupportLidarConf =
false;
702 outUsedScanMode->id = scanMode;
703 if (ifSupportLidarConf) {
707 ans =
getMaxDistance(outUsedScanMode->max_distance, outUsedScanMode->id);
713 ans =
getScanModeName(outUsedScanMode->scan_mode,
sizeof(outUsedScanMode->scan_mode), outUsedScanMode->id);
723 outUsedScanMode->us_per_sample = sampleRateTmp.express_sample_duration_us;
724 outUsedScanMode->max_distance = 16;
726 strcpy(outUsedScanMode->scan_mode,
"Express");
745 sl_lidar_payload_express_scan_t scanReq;
746 memset(&scanReq, 0,
sizeof(scanReq));
748 if (!ifSupportLidarConf) {
750 scanReq.working_mode = sl_u8(scanMode);
753 scanReq.working_mode = sl_u8(scanMode);
755 scanReq.working_flags = options;
793 count = std::min<size_t>(count, availBuffer->size());
795 std::copy(availBuffer->begin(), availBuffer->begin() + count, nodebuffer);
825 #ifdef _CPU_ENDIAN_BIG
826 info.firmware_version =
le16_to_cpu(info.firmware_version);
843 sl_lidar_response_device_info_t devInfo;
845 if (!ans)
return ans;
846 sl_u8 majorId = devInfo.model >> 4;
854 sl_lidar_payload_acc_board_flag_t
flag;
859 if (!ans)
return ans;
866 const sl_lidar_response_acc_board_flag_t* acc_board_flag
867 =
reinterpret_cast<const sl_lidar_response_acc_board_flag_t*
>(ans_frame->getDataBuf());
883 frequency = 1000000.0f / (count * sample_duration);
903 std::vector<sl_u8> reserve(2);
905 std::vector<sl_u8> answer;
907 size_t len = answer.size();
909 memcpy(&conf, &answer[0], len);
930 #ifdef _CPU_ENDIAN_BIG
931 health.error_code =
le16_to_cpu(health.error_code);
945 std::vector<_u8> answer(6, 0);
951 size_t len = answer.size();
953 memcpy(macAddrArray, &answer[0], len);
959 return ascendScanData_<sl_lidar_response_measurement_node_hq_t>(nodebuffer, count);
977 sl_lidar_response_desired_rot_speed_t desired_speed;
981 speed = desired_speed.pwm_ref;
983 speed = desired_speed.rpm;
996 serialChanel->
setDTR(
true);
998 serialChanel->
setDTR(
false);
1003 sl_lidar_payload_motor_pwm_t motor_pwm;
1004 motor_pwm.pwm_value = speed;
1008 if (!ans)
return ans;
1012 sl_lidar_payload_motor_pwm_t motor_rpm;
1013 motor_rpm.pwm_value = speed;
1016 if (!ans)
return ans;
1031 std::vector<sl_u8> answer;
1034 if (!ans)
return ans;
1036 const sl_u16 *min_answer =
reinterpret_cast<const sl_u16*
>(&answer[0]);
1041 if (!ans)
return ans;
1043 const sl_u16 *max_answer =
reinterpret_cast<const sl_u16*
>(&answer[0]);
1046 sl_lidar_response_desired_rot_speed_t desired_speed;
1048 if (!ans)
return ans;
1081 if (!cachedChannel->
open()) {
1088 cachedChannel->
flush();
1099 sl_u8 magicByteSeq[16];
1103 sl_u64 startTS =
getms();
1105 while (
getms() - startTS < 1500)
1107 if (cachedChannel->
write(magicByteSeq,
sizeof(magicByteSeq)) < 0)
1113 size_t dataCountGot;
1114 if (cachedChannel->
waitForData(1, 1, &dataCountGot)) {
1125 _u32 bpsDetected = 0;
1126 size_t dataCountGot;
1127 if (cachedChannel->
waitForData(4, 500, &dataCountGot)) {
1129 cachedChannel->
read(&bpsDetected, 4);
1130 if (baudRateDetected) *baudRateDetected = bpsDetected;
1133 cachedChannel->
close();
1135 ans =
_transeiver->openChannelAndBind(cachedChannel);
1140 sl_lidar_payload_new_bps_confirmation_t confirmation;
1141 confirmation.flag = 0x5F5F;
1142 confirmation.required_bps = requiredBaudRate;
1143 confirmation.param = 0;
1166 std::vector<sl_u8> answer;
1171 const sl_lidar_response_desired_rot_speed_t *p_answer =
reinterpret_cast<const sl_lidar_response_desired_rot_speed_t*
>(&answer[0]);
1172 motorSpeed = *p_answer;
1193 outSupport = (devinfo.firmware_version >= ((0x1 << 8) | 24));
1202 std::vector<_u8> answer;
1207 if (answer.size() <
sizeof(
_u16)) {
1210 const _u16* p_answer =
reinterpret_cast<const _u16*
>(&answer[0]);
1211 modeCount = *p_answer;
1217 if (type < 0x00010000 || type >0x0001FFFF)
1221 std::vector<sl_u8> requestPkt;
1222 requestPkt.resize(
sizeof(sl_lidar_payload_set_scan_conf_t) + payloadSize);
1223 if (!
payload) payloadSize = 0;
1224 sl_lidar_payload_set_scan_conf_t* query =
reinterpret_cast<sl_lidar_payload_set_scan_conf_t*
>(&requestPkt[0]);
1229 memcpy(&query[1],
payload, payloadSize);
1248 if (ans_frame->getPayloadSize() == 4) {
1250 return *(
const u_result*)(ans_frame->getDataBuf());
1263 std::vector<_u8> requestPkt;
1265 if (!
payload) payloadSize = 0;
1272 memcpy(&query[1],
payload, payloadSize);
1290 if (replied->type !=
type) {
1296 if (payLoadLen < 0) {
1300 outputBuf.resize(payLoadLen);
1302 memcpy(&outputBuf[0], replied->payload, payLoadLen);
1310 std::vector<_u8> answer;
1316 if (answer.size() <
sizeof(
_u32))
1321 sampleDurationRes = (float)(*
result / 256.0);
1330 std::vector<_u8> answer;
1336 if (answer.size() <
sizeof(
_u32))
1341 maxDistance = (float)(*
result >> 8);
1349 std::vector<_u8> answer;
1355 if (answer.size() <
sizeof(
_u8))
1359 const _u8*
result =
reinterpret_cast<const _u8*
>(&answer[0]);
1368 std::vector<_u8> answer;
1374 size_t len = std::min<size_t>(answer.size(), stringSize);
1377 memcpy(modeName, &answer[0], len);
1384 _u8 majorModelID = (modelID >> 4);
1396 _u8 majorModelID = (modelID >> 4);
1419 char stringBuffer[100];
1423 switch (majorType) {
1425 sprintf(stringBuffer,
"A%dM%d", (modelID >> 4), (modelID & 0xF));
1451 sprintf(stringBuffer,
"unknown(%x)", modelID);
1454 return std::string(stringBuffer);
1480 switch (majorType) {
1518 _u8 majorModelID = (devInfo.model >> 4);
1519 switch (majorModelID)
1524 return (devInfo.hardware_version >= 6) ? 256000 : 115200;
1531 if (devInfo.model == (0x82))
return 460800;
1559 static const _u32 LEGACY_SAMPLE_DURATION = 476;
1565 rateInfo.express_sample_duration_us = LEGACY_SAMPLE_DURATION;
1566 rateInfo.std_sample_duration_us = LEGACY_SAMPLE_DURATION;
1573 if (devinfo.firmware_version < ((0x1 << 8) | 17)) {
1589 memcpy(&rateInfo, ans_frame->getDataBuf(),
sizeof(rateInfo));
1591 #ifdef _CPU_ENDIAN_BIG
1592 rateInfo.express_sample_duration_us =
le16_to_cpu(rateInfo.express_sample_duration_us);
1593 rateInfo.std_sample_duration_us =
le16_to_cpu(rateInfo.std_sample_duration_us);
1659 if (
_dataunpacker->onSampleData(message->cmd, message->getDataBuf(), message->getPayloadSize()))