-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathsl_lidar_driver.cpp
More file actions
1987 lines (1674 loc) · 79.2 KB
/
sl_lidar_driver.cpp
File metadata and controls
1987 lines (1674 loc) · 79.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Slamtec LIDAR SDK
*
* Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd.
* http://www.slamtec.com
*
*/
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "sdkcommon.h"
#include "hal/abs_rxtx.h"
#include "hal/thread.h"
#include "hal/types.h"
#include "hal/assert.h"
#include "hal/locker.h"
#include "hal/socket.h"
#include "hal/event.h"
#include "sl_lidar_driver.h"
#include "sl_crc.h"
#include <algorithm>
#ifdef _WIN32
#define NOMINMAX
#undef min
#undef max
#endif
#if defined(__cplusplus) && __cplusplus >= 201103L
#ifndef _GXX_NULLPTR_T
#define _GXX_NULLPTR_T
typedef decltype(nullptr) nullptr_t;
#endif
#endif /* C++11. */
namespace sl {
static void printDeprecationWarn(const char* fn, const char* replacement)
{
fprintf(stderr, "*WARN* YOU ARE USING DEPRECATED API: %s, PLEASE MOVE TO %s\n", fn, replacement);
}
static void convert(const sl_lidar_response_measurement_node_t& from, sl_lidar_response_measurement_node_hq_t& to)
{
to.angle_z_q14 = (((from.angle_q6_checkbit) >> SL_LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) << 8) / 90; //transfer to q14 Z-angle
to.dist_mm_q2 = from.distance_q2;
to.flag = (from.sync_quality & SL_LIDAR_RESP_MEASUREMENT_SYNCBIT); // trasfer syncbit to HQ flag field
to.quality = (from.sync_quality >> SL_LIDAR_RESP_MEASUREMENT_QUALITY_SHIFT) << SL_LIDAR_RESP_MEASUREMENT_QUALITY_SHIFT; //remove the last two bits and then make quality from 0-63 to 0-255
}
static void convert(const sl_lidar_response_measurement_node_hq_t& from, sl_lidar_response_measurement_node_t& to)
{
to.sync_quality = (from.flag & SL_LIDAR_RESP_MEASUREMENT_SYNCBIT) | ((from.quality >> SL_LIDAR_RESP_MEASUREMENT_QUALITY_SHIFT) << SL_LIDAR_RESP_MEASUREMENT_QUALITY_SHIFT);
to.angle_q6_checkbit = 1 | (((from.angle_z_q14 * 90) >> 8) << SL_LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT);
to.distance_q2 = from.dist_mm_q2 > sl_u16(-1) ? sl_u16(0) : sl_u16(from.dist_mm_q2);
}
static sl_u32 _varbitscale_decode(sl_u32 scaled, sl_u32 & scaleLevel)
{
static const sl_u32 VBS_SCALED_BASE[] = {
SL_LIDAR_VARBITSCALE_X16_DEST_VAL,
SL_LIDAR_VARBITSCALE_X8_DEST_VAL,
SL_LIDAR_VARBITSCALE_X4_DEST_VAL,
SL_LIDAR_VARBITSCALE_X2_DEST_VAL,
0,
};
static const sl_u32 VBS_SCALED_LVL[] = {
4,
3,
2,
1,
0,
};
static const sl_u32 VBS_TARGET_BASE[] = {
(0x1 << SL_LIDAR_VARBITSCALE_X16_SRC_BIT),
(0x1 << SL_LIDAR_VARBITSCALE_X8_SRC_BIT),
(0x1 << SL_LIDAR_VARBITSCALE_X4_SRC_BIT),
(0x1 << SL_LIDAR_VARBITSCALE_X2_SRC_BIT),
0,
};
for (size_t i = 0; i < _countof(VBS_SCALED_BASE); ++i) {
int remain = ((int)scaled - (int)VBS_SCALED_BASE[i]);
if (remain >= 0) {
scaleLevel = VBS_SCALED_LVL[i];
return VBS_TARGET_BASE[i] + (remain << scaleLevel);
}
}
return 0;
}
static inline float getAngle(const sl_lidar_response_measurement_node_t& node)
{
return (node.angle_q6_checkbit >> SL_LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) / 64.f;
}
static inline void setAngle(sl_lidar_response_measurement_node_t& node, float v)
{
sl_u16 checkbit = node.angle_q6_checkbit & SL_LIDAR_RESP_MEASUREMENT_CHECKBIT;
node.angle_q6_checkbit = (((sl_u16)(v * 64.0f)) << SL_LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) | checkbit;
}
static inline float getAngle(const sl_lidar_response_measurement_node_hq_t& node)
{
return node.angle_z_q14 * 90.f / 16384.f;
}
static inline void setAngle(sl_lidar_response_measurement_node_hq_t& node, float v)
{
node.angle_z_q14 = sl_u32(v * 16384.f / 90.f);
}
static inline sl_u16 getDistanceQ2(const sl_lidar_response_measurement_node_t& node)
{
return node.distance_q2;
}
static inline sl_u32 getDistanceQ2(const sl_lidar_response_measurement_node_hq_t& node)
{
return node.dist_mm_q2;
}
template <class TNode>
static bool angleLessThan(const TNode& a, const TNode& b)
{
return getAngle(a) < getAngle(b);
}
template < class TNode >
static sl_result ascendScanData_(TNode * nodebuffer, size_t count)
{
float inc_origin_angle = 360.f / count;
size_t i = 0;
//Tune head
for (i = 0; i < count; i++) {
if (getDistanceQ2(nodebuffer[i]) == 0) {
continue;
}
else {
while (i != 0) {
i--;
float expect_angle = getAngle(nodebuffer[i + 1]) - inc_origin_angle;
if (expect_angle < 0.0f) expect_angle = 0.0f;
setAngle(nodebuffer[i], expect_angle);
}
break;
}
}
// all the data is invalid
if (i == count) return SL_RESULT_OPERATION_FAIL;
//Tune tail
for (i = count - 1; i >= 0; i--) {
if (getDistanceQ2(nodebuffer[i]) == 0) {
continue;
}
else {
while (i != (count - 1)) {
i++;
float expect_angle = getAngle(nodebuffer[i - 1]) + inc_origin_angle;
if (expect_angle > 360.0f) expect_angle -= 360.0f;
setAngle(nodebuffer[i], expect_angle);
}
break;
}
}
//Fill invalid angle in the scan
float frontAngle = getAngle(nodebuffer[0]);
for (i = 1; i < count; i++) {
if (getDistanceQ2(nodebuffer[i]) == 0) {
float expect_angle = frontAngle + i * inc_origin_angle;
if (expect_angle > 360.0f) expect_angle -= 360.0f;
setAngle(nodebuffer[i], expect_angle);
}
}
// Reorder the scan according to the angle value
std::sort(nodebuffer, nodebuffer + count, &angleLessThan<TNode>);
return SL_RESULT_OK;
}
class SlamtecLidarDriver :public ILidarDriver
{
public:
enum {
LEGACY_SAMPLE_DURATION = 476,
};
enum {
NORMAL_CAPSULE = 0,
DENSE_CAPSULE = 1,
};
enum {
A2A3_LIDAR_MINUM_MAJOR_ID = 2,
TOF_LIDAR_MINUM_MAJOR_ID = 6,
};
public:
SlamtecLidarDriver()
: _channel(NULL)
, _isConnected(false)
, _isScanning(false)
, _isSupportingMotorCtrl(MotorCtrlSupportNone)
, _cached_sampleduration_std(LEGACY_SAMPLE_DURATION)
, _cached_sampleduration_express(LEGACY_SAMPLE_DURATION)
, _cached_scan_node_hq_count(0)
, _cached_scan_node_hq_count_for_interval_retrieve(0)
{}
sl_result connect(IChannel* channel)
{
Result<nullptr_t> ans = SL_RESULT_OK;
if (!channel) return SL_RESULT_OPERATION_FAIL;
if (isConnected()) return SL_RESULT_ALREADY_DONE;
_channel = channel;
{
rp::hal::AutoLocker l(_lock);
ans = _channel->open();
if (!ans)
return SL_RESULT_OPERATION_FAIL;
_channel->flush();
}
_isConnected = true;
ans =checkMotorCtrlSupport(_isSupportingMotorCtrl,500);
return SL_RESULT_OK;
}
void disconnect()
{
if (_isConnected)
_channel->close();
}
bool isConnected()
{
return _isConnected;
}
sl_result reset(sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
{
rp::hal::AutoLocker l(_lock);
ans = _sendCommand(SL_LIDAR_CMD_RESET);
if (!ans) {
return ans;
}
}
return SL_RESULT_OK;
}
sl_result getAllSupportedScanModes(std::vector<LidarScanMode>& outModes, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
bool confProtocolSupported = false;
ans = checkSupportConfigCommands(confProtocolSupported);
if (!ans) return SL_RESULT_INVALID_DATA;
if (confProtocolSupported) {
// 1. get scan mode count
sl_u16 modeCount;
ans = getScanModeCount(modeCount);
if (!ans) return ans;
// 2. for loop to get all fields of each scan mode
for (sl_u16 i = 0; i < modeCount; i++) {
LidarScanMode scanModeInfoTmp;
memset(&scanModeInfoTmp, 0, sizeof(scanModeInfoTmp));
scanModeInfoTmp.id = i;
ans = getLidarSampleDuration(scanModeInfoTmp.us_per_sample, i);
if (!ans) return ans;
ans = getMaxDistance(scanModeInfoTmp.max_distance, i);
if (!ans) return ans;
ans = getScanModeAnsType(scanModeInfoTmp.ans_type, i);
if (!ans) return ans;
ans = getScanModeName(scanModeInfoTmp.scan_mode, i);
if (!ans) return ans;
outModes.push_back(scanModeInfoTmp);
}
return ans;
}
return ans;
}
sl_result getTypicalScanMode(sl_u16& outMode, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> answer;
bool lidarSupportConfigCmds = false;
ans = checkSupportConfigCommands(lidarSupportConfigCmds);
if (!ans) return ans;
if (lidarSupportConfigCmds) {
ans = getLidarConf(SL_LIDAR_CONF_SCAN_MODE_TYPICAL, answer, std::vector<sl_u8>(), timeoutInMs);
if (!ans) return ans;
if (answer.size() < sizeof(sl_u16)) {
return SL_RESULT_INVALID_DATA;
}
const sl_u16 *p_answer = reinterpret_cast<const sl_u16*>(&answer[0]);
outMode = *p_answer;
return ans;
}
//old version of triangle lidar
else {
outMode = SL_LIDAR_CONF_SCAN_COMMAND_EXPRESS;
return ans;
}
return ans;
}
sl_result startScan(bool force, bool useTypicalScan, sl_u32 options = 0, LidarScanMode* outUsedScanMode = nullptr)
{
Result<nullptr_t> ans = SL_RESULT_OK;
bool ifSupportLidarConf = false;
startMotor();
ans = checkSupportConfigCommands(ifSupportLidarConf);
if (!ans) return ans;
if (useTypicalScan){
sl_u16 typicalMode;
ans = getTypicalScanMode(typicalMode);
if (!ans) return ans;
//call startScanExpress to do the job
return startScanExpress(false, typicalMode, 0, outUsedScanMode);
}
// 'useTypicalScan' is false, just use normal scan mode
if (ifSupportLidarConf) {
if (outUsedScanMode) {
outUsedScanMode->id = SL_LIDAR_CONF_SCAN_COMMAND_STD;
ans = getLidarSampleDuration(outUsedScanMode->us_per_sample, outUsedScanMode->id);
if (!ans) return ans;
ans = getMaxDistance(outUsedScanMode->max_distance, outUsedScanMode->id);
if (!ans) return ans;
ans = getScanModeAnsType(outUsedScanMode->ans_type, outUsedScanMode->id);
if (!ans) return ans;
ans = getScanModeName(outUsedScanMode->scan_mode, outUsedScanMode->id);
if (!ans) return ans;
}
}
return startScanNormal(force);
}
sl_result startScanNormal(bool force, sl_u32 timeout = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
if (!isConnected()) return SL_RESULT_OPERATION_FAIL;
if (_isScanning) return SL_RESULT_ALREADY_DONE;
stop(); //force the previous operation to stop
setMotorSpeed();
{
rp::hal::AutoLocker l(_lock);
ans = _sendCommand(force ? SL_LIDAR_CMD_FORCE_SCAN : SL_LIDAR_CMD_SCAN);
if (!ans) return ans;
// waiting for confirmation
sl_lidar_ans_header_t response_header;
ans = _waitResponseHeader(&response_header, timeout);
if (!ans) return ans;
// verify whether we got a correct header
if (response_header.type != SL_LIDAR_ANS_TYPE_MEASUREMENT) {
return SL_RESULT_INVALID_DATA;
}
sl_u32 header_size = (response_header.size_q30_subtype & SL_LIDAR_ANS_HEADER_SIZE_MASK);
if (header_size < sizeof(sl_lidar_response_measurement_node_t)) {
return SL_RESULT_INVALID_DATA;
}
_isScanning = true;
_cachethread = CLASS_THREAD(SlamtecLidarDriver, _cacheScanData);
if (_cachethread.getHandle() == 0) {
return SL_RESULT_OPERATION_FAIL;
}
}
return SL_RESULT_OK;
}
sl_result startScanExpress(bool force, sl_u16 scanMode, sl_u32 options = 0, LidarScanMode* outUsedScanMode = nullptr, sl_u32 timeout = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
if (!isConnected()) return SL_RESULT_OPERATION_FAIL;
if (_isScanning) return SL_RESULT_ALREADY_DONE;
stop(); //force the previous operation to stop
bool ifSupportLidarConf = false;
ans = checkSupportConfigCommands(ifSupportLidarConf);
if (!ans) return SL_RESULT_INVALID_DATA;
if (outUsedScanMode) {
outUsedScanMode->id = scanMode;
if (ifSupportLidarConf) {
ans = getLidarSampleDuration(outUsedScanMode->us_per_sample, outUsedScanMode->id);
if (!ans) return SL_RESULT_INVALID_DATA;
ans = getMaxDistance(outUsedScanMode->max_distance, outUsedScanMode->id);
if (!ans) return SL_RESULT_INVALID_DATA;
ans = getScanModeAnsType(outUsedScanMode->ans_type, outUsedScanMode->id);
if (!ans) return SL_RESULT_INVALID_DATA;
ans = getScanModeName(outUsedScanMode->scan_mode, outUsedScanMode->id);
if (!ans) return SL_RESULT_INVALID_DATA;
}
}
//get scan answer type to specify how to wait data
sl_u8 scanAnsType;
if (ifSupportLidarConf) {
getScanModeAnsType(scanAnsType, scanMode);
}
else {
scanAnsType = SL_LIDAR_ANS_TYPE_MEASUREMENT_CAPSULED;
}
if (!ifSupportLidarConf || scanAnsType == SL_LIDAR_ANS_TYPE_MEASUREMENT) {
if (scanMode == SL_LIDAR_CONF_SCAN_COMMAND_STD) {
return startScan(force, false, 0, outUsedScanMode);
}
}
{
rp::hal::AutoLocker l(_lock);
startMotor();
sl_lidar_payload_express_scan_t scanReq;
memset(&scanReq, 0, sizeof(scanReq));
if (!ifSupportLidarConf){
if (scanMode != SL_LIDAR_CONF_SCAN_COMMAND_STD && scanMode != SL_LIDAR_CONF_SCAN_COMMAND_EXPRESS)
scanReq.working_mode = sl_u8(scanMode);
}else
scanReq.working_mode = sl_u8(scanMode);
scanReq.working_flags = options;
delay(5);
ans = _sendCommand(SL_LIDAR_CMD_EXPRESS_SCAN, &scanReq, sizeof(scanReq));
if (!ans) {
ans = _sendCommand(SL_LIDAR_CMD_EXPRESS_SCAN, &scanReq, sizeof(scanReq));
if (!ans)
return SL_RESULT_INVALID_DATA;
}
// waiting for confirmation
sl_lidar_ans_header_t response_header;
ans = _waitResponseHeader(&response_header, timeout);
if (!ans) return ans;
// verify whether we got a correct header
if (response_header.type != scanAnsType) {
return SL_RESULT_INVALID_DATA;
}
sl_u32 header_size = (response_header.size_q30_subtype & SL_LIDAR_ANS_HEADER_SIZE_MASK);
if (scanAnsType == SL_LIDAR_ANS_TYPE_MEASUREMENT_CAPSULED) {
if (header_size < sizeof(sl_lidar_response_capsule_measurement_nodes_t)) {
return SL_RESULT_INVALID_DATA;
}
_cached_capsule_flag = NORMAL_CAPSULE;
_isScanning = true;
_cachethread = CLASS_THREAD(SlamtecLidarDriver, _cacheCapsuledScanData);
}
else if (scanAnsType == SL_LIDAR_ANS_TYPE_MEASUREMENT_DENSE_CAPSULED) {
if (header_size < sizeof(sl_lidar_response_capsule_measurement_nodes_t)) {
return SL_RESULT_INVALID_DATA;
}
_cached_capsule_flag = DENSE_CAPSULE;
_isScanning = true;
_cachethread = CLASS_THREAD(SlamtecLidarDriver, _cacheCapsuledScanData);
}
else if (scanAnsType == SL_LIDAR_ANS_TYPE_MEASUREMENT_HQ) {
if (header_size < sizeof(sl_lidar_response_hq_capsule_measurement_nodes_t)) {
return SL_RESULT_INVALID_DATA;
}
_isScanning = true;
_cachethread = CLASS_THREAD(SlamtecLidarDriver, _cacheHqScanData);
}
else {
if (header_size < sizeof(sl_lidar_response_ultra_capsule_measurement_nodes_t)) {
return SL_RESULT_INVALID_DATA;
}
_isScanning = true;
_cachethread = CLASS_THREAD(SlamtecLidarDriver, _cacheUltraCapsuledScanData);
}
if (_cachethread.getHandle() == 0) {
return SL_RESULT_OPERATION_FAIL;
}
}
return SL_RESULT_OK;
}
sl_result stop(sl_u32 timeout = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
_disableDataGrabbing();
{
rp::hal::AutoLocker l(_lock);
ans = _sendCommand(SL_LIDAR_CMD_STOP);
if (!ans) return ans;
}
delay(100);
if(_isSupportingMotorCtrl == MotorCtrlSupportPwm)
setMotorSpeed(0);
return SL_RESULT_OK;
}
sl_result grabScanDataHq(sl_lidar_response_measurement_node_hq_t* nodebuffer, size_t& count, sl_u32 timeout = DEFAULT_TIMEOUT)
{
switch (_dataEvt.wait(timeout))
{
case (unsigned long int)rp::hal::Event::EVENT_TIMEOUT:
count = 0;
return SL_RESULT_OPERATION_TIMEOUT;
case (unsigned long int)rp::hal::Event::EVENT_OK:
{
if (_cached_scan_node_hq_count == 0) return SL_RESULT_OPERATION_TIMEOUT; //consider as timeout
rp::hal::AutoLocker l(_lock);
size_t size_to_copy = std::min(count, _cached_scan_node_hq_count);
memcpy(nodebuffer, _cached_scan_node_hq_buf, size_to_copy * sizeof(sl_lidar_response_measurement_node_hq_t));
count = size_to_copy;
_cached_scan_node_hq_count = 0;
}
return SL_RESULT_OK;
default:
count = 0;
return SL_RESULT_OPERATION_FAIL;
}
}
sl_result getDeviceInfo(sl_lidar_response_device_info_t& info, sl_u32 timeout = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
_disableDataGrabbing();
delay(20);
{
rp::hal::AutoLocker l(_lock);
ans = _sendCommand(SL_LIDAR_CMD_GET_DEVICE_INFO);
if (!ans) return ans;
return _waitResponse(info, SL_LIDAR_ANS_TYPE_DEVINFO);
}
}
sl_result checkMotorCtrlSupport(MotorCtrlSupport & support, sl_u32 timeout = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
support = MotorCtrlSupportNone;
_disableDataGrabbing();
{
sl_lidar_response_device_info_t devInfo;
ans = getDeviceInfo(devInfo, 500);
if (!ans) return ans;
sl_u8 majorId = devInfo.model >> 4;
if (majorId >= TOF_LIDAR_MINUM_MAJOR_ID) {
support = MotorCtrlSupportRpm;
return ans;
}
else if(majorId >= A2A3_LIDAR_MINUM_MAJOR_ID){
rp::hal::AutoLocker l(_lock);
sl_lidar_payload_acc_board_flag_t flag;
flag.reserved = 0;
ans = _sendCommand(SL_LIDAR_CMD_GET_ACC_BOARD_FLAG, &flag, sizeof(flag));
if (!ans) return ans;
sl_lidar_response_acc_board_flag_t acc_board_flag;
ans = _waitResponse(acc_board_flag, SL_LIDAR_ANS_TYPE_ACC_BOARD_FLAG);
if (acc_board_flag.support_flag & SL_LIDAR_RESP_ACC_BOARD_FLAG_MOTOR_CTRL_SUPPORT_MASK) {
support = MotorCtrlSupportPwm;
}
return ans;
}
}
return SL_RESULT_OK;
}
sl_result getFrequency(const LidarScanMode& scanMode, const sl_lidar_response_measurement_node_hq_t* nodes, size_t count, float& frequency)
{
float sample_duration = scanMode.us_per_sample;
frequency = 1000000.0f / (count * sample_duration);
return SL_RESULT_OK;
}
sl_result setLidarIpConf(const sl_lidar_ip_conf_t& conf, sl_u32 timeout)
{
sl_result ans = setLidarConf(SL_LIDAR_CONF_LIDAR_STATIC_IP_ADDR, &conf, sizeof(sl_lidar_ip_conf_t), timeout);
return ans;
}
sl_result getHealth(sl_lidar_response_device_health_t& health, sl_u32 timeout = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
if (!isConnected())
return SL_RESULT_OPERATION_FAIL;
_disableDataGrabbing();
{
rp::hal::AutoLocker l(_lock);
ans = _sendCommand(SL_LIDAR_CMD_GET_DEVICE_HEALTH);
if (!ans) return ans;
delay(50);
ans = _waitResponse(health, SL_LIDAR_ANS_TYPE_DEVHEALTH);
}
return ans;
}
sl_result getDeviceMacAddr(sl_u8* macAddrArray, sl_u32 timeoutInMs)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_LIDAR_MAC_ADDR, answer, std::vector<sl_u8>(), timeoutInMs);
if (!ans) return ans;
int len = answer.size();
if (0 == len) return SL_RESULT_INVALID_DATA;
memcpy(macAddrArray, &answer[0], len);
return ans;
}
sl_result ascendScanData(sl_lidar_response_measurement_node_hq_t * nodebuffer, size_t count)
{
return ascendScanData_<sl_lidar_response_measurement_node_hq_t>(nodebuffer, count);
}
sl_result getScanDataWithIntervalHq(sl_lidar_response_measurement_node_hq_t * nodebuffer, size_t & count)
{
size_t size_to_copy = 0;
{
rp::hal::AutoLocker l(_lock);
if (_cached_scan_node_hq_count_for_interval_retrieve == 0) {
return SL_RESULT_OPERATION_TIMEOUT;
}
//copy all the nodes(_cached_scan_node_count_for_interval_retrieve nodes) in _cached_scan_node_buf_for_interval_retrieve
size_to_copy = _cached_scan_node_hq_count_for_interval_retrieve;
memcpy(nodebuffer, _cached_scan_node_hq_buf_for_interval_retrieve, size_to_copy * sizeof(sl_lidar_response_measurement_node_hq_t));
_cached_scan_node_hq_count_for_interval_retrieve = 0;
}
count = size_to_copy;
return SL_RESULT_OK;
}
sl_result setMotorSpeed(sl_u16 speed = DEFAULT_MOTOR_SPEED)
{
Result<nullptr_t> ans = SL_RESULT_OK;
if(speed == DEFAULT_MOTOR_SPEED){
sl_lidar_response_desired_rot_speed_t desired_speed;
ans = getDesiredSpeed(desired_speed);
if (!ans) return ans;
if(_isSupportingMotorCtrl == MotorCtrlSupportPwm)
speed = desired_speed.pwm_ref;
else
speed = desired_speed.rpm;
}
switch (_isSupportingMotorCtrl)
{
case MotorCtrlSupportNone:
break;
case MotorCtrlSupportPwm:
sl_lidar_payload_motor_pwm_t motor_pwm;
motor_pwm.pwm_value = speed;
ans = _sendCommand(SL_LIDAR_CMD_SET_MOTOR_PWM, (const sl_u8 *)&motor_pwm, sizeof(motor_pwm));
if (!ans) return ans;
break;
case MotorCtrlSupportRpm:
sl_lidar_payload_motor_pwm_t motor_rpm;
motor_rpm.pwm_value = speed;
ans = _sendCommand(SL_LIDAR_CMD_HQ_MOTOR_SPEED_CTRL, (const sl_u8 *)&motor_rpm, sizeof(motor_rpm));
if (!ans) return ans;
break;
}
return SL_RESULT_OK;
}
sl_result getMotorInfo(LidarMotorInfo &motorInfo, sl_u32 timeoutInMs)
{
Result<nullptr_t> ans = SL_RESULT_OK;
rp::hal::AutoLocker l(_lock);
{
std::vector<sl_u8> answer;
ans = getLidarConf(RPLIDAR_CONF_MIN_ROT_FREQ, answer, std::vector<sl_u8>());
if (!ans) return ans;
const sl_u16 *min_answer = reinterpret_cast<const sl_u16*>(&answer[0]);
motorInfo.min_speed = *min_answer;
ans = getLidarConf(RPLIDAR_CONF_MAX_ROT_FREQ, answer, std::vector<sl_u8>());
if (!ans) return ans;
const sl_u16 *max_answer = reinterpret_cast<const sl_u16*>(&answer[0]);
motorInfo.max_speed = *max_answer;
sl_lidar_response_desired_rot_speed_t desired_speed;
ans = getDesiredSpeed(desired_speed);
if (!ans) return ans;
if(motorInfo.motorCtrlSupport == MotorCtrlSupportPwm)
motorInfo.desired_speed = desired_speed.pwm_ref;
else
motorInfo.desired_speed = desired_speed.rpm;
}
return SL_RESULT_OK;
}
protected:
sl_result startMotor()
{
return setMotorSpeed(600);
}
sl_result getDesiredSpeed(sl_lidar_response_desired_rot_speed_t & motorSpeed, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_DESIRED_ROT_FREQ, answer, std::vector<sl_u8>(), timeoutInMs);
if (!ans) return ans;
const sl_lidar_response_desired_rot_speed_t *p_answer = reinterpret_cast<const sl_lidar_response_desired_rot_speed_t*>(&answer[0]);
motorSpeed = *p_answer;
return SL_RESULT_OK;
}
sl_result checkSupportConfigCommands(bool& outSupport, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
sl_lidar_response_device_info_t devinfo;
ans = getDeviceInfo(devinfo, timeoutInMs);
if (!ans) return ans;
sl_u16 modecount;
ans = getScanModeCount(modecount, 250);
if ((sl_result)ans == SL_RESULT_OK)
outSupport = true;
return SL_RESULT_OK;
}
sl_result getScanModeCount(sl_u16& modeCount, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_SCAN_MODE_COUNT, answer, std::vector<sl_u8>(), timeoutInMs);
if (!ans) return ans;
const sl_u16 *p_answer = reinterpret_cast<const sl_u16*>(&answer[0]);
modeCount = *p_answer;
return SL_RESULT_OK;
}
sl_result setLidarConf(sl_u32 type, const void* payload, size_t payloadSize, sl_u32 timeout)
{
if (type < 0x00010000 || type >0x0001FFFF)
return SL_RESULT_INVALID_DATA;
std::vector<sl_u8> requestPkt;
requestPkt.resize(sizeof(sl_lidar_payload_set_scan_conf_t) + payloadSize);
if (!payload) payloadSize = 0;
sl_lidar_payload_set_scan_conf_t* query = reinterpret_cast<sl_lidar_payload_set_scan_conf_t*>(&requestPkt[0]);
query->type = type;
if (payloadSize)
memcpy(&query[1], payload, payloadSize);
sl_result ans;
{
rp::hal::AutoLocker l(_lock);
if (IS_FAIL(ans = _sendCommand(SL_LIDAR_CMD_SET_LIDAR_CONF, &requestPkt[0], requestPkt.size()))) {//
return ans;
}
delay(20);
// waiting for confirmation
sl_lidar_ans_header_t response_header;
if (IS_FAIL(ans = _waitResponseHeader(&response_header, timeout))) {
return ans;
}
// verify whether we got a correct header
if (response_header.type != SL_LIDAR_ANS_TYPE_SET_LIDAR_CONF) {
return SL_RESULT_INVALID_DATA;
}
sl_u32 header_size = (response_header.size_q30_subtype & SL_LIDAR_ANS_HEADER_SIZE_MASK);
if (header_size < sizeof(type)) {
return SL_RESULT_INVALID_DATA;
}
if (!_channel->waitForData(header_size, timeout)) {
return SL_RESULT_OPERATION_TIMEOUT;
}
delay(100);
struct _sl_lidar_response_set_lidar_conf {
sl_u32 type;
sl_u32 result;
} answer;
_channel->read(reinterpret_cast<sl_u8*>(&answer), header_size);
return answer.result;
}
}
sl_result getLidarConf(sl_u32 type, std::vector<sl_u8> &outputBuf, const std::vector<sl_u8> &reserve = std::vector<sl_u8>(), sl_u32 timeout = DEFAULT_TIMEOUT)
{
sl_lidar_payload_get_scan_conf_t query;
query.type = type;
int sizeVec = reserve.size();
int maxLen = sizeof(query.reserved) / sizeof(query.reserved[0]);
if (sizeVec > maxLen) sizeVec = maxLen;
if (sizeVec > 0)
memcpy(query.reserved, &reserve[0], reserve.size());
Result<nullptr_t> ans = SL_RESULT_OK;
{
rp::hal::AutoLocker l(_lock);
ans = _sendCommand(SL_LIDAR_CMD_GET_LIDAR_CONF, &query, sizeof(query));
if (!ans) return ans;
//delay(50);
// waiting for confirmation
sl_lidar_ans_header_t response_header;
ans = _waitResponseHeader(&response_header, timeout);
if (!ans)return ans;
// verify whether we got a correct header
if (response_header.type != SL_LIDAR_ANS_TYPE_GET_LIDAR_CONF) {
return SL_RESULT_INVALID_DATA;
}
sl_u32 header_size = (response_header.size_q30_subtype & SL_LIDAR_ANS_HEADER_SIZE_MASK);
if (header_size < sizeof(type)) {
return SL_RESULT_INVALID_DATA;
}
//delay(100);
if (!_channel->waitForData(header_size, timeout)) {
return SL_RESULT_OPERATION_TIMEOUT;
}
std::vector<sl_u8> dataBuf;
dataBuf.resize(header_size);
_channel->read(reinterpret_cast<sl_u8 *>(&dataBuf[0]), header_size);
//check if returned type is same as asked type
sl_u32 replyType = -1;
memcpy(&replyType, &dataBuf[0], sizeof(type));
if (replyType != type) {
return SL_RESULT_INVALID_DATA;
}
//copy all the payload into &outputBuf
int payLoadLen = header_size - sizeof(type);
//do consistency check
if (payLoadLen <= 0) {
return SL_RESULT_INVALID_DATA;
}
//copy all payLoadLen bytes to outputBuf
outputBuf.resize(payLoadLen);
memcpy(&outputBuf[0], &dataBuf[0] + sizeof(type), payLoadLen);
}
return SL_RESULT_OK;
}
sl_result getLidarSampleDuration(float& sampleDurationRes, sl_u16 scanModeID, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> reserve(2);
memcpy(&reserve[0], &scanModeID, sizeof(scanModeID));
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_SCAN_MODE_US_PER_SAMPLE, answer, reserve, timeoutInMs);
if (!ans) return ans;
if (answer.size() < sizeof(sl_u32)){
return SL_RESULT_INVALID_DATA;
}
const sl_u32 *result = reinterpret_cast<const sl_u32*>(&answer[0]);
sampleDurationRes = (float)(*result >> 8);
return ans;
}
sl_result getMaxDistance(float &maxDistance, sl_u16 scanModeID, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> reserve(2);
memcpy(&reserve[0], &scanModeID, sizeof(scanModeID));
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_SCAN_MODE_MAX_DISTANCE, answer, reserve, timeoutInMs);
if (!ans) return ans;
if (answer.size() < sizeof(sl_u32)){
return SL_RESULT_INVALID_DATA;
}
const sl_u32 *result = reinterpret_cast<const sl_u32*>(&answer[0]);
maxDistance = (float)(*result >> 8);
return ans;
}
sl_result getScanModeAnsType(sl_u8 &ansType, sl_u16 scanModeID, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> reserve(2);
memcpy(&reserve[0], &scanModeID, sizeof(scanModeID));
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_SCAN_MODE_ANS_TYPE, answer, reserve, timeoutInMs);
if (!ans) return ans;
if (answer.size() < sizeof(sl_u8)){
return SL_RESULT_INVALID_DATA;
}
const sl_u8 *result = reinterpret_cast<const _u8*>(&answer[0]);
ansType = *result;
return ans;
}
sl_result getScanModeName(char* modeName, sl_u16 scanModeID, sl_u32 timeoutInMs = DEFAULT_TIMEOUT)
{
Result<nullptr_t> ans = SL_RESULT_OK;
std::vector<sl_u8> reserve(2);
memcpy(&reserve[0], &scanModeID, sizeof(scanModeID));
std::vector<sl_u8> answer;
ans = getLidarConf(SL_LIDAR_CONF_SCAN_MODE_NAME, answer, reserve, timeoutInMs);
if (!ans) return ans;
int len = answer.size();
if (0 == len) return SL_RESULT_INVALID_DATA;
memcpy(modeName, &answer[0], len);
return ans;
}
sl_result negotiateSerialBaudRate(sl_u32 requiredBaudRate, sl_u32 * baudRateDetected)
{
// ask the LIDAR to stop working first...
stop();
_channel->flush();
// wait for a while