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
| #include "OrdLidar.h"
|
| #include "KMachine.h"
|
| int nPosX;
| int nPosY;
| int nPosZ;
|
| int nPosZ1,nPosZ2;
|
| int pCount1=0;
| int pCount2=0;
|
| int eCount1;
| int eCount2;
|
| int dCount1=0;
| int dCount2=0;
|
| int vCount1=0;
| int vCount2=0;
|
| int results[32];
|
|
| void Uart3SendPacket(char * str, int len);
| void Uart5SendPacket(char * str, int len);
|
| //ÕýÏÒ±í£¬0 - 360 ´ú±í 0 - 2PI,½á¹û0 - 1000 ±íʾ0 - 1£¬±íÖÐÖ»ÓÐ0 - PI/2¼´1/4ÖÜÆÚµÄÊý¾Ý£¬ÆäËûµÄÊý¾ÝÓöԳƺ;µÏñµÃµ½¡£
| const int SinTable[] =
| {
| 0,17,35, 52, 70, 87, 105, 122, 139, 156, 174, 191, 208, 225, 242, 259, 276, 292,
| 309, 326, 342, 358, 375, 391, 407, 423, 438, 454, 469, 485, 500, 515, 530, 545,
| 559, 574, 588, 602, 616, 629, 643, 656, 669, 682, 695, 707, 719, 731, 743, 755,
| 766, 777, 788, 799, 809, 819, 829, 839, 848, 857, 866, 875, 883, 891, 899, 906,
| 914, 921, 927, 934, 940, 946, 951, 956, 961, 966, 970, 974, 978, 982, 985, 988,
| 990, 993, 995, 996, 998, 999, 999, 1000, 1000,
| 1000, 999, 999, 998, 996, 995,
| 993, 990, 988, 985, 982, 978, 974, 970, 966, 961, 956, 951, 946, 940, 934, 927,
| 921, 914, 906, 899, 891, 883, 875, 866, 857, 848, 839, 829, 819, 809, 799, 788,
| 777, 766, 755, 743, 731, 719, 707, 695, 682, 669, 656, 643, 629, 616, 602, 588,
| 574, 559, 545, 530, 515, 500, 485, 469, 454, 438, 423, 407, 391, 375, 358, 342,
| 326, 309, 292, 276, 259, 242, 225, 208, 191, 174, 156, 139, 122, 105, 87, 70,
| 52, 35, 17, 0, -17, -35, -52, -70, -87, -105, -122, -139, -156, -174, -191,
| -208, -225, -242, -259, -276, -292, -309, -326, -342, -358, -375, -391, -407,
| -423, -438, -454, -469, -485, -500, -515, -530, -545, -559, -574, -588, -602,
| -616, -629, -643, -656, -669, -682, -695, -707, -719, -731, -743, -755, -766,
| -777, -788, -799, -809, -819, -829, -839, -848, -857, -866, -875, -883, -891,
| -899, -906, -914, -921, -927, -934, -940, -946, -951, -956, -961, -966, -970,
| -974, -978, -982, -985, -988, -990, -993, -995, -996, -998, -999, -999, -1000,
| -1000, -1000, -999, -999, -998, -996, -995, -993, -990, -988, -985, -982, -978,
| -974, -970, -966, -961, -956, -951, -946, -940, -934, -927, -921, -914, -906,
| -899, -891, -883, -875, -866, -857, -848, -839, -829, -819, -809, -799, -788,
| -777, -766, -755, -743, -731, -719, -707, -695, -682, -669, -656, -643, -629,
| -616, -602, -588, -574, -559, -545, -530, -515, -500, -485, -469, -454, -438,
| -423, -407, -391, -375, -358, -342, -326, -309, -292, -276, -259, -242, -225,
| -208, -191, -174, -156, -139, -122, -105, -87, -70, -52, -35, -17
| };
|
|
|
| char StartCMD[2] = { 0xA5,0x60 };
| char StopCMD[2] = { 0xA5,0x65 };
|
|
| int StartAngle;
| int EndAngle;
|
| int OrdLidarStart(int nIdx)
| {
| if (nIdx == 0 || nIdx == -1 ) {
| Uart3SendPacket(StartCMD,sizeof(StartCMD));
| }
| if (nIdx == 1 || nIdx == -1) {
| Uart5SendPacket(StartCMD,sizeof(StartCMD));
| }
| return 0;
| };
|
|
| int OrdLidarStop(int nIdx){
| if (nIdx == 0 || nIdx == -1 ) {
| Uart3SendPacket(StopCMD,sizeof(StopCMD));
| }
| if (nIdx == 1 || nIdx == -1) {
| Uart5SendPacket(StopCMD,sizeof(StopCMD));
| }
| return 0;
| };
|
| char startFlag0 = 0;
| char startFlag1 = 0;
|
| #define VALID_DATA_MAX 400
| stLidarDot validData0[400];
| stLidarDot validData1[400];
|
| int nValidCount0 = 0;
| int nValidCount1 = 0;
|
| #define ORG_SUB_DEGREE 100
| #define SUB_DEGREE 64 //64
|
| #define START_DEGREE 110
| #define END_DEGREE 250
|
| #define MAX_DISTANCE 500 //mm
|
| //ÕûÊýÇóSINÖµ£¬´øÏßÐÔ²åÖµ¹¦ÄÜ£¬ ÊäÈë0 - 360*64 Ϊ 0 - 2PI,Ò»¸öÖÜÆÚ;Êä³ö-1000 ÖÁ +1000,´ú±í-1µ½+1;
| int sini(int a)
| {
| if (a <0) {a += 360 * SUB_DEGREE;}
| a = a % (360 * SUB_DEGREE);
| int b;
| b=a / SUB_DEGREE;
| int xx = a % SUB_DEGREE;
| if (b< 90 )
| {
| int d1= SinTable[b ];
| int d2 =SinTable[b+1 ];
| int d;
| d = ((SUB_DEGREE-xx) * d1 + xx * d2 ) / SUB_DEGREE;
|
| return (d);
| }
| else if (b<180 )
| {
| int c;
| c=180-b;
|
| int d1= SinTable[c ];
| int d2 =SinTable[c-1 ];
| int d;
| d = ((SUB_DEGREE-xx) * d1 + xx * d2 ) / SUB_DEGREE;
|
| return (d);
| }
|
| else if (b<270)
| {
| int c;
| c=b-180;
| int d1= SinTable[c ];
| int d2 =SinTable[c+1 ];
| int d;
| d = ((SUB_DEGREE-xx) * d1 + xx * d2 ) / SUB_DEGREE;
|
| return (-d);
| }
| else
| {
| int c;
| c=360 -b;
| int d1= SinTable[c ];
| int d2 =SinTable[c-1 ];
| int d;
| d = ((SUB_DEGREE-xx) * d1 + xx * d2 ) / SUB_DEGREE;
|
| return (-d);
| }
|
| // return a;
| }
|
| int cosi(int a)
| {
| return sini(a+90*SUB_DEGREE);
| }
|
| int OrdLidarParsePkt(int nLidarIdx, OradarLidarFrame * pLindarPkt, int len1)
| {
| int iRet = 0;
| if (pLindarPkt->header != 0x54) return 0; // check for start sign
| int nDotNum = pLindarPkt->ver_len &0x1f;
| if (len1 != nDotNum*3 +11) return -1;
| if (nDotNum<2) return 0;
|
| int startAngle = (pLindarPkt->start_angle) * SUB_DEGREE / ORG_SUB_DEGREE; // degree * 100;
| int endAngle = (pLindarPkt->end_angle ) * SUB_DEGREE / ORG_SUB_DEGREE; // degree * 100;
| if (startAngle >= endAngle) {eCount1++; return 0;}
| int diffAngle = (endAngle - startAngle);
| int eachAngle = diffAngle / (nDotNum - 1);
|
| if (nLidarIdx == 0)
| {
| pCount1++;
| for (int i = 0;i < nDotNum && i < 40 ;i++)
| {
| unsigned char confidence = pLindarPkt->point[i].confidence;
| int angle = eachAngle * i + startAngle; // degree * 64
| dCount1++;
|
| if (angle < START_DEGREE * SUB_DEGREE ) startFlag0 = 1;
| if (angle >= START_DEGREE * SUB_DEGREE && angle < END_DEGREE * SUB_DEGREE) // &&x>100000&&x<250000)
| {
| if (pLindarPkt->point[i].confidence < 30) continue;
| int value = pLindarPkt->point[i].distance; // distance
|
| int x = value * sini(angle )/1000; // mm
| int z = -value * cosi(angle )/1000; // mm
|
| //vectorX.push_back(x);
| //vectorY.push_back(y);
| validData0[nValidCount0].distance = value;
| validData0[nValidCount0].x = x;
| validData0[nValidCount0].y = z;
| if (nValidCount0 < VALID_DATA_MAX - 1 ) nValidCount0 ++;
| //vCount1++;
| }
|
| if (angle > END_DEGREE * SUB_DEGREE)
| {
| if (startFlag0 == 1 && nValidCount0 > 0)
| {
| vCount1 = nValidCount0;
| ProcessPos(0,validData0,nValidCount0);
|
|
| }
| startFlag0 = 0;
| nValidCount0 = 0;
| }
| }
| }
|
| if (nLidarIdx == 1)
| {
| pCount2++;
| for (int i = 0;i < nDotNum && i < 40 ;i++)
| {
| unsigned char confidence = pLindarPkt->point[i].confidence;
| int angle = eachAngle * i + startAngle;
|
| dCount2++;
|
| if (angle < START_DEGREE * SUB_DEGREE) startFlag1 = 1;
| if (angle >= START_DEGREE * SUB_DEGREE && angle < END_DEGREE * SUB_DEGREE ) // &&x>100000&&x<250000)
| {
| if (confidence < 30) continue;
| int value = pLindarPkt->point[i].distance; // distance
|
| int x = value * sini(angle - 180 * SUB_DEGREE )/1000; // mm
| int y = value * cosi(angle - 180 * SUB_DEGREE )/1000; // mm //vectorX.push_back(x);
| //vectorY.push_back(y);
| validData1[nValidCount1].distance = value;
| validData1[nValidCount1].x = x;
| validData1[nValidCount1].y = y;
| if (nValidCount1 < VALID_DATA_MAX - 1 ) nValidCount1 ++;
| //vCount2 ++;
| }
|
| if (angle > END_DEGREE * SUB_DEGREE)
| {
| if (startFlag1 == 1 && nValidCount1 > 0 )
| {
| vCount2 = nValidCount1;
|
| int minDisIndex = 0; int minDistance = 55555;
| int minZ = 9999; int minZIndex= -1;
| int planeCount =0;
| int firstmin =1000, firstmax = -1000;
| int secondmin = 1000, secondmax = -1000;
|
| int firstCount=0; int secondCount =0; int midCount =0;
|
| // Çó minZ ºÍ minDistance;
| for (int j = 0;j < nValidCount1;j++){
| if (validData1[j].distance < 50) continue; // skip too small points;
| if (validData1[j].y < 40) continue; // skip too small points;
| if (validData1[j].x < -200 || validData1[j].x > 500) continue;
| if (validData1[j].y < minZ) { minZIndex = j; minZ = validData1[j].y; }
| if (validData1[j].distance < minDistance) {
| minDisIndex = j; minDistance = validData1[j].distance;
| }
| }
|
| for (int j = 0;j < nValidCount1 && minZ<500;j++)
| {
| int x = validData1[j].x;
| int y = validData1[j].y;
| int d = validData1[j].distance;
|
| if (d < 50) continue; // skip too small points;
| if (y < 40) continue; // skip too small points;
| if (x < -200 || x > 500) continue;
|
| // Ö»²éÕÒ µ×²¿ 50mm Êý¾Ý
|
| if (y > minZ + 50) {
| if ((firstCount >0 && secondCount ==0)
| || (secondCount >0 && firstCount ==0)) {
| midCount++;
| }
| continue;
| }
| planeCount++;
| if (x > -200 && x < 500)
| {
| if (x < firstmin) firstmin = x;
| if (x > secondmax) secondmax = x;
| }
|
| // ºóÑØ
| if (x > -200 && x < 100)
| {
| if (validData1[j].x > firstmax) firstmax =x;
| firstCount++;
| }
| // Ç°ÑØ
| if (x > 200 && x < 500 )
| {
| if (x < secondmin) secondmin = x;
| secondCount++;
| }
|
| }
| int avg=0;
| if (firstCount > 0 && secondCount > 0 && midCount > 0 ){
| avg = (secondmin + firstmax) / 2;
| }else if (planeCount>10) {
| avg = (firstmin + secondmax) /2;
| }
|
| nPosY = (nPosY *3 + avg) /4;
| if (minZ < 9999) {
| nPosZ2 = (minZ + nPosZ2 *3)/4;
| } else {
| // results[4]= nValidCount1;
| // results[5]= minDistance;
|
| }
| // nPosY = validData1[minDisIndex].x /1000;
| // nPosZ2 = validData1[minDisIndex].x /1000;
| // nPosZ2 = midcount;
| }
| startFlag1 = 0;
| nValidCount1 = 0;
| }
| }
| }
| nPosZ = ((nPosZ1 + nPosZ2) + nPosZ *2)/4;
|
| // nPosX = pCount1;
| // nPosY = pCount2;
| return iRet;
| }
|
|