KIVA.c
14.3 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
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
#include "KIVA.h"
#if P_SETUP_PLATFORM_TYPE == 5
void initPlaformParam(void) //参数初始化
{
// Uart_Printf(COM1, "参数初始化\r\n");
// Y9(0); // 关闭agv自动充电触点
// agv.Command.standSiteID = 1;
// agv.Command.Au_Hand = 0;
// agv.Public.i_StartFirst = false;
// agv.Public.i_UpdatePathSig = 1;
// DriverMotor1.Command.speedSlope = 5;
// DriverMotor2.Command.speedSlope = 5;
// DriverLifter1.Command.speedSlope = 50;
// DriverShifter1.Command.speedSlope = 50;
// #if P_SETUP_SINGLE_PLAYER == 0 //0不开启单机测试 1开启
// SetAlarm(0x0040);
// #endif
}
//在静止下计算,在运动中补偿
float CalculateAngleOffset(float curAngle)
{
static float allowOffset = 20;
int dir = 1;//
float angle = 0;
if(fabs(curAngle) < allowOffset)
{
if(curAngle > 0)//左偏
{
dir = 1;
angle = fabs(curAngle);
}
else//右偏
{
dir = -1;
angle = fabs(curAngle);
}
}
else if(fabs(curAngle - 90) < allowOffset)
{
if(curAngle > 90)//左偏
{
dir = 1;
angle = fabs(curAngle - 90);
}
else//右偏
{
dir = -1;
angle = fabs(curAngle - 90);
}
}
else if(fabs(fabs(curAngle) - 180) < allowOffset)
{
if(curAngle > 0)//右偏
{
dir = -1;
angle = fabs(fabs(curAngle) - 180);
}
else//左偏
{
dir = 1;
angle = fabs(fabs(curAngle) - 180);
}
}
else if(fabs(curAngle + 90) < allowOffset)
{
if(curAngle > -90)//左偏
{
dir = 1;
angle = fabs(curAngle + 90);
}
else//右偏
{
dir = -1;
angle = fabs(curAngle + 90);
}
}
return dir*angle;
}
u8 liftFirstToZero1 = 1;
void lifterRunAuto() //举升自动运行,运行状态0不动 1上升 2下降
{
switch (Lifter1.runState)
{
case 0:
Lifter1.setSpeed = 0;
break;
case 1:
Lifter1.setSpeed = 2000000;
break;
case 2:
Lifter1.setSpeed = -2000000;
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default:
break;
}
if(liftFirstToZero1)
{
Lifter1.setSpeed = -2000000;
}
//升降限制
if (agv.Public.i_Lift1DownLimit && Lifter1.setSpeed < 0)
{
liftFirstToZero1 = 0;
Lifter1.setSpeed = 0;
Lifter1.runState = 0;
}
else if(agv.Public.i_Lift1UpLimit && Lifter1.setSpeed > 0)
{
Lifter1.setSpeed = 0;
Lifter1.runState = 0;
}
//举升状态
if(agv.Public.i_Lift1DownLimit)
{
Lifter1.liftState = -1;
}
else if(agv.Public.i_Lift1UpLimit)
{
Lifter1.liftState = 1;
}
else
Lifter1.liftState = 0;
}
//int turnCount = 0;
float palstanceOffset = 0.005,palstanceMin = 0.12,setArriveAngle = 1,setPalstance = 0,upSlopeSpeed = 0.00035,downSlopeSpeed = 0.003;
float angleOffset = 0;
void RotateRunAuto() //举升自动运行,运行状态0不动 1左转90 2左转180 3右转90 4右转180 5左转联动90 6左转联动180 7右转联动90 8右转联动180
{
static float lastAngle = 0,diffAngle = 0,alreadyTurnAngle = 0,lastPalstance = 0;
static u8 TurnFlag = 0;
float PalstanceMax = (float)agv.Parameter.HandTurnSpeed/360 + palstanceOffset;
if(!Rotate1.runState)
{
angleOffset = CalculateAngleOffset(Rotate1.encodeAngle);
}
if(!(Rotate1.runState == 1 || Rotate1.runState == 2))
{
if(!TurnFlag && agv.Command.SetPlatformTurnAngle != 0)
{
if(agv.Command.HandMotorState == 5)
{
Rotate1.runState = 5;
TurnFlag = 1;
}
else if(agv.Command.HandMotorState == 6)
{
Rotate1.runState = 6;
TurnFlag = 1;
}
else if(agv.Command.CurDirection == 3)
{
Rotate1.runState = 3;
TurnFlag = 1;
}
else if(agv.Command.CurDirection == 4)
{
Rotate1.runState = 4;
TurnFlag = 1;
}
}
else if(Rotate1.runState == 0 && agv.Command.CurDirection != 4 && agv.Command.CurDirection != 3
&& agv.Command.HandMotorState!=5 && agv.Command.HandMotorState!=6 )
{
TurnFlag = 0;
}
}
alreadyTurnAngle = Rotate1.encodeAngle - lastAngle;
if(alreadyTurnAngle > 180)
alreadyTurnAngle -= 360;
if(alreadyTurnAngle < -180)
alreadyTurnAngle += 360;
diffAngle = fabs(fabs(alreadyTurnAngle) - agv.Command.SetPlatformTurnAngle);//fabs(Rotate1.encodeAngle - lastAngle);
if(Rotate1.runState == 2 || Rotate1.runState == 3 || Rotate1.runState == 4 || Rotate1.runState == 5 || Rotate1.runState == 6)
{
if(diffAngle > 30)//平台旋转角度
{
setPalstance = PalstanceMax;
}
else if(diffAngle > 3)
{
setPalstance = mapping(diffAngle,3,30,palstanceMin,PalstanceMax);
}
else
{
setPalstance = palstanceMin;
}
// if(diffAngle > 30)
// {
//// setPalstance = fabs(KincoStruct1.setSpeed)/360;
// if(fabs(KincoStruct1.setSpeed)/360 >= palstanceMin)
// {
// setPalstance = fabs(KincoStruct1.setSpeed)/360;
// }
// else
// {
// setPalstance = palstanceMin;
// }
// }
// else
// {
// if(fabs(KincoStruct1.setSpeed)/360 >= palstanceMin)
// {
// setPalstance = fabs(KincoStruct1.setSpeed)/360;
// }
// else
// {
// setPalstance = palstanceMin;
// }
// }
}
// if(setPalstance > lastPalstance)
// {
// setPalstance = lastPalstance + upSlopeSpeed;
// }
// else if(setPalstance < lastPalstance)
// {
// setPalstance = lastPalstance - downSlopeSpeed;
// }
// if(setPalstance < 0)
// {
// setPalstance = 0;
// }
// lastPalstance = setPalstance;
switch (Rotate1.runState)
{
case 0:
Rotate1.setSpeed = 0;
lastAngle = Rotate1.encodeAngle;
break;
case 1://右转90度
Rotate1.setSpeed = -500000;//PalstanceMax*6666666.7;//-500000;
// if(fabs(diffAngle) <=setArriveAngle)
// {
// Rotate1.setSpeed = 0;
// Rotate1.runState = 0;
// }
break;
case 2://左转
Rotate1.setSpeed = setPalstance*6666666.7;
if(fabs(diffAngle) <=setArriveAngle)
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
}
break;
case 3://右转
Rotate1.setSpeed = -setPalstance*6666666.7;//IMU.palstance
if(diffAngle < 5 && (Rotate1.encodeAngle > -10 && Rotate1.encodeAngle < 10))//回零点且转盘快到位
{
if(X1)
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
Rotate1.encodeAngle = 0;
lastAngle = 0;
}
}
else if(diffAngle < setArriveAngle)
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
}
break;
case 4://
Rotate1.setSpeed = setPalstance*6666666.7;
if(diffAngle < 5 && (Rotate1.encodeAngle > -10 && Rotate1.encodeAngle < 10))//回零点且转盘快到位
{
if(X1)//零点触发
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
Rotate1.encodeAngle = 0;
lastAngle = 0;
}
}
else if(diffAngle < setArriveAngle)
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
}
break;
case 5://左转联动
Rotate1.setSpeed = -setPalstance*6666666.7;//IMU.palstance
// Rotate1.setSpeed = -fabs(KincoStruct1.encodeSpeed/360)*6666666.7;
if(diffAngle < 5 && (Rotate1.encodeAngle > -10 && Rotate1.encodeAngle < 10))//回零点且转盘快到位
{
if(X1)//零点触发
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
Rotate1.encodeAngle = 0;
lastAngle = 0;
}
}
else if(diffAngle < setArriveAngle)
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
}
break;
case 6://右转联动
Rotate1.setSpeed = setPalstance*6666666.7;
if(diffAngle < 5 && (Rotate1.encodeAngle > -10 && Rotate1.encodeAngle < 10))//回零点且转盘快到位
{
if(X1)//零点触发
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
Rotate1.encodeAngle = 0;
lastAngle = 0;
}
}
else if(diffAngle < setArriveAngle)
{
Rotate1.setSpeed = 0;
Rotate1.runState = 0;
}
break;
case 7:
break;
case 8:
break;
default:
break;
}
}
void checkMaterialState() //物料检测
{
//下层物料检测
if (agv.Public.i_Shift1ArrivalLeft && agv.Public.i_Shift1ArrivalRight)
{
agv.Public.i_MaterialFlagFirst = 1;
}
else if (!agv.Public.i_Shift1ArrivalLeft && !agv.Public.i_Shift1ArrivalRight && !agv.Public.i_Shift1_LimitLeft && !agv.Public.i_Shift1_LimitRight)
{
agv.Public.i_MaterialFlagFirst = 0;
}
else
agv.Public.i_MaterialFlagFirst = 2;
}
void platformControlManual() //平台手动运行
{
// lifterRunManu(&DriverLifter1);
// shifterRunManu(&DriverShifter1);
lifterRunAuto();
RotateRunAuto();
}
void platformControlAuto() //平台自动对接
{
lifterRunAuto();
RotateRunAuto();
}
void X_Input()
{
static int lastTime = 0;
// agv.Public.i_RadarSigFront_1 = MasterInput.IN1 = !X12;//前壁障减速1
// agv.Public.i_RadarSigFront_2 = MasterInput.IN2 = !X13;//前壁障减速2
// agv.Public.i_RadarSigFront_3 = MasterInput.IN3 = !X14;//前壁障减速3
// agv.Public.i_RadarSigBack_1 = MasterInput.IN4 = !X4;//后壁障减速1
// agv.Public.i_RadarSigBack_2 = MasterInput.IN5 = !X5;//后壁障减速2
// agv.Public.i_RadarSigBack_3 = MasterInput.IN6 = !X6;//后壁障减速3
agv.Public.i_QuickStopSig = MasterInput.IN7 = X6;// 急停 常开
// if(!agv.Command.Au_Hand)
// {
// agv.Public.i_HandLiftSig = X7;
// agv.Public.i_HandDownSig = X8;
// if(agv.Public.i_HandLiftSig)
// Lifter1.runState = 1;
// else if(agv.Public.i_HandDownSig)
// Lifter1.runState = 2;
//
// agv.Public.i_HandFrontSig = X9;
// agv.Public.i_HandBackSig = X10;
// agv.Public.i_HandTurnLeftSig = X11;
// agv.Public.i_HandTurnRightSig = X12;
// if(agv.Public.i_HandFrontSig)
// agv.Command.HandMotorState = 1;
// else if(agv.Public.i_HandBackSig)
// agv.Command.HandMotorState = 2;
// else if(agv.Public.i_HandTurnLeftSig)
// agv.Command.HandMotorState = 3;
// else if(agv.Public.i_HandTurnRightSig)
// agv.Command.HandMotorState = 4;
// else
// agv.Command.HandMotorState = 0;
// ;
//
// }
// agv.Public.i_BumperSig = MasterInput.IN8 = X8;//防撞条
// agv.Public.i_StartSig = MasterInput.IN9 = X9;// 启动
// agv.Public.i_StopSig = MasterInput.IN10 = X10;//暂停
// agv.Public.i_LightStopFlagL = MasterInput.IN11 = X11;//左停靠
// agv.Public.i_LightStopFlagR = MasterInput.IN12 = X12;//右停靠
//
agv.Public.i_Lift1UpLimit = MasterInput.IN13 = X4;//升降平台上限位
agv.Public.i_Lift1DownLimit = MasterInput.IN14 = X3;//升降平台下限位
// agv.Public.i_Shift1_LimitLeft = MasterInput.IN15 = X15;//左极限
// agv.Public.i_Shift1_LimitRight = MasterInput.IN18 = X18;//右极限
// agv.Public.i_Shift1ArrivalLeft = MasterInput.IN16 = X16;//左到位对射
// agv.Public.i_Shift1ArrivalRight = MasterInput.IN17 = !X17;//右到位对射
if(agv.Public.i_StartSig)//第一次上电按启动复位举升
{
agv.Public.i_StartFirst = true;
}
if(agv.Public.i_Lift1DownLimit)//下限位触发 位置清零
{
Lifter1.encodePos = 0;
}
// if(agv.Public.i_StopSig)
// {
// if(agv.Public.SystemTime - lastTime >= 3000)
// {
// resetLiftFlag = 1;
// }
// }
// else
// lastTime = agv.Public.SystemTime;
}
void platformDataProcess(void)
{
X_Input();//IO输入信号
//自动模式且开启红外,雷达避障信号
if(agv.Command.Au_Hand&&agv.Command.Barrier_ONOFF)
{
if(agv.Command.CurDirection||agv.Command.HandMotorState)//前进方向
{
if(agv.Public.i_RadarSigFront_3)//最里层雷达避障
{
SetAlarm(0x0001);//停车
}
else
{
Reset_Alarm(0x0001);//取消停车避障
}
if(agv.Public.i_RadarSigFront_2)//二级减速
{
agv.Public.i_DoubleReduction = 1;//开启二级减速
}
else
{
agv.Public.i_DoubleReduction = 0;//取消二级减速
}
if(agv.Public.i_RadarSigFront_1)//一级减速
{
agv.Public.i_Reduction = 1;//开启一级减速
}
else
{
agv.Public.i_Reduction = 0;//取消一级减速
}
}
}
else//不开启避障
{
Reset_Alarm(0x0001);//取消停车避障
agv.Public.i_Reduction = 0;//取消一级减速
agv.Public.i_DoubleReduction = 0;//取消二级减速
}
//防撞条
if(agv.Public.i_BumperSig)
{
SetAlarm(0x0006);//停车,防撞条报警和暂停触发
}
else
Reset_Alarm(0x0002);//取消防撞条报警
//急停触发
if (agv.Public.i_QuickStopSig)
{
SetAlarm(0x0100);//急停和暂停触发,防止再启动
}
else
{
Reset_Alarm(0x0100);//取消急停报警
}
if(agv.Public.i_StartSig)//启动
{
Reset_Alarm(0x0004);
}
if(agv.Public.i_StopSig)
{
SetAlarm(0x0004);//暂停
}
if(agv.Public.i_Lift1DownLimit)//拉线编码器清零
{
TIM_SetCounter(TIM1, 0);
}
//启动和暂停指示灯
// if(agv.Command.Au_Hand && agv.Public.Error_Flag==0)//自动模式无异常显示启动灯
// {
// Y8(1);
// Y9(0);
// }
// else
// {
// Y8(0);
// Y9(1);
// }
agv.Public.i_quadEncoder1 = TIM_GetCounter(TIM1);
agv.Public.i_quadEncoder2 = TIM_GetCounter(TIM2);
agv.Public.i_quadEncoder3 = TIM_GetCounter(TIM3);
agv.Public.i_quadEncoder4 = TIM_GetCounter(TIM4);
agv.Public.i_LiftHeightFirst = agv.Public.i_quadEncoder1/4;
checkMaterialState(); //物料检测
//自动模式脱轨判断
if(agv.Command.Au_Hand)
{
//前进后退,弧线左右转做脱轨判断
if(agv.Command.CurDirection == 1||agv.Command.CurDirection == 2||agv.Command.CurDirection == 7
||agv.Command.CurDirection == 8||agv.Command.CurDirection == 9||agv.Command.CurDirection == 10)
{
if(fabs(navi.Public.CenterOffset) > 100)//位置偏差大于10CM,脱轨
{
SetAlarm(0x0008);//脱轨报警
}
else
Reset_Alarm(0x0008);//取消脱轨报警
}
else
Reset_Alarm(0x0008);//取消脱轨报警
}
}
/*SETVMOT动作指令解析*/
void commandActionAnalysis(int dataOne, int dataTwo)
{
switch (dataOne)
{
case 1: //举升1 下降2
Lifter1.runState = dataTwo;
break;
case 2: //左旋转
Rotate1.runState = dataOne;
agv.Command.SetPlatformTurnAngle = dataTwo;
break;
case 3: //右旋转
Rotate1.runState = dataOne;
agv.Command.SetPlatformTurnAngle = dataTwo;
break;
case 4: //打开充电状态
switch (dataTwo) //小车运动方式设置 //
{
case 0:
Y8(0); //关闭充电器触点
break;
case 1:
Y8(1); //打开充电器触点
break;
}
case 5: //AGV原地旋转左转
agv.Command.HandMotorState = dataOne;
agv.Command.SetChassisTurnAngle = (float)dataTwo/57.3;
agv.Command.SetPlatformTurnAngle = dataTwo;
break;
case 6: //AGV原地旋转左转
agv.Command.HandMotorState = dataOne;
agv.Command.SetChassisTurnAngle = (float)dataTwo/57.3;
agv.Command.SetPlatformTurnAngle = dataTwo;
break;
case 7: //打开充电状态
switch (dataTwo) //小车运动方式设置 //
{
case 0:
Reset_Alarm(0x0004);//启动
break;
case 1:
SetAlarm(0x0004);//停车
break;
}
}
}
#endif