-
Notifications
You must be signed in to change notification settings - Fork 2
/
RPLIDAR.h
2537 lines (2253 loc) · 86.5 KB
/
RPLIDAR.h
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
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef RPLIDAR_H
#define RPLIDAR_H
#include "OSMisc.h"
#include "RS232Port.h"
#ifndef DISABLE_RPLIDARTHREAD
#include "OSThread.h"
#endif // !DISABLE_RPLIDARTHREAD
#if !defined(__cplusplus) && defined(ENABLE_RPLIDAR_SDK_SUPPORT)
#undef ENABLE_RPLIDAR_SDK_SUPPORT
#endif // !defined(__cplusplus) && defined(ENABLE_RPLIDAR_SDK_SUPPORT)
// Temp...
//#undef ENABLE_RPLIDAR_SDK_SUPPORT
//#define FORCE_RPLIDAR_SDK_V1
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
#ifdef _MSC_VER
// Disable some Visual Studio warnings.
#pragma warning(disable : 4200)
#endif // _MSC_VER
#ifdef FORCE_RPLIDAR_SDK_V1
#include "../include/rplidar.h"
using namespace rp::standalone::rplidar;
// Different depending on the SDK version...
#ifdef SLAMTEC_LIDAR_SDK_VERSION
#define DRIVER_TYPE_SERIALPORT CHANNEL_TYPE_SERIALPORT
#define DRIVER_TYPE_TCP CHANNEL_TYPE_TCP
#define DRIVER_TYPE_UDP CHANNEL_TYPE_UDP
#else
#define CHANNEL_TYPE_SERIALPORT DRIVER_TYPE_SERIALPORT
#define CHANNEL_TYPE_TCP DRIVER_TYPE_TCP
#define CHANNEL_TYPE_UDP 0x2
#define SL_IS_OK(x) IS_OK(x)
#define SL_IS_FAIL(x) IS_FAIL(x)
#define SL_LIDAR_STATUS_ERROR RPLIDAR_STATUS_ERROR
#define SL_LIDAR_RESP_MEASUREMENT_SYNCBIT RPLIDAR_RESP_MEASUREMENT_SYNCBIT
#define SL_LIDAR_CONF_SCAN_COMMAND_EXPRESS RPLIDAR_CONF_SCAN_COMMAND_EXPRESS
#define sl_lidar_response_device_health_t rplidar_response_device_health_t
#define sl_lidar_response_device_info_t rplidar_response_device_info_t
#define sl_lidar_response_measurement_node_hq_t rplidar_response_measurement_node_hq_t
#endif // SLAMTEC_LIDAR_SDK_VERSION
#else
#include "../include/sl_lidar.h"
#include "../include/sl_lidar_driver.h"
using namespace sl;
#endif // FORCE_RPLIDAR_SDK_V1
#ifdef _MSC_VER
// Restore the Visual Studio warnings previously disabled.
#pragma warning(default : 4200)
#endif // _MSC_VER
#include <deque>
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
// Need to be undefined at the end of the file...
// min and max might cause incompatibilities...
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif // !max
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif // !min
#define TIMEOUT_MESSAGE_RPLIDAR 4.0 // In s.
// Should be at least 2 * number of bytes to be sure to contain entirely the biggest desired message (or group of messages) + 1.
#define MAX_NB_BYTES_RPLIDAR 1024
#pragma region RPLIDAR-SPECIFIC DEFINITIONS
#ifndef ENABLE_RPLIDAR_SDK_SUPPORT
#define MIN_BUF_LEN_RPLIDAR 2
#define NB_BYTES_RESPONSE_DESCRIPTOR_RPLIDAR 7
#define NB_BYTES_CHECKSUM_RPLIDAR 1
#define START_FLAG1_RPLIDAR 0xA5
#define START_FLAG2_RPLIDAR 0x5A
#define SEND_MODE_MASK_RPLIDAR 0xC0
#define SEND_MODE_SHIFT_RPLIDAR 6
#define DATA_RESPONSE_LENGTH_MASK_RPLIDAR 0x3FFFFFFF
// Single Request-Single Response Mode
// Single Request-Multiple Response Mode
// Single Request-No Response : need to wait a little bit after the request wince there will be no response...
#define SINGLE_REQUEST_SINGLE_RESPONSE_SEND_MODE_RPLIDAR 0x00
#define SINGLE_REQUEST_MULTIPLE_RESPONSE_SEND_MODE_RPLIDAR 0x01
// Commands without payload and response.
#define STOP_REQUEST_RPLIDAR 0x25
#define SCAN_REQUEST_RPLIDAR 0x20
#define FORCE_SCAN_REQUEST_RPLIDAR 0x21
#define RESET_REQUEST_RPLIDAR 0x40
// Commands without payload but have response
#define GET_INFO_REQUEST_RPLIDAR 0x50
#define GET_HEALTH_REQUEST_RPLIDAR 0x52
#define GET_SAMPLERATE_REQUEST_RPLIDAR 0x59 // Added in FW ver 1.17.
// Commands with payload and without response
#define SET_MOTOR_SPEED_CTRL_REQUEST_RPLIDAR 0xA8
#define NEW_BAUDRATE_CONFIRM_REQUEST_RPLIDAR 0x90 // Added in FW ver 1.30. // Undocumented...
// Commands with payload and have response
#define EXPRESS_SCAN_REQUEST_RPLIDAR 0x82 // Added in FW ver 1.17.
#define HQ_SCAN_REQUEST_RPLIDAR 0x83 // Added in FW ver 1.24.
#define GET_LIDAR_CONF_REQUEST_RPLIDAR 0x84 // Added in FW ver 1.24.
#define SET_LIDAR_CONF_REQUEST_RPLIDAR 0x85 // Added in FW ver 1.24. // Undocumented...
// Commands added for A2 to set RPLIDAR motor PWM when using accessory board.
#define SET_MOTOR_PWM_REQUEST_RPLIDAR 0xF0 // Undocumented...
#define GET_ACC_BOARD_FLAG_REQUEST_RPLIDAR 0xFF // Undocumented...
// Responses.
#define DEVINFO_RESPONSE_RPLIDAR 0x04
#define DEVHEALTH_RESPONSE_RPLIDAR 0x06
#define MEASUREMENT_RESPONSE_RPLIDAR 0x81
#define MEASUREMENT_CAPSULED_RESPONSE_RPLIDAR 0x82 // Added in FW ver 1.17.
#define MEASUREMENT_HQ_RESPONSE_RPLIDAR 0x83 // Added in FW ver 1.17.
#define SAMPLERATE_RESPONSE_RPLIDAR 0x15 // Added in FW ver 1.17.
#define MEASUREMENT_CAPSULED_ULTRA_RESPONSE_RPLIDAR 0x84 // Added in FW ver 1.23alpha.
#define MEASUREMENT_DENSE_CAPSULED_RESPONSE_RPLIDAR 0x85 // Added in FW ver 1.24.
#define GET_LIDAR_CONF_RESPONSE_RPLIDAR 0x20 // Added in FW ver 1.24.
#define SET_LIDAR_CONF_RESPONSE_RPLIDAR 0x21 // Added in FW ver 1.24.
#define ACC_BOARD_FLAG_RESPONSE_RPLIDAR 0xFF // Added in FW ver 1.24.
#define LIDAR_CONF_SCAN_MODE_COUNT_RPLIDAR 0x00000070
#define LIDAR_CONF_SCAN_MODE_US_PER_SAMPLE_RPLIDAR 0x00000071
#define LIDAR_CONF_SCAN_MODE_MAX_DISTANCE_RPLIDAR 0x00000074
#define LIDAR_CONF_SCAN_MODE_ANS_TYPE_RPLIDAR 0x00000075
#define LIDAR_CONF_LIDAR_MAC_ADDR_RPLIDAR 0x00000079
#define LIDAR_CONF_SCAN_MODE_TYPICAL_RPLIDAR 0x0000007C
#define LIDAR_CONF_SCAN_MODE_NAME_RPLIDAR 0x0000007F
#define LIDAR_CONF_DETECTED_SERIAL_BPS_RPLIDAR 0x000000A1
#else
#endif // !ENABLE_RPLIDAR_SDK_SUPPORT
#define NB_BYTES_SERIAL_NUMBER_RPLIDAR 16
#define MAX_BUF_LEN_SERIAL_NUMBER_RPLIDAR (2*NB_BYTES_SERIAL_NUMBER_RPLIDAR+1)
#define STATUS_OK_RPLIDAR 0x00
#define STATUS_WARNING_RPLIDAR 0x01
#define STATUS_ERROR_RPLIDAR 0x02
// Undocumented...
#define MAX_MOTOR_PWM_RPLIDAR 1023
#define DEFAULT_MOTOR_PWM_RPLIDAR 660
#define DEFAULT_MOTOR_SPEED_RPLIDAR (0xFFFFu)
#define SCAN_MODE_RPLIDAR 0
#define LEGACY_EXPRESS_SCAN_MODE_RPLIDAR 254
#define FORCE_SCAN_MODE_RPLIDAR 255
#define CHECK_BIT_MASK_SCAN_DATA_RESPONSE_RPLIDAR 0x01
#define START_BIT_MASK_SCAN_DATA_RESPONSE_RPLIDAR 0x01
#define INVERTED_START_BIT_MASK_SCAN_DATA_RESPONSE_RPLIDAR 0x02
#define MAX_NB_MEASUREMENTS_PER_SCAN_RPLIDAR 8192
#define NB_BYTES_SCAN_DATA_RESPONSE_RPLIDAR 5
#define NB_BYTES_FORCE_SCAN_DATA_RESPONSE_RPLIDAR NB_BYTES_SCAN_DATA_RESPONSE_RPLIDAR
#define NB_BYTES_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR 84
#define NB_BYTES_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR 132
#define NB_BYTES_DENSE_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR 84
#define NB_CABINS_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR 16
#define NB_CABINS_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR 32
#define NB_CABINS_DENSE_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR 40
#define NB_MEASUREMENTS_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR (2*NB_CABINS_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR)
#define NB_MEASUREMENTS_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR (3*NB_CABINS_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR)
#define NB_MEASUREMENTS_DENSE_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR NB_CABINS_DENSE_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR
// Arbitrarily set to the max of the different scan modes...
#define NB_MEASUREMENTS_OTHER_SCAN_DATA_RESPONSE_RPLIDAR NB_MEASUREMENTS_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR
#define MAX_NB_SCAN_MODES_RPLIDAR 16
struct RPLIDARSCANMODE
{
int id;
double us_per_sample;
double max_distance;
int ans_type;
char scan_mode[64];
};
typedef struct RPLIDARSCANMODE RPLIDARSCANMODE;
struct RPLIDARSCAN
{
double distance;
double angle;
BOOL bNewScan;
int quality;
};
typedef struct RPLIDARSCAN RPLIDARSCAN;
#pragma endregion
struct RPLIDAR
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
#ifdef FORCE_RPLIDAR_SDK_V1
RPlidarDriver* drv;
void* channel; // Kept as padding for MATLAB compatibility since it does not seem to support C++ types in the configuration used...
std::deque<sl_lidar_response_measurement_node_hq_t>* nodes_cache;
#else
ILidarDriver* drv;
IChannel* channel;
std::deque<sl_lidar_response_measurement_node_hq_t>* nodes_cache;
#endif // FORCE_RPLIDAR_SDK_V1
#else
void* drv; // Kept as padding for MATLAB compatibility since it does not seem to support C++ types in the configuration used...
void* channel; // Kept as padding for MATLAB compatibility since it does not seem to support C++ types in the configuration used...
void* nodes_cache; // Kept as padding for MATLAB compatibility since it does not seem to support C++ types in the configuration used...
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
RS232PORT RS232Port; // Kept as padding for MATLAB compatibility since it does not seem to support C++ types in the configuration used...
int model;
int hardware;
int firmware_major;
int firmware_minor;
char SerialNumber[MAX_BUF_LEN_SERIAL_NUMBER_RPLIDAR];
int Tstandard; // Time in us used when RPLIDAR takes a single laser ranging in SCAN mode.
int Texpress; // Time in us used when RPLIDAR takes a single laser ranging in EXPRESS_SCAN mode.
int esversion; // Express Scan version.
unsigned char esdata_prev[NB_BYTES_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR];
unsigned char eesdata_prev[NB_BYTES_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR];
unsigned char desdata_prev[NB_BYTES_DENSE_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR];
FILE* pfSaveFile; // Used to save raw data, should be handled specifically...
//RPLIDARDATA LastRPLIDARData;
char szCfgFilePath[256];
// Parameters.
char szDevPath[256];
int BaudRate;
int timeout;
int threadperiod;
BOOL bSaveRawData;
BOOL bStartScanModeAtStartup;
int ScanMode;
int motordelay;
int motorPWM;
int maxhist;
double alpha_max_err;
double d_max_err;
};
typedef struct RPLIDAR RPLIDAR;
inline double AngleDiffRPLIDAR(double start_angle_1, double start_angle_2)
{
if (start_angle_1 <= start_angle_2) return start_angle_2-start_angle_1;
else return 360+start_angle_2-start_angle_1;
}
#ifndef ENABLE_RPLIDAR_SDK_SUPPORT
// req must contain a valid request of reqlen bytes.
inline unsigned char ComputeChecksumRPLIDAR(unsigned char* req, int reqlen)
{
int i = 0;
unsigned char res = 0;
while (i < reqlen-NB_BYTES_CHECKSUM_RPLIDAR)
{
res ^= req[i];
i++;
}
return res;
}
// req must contain a valid request of reqlen bytes.
inline int GetSendModeFromResponseDescriptorRPLIDAR(unsigned char* req, int reqlen)
{
UNREFERENCED_PARAMETER(reqlen);
return ((SEND_MODE_MASK_RPLIDAR & req[5]) >> SEND_MODE_SHIFT_RPLIDAR);
}
// req must contain a valid request of reqlen bytes.
inline int GetDataResponseLengthFromResponseDescriptorRPLIDAR(unsigned char* req, int reqlen)
{
int len = 0;
UNREFERENCED_PARAMETER(reqlen);
len = ((req[5]<<24)|(req[4]<<16)|(req[3]<<8)|(req[2])) & DATA_RESPONSE_LENGTH_MASK_RPLIDAR;
return len;
}
/*
Return : EXIT_SUCCESS if the beginning of buf contains a valid response descriptor (there might be other data at the end),
EXIT_OUT_OF_MEMORY if the response descriptor is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if there is an error (check *pnbBytesToDiscard to know how
many bytes can be safely discarded).
*/
inline int AnalyzeResponseDescriptorRPLIDAR(unsigned char* buf, int buflen, int* pDataResponseLength, int* pSendMode, int* pDataType,
int* pnbBytesToRequest, int* pnbBytesToDiscard)
{
*pDataResponseLength = 0;
*pnbBytesToRequest = -1;
*pnbBytesToDiscard = 0;
if (buflen < NB_BYTES_RESPONSE_DESCRIPTOR_RPLIDAR)
{
*pnbBytesToRequest = NB_BYTES_RESPONSE_DESCRIPTOR_RPLIDAR-buflen;
return EXIT_OUT_OF_MEMORY;
}
if ((buf[0] != START_FLAG1_RPLIDAR)||(buf[1] != START_FLAG2_RPLIDAR))
{
printf("Warning : RPLIDAR bad start flag. \n");
*pnbBytesToDiscard = 1; // We are only sure that the first start flag byte can be discarded...
return EXIT_FAILURE;
}
*pDataResponseLength = GetDataResponseLengthFromResponseDescriptorRPLIDAR(buf, buflen);
*pSendMode = GetSendModeFromResponseDescriptorRPLIDAR(buf, buflen);
*pDataType = buf[6];
return EXIT_SUCCESS;
}
/*
Return : EXIT_SUCCESS if the beginning of *pFoundResponseDescriptor contains a valid response descriptor (there might be other data
at the end),
EXIT_OUT_OF_MEMORY if the response descriptor is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible response descriptor could be found.
Data in the beginning of buf might have been discarded (check *pnbBytesDiscarded to know how many bytes were discarded).
*/
inline int FindResponseDescriptorRPLIDAR(unsigned char* buf, int buflen, int* pDataResponseLength, int* pSendMode, int* pDataType,
int* pnbBytesToRequest, unsigned char** pFoundResponseDescriptor, int* pnbBytesDiscarded)
{
int res = EXIT_FAILURE, nbBytesToRequest = -1, nbBytesToDiscard = 0;
*pnbBytesToRequest = -1;
*pFoundResponseDescriptor = buf;
*pnbBytesDiscarded = 0;
for (;;)
{
res = AnalyzeResponseDescriptorRPLIDAR(*pFoundResponseDescriptor, buflen-(*pnbBytesDiscarded), pDataResponseLength, pSendMode, pDataType, &nbBytesToRequest, &nbBytesToDiscard);
switch (res)
{
case EXIT_SUCCESS:
return EXIT_SUCCESS;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
return EXIT_OUT_OF_MEMORY;
default:
(*pFoundResponseDescriptor) += nbBytesToDiscard;
(*pnbBytesDiscarded) += nbBytesToDiscard;
if (buflen-(*pnbBytesDiscarded) <= 0)
{
*pFoundResponseDescriptor = NULL;
return EXIT_FAILURE;
}
break;
}
}
}
inline int GetResponseDescriptorRPLIDAR(RPLIDAR* pRPLIDAR, int* pDataResponseLength, int* pSendMode, int* pDataType)
{
unsigned char recvbuf[MAX_NB_BYTES_RPLIDAR];
int BytesReceived = 0, recvbuflen = 0, res = EXIT_FAILURE, nbBytesToRequest = 0, nbBytesDiscarded = 0;
unsigned char* ptr = NULL;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = MAX_NB_BYTES_RPLIDAR-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
// Suppose that there are not so many data to discard.
// First try to get directly the desired response descriptor...
nbBytesToRequest = NB_BYTES_RESPONSE_DESCRIPTOR_RPLIDAR;
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, recvbuf, nbBytesToRequest) != EXIT_SUCCESS)
{
printf("Error reading data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
BytesReceived += nbBytesToRequest;
for (;;)
{
res = FindResponseDescriptorRPLIDAR(recvbuf, BytesReceived, pDataResponseLength, pSendMode, pDataType, &nbBytesToRequest, &ptr, &nbBytesDiscarded);
if (res == EXIT_SUCCESS) break;
if (res == EXIT_FAILURE)
{
nbBytesToRequest = min(NB_BYTES_RESPONSE_DESCRIPTOR_RPLIDAR, nbBytesDiscarded);
}
memmove(recvbuf, recvbuf+nbBytesDiscarded, BytesReceived-nbBytesDiscarded);
BytesReceived -= nbBytesDiscarded;
if (BytesReceived+nbBytesToRequest > recvbuflen)
{
printf("Error reading data from a RPLIDAR : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, recvbuf+BytesReceived, nbBytesToRequest) != EXIT_SUCCESS)
{
printf("Error reading data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
BytesReceived += nbBytesToRequest;
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_RPLIDAR)
{
printf("Error reading data from a RPLIDAR : Response descriptor timeout. \n");
return EXIT_TIMEOUT;
}
}
if (BytesReceived-nbBytesDiscarded-NB_BYTES_RESPONSE_DESCRIPTOR_RPLIDAR > 0)
{
printf("Warning getting data from a RPLIDAR : Unexpected data after a response descriptor. \n");
}
return EXIT_SUCCESS;
}
/*
Return : EXIT_SUCCESS if the beginning of buf contains a valid data response (there might be other data at the end),
EXIT_OUT_OF_MEMORY if the data response is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if there is an error (check *pnbBytesToDiscard to know how
many bytes can be safely discarded).
*/
inline int AnalyzeScanDataResponseRPLIDAR(unsigned char* buf, int buflen, BOOL* pbNewScan, int* pQuality, double* pAngle, double* pDistance,
int* pnbBytesToRequest, int* pnbBytesToDiscard)
{
unsigned short angle_q6 = 0;
unsigned short distance_q2 = 0;
*pnbBytesToRequest = -1;
*pnbBytesToDiscard = 0;
if (buflen < NB_BYTES_SCAN_DATA_RESPONSE_RPLIDAR)
{
*pnbBytesToRequest = NB_BYTES_SCAN_DATA_RESPONSE_RPLIDAR-buflen;
return EXIT_OUT_OF_MEMORY;
}
*pbNewScan = (buf[0] & START_BIT_MASK_SCAN_DATA_RESPONSE_RPLIDAR);
if (*pbNewScan == ((buf[0] & INVERTED_START_BIT_MASK_SCAN_DATA_RESPONSE_RPLIDAR) >> 1))
{
printf("Warning : RPLIDAR bad inversed start bit. \n");
*pnbBytesToDiscard = 1; // We are only sure that the first byte can be discarded...
return EXIT_FAILURE;
}
if ((buf[1] & CHECK_BIT_MASK_SCAN_DATA_RESPONSE_RPLIDAR) != 1)
{
printf("Warning : RPLIDAR bad check bit. \n");
*pnbBytesToDiscard = 1; // We are only sure that the first byte can be discarded...
return EXIT_FAILURE;
}
*pQuality = (unsigned char)(buf[0] >> 2);
angle_q6 = (buf[2] << 7)|(buf[1] >> 1);
distance_q2 = (buf[4] << 8)|buf[3];
// Convert in rad.
*pAngle = fmod_2PI_deg2rad(-angle_q6/64.0);
// Convert in m.
*pDistance = distance_q2/4000.0;
return EXIT_SUCCESS;
}
/*
Return : EXIT_SUCCESS if the beginning of *pFoundScanDataResponse contains a valid data response (there might be other data
at the end),
EXIT_OUT_OF_MEMORY if the data response is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible data response could be found.
Data in the beginning of buf might have been discarded (check *pnbBytesDiscarded to know how many bytes were discarded).
*/
inline int FindScanDataResponseRPLIDAR(unsigned char* buf, int buflen, BOOL* pbNewScan, int* pQuality, double* pAngle, double* pDistance,
int* pnbBytesToRequest, unsigned char** pFoundScanDataResponse, int* pnbBytesDiscarded)
{
int res = EXIT_FAILURE, nbBytesToRequest = -1, nbBytesToDiscard = 0;
*pnbBytesToRequest = -1;
*pFoundScanDataResponse = buf;
*pnbBytesDiscarded = 0;
for (;;)
{
res = AnalyzeScanDataResponseRPLIDAR(*pFoundScanDataResponse, buflen-(*pnbBytesDiscarded), pbNewScan, pQuality, pAngle, pDistance, &nbBytesToRequest, &nbBytesToDiscard);
switch (res)
{
case EXIT_SUCCESS:
return EXIT_SUCCESS;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
return EXIT_OUT_OF_MEMORY;
default:
(*pFoundScanDataResponse) += nbBytesToDiscard;
(*pnbBytesDiscarded) += nbBytesToDiscard;
if (buflen-(*pnbBytesDiscarded) <= 0)
{
*pFoundScanDataResponse = NULL;
return EXIT_FAILURE;
}
break;
}
}
}
/*
Return : EXIT_SUCCESS if the beginning of buf contains a valid data response (there might be other data at the end),
EXIT_OUT_OF_MEMORY if the data response is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if there is an error (check *pnbBytesToDiscard to know how
many bytes can be safely discarded).
*/
inline int AnalyzeExpressScanDataResponseRPLIDAR(unsigned char* buf, int buflen, int nb_bytes_response,
int* pnbBytesToRequest, int* pnbBytesToDiscard)
{
unsigned char sync = 0, ChkSum = 0;
*pnbBytesToRequest = -1;
*pnbBytesToDiscard = 0;
if (buflen < nb_bytes_response)
{
*pnbBytesToRequest = nb_bytes_response-buflen;
return EXIT_OUT_OF_MEMORY;
}
sync = (buf[0] & 0xF0)|(buf[1] >> 4);
if (sync != START_FLAG1_RPLIDAR)
{
printf("Warning : RPLIDAR bad sync1 or sync2. \n");
*pnbBytesToDiscard = 1; // We are only sure that the first byte can be discarded...
return EXIT_FAILURE;
}
ChkSum = (buf[1] << 4)|(buf[0] & 0x0F);
// Force ComputeChecksumRPLIDAR() to compute until the last byte...
if (ChkSum != ComputeChecksumRPLIDAR(buf+2, nb_bytes_response-1))
{
printf("Warning : RPLIDAR bad ChkSum. \n");
*pnbBytesToDiscard = 1; // We are only sure that the first byte can be discarded...
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Return : EXIT_SUCCESS if the beginning of *pFoundScanDataResponse contains a valid data response (there might be other data
at the end),
EXIT_OUT_OF_MEMORY if the data response is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible data response could be found.
Data in the beginning of buf might have been discarded (check *pnbBytesDiscarded to know how many bytes were discarded).
*/
inline int FindExpressScanDataResponseRPLIDAR(unsigned char* buf, int buflen, int nb_bytes_response,
int* pnbBytesToRequest, unsigned char** pFoundExpressScanDataResponse, int* pnbBytesDiscarded)
{
int res = EXIT_FAILURE, nbBytesToRequest = -1, nbBytesToDiscard = 0;
*pnbBytesToRequest = -1;
*pFoundExpressScanDataResponse = buf;
*pnbBytesDiscarded = 0;
for (;;)
{
res = AnalyzeExpressScanDataResponseRPLIDAR(*pFoundExpressScanDataResponse, buflen-(*pnbBytesDiscarded), nb_bytes_response, &nbBytesToRequest, &nbBytesToDiscard);
switch (res)
{
case EXIT_SUCCESS:
return EXIT_SUCCESS;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
return EXIT_OUT_OF_MEMORY;
default:
(*pFoundExpressScanDataResponse) += nbBytesToDiscard;
(*pnbBytesDiscarded) += nbBytesToDiscard;
if (buflen-(*pnbBytesDiscarded) <= 0)
{
*pFoundExpressScanDataResponse = NULL;
return EXIT_FAILURE;
}
break;
}
}
}
// unsigned char esdataresponse[nb_bytes_response] with nb_bytes_response=NB_BYTES_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR
// or NB_BYTES_EXTENDED_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR or NB_BYTES_DENSE_EXPRESS_SCAN_DATA_RESPONSE_RPLIDAR
inline int GetRawExpressScanDataResponseRPLIDAR(RPLIDAR* pRPLIDAR, unsigned char* esdataresponse, int nb_bytes_response)
{
unsigned char recvbuf[MAX_NB_BYTES_RPLIDAR];
int BytesReceived = 0, recvbuflen = 0, res = EXIT_FAILURE, nbBytesToRequest = 0, nbBytesDiscarded = 0;
unsigned char* ptr = NULL;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
recvbuflen = MAX_NB_BYTES_RPLIDAR-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
// Suppose that there are not so many data to discard.
// First try to get directly the desired data response...
nbBytesToRequest = nb_bytes_response;
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, recvbuf, nbBytesToRequest) != EXIT_SUCCESS)
{
printf("Error reading data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
BytesReceived += nbBytesToRequest;
for (;;)
{
res = FindExpressScanDataResponseRPLIDAR(recvbuf, BytesReceived, nb_bytes_response, &nbBytesToRequest, &ptr, &nbBytesDiscarded);
if (res == EXIT_SUCCESS) break;
if (res == EXIT_FAILURE)
{
nbBytesToRequest = min(nb_bytes_response, nbBytesDiscarded);
}
memmove(recvbuf, recvbuf+nbBytesDiscarded, BytesReceived-nbBytesDiscarded);
BytesReceived -= nbBytesDiscarded;
if (BytesReceived+nbBytesToRequest > recvbuflen)
{
printf("Error reading data from a RPLIDAR : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, recvbuf+BytesReceived, nbBytesToRequest) != EXIT_SUCCESS)
{
printf("Error reading data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
BytesReceived += nbBytesToRequest;
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_RPLIDAR)
{
printf("Error reading data from a RPLIDAR : Data response timeout. \n");
return EXIT_TIMEOUT;
}
}
if (BytesReceived-nbBytesDiscarded-nb_bytes_response > 0)
{
printf("Warning getting data from a RPLIDAR : Unexpected data after a data response. \n");
}
// Store the data...
memcpy(esdataresponse, recvbuf, nb_bytes_response);
return EXIT_SUCCESS;
}
inline int GetConfRPLIDAR(RPLIDAR* pRPLIDAR, int type, unsigned char* payload, int payloadsize, unsigned char* resppayload, int resppayloadsize)
{
unsigned char reqbuf[136];
int DataResponseLength = 0, SendMode = 0, DataType = 0;
if ((payloadsize < 0)||(payloadsize > 128))
{
printf("Error writing data to a RPLIDAR : Invalid payloadsize. \n");
return EXIT_FAILURE;
}
memset(reqbuf, 0, sizeof(reqbuf));
reqbuf[0] = START_FLAG1_RPLIDAR;
reqbuf[1] = GET_LIDAR_CONF_REQUEST_RPLIDAR;
reqbuf[2] = (unsigned char)(4+payloadsize);
memcpy(reqbuf+3, (unsigned char*)&type, 4);
if (payloadsize > 0) memcpy(reqbuf+7, payload, payloadsize);
reqbuf[7+payloadsize] = ComputeChecksumRPLIDAR(reqbuf, 8+payloadsize);
// Send request.
if (WriteAllRS232Port(&pRPLIDAR->RS232Port, reqbuf, 8+payloadsize) != EXIT_SUCCESS)
{
printf("Error writing data to a RPLIDAR. \n");
return EXIT_FAILURE;
}
// Receive the response descriptor.
if (GetResponseDescriptorRPLIDAR(pRPLIDAR, &DataResponseLength, &SendMode, &DataType) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
// Quick check of the response descriptor.
if (DataType != GET_LIDAR_CONF_RESPONSE_RPLIDAR)
{
printf("A RPLIDAR is not responding correctly : Bad response descriptor. \n");
return EXIT_FAILURE;
}
if (DataResponseLength > resppayloadsize)
{
printf("Error writing data to a RPLIDAR : Invalid resppayloadsize. \n");
return EXIT_FAILURE;
}
// Receive the data response.
memset(resppayload, 0, DataResponseLength);
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, resppayload, DataResponseLength) != EXIT_SUCCESS)
{
printf("A RPLIDAR is not responding correctly. \n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#endif // !ENABLE_RPLIDAR_SDK_SUPPORT
inline int StopRequestRPLIDAR(RPLIDAR* pRPLIDAR)
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
pRPLIDAR->nodes_cache->clear();
if (SL_IS_FAIL(pRPLIDAR->drv->stop()))
{
printf("A RPLIDAR is not responding correctly : stop() failed. \n");
return EXIT_FAILURE;
}
#else
unsigned char reqbuf[] = {START_FLAG1_RPLIDAR,STOP_REQUEST_RPLIDAR};
// Send request.
if (WriteAllRS232Port(&pRPLIDAR->RS232Port, reqbuf, sizeof(reqbuf)) != EXIT_SUCCESS)
{
printf("Error writing data to a RPLIDAR. \n");
return EXIT_FAILURE;
}
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
// Host systems should wait for at least 1 millisecond (ms) before sending another request.
//uSleep(1000);
// https://github.com/Slamtec/rplidar_sdk/issues/36.
//mSleep(20);
mSleep(pRPLIDAR->motordelay);
return EXIT_SUCCESS;
}
inline int ResetRequestRPLIDAR(RPLIDAR* pRPLIDAR)
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
if (SL_IS_FAIL(pRPLIDAR->drv->reset()))
{
printf("A RPLIDAR is not responding correctly : reset() failed. \n");
return EXIT_FAILURE;
}
#else
unsigned char reqbuf[] = {START_FLAG1_RPLIDAR,RESET_REQUEST_RPLIDAR};
// Send request.
if (WriteAllRS232Port(&pRPLIDAR->RS232Port, reqbuf, sizeof(reqbuf)) != EXIT_SUCCESS)
{
printf("Error writing data to a RPLIDAR. \n");
return EXIT_FAILURE;
}
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
// Host systems should wait for at least 2 milliseconds (ms) before sending another request.
//uSleep(2000);
// https://github.com/Slamtec/rplidar_sdk/issues/36.
//mSleep(20);
mSleep(pRPLIDAR->motordelay);
return EXIT_SUCCESS;
}
// Undocumented...
// https://github.com/Slamtec/rplidar_sdk/issues/36?
inline int GetStartupMessageRPLIDAR(RPLIDAR* pRPLIDAR)
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
mSleep(100);
#ifdef FORCE_RPLIDAR_SDK_V1
if (IS_FAIL(pRPLIDAR->drv->clearNetSerialRxCache()))
{
printf("A RPLIDAR is not responding correctly : clearNetSerialRxCache() failed. \n");
return EXIT_FAILURE;
}
#else
pRPLIDAR->channel->flush();
#endif // FORCE_RPLIDAR_SDK_V1
#else
//unsigned char databuf[57]; // A2...
//unsigned char databuf[MAX_NB_BYTES_RPLIDAR];
//int nbReadBytes = 0;
//// Undocumented response for A2...
//memset(databuf, 0, sizeof(databuf));
//if (ReadAllRS232Port(&pRPLIDAR->RS232Port, databuf, sizeof(databuf)) != EXIT_SUCCESS)
//{
// printf("A RPLIDAR is not responding correctly. \n");
// return EXIT_FAILURE;
//}
// Undocumented response...
mSleep(100);
//memset(databuf, 0, sizeof(databuf));
//if (ReadRS232Port(&pRPLIDAR->RS232Port, databuf, sizeof(databuf), &nbReadBytes) != EXIT_SUCCESS)
//{
// //printf("Warning : A RPLIDAR might not be responding correctly. \n");
// //return EXIT_FAILURE;
//}
if (PurgeRS232Port(&pRPLIDAR->RS232Port) != EXIT_SUCCESS)
{
printf("Error purging data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
// Need to purge twice on Mac OS otherwise next read() would fail...?
if (PurgeRS232Port(&pRPLIDAR->RS232Port) != EXIT_SUCCESS)
{
printf("Error purging data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
return EXIT_SUCCESS;
}
inline int ClearCacheRPLIDAR(RPLIDAR* pRPLIDAR)
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
#ifdef FORCE_RPLIDAR_SDK_V1
if (IS_FAIL(pRPLIDAR->drv->clearNetSerialRxCache()))
{
printf("A RPLIDAR is not responding correctly : clearNetSerialRxCache() failed. \n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
#else
pRPLIDAR->channel->flush();
return EXIT_SUCCESS;
#endif // FORCE_RPLIDAR_SDK_V1
#else
if (PurgeRS232Port(&pRPLIDAR->RS232Port) != EXIT_SUCCESS)
{
printf("Error purging data from a RPLIDAR. \n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
}
inline int GetHealthRequestRPLIDAR(RPLIDAR* pRPLIDAR, BOOL* pbProtectionStop)
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
sl_lidar_response_device_health_t healthinfo;
if (SL_IS_FAIL(pRPLIDAR->drv->getHealth(healthinfo)))
{
printf("A RPLIDAR is not responding correctly : getHealth() failed. \n");
return EXIT_FAILURE;
}
// Analyze the data response.
*pbProtectionStop = (healthinfo.status == SL_LIDAR_STATUS_ERROR)? TRUE: FALSE;
#else
unsigned char reqbuf[] = {START_FLAG1_RPLIDAR,GET_HEALTH_REQUEST_RPLIDAR};
unsigned char databuf[3];
int DataResponseLength = 0, SendMode = 0, DataType = 0;
// Send request.
if (WriteAllRS232Port(&pRPLIDAR->RS232Port, reqbuf, sizeof(reqbuf)) != EXIT_SUCCESS)
{
printf("Error writing data to a RPLIDAR. \n");
return EXIT_FAILURE;
}
// Receive the response descriptor.
if (GetResponseDescriptorRPLIDAR(pRPLIDAR, &DataResponseLength, &SendMode, &DataType) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
// Quick check of the response descriptor.
if ((DataResponseLength != 3)||(DataType != DEVHEALTH_RESPONSE_RPLIDAR))
{
printf("A RPLIDAR is not responding correctly : Bad response descriptor. \n");
return EXIT_FAILURE;
}
// Receive the data response.
memset(databuf, 0, sizeof(databuf));
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, databuf, sizeof(databuf)) != EXIT_SUCCESS)
{
printf("A RPLIDAR is not responding correctly. \n");
return EXIT_FAILURE;
}
// Analyze the data response.
*pbProtectionStop = (databuf[0] == STATUS_ERROR_RPLIDAR)? TRUE: FALSE;
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
return EXIT_SUCCESS;
}
// SerialNumber must be a null-terminated string of at least MAX_BUF_LEN_SERIAL_NUMBER_RPLIDAR bytes, including 0.
inline int GetInfoRequestRPLIDAR(RPLIDAR* pRPLIDAR, int* pModelID, int* pHardwareVersion, int* pFirmwareMajor, int* pFirmwareMinor, char* SerialNumber)
{
int i = 0;
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
sl_lidar_response_device_info_t devinfo;
if (SL_IS_FAIL(pRPLIDAR->drv->getDeviceInfo(devinfo)))
{
printf("A RPLIDAR is not responding correctly : getDeviceInfo() failed. \n");
return EXIT_FAILURE;
}
// Analyze the data response.
*pModelID = devinfo.model;
*pFirmwareMinor = (devinfo.firmware_version & 0x00FF);
*pFirmwareMajor = (devinfo.firmware_version >> 8);
*pHardwareVersion = devinfo.hardware_version;
// 128bit unique serial number, when converting to text in hex, the Least Significant Byte prints first.
memset(SerialNumber, 0, MAX_BUF_LEN_SERIAL_NUMBER_RPLIDAR);
for (i = 0; i < NB_BYTES_SERIAL_NUMBER_RPLIDAR; i++)
{
sprintf(SerialNumber+2*i, "%02X", (int)(unsigned char)devinfo.serialnum[i]);
}
#else
unsigned char reqbuf[] = {START_FLAG1_RPLIDAR,GET_INFO_REQUEST_RPLIDAR};
unsigned char databuf[20];
int DataResponseLength = 0, SendMode = 0, DataType = 0;
// Send request.
if (WriteAllRS232Port(&pRPLIDAR->RS232Port, reqbuf, sizeof(reqbuf)) != EXIT_SUCCESS)
{
printf("Error writing data to a RPLIDAR. \n");
return EXIT_FAILURE;
}
// Receive the response descriptor.
if (GetResponseDescriptorRPLIDAR(pRPLIDAR, &DataResponseLength, &SendMode, &DataType) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
// Quick check of the response descriptor.
if ((DataResponseLength != 20)||(DataType != DEVINFO_RESPONSE_RPLIDAR))
{
printf("A RPLIDAR is not responding correctly : Bad response descriptor. \n");
return EXIT_FAILURE;
}
// Receive the data response.
memset(databuf, 0, sizeof(databuf));
if (ReadAllRS232Port(&pRPLIDAR->RS232Port, databuf, sizeof(databuf)) != EXIT_SUCCESS)
{
printf("A RPLIDAR is not responding correctly. \n");
return EXIT_FAILURE;
}
// Analyze the data response.
*pModelID = databuf[0];
*pFirmwareMinor = databuf[1];
*pFirmwareMajor = databuf[2];
*pHardwareVersion = databuf[3];
// 128bit unique serial number, when converting to text in hex, the Least Significant Byte prints first.
memset(SerialNumber, 0, MAX_BUF_LEN_SERIAL_NUMBER_RPLIDAR);
for (i = 0; i < NB_BYTES_SERIAL_NUMBER_RPLIDAR; i++)
{
sprintf(SerialNumber+2*i, "%02X", (int)(unsigned char)databuf[i+4]);
}
#endif // ENABLE_RPLIDAR_SDK_SUPPORT
return EXIT_SUCCESS;
}
// Probably deprecated since it might depend on the scan mode...
// Tstandard, Texpress : Time in us used when RPLIDAR takes a single laser ranging in SCAN and EXPRESS_SCAN modes.
inline int GetSampleRateRequestRPLIDAR(RPLIDAR* pRPLIDAR, int* pTstandard, int* pTexpress)
{
#ifdef ENABLE_RPLIDAR_SDK_SUPPORT
UNREFERENCED_PARAMETER(pRPLIDAR);
UNREFERENCED_PARAMETER(pTstandard);
UNREFERENCED_PARAMETER(pTexpress);
printf("RPLIDAR : Not implemented. \n");
return EXIT_NOT_IMPLEMENTED;
#else
unsigned char reqbuf[] = {START_FLAG1_RPLIDAR,GET_SAMPLERATE_REQUEST_RPLIDAR};
unsigned char databuf[4];
int DataResponseLength = 0, SendMode = 0, DataType = 0;
// Send request.
if (WriteAllRS232Port(&pRPLIDAR->RS232Port, reqbuf, sizeof(reqbuf)) != EXIT_SUCCESS)
{
printf("Error writing data to a RPLIDAR. \n");
return EXIT_FAILURE;
}
// Receive the response descriptor.
if (GetResponseDescriptorRPLIDAR(pRPLIDAR, &DataResponseLength, &SendMode, &DataType) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
// Quick check of the response descriptor.
if ((DataResponseLength != 4)||(DataType != SAMPLERATE_RESPONSE_RPLIDAR))
{