forked from YDLIDAR/ydlidar_arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YDLidar.cpp
537 lines (416 loc) · 13 KB
/
YDLidar.cpp
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
/*
* YDLIDAR SYSTEM
* YDLIDAR Arduino
*
* Copyright 2015 - 2018 EAI TEAM
* http://www.eaibot.com
*
*/
#include "YDLidar.h"
#ifdef AI_CUSTOMIZED_LIB
#include "ComStr.h"
#include "TickTime.hpp"
#include <cmath>
#include "Debug.hpp"
#define millis() TickTime::getTimeMs()
#endif // #ifdef AI_CUSTOMIZED_LIB
YDLidar::YDLidar()
: _bined_serialdev(NULL) {
point.distance = 0;
point.angle = 0;
point.quality = 0;
}
YDLidar::~YDLidar() {
end();
}
// open the given serial interface and try to connect to the YDLIDAR
bool YDLidar::begin(HardwareSerial &serialobj, uint32_t baudrate) {
if (isOpen()) {
end();
}
_bined_serialdev = &serialobj;
_bined_serialdev->end();
_bined_serialdev->begin(baudrate);
return true;
}
// close the currently opened serial interface
void YDLidar::end(void) {
if (isOpen()) {
_bined_serialdev->end();
_bined_serialdev = NULL;
}
}
// check whether the serial interface is opened
bool YDLidar::isOpen(void) {
return _bined_serialdev ? true : false;
}
// ask the YDLIDAR for its device health
result_t YDLidar::getHealth(device_health &health, uint32_t timeout) {
result_t ans;
uint8_t recvPos = 0;
uint32_t currentTs = millis();
uint32_t remainingtime;
uint8_t *infobuf = (uint8_t *)&health;
lidar_ans_header response_header;
if (!isOpen()) {
return RESULT_FAIL;
}
{
ans = sendCommand(LIDAR_CMD_GET_DEVICE_HEALTH, NULL, 0);
if (ans != RESULT_OK) {
return ans;
}
if ((ans = waitResponseHeader(&response_header, timeout)) != RESULT_OK) {
return ans;
}
if (response_header.type != LIDAR_ANS_TYPE_DEVHEALTH) {
return RESULT_FAIL;
}
if (response_header.size < sizeof(device_health)) {
return RESULT_FAIL;
}
while ((remainingtime = millis() - currentTs) <= timeout) {
int currentbyte = _bined_serialdev->read();
if (currentbyte < 0) {
continue;
}
infobuf[recvPos++] = currentbyte;
if (recvPos == sizeof(device_health)) {
return RESULT_OK;
}
}
}
return RESULT_TIMEOUT;
}
// ask the YDLIDAR for its device info
result_t YDLidar::getDeviceInfo(device_info &info, uint32_t timeout) {
result_t ans;
uint8_t recvPos = 0;
uint32_t currentTs = millis();
uint32_t remainingtime;
uint8_t *infobuf = (uint8_t *)&info;
lidar_ans_header response_header;
if (!isOpen()) {
return RESULT_FAIL;
}
{
ans = sendCommand(LIDAR_CMD_GET_DEVICE_INFO, NULL, 0);
if (ans != RESULT_OK) {
return ans;
}
if ((ans = waitResponseHeader(&response_header, timeout)) != RESULT_OK) {
return ans;
}
if (response_header.type != LIDAR_ANS_TYPE_DEVINFO) {
return RESULT_FAIL;
}
if (response_header.size < sizeof(lidar_ans_header)) {
return RESULT_FAIL;
}
while ((remainingtime = millis() - currentTs) <= timeout) {
int currentbyte = _bined_serialdev->read();
if (currentbyte < 0) {
continue;
}
infobuf[recvPos++] = currentbyte;
if (recvPos == sizeof(device_info)) {
return RESULT_OK;
}
}
}
return RESULT_TIMEOUT;
}
// stop the scanPoint operation
result_t YDLidar::stop(void) {
if (!isOpen()) {
return RESULT_FAIL;
}
result_t ans = sendCommand(LIDAR_CMD_FORCE_STOP, NULL, 0);
return ans;
}
// start the scanPoint operation
result_t YDLidar::startScan(bool force, uint32_t timeout) {
result_t ans;
if (!isOpen()) {
return RESULT_FAIL;
}
stop(); //force the previous operation to stop
{
if ((ans = sendCommand(force ? LIDAR_CMD_FORCE_SCAN : LIDAR_CMD_SCAN, NULL, 0)) != RESULT_OK) {
return ans;
}
lidar_ans_header response_header;
if ((ans = waitResponseHeader(&response_header, timeout)) != RESULT_OK) {
return ans;
}
if (response_header.type != LIDAR_ANS_TYPE_MEASUREMENT) {
return RESULT_FAIL;
}
if (response_header.size < sizeof(node_info)) {
return RESULT_FAIL;
}
}
return RESULT_OK;
}
// wait scan data
result_t YDLidar::waitScanDot(uint32_t timeout) {
int recvPos = 0;
uint32_t startTs = millis();
uint32_t waitTime;
uint8_t nowPackageNum;
node_info node;
static node_package package;
static uint16_t package_Sample_Index = 0;
static float IntervalSampleAngle = 0;
static float IntervalSampleAngle_LastPackage = 0;
static uint16_t FirstSampleAngle = 0;
static uint16_t LastSampleAngle = 0;
static uint16_t CheckSum = 0;
static uint16_t CheckSumCal = 0;
static uint16_t SampleNumlAndCTCal = 0;
static uint16_t LastSampleAngleCal = 0;
static bool CheckSumResult = true;
static uint16_t Valu8Tou16 = 0;
uint8_t *packageBuffer = (uint8_t *)&package.package_Head;
uint8_t package_Sample_Num = 0;
int32_t AngleCorrectForDistance;
int package_recvPos = 0;
if (package_Sample_Index == 0) {
recvPos = 0;
while ((waitTime = millis() - startTs) <= timeout) {
int currentByte = _bined_serialdev->read();
if (currentByte < 0) {
continue;
}
switch (recvPos) {
case 0:
if (currentByte != (PH & 0xFF)) {
continue;
}
break;
case 1:
CheckSumCal = PH;
if (currentByte != (PH >> 8)) {
recvPos = 0;
continue;
}
break;
case 2:
SampleNumlAndCTCal = currentByte;
if (((currentByte&0x01) != CT_Normal) && ((currentByte & 0x01) != CT_RingStart)) {
recvPos = 0;
continue;
}
break;
case 3:
SampleNumlAndCTCal += (currentByte << LIDAR_RESP_MEASUREMENT_ANGLE_SAMPLE_SHIFT);
package_Sample_Num = currentByte;
break;
case 4:
if (currentByte & LIDAR_RESP_MEASUREMENT_CHECKBIT) {
FirstSampleAngle = currentByte;
} else {
recvPos = 0;
continue;
}
break;
case 5:
FirstSampleAngle += (currentByte << LIDAR_RESP_MEASUREMENT_ANGLE_SAMPLE_SHIFT);
CheckSumCal ^= FirstSampleAngle;
FirstSampleAngle = FirstSampleAngle >> 1;
break;
case 6:
if (currentByte & LIDAR_RESP_MEASUREMENT_CHECKBIT) {
LastSampleAngle = currentByte;
} else {
recvPos = 0;
continue;
}
break;
case 7:
LastSampleAngle += (currentByte << LIDAR_RESP_MEASUREMENT_ANGLE_SAMPLE_SHIFT);
LastSampleAngleCal = LastSampleAngle;
LastSampleAngle = LastSampleAngle >> 1;
if (package_Sample_Num == 1) {
IntervalSampleAngle = 0;
} else {
if (LastSampleAngle < FirstSampleAngle) {
if ((FirstSampleAngle > 17280) && (LastSampleAngle < 5760)) {
IntervalSampleAngle = ((float)(23040 + LastSampleAngle - FirstSampleAngle)) /
(package_Sample_Num - 1);
IntervalSampleAngle_LastPackage = IntervalSampleAngle;
} else {
IntervalSampleAngle = IntervalSampleAngle_LastPackage;
}
} else {
IntervalSampleAngle = ((float)(LastSampleAngle - FirstSampleAngle)) / (package_Sample_Num - 1);
IntervalSampleAngle_LastPackage = IntervalSampleAngle;
}
}
break;
case 8:
CheckSum = currentByte;
break;
case 9:
CheckSum += (currentByte << LIDAR_RESP_MEASUREMENT_ANGLE_SAMPLE_SHIFT);
break;
#ifdef AI_CUSTOMIZED_LIB // need a default to pass -Wswitch-default
default:
PLATFORM_WARNING("%s %s line %d\n", ComStr::cInvalidCase, __FILE__, __LINE__);
break;
#endif // #ifdef AI_CUSTOMIZED_LIB
}
packageBuffer[recvPos++] = currentByte;
if (recvPos == PackagePaidBytes) {
package_recvPos = recvPos;
break;
}
}
if (PackagePaidBytes == recvPos) {
startTs = millis();
recvPos = 0;
int package_sample_sum = package_Sample_Num << 1;
while ((waitTime = millis() - startTs) <= timeout) {
int currentByte = _bined_serialdev->read();
if (currentByte < 0) {
continue;
}
if ((recvPos & 1) == 1) {
Valu8Tou16 += (currentByte << LIDAR_RESP_MEASUREMENT_ANGLE_SAMPLE_SHIFT);
CheckSumCal ^= Valu8Tou16;
} else {
Valu8Tou16 = currentByte;
}
packageBuffer[package_recvPos + recvPos] = currentByte;
recvPos++;
if (package_sample_sum == recvPos) {
package_recvPos += recvPos;
break;
}
}
if (package_sample_sum != recvPos) {
return RESULT_FAIL;
}
} else {
return RESULT_FAIL;
}
CheckSumCal ^= SampleNumlAndCTCal;
CheckSumCal ^= LastSampleAngleCal;
if (CheckSumCal != CheckSum) {
CheckSumResult = false;
} else {
CheckSumResult = true;
}
}
uint8_t package_CT;
package_CT = package.package_CT;
if ((package_CT&0x01) == CT_Normal) {
node.sync_quality = Node_Default_Quality + Node_NotSync;
} else {
node.sync_quality = Node_Default_Quality + Node_Sync;
}
if (CheckSumResult == true) {
node.distance_q2 = package.packageSampleDistance[package_Sample_Index];
if (node.distance_q2 / 4 != 0) {
#ifdef AI_CUSTOMIZED_LIB // to suppress -Wdouble-promotion
AngleCorrectForDistance = (int32_t)((atanf(((21.8f * (155.3f - (node.distance_q2 * 0.25f))) /
155.3f) / (node.distance_q2 * 0.25f))) * 3666.93f);
#else
AngleCorrectForDistance = (int32_t)((atan(((21.8 * (155.3 - (node.distance_q2 * 0.25f))) /
155.3) / (node.distance_q2 * 0.25f))) * 3666.93);
#endif // #ifdef AI_CUSTOMIZED_LIB
} else {
AngleCorrectForDistance = 0;
}
float sampleAngle = IntervalSampleAngle * package_Sample_Index;
if ((FirstSampleAngle + sampleAngle + AngleCorrectForDistance) < 0) {
node.angle_q6_checkbit = (((uint16_t)(FirstSampleAngle + sampleAngle + AngleCorrectForDistance +
23040)) << LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) + LIDAR_RESP_MEASUREMENT_CHECKBIT;
} else {
if ((FirstSampleAngle + sampleAngle + AngleCorrectForDistance) > 23040) {
node.angle_q6_checkbit = ((uint16_t)((FirstSampleAngle + sampleAngle + AngleCorrectForDistance -
23040)) << LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) + LIDAR_RESP_MEASUREMENT_CHECKBIT;
} else {
node.angle_q6_checkbit = ((uint16_t)((FirstSampleAngle + sampleAngle + AngleCorrectForDistance)) <<
LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) + LIDAR_RESP_MEASUREMENT_CHECKBIT;
}
}
} else {
node.sync_quality = Node_Default_Quality + Node_NotSync;
node.angle_q6_checkbit = LIDAR_RESP_MEASUREMENT_CHECKBIT;
node.distance_q2 = 0;
package_Sample_Index = 0;
return RESULT_FAIL;
}
point.distance = node.distance_q2 * 0.25f;
point.angle = (node.angle_q6_checkbit >> LIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) / 64.0f;
point.quality = (node.sync_quality >> LIDAR_RESP_MEASUREMENT_QUALITY_SHIFT);
point.startBit = (node.sync_quality & LIDAR_RESP_MEASUREMENT_SYNCBIT);
package_Sample_Index++;
nowPackageNum = package.nowPackageNum;
if (package_Sample_Index >= nowPackageNum) {
package_Sample_Index = 0;
}
return RESULT_OK;
}
//send data to serial
result_t YDLidar::sendCommand(uint8_t cmd, const void *payload, size_t payloadsize) {
cmd_packet pkt_header;
cmd_packet *header = &pkt_header;
uint8_t checksum = 0;
if (payloadsize && payload) {
cmd |= LIDAR_CMDFLAG_HAS_PAYLOAD;
}
header->syncByte = LIDAR_CMD_SYNC_BYTE;
header->cmd_flag = cmd & 0xff;
_bined_serialdev->write((uint8_t *)header, 2) ;
if ((cmd & LIDAR_CMDFLAG_HAS_PAYLOAD)) {
checksum ^= LIDAR_CMD_SYNC_BYTE;
checksum ^= (cmd & 0xff);
checksum ^= (payloadsize & 0xFF);
for (size_t pos = 0; pos < payloadsize; ++pos) {
checksum ^= ((uint8_t *)payload)[pos];
}
uint8_t sizebyte = payloadsize;
_bined_serialdev->write(&sizebyte, 1);
_bined_serialdev->write((const uint8_t *)payload, sizebyte);
_bined_serialdev->write(&checksum, 1);
}
return RESULT_OK;
}
// wait response header
result_t YDLidar::waitResponseHeader(lidar_ans_header *header, uint32_t timeout) {
int recvPos = 0;
uint32_t startTs = millis();
uint8_t *headerBuffer = (uint8_t *)(header);
uint32_t waitTime;
while ((waitTime = millis() - startTs) <= timeout) {
int currentbyte = _bined_serialdev->read();
if (currentbyte < 0) {
continue;
}
switch (recvPos) {
case 0:
if (currentbyte != LIDAR_ANS_SYNC_BYTE1) {
continue;
}
break;
case 1:
if (currentbyte != LIDAR_ANS_SYNC_BYTE2) {
recvPos = 0;
continue;
}
break;
#ifdef AI_CUSTOMIZED_LIB // need a default to pass -Wswitch-default
default:
PLATFORM_WARNING("%s %s line %d\n", ComStr::cInvalidCase, __FILE__, __LINE__);
break;
#endif // #ifdef AI_CUSTOMIZED_LIB
}
headerBuffer[recvPos++] = currentbyte;
if (recvPos == sizeof(lidar_ans_header)) {
return RESULT_OK;
}
}
return RESULT_TIMEOUT;
}