/
lidars.cpp
288 lines (244 loc) · 5.98 KB
/
lidars.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
#include <wiringSerial.h>
#include <vector>
#include "lidars.hpp"
#ifdef LDLIDAR
void startLidar(int ld) {
}
void stopLidar(int ld) {
}
bool readLidar(int ld, std::vector<PolarPoint> &pointsOut) {
static uint8_t waitMotor = WAITMOTOR;
uint8_t current;
static uint8_t n = 0;
static uint8_t o = 0;
static uint8_t p = 0;
static uint16_t motorSpeed;
static uint16_t startAngle;
static uint16_t distances[NBMEASURESPACK];
static uint8_t confidences[NBMEASURESPACK];
static uint16_t endAngle;
static uint16_t timestamp;
static uint8_t crc = 0;
static std::vector<PolarPoint> points;
static uint16_t oldAngle = 0;
bool done = false;
while(serialDataAvail(ld)) {
current = serialGetchar(ld);
if(n < 46)
crc = LDCRC[crc ^ current];
switch(n) {
case 0:
if(current == 0x54)
n = 1;
break;
case 1:
if(current == 0x2c)
n = 2;
else
n = 0;
break;
case 2:
motorSpeed = current;
n = 3;
break;
case 3:
motorSpeed |= current << 8;
n = 4;
break;
case 4:
startAngle = current;
n = 5;
break;
case 5:
startAngle |= current << 8;
n = 6;
break;
default:
switch(o) {
case 0:
distances[p] = current;
o = 1;
break;
case 1:
distances[p] |= current << 8;
o = 2;
break;
case 2:
confidences[p] = current;
o = 0;
p++;
break;
}
n++;
break;
case 42: // 6 + NBMEASURESPACK * 3
endAngle = current;
n = 43;
break;
case 43: {
endAngle |= current << 8;
n = 44;
} break;
case 44:
timestamp = current;
n = 45;
break;
case 45:
timestamp |= current << 8;
n = 46;
break;
case 46:
if(current == crc) {
uint16_t diff = (endAngle + 36000 - startAngle) % 36000;
for(uint8_t i = 0; i < NBMEASURESPACK; i++) {
if(distances[i] < DISTANCEMIN || confidences[i] < CONFIDENCEMIN)
continue;
uint16_t angle = startAngle + diff * i / (NBMEASURESPACK - 1);
angle = angle * 65536 / 36000;
points.push_back({distances[i], angle});
if(oldAngle > angle && !points.empty()) {
pointsOut = points;
points.clear();
if(waitMotor)
waitMotor--;
else
done = true;
}
oldAngle = angle;
}
}
n = 0;
p = 0;
crc = 0;
break;
}
}
return done;
}
#endif
#ifdef RPLIDAR
void startLidar(int ld) {
serialPutchar(ld, 0xa5);
serialPutchar(ld, 0x82);
serialPutchar(ld, 0x05);
serialPutchar(ld, 0x00);
serialPutchar(ld, 0x00);
serialPutchar(ld, 0x00);
serialPutchar(ld, 0x00);
serialPutchar(ld, 0x00);
serialPutchar(ld, 0x22);
}
void stopLidar(int ld) {
serialPutchar(ld, 0xa5);
serialPutchar(ld, 0x25);
}
bool readLidar(int ld, std::vector<PolarPoint> &pointsOut) {
static uint8_t init = 0;
uint8_t current;
static uint8_t n = 0;
static uint8_t checksum;
static uint8_t sum = 0;
static uint16_t startAngleQ6;
static uint16_t oldStartAngleQ6 = 0;
static int32_t oldAngleBrutQ6 = 0;
static uint8_t deltaAnglesQ3[NBMEASURESCABIN];
static uint16_t distances[NBMEASURESCABIN];
static uint8_t o = 0;
static uint8_t p = 0;
static uint16_t j = 0;
static std::vector<PolarPoint> points;
bool done = false;
while(serialDataAvail(ld)) {
current = serialGetchar(ld);
switch(n) {
case 0: // Début de réception de l'en-tête
if(current >> 4 == 0xA) {
checksum = current & 0xF;
n = 1;
}
break;
case 1:
if(current >> 4 == 0x5) {
checksum |= current << 4;
n = 2;
} else
n = 0;
break;
case 2:
sum ^= current;
startAngleQ6 = current;
n = 3;
break;
case 3: {
sum ^= current;
startAngleQ6 |= (current & 0x7F) << 8;
//bool start = current >> 7; // Fin de réception de l'en-tête
if(init < NBINITS) { // Ne pas calculer pendant la synchronisation ou sans les cabines
init++;
n = 4;
break;
}
uint16_t diffAngleQ6 = startAngleQ6 - oldStartAngleQ6; // Calculer l'angle entre deux mesures de référence
if(oldStartAngleQ6 > startAngleQ6)
diffAngleQ6 += FULLTURNQ6;
int32_t diffAngleTotalQ6 = 0;
for(uint8_t i = 0; i < NBMEASURESCABIN; i++) {
// Calculer l'angle non compensé
int32_t angleBrutQ6 = (oldStartAngleQ6 + diffAngleTotalQ6 / NBMEASURESCABIN) % FULLTURNQ6;
diffAngleTotalQ6 += diffAngleQ6;
if(oldAngleBrutQ6 > angleBrutQ6 && !points.empty()) { // Détection du passage par zéro de l'angle non compensé
pointsOut = points;
points.clear();
done = true;
}
oldAngleBrutQ6 = angleBrutQ6;
if(distances[i] >= DISTANCEMIN) { // Si la lecture est valide
int32_t angle = angleBrutQ6 - (deltaAnglesQ3[i] << 3); // Calculer l'angle compensé
angle = angle * 65536 / FULLTURNQ6; // Remise à l'échelle de l'angle
points.push_back({distances[i], uint16_t(angle)});
}
}
oldStartAngleQ6 = startAngleQ6;
n = 4;
} break;
default: // Début de réception des cabines
sum ^= current;
switch(o) {
case 0:
deltaAnglesQ3[p] = (current & 0b11) << 4;
distances[p] = current >> 2;
o = 1;
break;
case 1:
distances[p] |= current << 6;
o = 2;
break;
case 2:
deltaAnglesQ3[p + 1] = (current & 0b11) << 4;
distances[p + 1] = current >> 2;
o = 3;
break;
case 3:
distances[p + 1] |= current << 6;
o = 4;
break;
case 4:
deltaAnglesQ3[p] |= current & 0b1111;
deltaAnglesQ3[p + 1] |= current >> 4;
o = 0;
p += 2;
if(p == NBMEASURESCABIN) {
if(sum != checksum)
init = NBINITS - 1; // Ne pas faire les calculs pour ces cabines
n = 0;
p = 0;
sum = 0;
}
break;
}
break;
}
}
return done;
}
#endif