QRcode.c
15.9 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
#include "QRcode.h"
KincoStructInfo KincoStruct1,KincoStruct2;
IMUdata IMU;
LifterStructInfo Lifter1;
RotateStructInfo Rotate1;
#if P_SETUP_NAV_TYPE==1
/******二维码坐标更新*******/
void CameraDataUpdate()
{
CurrentCenterPoint.TarX = Camera.XCoordingData + navi.Public.DistanceAgvToSTART; //X补偿
CurrentCenterPoint.TarY = Camera.YCoordingData + navi.Public.CenterOffset; //Y补偿
CurrentCenterPoint.CurAngle = Camera.angle + agv.Command.W_compensation; //W补偿
if(CurrentCenterPoint.CurAngle > PI)
{
CurrentCenterPoint.CurAngle -= 2*PI;
}
else if(CurrentCenterPoint.CurAngle < -PI)
{
CurrentCenterPoint.CurAngle += 2*PI;
}
}
unsigned char ArriveJugement()//到达判断
{
static u8 lastStopFlagL = 0,lastStopFlagR = 0;
if(Camera.XCoordingData == TargetPoint.TarX&&Camera.YCoordingData == TargetPoint.TarY)//识别到二维码停车
{
if (fabs(Camera.X_pixel) < POSARRIVE_RANGE)
{
return 1;
}
else
{
return 0;
}
}
return 0;
}
//更新执行站点
void pathUpdate(u8 *CurrentID)
{
static int lastTime = 0;
static float lastAnglediff = 0;
/*****************到达站点判断以及切换站点**********************/
if (agv.Command.CurDirection == 1 || agv.Command.CurDirection == 2) //直行判断
{
//到达站点判断
if (ArriveJugement()==1)
{
*CurrentID += 1;
navi.Public.DistanceAgvToSTART = 0;
/****单机测试***/
#if P_SETUP_SINGLE_PLAYER == 1
if (*CurrentID >= traffic_land_marks.size)
{
*CurrentID = 1;
traffic_land_marks.land_marks[0].Direction = 3;
traffic_land_marks.land_marks[1].Direction = 3;
traffic_land_marks.land_marks[2].Direction = 3;
traffic_land_marks.land_marks[3].Direction = 4;
}
// }
#elif P_SETUP_SINGLE_PLAYER == 0
if (*CurrentID >= traffic_land_marks.size)
{
if(Lifter1.liftState == 1||CameraTwo.UpdataFlag == 0)
{
SetAlarm(0x200);
agv.Public.i_UpdatePathSig = 1;
}
else if(Lifter1.liftState == -1)
{
SetAlarm(0x020);//货架纠偏
agv.Public.i_PlatformStopFlag = 1;
}
}
#endif
Uart_Printf(COM1, "站点更新,当前站点 = %d 当前方向 = %d Error_Flag = %x\r\n", agv.Command.standSiteID, agv.Command.CurDirection, agv.Public.Error_Flag);
}
lastAnglediff = 0;
}
else if (agv.Command.CurDirection == 7 || agv.Command.CurDirection == 8 || agv.Command.CurDirection == 9 || agv.Command.CurDirection == 10) //弧线转弯
{
//站点更新
if ((fabs(navi.Public.AngleDifference) <= TURNINGOFF_ANGLE)||(navi.Public.AngleDifference*lastAnglediff<0))
{
*CurrentID += 1;
}
lastAnglediff = navi.Public.AngleDifference;
}
agv.Command.LandMarkID = traffic_land_marks.land_marks[*CurrentID - 1].LandMarkID;
agv.Command.DispatchSpeed = traffic_land_marks.land_marks[*CurrentID - 1].DispatchSpeed; //系统下发速度
agv.Command.CurDirection = traffic_land_marks.land_marks[*CurrentID - 1].Direction; //当前站点运行方向
agv.Command.LaserArea = traffic_land_marks.land_marks[*CurrentID - 1].LaserArea;
if (*CurrentID < traffic_land_marks.size) //当前站点不是最后站点,保存下一个站点方向和光电停车信号
{
agv.Command.LightStopSig = traffic_land_marks.land_marks[*CurrentID].LightStopSig; //目标站点广电信号
agv.Command.NextDirection = traffic_land_marks.land_marks[*CurrentID].Direction; //下个站点运行方向
agv.Command.NextDispatchSpeed = traffic_land_marks.land_marks[*CurrentID].DispatchSpeed;
}
else //已经是最后一个站点,没有下一个站点清零
{
agv.Command.LightStopSig = 0;
agv.Command.NextDirection = 0;
agv.Command.NextDispatchSpeed = 0;
}
StartPoint.TarX = traffic_land_marks.land_marks[*CurrentID - 1].pose[0]; //当前点(起点)
StartPoint.TarY = traffic_land_marks.land_marks[*CurrentID - 1].pose[1];
TargetPoint.TarX = traffic_land_marks.land_marks[*CurrentID].pose[0]; //目标点(终点)
TargetPoint.TarY = traffic_land_marks.land_marks[*CurrentID].pose[1];
if(StartPoint.TarX == TargetPoint.TarX)
{
if(TargetPoint.TarY > StartPoint.TarY)
{
agv.Command.RunState = 1;
}
else
agv.Command.RunState = 3;
}
else if(StartPoint.TarY == TargetPoint.TarY)
{
if(TargetPoint.TarX > StartPoint.TarX)
{
agv.Command.RunState = 0;
}
else
agv.Command.RunState = 2;
}
if (*CurrentID >= 2 && *CurrentID < traffic_land_marks.size - 1) //实现弧线转弯需要有四个点位计算
{
PointOne.TarX = traffic_land_marks.land_marks[*CurrentID - 2].pose[0];
PointOne.TarY = traffic_land_marks.land_marks[*CurrentID - 2].pose[1];
PointTwo.TarX = traffic_land_marks.land_marks[*CurrentID - 1].pose[0];
PointTwo.TarY = traffic_land_marks.land_marks[*CurrentID - 1].pose[1];
PointThree.TarX = traffic_land_marks.land_marks[*CurrentID].pose[0];
PointThree.TarY = traffic_land_marks.land_marks[*CurrentID].pose[1];
PointFour.TarX = traffic_land_marks.land_marks[*CurrentID + 1].pose[0];
PointFour.TarY = traffic_land_marks.land_marks[*CurrentID + 1].pose[1];
}
}
void getPosAngleOffset(float *PosOffset,float *Theta)
{
double Vx = (KincoStruct1.encodeSpeed+KincoStruct2.encodeSpeed)/2*1;//实际线速度
static int lastTime = 0,lastTime2 = 0;
static int i = 0;
*Theta = IMU.angle + IMU.angleCompensation;
if(*Theta > 2*PI)
{
*Theta -= 2*PI;
}
else if(*Theta < -2*PI)
{
*Theta += 2*PI;
}
if(Camera.UpdataFlag == 1)//有码
{
IMU.angleCompensation = Camera.angle - IMU.angle;
// if(agv.Command.RunState == 2 || agv.Command.RunState == 0)
// {
// if(agv.Command.SetBaseSpeed > 500)
// {
// if(agv.Command.RunState == 2)
// {
// *PosOffset = Camera.Y_pixel*1;
// }
// else if(agv.Command.RunState == 0)
// {
// *PosOffset = Camera.Y_pixel*1 + 5;
// }
// else if(agv.Command.RunState == 3)
// {
// *PosOffset = Camera.Y_pixel*1 + 5;
// }
// else
{
*PosOffset = Camera.Y_pixel*1;// + 5;
}
// }
// else
// *PosOffset = Camera.Y_pixel*1;
// }
// else
// {
// *PosOffset = Camera.Y_pixel*1+13;
// }
if(i++ >20)
{
i = 0;
Uart_Printf(COM1,"速度 = %.2f %.2f Vx = %.2f 角速度 = %.5f 行走距离 = %.2f 位置偏移 = %.2f 角度 = %.2f 惯导角度 = %.2f 距离目标 = %.2f\r\n",
KincoStruct1.encodeSpeed,KincoStruct2.encodeSpeed,Vx,IMU.palstance,navi.Public.DistanceAgvToSTART,*PosOffset,*Theta*57.3,IMU.angle*57.3,navi.Public.DistanceAgvToTARGET);
}
}
else//无码
{
if(agv.Public.SystemTime - lastTime >= 20)//20Ms积分一次
{
lastTime = agv.Public.SystemTime;
CalculateXBias(Vx,0,IMU.palstance,0.02,0,PosOffset,Theta);
Uart_Printf(COM1,"速度 = %.2f %.2f 角速度 = %.5f 行走距离 = %.2f 位置偏移 = %.2f 角度 = %.2f 惯导角度 = %.2f 距离目标 = %.2f\r\n",
KincoStruct1.encodeSpeed,KincoStruct2.encodeSpeed,IMU.palstance,navi.Public.DistanceAgvToSTART,*PosOffset,*Theta*57.3,IMU.angle*57.3,navi.Public.DistanceAgvToTARGET);
}
}
if((agv.Public.SystemTime - lastTime2 >= 30))
{
lastTime2 = agv.Public.SystemTime;
CalculateXBias(Vx,0,IMU.palstance,0.03,&navi.Public.DistanceAgvToSTART,0,Theta);
}
// if(i ++ > 10)
// {
// i = 0;
// Uart_Printf(COM1,"速度 = %.2f %.2f Vx = %.2f 角速度 = %.5f 行走距离 = %.2f 位置偏移 = %.2f 角度 = %.2f 惯导角度 = %.2f\r\n",
// KincoStruct1.encodeSpeed,KincoStruct2.encodeSpeed,Vx,IMU.palstance,navi.Public.DistanceAgvToSTART,*PosOffset,*Theta*57.3,IMU.angle*57.3);
// }
}
//计算偏移量 设置不同运动方式的pid值
void calculateOffsetValue(unsigned char Direction)
{
if (Direction == 7 || Direction == 8 || Direction == 9 || Direction == 10) //转弯需要计算圆心坐标
{
CircleCenterPoint = GetCircleCenterPoint(CurrentCenterPoint,PointThree,PointFour); //计算圆心坐标
navi.Private.SetCalculationRadius = TwoPointDistance(PointTwo, CircleCenterPoint); //入弯点到圆心的距离
}
// getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
switch (Direction)
{
case 0:
break;
case 1: //前进
getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
navi.Private.TarAngle = CalculatingDirectionAngle(StartPoint, TargetPoint); //求出目标方向角度
navi.Public.AngleDifference = CalculatingCurrentAndTargetAngle(CurrentCenterPoint.CurAngle, navi.Private.TarAngle); //角度偏移
// navi.Public.CenterOffset = -getPosAngleOffset(); //车中心横向偏移量
//根据不同车型和不同方向设置pid参数
if(agv.Command.SetBaseSpeed >= 500)
{
pid = navi.PIDPara[0];
}
else if(agv.Command.SetBaseSpeed <500 && agv.Command.SetBaseSpeed >= 200)
{
pid = navi.PIDPara[1];
}
else
{
pid = navi.PIDPara[2];
}
if (navi.Public.DistanceAgvToTARGET < agv.Parameter.OnlyAnglePos) //小于500MM只纠角度
{
pid = navi.PIDPara[3];
}
break;
case 2: //后退
getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
navi.Private.TarAngle = CalculatingDirectionAngle(StartPoint, TargetPoint); //求出目标方向角度
navi.Public.AngleDifference = CalculatingCurrentAndTargetAngle(CurrentCenterPoint.CurAngle, navi.Private.TarAngle); //角度偏移
// navi.Public.CenterOffset = getPosAngleOffset(); //车中心横向偏移量
pid = navi.PIDPara[1];
// if (navi.Public.VerticalDistanceAgvToTARGET < agv.Parameter.OnlyAnglePos) //小于500MM只纠角度
// {
// pid = navi.PIDPara[3];
// pid.AngleCofficient *= -1;
// }
break;
case 3:
getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
break;
case 4:
getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
break;
case 5:
getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
break;
case 6:
getPosAngleOffset(&navi.Public.CenterOffset,&CurrentCenterPoint.CurAngle);//获取当前角度和位置偏移量
break;
default:
break;
}
}
//根据P值,I值,D值,位置偏差权重,角度偏差权重,输出偏移值
void offsetCompensationOutput(navigationPID *Pid)
{
static int i = 0;
static float LastCenterOffset = 0, LastAngleOffset = 0;
static float Error = 0, DcalError = 0, LastError = 0, SumError = 0;
//当位置和角度偏差变化重新计算
if (LastCenterOffset != navi.Public.CenterOffset || LastAngleOffset != navi.Public.AngleDifference)
{
LastCenterOffset = navi.Public.CenterOffset;
LastAngleOffset = navi.Public.AngleDifference;
//取角度和位置偏差做为偏差补偿量
Error = navi.Public.CenterOffset * Pid->PosCofficient + navi.Public.AngleDifference * 180 / PI * Pid->AngleCofficient;
DcalError = Error - LastError; //误差变化量
LastError = Error; //记录上次偏差
}
navi.Private.OutputOffset = Error * Pid->Kp + SumError * Pid->Ki + DcalError * Pid->Kd;
if (navi.Private.OutputOffset >= Pid->MaxLimit)
{
navi.Private.OutputOffset = Pid->MaxLimit;
}
else if (navi.Private.OutputOffset <= -Pid->MaxLimit)
{
navi.Private.OutputOffset = -Pid->MaxLimit;
}
#if PRINT_TYPE_SELECT == 1
if (i++ > PRINT_FREQUENCY)
{
i = 0;
// Uart_Printf(COM1,"当前方向 = %d,角度偏差 = %.2f,位置偏差 = %.2f,输出补偿值 = %.2f\r\n",
// agv.Command.CurDirection,navi.Public.AngleDifference*57.3,navi.Public.CenterOffset,navi.Private.OutputOffset);
// Uart_Printf(COM1, "X = %d Y = %d, 当前角度 = %.2f 目标角度 = %.2f 角度偏差 = %.2f,位置偏差 = %.2f,输出补偿值 = %.2f\r\n",
// XCoordingData, YCoordingData,
// CurrentCenterPoint.CurAngle,navi.Private.TarAngle,
// navi.Public.AngleDifference * 57.3, navi.Public.CenterOffset, navi.Private.OutputOffset);
}
#endif
}
float CalculateDiffAngle(float A,float B)
{
static float angle = 0;
if(fabs(A - B) > 180)
{
// if(A > B)
// angle = fabs(A - B - 360);
// else if(A < B)
angle = fabs(fabs(A - B) - 360);
}
else
angle = fabs(A - B);
return angle;
}
float judgeOffset = 8,controlOffset = 3;//一个是判断误差,一个是控制误差
void LookforPlatformPos()
{
static u8 StepFlag = 1,SelectFlag = 1;
if((fabs(CameraTwo.X_pixel) < judgeOffset && fabs(CameraTwo.Y_pixel) < judgeOffset) && SelectFlag == 1)//误差允许范围之内
{
Reset_Alarm(0x020);
SetAlarm(0x200);
agv.Public.i_UpdatePathSig = 1;
agv.Public.i_PlatformStopFlag = 0;
}
else
{
SelectFlag = 0;//关闭判断到位标志,开始纠偏
if(StepFlag == 1)//第一步纠正前后方向
{
if(CameraTwo.X_pixel > controlOffset)//超过允许误差
{
//后退
agv.Command.AGVControlState = 1;
}
else if(CameraTwo.X_pixel < - controlOffset)
{
//前进
agv.Command.AGVControlState = 2;
}
else
{
agv.Command.AGVControlState = 0;
StepFlag = 2;
}
CameraTwo.lastCurAngle = CalculateCurAngle(CameraTwo.angle);//当前处于哪个轴
}
else if(StepFlag == 2)//第二步左转
{
agv.Command.AGVControlState = 3;//开始转弯
if(fabs(CalculateDiffAngle(CameraTwo.angle,CameraTwo.lastCurAngle) - 90) < 0.5)//转到位
{
agv.Command.AGVControlState = 0;
CameraTwo.lastCurAngle = CalculateCurAngle(CameraTwo.angle);
StepFlag = 3;
}
}
else if(StepFlag == 3)//第三步纠正前后方向
{
if(CameraTwo.X_pixel > controlOffset)//超过允许误差
{
//后退
agv.Command.AGVControlState = 1;
}
else if(CameraTwo.X_pixel < -controlOffset)
{
//前进
agv.Command.AGVControlState = 2;
}
else
{
agv.Command.AGVControlState = 0;
StepFlag = 4;
}
}
else if(StepFlag == 4)//第四步回正
{
agv.Command.AGVControlState = 4;//开始转弯
if(fabs(CalculateDiffAngle(CameraTwo.angle,CameraTwo.lastCurAngle) - 90) < 0.5)//转到位
{
agv.Command.AGVControlState = 0;
CameraTwo.lastCurAngle = CalculateCurAngle(CameraTwo.angle);
StepFlag = 1;
SelectFlag = 1;
}
}
}
}
void LookforPlatformaSetSpeed()
{
static float diffAngle = 0,SpeedValue = 0;
diffAngle = fabs(CalculateDiffAngle(CameraTwo.angle,CameraTwo.lastCurAngle) - 90);
switch (agv.Command.AGVControlState)
{
case 0x00:
KincoStruct1.setSpeed = 0;
KincoStruct2.setSpeed = 0;
break;
case 0x01: //前进
KincoStruct1.setSpeed = 5;
KincoStruct2.setSpeed = 5;
break;
case 0x02: //后退
KincoStruct1.setSpeed = -5;
KincoStruct2.setSpeed = -5;
break;
case 0x03: //左转
if(diffAngle > 30)
{
KincoStruct1.setSpeed = -agv.Parameter.HandTurnSpeed;
KincoStruct2.setSpeed = agv.Parameter.HandTurnSpeed;
}
else if(diffAngle > 0.5)
{
SpeedValue = mapping(diffAngle,0.5,30,20,agv.Parameter.HandTurnSpeed);
KincoStruct1.setSpeed = -SpeedValue;
KincoStruct2.setSpeed = SpeedValue;
}
else
{
KincoStruct1.setSpeed = 0;
KincoStruct2.setSpeed = 0;
}
break;
case 0x04: //右转
if(diffAngle > 30)
{
KincoStruct1.setSpeed = agv.Parameter.HandTurnSpeed;
KincoStruct2.setSpeed = -agv.Parameter.HandTurnSpeed;
}
else if(diffAngle > 0.5)
{
SpeedValue = mapping(diffAngle,0.5,30,20,agv.Parameter.HandTurnSpeed);
KincoStruct1.setSpeed = SpeedValue;
KincoStruct2.setSpeed = -SpeedValue;
}
else
{
KincoStruct1.setSpeed = 0;
KincoStruct2.setSpeed = 0;
}
break;
}
}
void QRcodeNavigation()
{
CameraDataUpdate();
pathUpdate(&agv.Command.standSiteID); //执行路径更新 和站点++判断。
calculateOffsetValue(agv.Command.CurDirection); //计算偏移量和设置不同运动方式的pid值
offsetCompensationOutput(&pid); //计算处理得到补偿值***********
// CalculateDistance();
if(agv.Public.i_PlatformStopFlag)//寻找货架二维码中心位置
{
LookforPlatformPos();
if(!(agv.Public.Error_Flag & 0x0100))//不是急停情况下
{
LookforPlatformaSetSpeed();//货架纠偏速度发送
}
}
}
#endif