Control.cs
49.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.Script.Serialization;
using AGVControl;
using RestSharp;
namespace BLL
{
public class Control
{
private bool loop;
//private int areaC_Index;
private Thread tAgvCall;
private Thread tAgvState;
//public List<string> Marks;
private const int REG_STATUS = 20;
//private List<string> shelfLockedNodeNames;
public delegate void AgvChangedEvent(int agvIndex);
public event AgvChangedEvent AgvChanged;
public event AgvChangedEvent AgvOnline;
public Control()
{
}
public void Start()
{
loop = true;
tAgvState = new Thread(new ThreadStart(AgvState));
tAgvCall = new Thread(new ThreadStart(AgvCall));
tAgvState.Start();
tAgvCall.Start();
}
public void Stop()
{
loop = false;
}
private void AgvState()
{
bool rtn;
while (loop)
{
Thread.Sleep(1000);
for (int i = 0; i < Common.agvInfo.Count; i++)
{
if (!CheckOnline(i)) continue;
if (!loop) break;
//获取AGV状态
rtn = Common.mir.Get_State(Common.agvInfo[i], out int stateID, out string stateText, out int battery, out string mission_text, out Agv_Info.clsPosition position);
Common.mir.Get_IO_Status(Common.agvInfo[i], out bool[] input, out bool[] output);
bool change = false;
if (rtn) change = Common.agvInfo[i].SetState(stateID, stateText, battery, mission_text, position,input,output);
//上报异常
if (Common.agvInfo[i].MissionText != mission_text)
{
Common.agvInfo[i].MissionText = mission_text;
if (mission_text.Equals("停靠") || mission_text.ToLower().Equals("DOCKING"))
{
Common.agvInfo[i].DockingInfo.startTime = DateTime.Now;
Common.agvInfo[i].DockingInfo.IsDocking = true;
}
}
bool isAlarm = false;
List<AlarmMsg> msglist = new List<AlarmMsg>();
if (Common.agvInfo[i].DockingInfo.IsDocking && (DateTime.Now - Common.agvInfo[i].DockingInfo.startTime).TotalMinutes.Equals(1))
{
isAlarm = true;
Common.agvInfo[i].DockingInfo.IsDocking = false;
msglist.Add(new AlarmMsg(Common.agvInfo[i].Name, "agv." + Common.agvInfo[i].Name + ".Docking", mission_text));
}
if (battery <= 10)
{
isAlarm = true;
msglist.Add(new AlarmMsg(Common.agvInfo[i].Name, "agv." + Common.agvInfo[i].Name + ".battery", "电量 " + battery.ToString() + "%"));
}
if (stateText.Equals("Error") || stateText.Equals("EmergencyStop") || stateText.Equals("Pause"))
{
isAlarm = true;
msglist.Add(new AlarmMsg(Common.agvInfo[i].Name, "agv." + Common.agvInfo[i].Name + ".Error.EmergencyStop", "agv状态 " + stateText));
}
if (isAlarm)
BLL.AGVManager.updateDeviceAlarmMsg(msglist);
//获取地点任务状态
Thread.Sleep(50);
rtn = Common.mir.Get_Register(Common.agvInfo[i], REG_STATUS, out int regValue);
if (rtn) Common.agvInfo[i].GetPlace(regValue);
if (change)
{
Common.LogInfo(string.Format("{0} Get_State StateID={1}, StateText={2}, Battery={3}, Mission_text={4}", Common.agvInfo[i].Name, stateID, stateText, battery, mission_text), false);
}
//执行任务更新状态
if (stateID == 5 || change)
{
Common.log.Debug(string.Format("{0} Get_Register PLC{1}={2}", Common.agvInfo[i].Name, REG_STATUS, regValue));
AgvChanged?.Invoke(i);
}
//获取任务队列
//rtn = Common.mir.Get_Mission_Queue(Common.agvInfo[i], out List<string> mission);
//if (rtn)
//{
// string[] arr = new string[mission.Count];
// for (int j = 0; j < mission.Count; j++)
// arr[j] = Common.agvMission.FirstOrDefault(q => q.Value == mission[j]).Key;
// string missionKey = string.Join(",", arr);
// if (Common.agvInfo[i].MissionQueue != missionKey)
// {
// Common.agvInfo[i].MissionQueue = missionKey;
// AgvChanged?.Invoke(i);
// }
//}
}
}
}
private void AgvCall()
{
while (loop)
{
Thread.Sleep(1000);
for (int i = 0; i < Common.agvInfo.Count; i++)
{
if (!loop) break;
if (!Common.agvInfo[i].IsCon) continue; //AGV网络连接
if (!Common.agvInfo[i].IsUse) continue; //AGV是否可用
//Ready,Pause,Executing
if (Common.agvInfo[i].StateID != 3 && Common.agvInfo[i].StateID != 4 && Common.agvInfo[i].StateID != 5)
{
Common.LogInfo(string.Format("{0}不能调用 StateID={1}, StateText={2}", Common.agvInfo[i].Name, Common.agvInfo[i].StateID, Common.agvInfo[i].StateText));
continue;
}
switch (Common.agvInfo[i].PlaceState)
{
case PlaceState.None:
StateNone(i);
break;
case PlaceState.Move:
StateMove(i);
break;
case PlaceState.MoveFinish:
StateMoveFinish(i);
break;
case PlaceState.Enter:
StateEnter(i);
break;
case PlaceState.EnterFinish:
StateEnterFinish(i);
break;
case PlaceState.Leave:
StateLeave(i);
break;
case PlaceState.LeaveFinish:
StateLeaveFinish(i);
break;
case PlaceState.Error:
StateError(i);
break;
}
}
}
}
/// <summary>
/// 计算当前小车距离最近的任务点(只针对产线)
/// </summary>
/// <param name="agv"></param>
/// <returns>节点名称</returns>
private Common.MissionStru CalculateNearNode(Agv_Info agv)
{
Common.MissionStru missionNode = new Common.MissionStru("","");
double minDis = Double.MaxValue;
try
{
if (Common.missionList.Count == 0)
return missionNode;
foreach (var item in Common.missionList)
{
int index = Common.nodeInfo.FindIndex(s => s.Name == item.NodeName);
if (index > -1)
{
double dis = Math.Sqrt(Math.Pow((agv.Position.x - Common.nodeInfo[index].position.X), 2) + Math.Pow((agv.Position.y - Common.nodeInfo[index].position.Y), 2));
Common.LogInfo(string.Format("{0} 距离{1}={2}", agv.Name, Common.nodeInfo[index].Name, dis.ToString("f2")), false);
if (dis < minDis)
{
minDis = dis;
missionNode = item;
}
}
}
Common.LogInfo(string.Format("{0} 运动到产线 {1}", agv.Name, missionNode), false);
return missionNode;
}
catch (Exception e)
{
Common.log.Error("CalculateNearNode + " + e.ToString());
}
return missionNode;
}
private bool CheckOnline(int idx)
{
bool rtn = Common.mir.CheckIP(Common.agvInfo[idx].IP);
if (rtn)
{
if (!Common.agvInfo[idx].IsCon)
{
Common.agvInfo[idx].IsCon = true;
Common.LogInfo(Common.agvInfo[idx].Name + " Online");
AgvOnline?.Invoke(idx);
AgvChanged?.Invoke(idx);
}
}
else
{
if (Common.agvInfo[idx].IsCon)
{
Common.agvInfo[idx].IsCon = false;
Common.LogInfo(Common.agvInfo[idx].Name + " Offline");
AgvOnline?.Invoke(idx);
AgvChanged?.Invoke(idx);
}
}
return rtn;
}
/// <summary>
/// agv空闲
/// </summary>
/// <param name="idx"></param>
private void StateNone(int idx)
{
Agv_Info agv = Common.agvInfo[idx];
if (agv.TaskSend != "") return;
//检查是否需要充电
if (CheckIsNeedCharge(agv))
return;
if (CheckA6State(agv))
return;
CheckEmptyShelf(agv);
#region 出空料架,带节点状态-不用
//ClientLevel clientLevel = Common.linePlace.Values.Max<ClientLevel>();
////出空料架
//foreach (var item in Common.linePlace)
//{
// if (!clientLevel.Equals(item.Value))
// continue;
// string name = item.Key;
// index = Common.nodeInfo.FindIndex(s => s.Name.Equals(name) && s.Action == ClientAction.NeedLeave && s.IsUse);
// if (index > -1)
// {
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + name]);
// if (rtn)
// {
// Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
// Common.linePlace.Remove(name);
// }
// agv.TaskSend = rtn ? "Move" + name : "";
// }
// Common.LogInfo(string.Format("[{0}-{1}] 出空料架.", name, item.Value));
// if (Common.linePlace.Count.Equals(0).Equals(false))
// Common.LogInfo("剩余需要出空料架的节点:" + string.Join(",", Common.linePlace.Keys.ToArray())
// + ";对应紧急程度:" + string.Join(",", Common.linePlace.Values.ToArray()));
// if (rtn) break;
//}
//for (int i = 0; i < Common.linePlace.Count; i++)
//{
// string name = Common.linePlace[i];
// index = Common.nodeInfo.FindIndex(s => s.Name.Equals(name) && s.IsUse);
// if (index > -1)
// {
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + name]);
// if (rtn)
// {
// Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
// Common.LogInfo(string.Format("[{0}] 出空料架.", name));
// Common.linePlace.RemoveAt(i);
// System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
// }
// agv.TaskSend = rtn ? "Move" + name : "";
// }
// if (Common.linePlace.Count.Equals(0).Equals(false))
// Common.LogInfo("剩余需要出空料架的节点:" + string.Join(",", Common.linePlace.ToArray()));
// if (rtn) break;
//}
#endregion
}
/// <summary>
/// 判断是否需要充电
/// </summary>
/// <param name="agv"></param>
/// <returns></returns>
private bool CheckIsNeedCharge(Agv_Info agv)
{
if (Common.chargeStatus.AutoCharge)
{
if (agv.Battery <= Common.chargeStatus.chargeMin)
{
Common.LogInfo(agv.Name + " 电量小于20%", false);
if (StatusCharge(agv))
return true;
}
else if (agv.Battery <= Common.chargeStatus.chargeMax && agv.WaitTime >= Common.chargeStatus.chargeWait * 60000)
{
Common.LogInfo(agv.Name + " 闲置时间超过" + Common.chargeStatus.chargeWait + "分钟", false);
if (StatusCharge(agv))
return true;
}
return false;
}
return false;
}
/// <summary>
/// 充电
/// </summary>
/// <param name="agv"></param>
/// <returns>充电任务结果</returns>
private bool StatusCharge(Agv_Info agv)
{
bool rtn;
string log;
double sp = (DateTime.Now.Ticks - Common.chargeStatus.chargeInterval) / 10000000.0;
//防止两车同时充电卡住的情况
//if (sp < 60)
//{
// Common.LogInfo(agv.Name + " 与上一辆车的充电时间间隔为" + sp.ToString() + ",小于60s,不能充电。", false);
// return false;
//}
#region 随机充电位置
//if (Common.chargeStatus.charge3 == "")
//{
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge3"]);
// if (rtn)
// {
// agv.TaskSend = "AutoCharge3";
// Common.chargeStatus.charge3 = agv.Name;
// Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
// log = string.Format("{0} AutoCharge3", agv.Name);
// Common.LogInfo(log);
// Common.mir.State_Ready(agv);
// }
// else
// {
// agv.TaskSend = "";
// log = string.Format("{0} AutoCharge3 失败", agv.Name);
// //防止上一个任务已执行但返回失败时,删除任务
// //Common.mir.Del_Mission(agv);
// Common.LogInfo(log);
// }
// return rtn;
//}
//else if (Common.chargeStatus.charge4 == "")
//{
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge4"]);
// if (rtn)
// {
// agv.TaskSend = "AutoCharge4";
// Common.chargeStatus.charge4 = agv.Name;
// Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
// log = string.Format("{0} AutoCharge4", agv.Name);
// Common.LogInfo(log);
// Common.mir.State_Ready(agv);
// }
// else
// {
// agv.TaskSend = "";
// log = string.Format("{0} AutoCharge4 失败", agv.Name);
// //防止上一个任务已执行但返回失败时,删除任务
// //Common.mir.Del_Mission(agv);
// Common.LogInfo(log);
// }
// return rtn;
//}
//else if (Common.chargeStatus.charge5 == "")
//{
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge5"]);
// if (rtn)
// {
// agv.TaskSend = "AutoCharge5";
// Common.chargeStatus.charge5 = agv.Name;
// Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
// log = string.Format("{0} AutoCharge5", agv.Name);
// Common.LogInfo(log);
// Common.mir.State_Ready(agv);
// }
// else
// {
// agv.TaskSend = "";
// log = string.Format("{0} AutoCharge5 失败", agv.Name);
// //防止上一个任务已执行但返回失败时,删除任务
// //Common.mir.Del_Mission(agv);
// Common.LogInfo(log);
// }
// return rtn;
//}
//else if (Common.chargeStatus.charge6 == "")
//{
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge6"]);
// if (rtn)
// {
// agv.TaskSend = "AutoCharge6";
// Common.chargeStatus.charge4 = agv.Name;
// Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
// log = string.Format("{0} AutoCharge6", agv.Name);
// Common.LogInfo(log);
// Common.mir.State_Ready(agv);
// }
// else
// {
// agv.TaskSend = "";
// log = string.Format("{0} AutoCharge6 失败", agv.Name);
// //防止上一个任务已执行但返回失败时,删除任务
// //Common.mir.Del_Mission(agv);
// Common.LogInfo(log);
// }
// return rtn;
//}
//else
//{
// return false;
//}
#endregion
#region 指定充电位置
if (agv.IP == "10.85.199.72")//1764
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge3"]);
if (rtn)
{
//agv.TaskSend = "AutoCharge3";
agv.TaskSend = "";
Common.chargeStatus.charge3 = agv.Name;
Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
log = string.Format("{0} AutoCharge3", agv.Name);
Common.LogInfo(log);
Common.mir.State_Ready(agv);
}
else
{
agv.TaskSend = "";
log = string.Format("{0} AutoCharge3 失败", agv.Name);
//防止上一个任务已执行但返回失败时,删除任务
//Common.mir.Del_Mission(agv);
Common.LogInfo(log);
}
return rtn;
}
else if (agv.IP == "10.85.199.71")//1763
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge4"]);
if (rtn)
{
//agv.TaskSend = "AutoCharge4";
agv.TaskSend = "";
Common.chargeStatus.charge4 = agv.Name;
Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
log = string.Format("{0} AutoCharge4", agv.Name);
Common.LogInfo(log);
Common.mir.State_Ready(agv);
}
else
{
agv.TaskSend = "";
log = string.Format("{0} AutoCharge4 失败", agv.Name);
//防止上一个任务已执行但返回失败时,删除任务
//Common.mir.Del_Mission(agv);
Common.LogInfo(log);
}
return rtn;
}
else if (agv.IP == "10.85.199.73")//1767
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge5"]);
if (rtn)
{
//agv.TaskSend = "AutoCharge5";
agv.TaskSend = "";
Common.chargeStatus.charge5 = agv.Name;
Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
log = string.Format("{0} AutoCharge5", agv.Name);
Common.LogInfo(log);
Common.mir.State_Ready(agv);
}
else
{
agv.TaskSend = "";
log = string.Format("{0} AutoCharge5 失败", agv.Name);
//防止上一个任务已执行但返回失败时,删除任务
//Common.mir.Del_Mission(agv);
Common.LogInfo(log);
}
return rtn;
}
else if (agv.IP == "10.85.199.74")//1768
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["AutoCharge6"]);
if (rtn)
{
//agv.TaskSend = "AutoCharge6";
agv.TaskSend = "";
Common.chargeStatus.charge4 = agv.Name;
Common.chargeStatus.chargeInterval = DateTime.Now.Ticks;
log = string.Format("{0} AutoCharge6", agv.Name);
Common.LogInfo(log);
Common.mir.State_Ready(agv);
}
else
{
agv.TaskSend = "";
log = string.Format("{0} AutoCharge6 失败", agv.Name);
//防止上一个任务已执行但返回失败时,删除任务
//Common.mir.Del_Mission(agv);
Common.LogInfo(log);
}
return rtn;
}
else
{
return false;
}
#endregion
}
/// <summary>
///A6是否有满料架要出
/// </summary>
/// <param name="agv"></param>
private bool CheckA6State(Agv_Info agv)
{
bool rtn;
int index;
string RFID = "";
//A6出满料
rtn = FindA6Leave(out string nextNode);
if (rtn)
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["MoveA6"]);
if (rtn)
{
agv.NextPlace = nextNode;
index = FindNode("A6");
Common.nodeInfo[index].AgvName = agv.Name;
RFID = Common.nodeInfo[index].RFID;
agv.RFID = RFID;
index = FindNode(nextNode);
Common.nodeInfo[index].AgvName = agv.Name;
}
Common.LogInfo(string.Format("A6有满料架[{0}]要出,目的地为{1}", RFID, nextNode));
agv.TaskSend = rtn ? "MoveA6" : "";
return true;
}
return false;
}
/// <summary>
/// 查看是否有空料架出,有则根据距离分配任务
/// </summary>
/// <param name="agv"></param>
/// <returns>true:有空料架要出</returns>
private bool CheckEmptyShelf(Agv_Info agv)
{
//if (Common.linePlace.Count == 0)
//{
// //Common.LogInfo("当前无空料架要出");
// return false;
//}
bool rtn = false;
//int index = -1;
//foreach (var nodeName in Common.linePlace)
//{
// index = Common.nodeInfo.FindIndex(s => s.Name.Equals(nodeName) && s.IsUse);
// if (index > -1)
// {
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + nodeName]);
// if (rtn)
// {
// Common.nodeInfo[index].AgvName = agv.Name;
// Common.LogInfo(string.Format("[{0}] 出空料架.", nodeName));
// Common.linePlace.Remove(nodeName);
// System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
// }
// agv.TaskSend = rtn ? "Move" + nodeName : "";
// }
// if (Common.linePlace.Count.Equals(0).Equals(false))
// Common.LogInfo("剩余需要出空料架的节点:" + string.Join(",", Common.linePlace.ToArray()));
// if (rtn) return true;
//}
Common.MissionStru missionNode = CalculateNearNode(agv);
if (missionNode.NodeName.Equals(""))
{
return false;
}
int index = Common.nodeInfo.FindIndex(s => s.Name.Equals(missionNode.NodeName) && s.AgvName == "" && s.IsUse);
if (index > -1)
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + missionNode.NodeName]);
if (rtn)
{
Common.nodeInfo[index].AgvName = agv.Name;
Common.LogInfo(string.Format("[{0}] 出空料架.", missionNode.NodeName));
Common.missionList.Remove(missionNode);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Common.CONFIG_PATH + "LinePlace.txt"))
{
foreach (var item in Common.missionList)
{
file.WriteLine(string.Format("{0},{1}", item.CreateTime,item.NodeName));
}
}
}
agv.TaskSend = rtn ? "Move" + missionNode.NodeName : "";
}
if (Common.missionList.Count.Equals(0).Equals(false))
{
AgvChanged?.Invoke(0);
Common.LogInfo("剩余需要出空料架的节点:");
Common.missionList.ForEach(s => Common.LogInfo(string.Format("创建时间:{0},任务节点{1}", s.CreateTime, s.NodeName)));
}
if (rtn) return true;
return false;
}
/// <summary>
/// agv运动
/// </summary>
/// <param name="idx"></param>
private void StateMove(int idx)
{
//bool rtn;
//Agv_Info agv = Common.agvInfo[idx];
//int index = FindNode(agv.Place);
//if (index == -1) return;
//ClientNode node = Common.nodeInfo[index];
//switch (agv.Place)
//{
// case "":
// agv.TaskSend = "";
// agv.NextPlace = "";
// break;
//}
}
/// <summary>
/// agv运动完成
/// </summary>
/// <param name="idx"></param>
private void StateMoveFinish(int idx)
{
bool rtn;
Agv_Info agv = Common.agvInfo[idx];
int index = FindNode(agv.Place);
if (index == -1) return;
ClientNode node = Common.nodeInfo[index];
switch (agv.Place)
{
case "A5":
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以进入料架", agv.Name, agv.Place));
}
else
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
}
break;
case "A6":
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以进入料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.MayLeave)
{
if (agv.TaskSend == "Enter") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Enter"]);
agv.TaskSend = rtn ? "Enter" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以出去料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedEnter)
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedLeave)
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyLeave信号", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedEnterLeave)
{
if(agv.IsExistShelf)//车上有料架
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
}
else
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyLeave信号", agv.Name, agv.Place));
}
}
break;
case "E1":
case "E2":
case "E3":
case "E4":
case "E5":
case "E6":
case "E7":
case "E8":
case "E9":
case "E10":
case "E11":
case "E12":
case "E14":
case "E15":
case "E16":
case "E21":
case "E22":
//C是大料架,D是小料架
//大料架
//if (agv.RFID.StartsWith("C"))
//{
// if (Common.linePlace.Contains(node.Name))
// {
// int nodeIdx = Common.nodeInfo.FindIndex(s => s.Name == "A5" && (s.Action == ClientAction.NeedC || s.Action == ClientAction.NeedEnter) && s.AgvName == "" && s.IsUse);
// if (nodeIdx > -1)
// {
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + Common.nodeInfo[nodeIdx].Name]);
// if (rtn)
// {
// agv.TaskSend = rtn ? "Move" + Common.nodeInfo[nodeIdx].Name : "";
// Common.nodeInfo[nodeIdx].AgvName = agv.Name;
// Common.linePlace.Remove(node.Name);
// Common.LogInfo(string.Format("{0}载大料架运往{1}[RFID={2}][{3}],", agv.Name, Common.nodeInfo[nodeIdx].Name, agv.RFID, Common.nodeInfo[nodeIdx].Action));
// return;
// }
// }
// nodeIdx = Common.nodeInfo.FindIndex(s => s.Name == "A6" && (s.Action == ClientAction.NeedEnter || s.Action == ClientAction.NeedEnterLeave) && s.AgvName == "" && s.IsUse);
// if (nodeIdx > -1)
// {
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + Common.nodeInfo[nodeIdx].Name]);
// if (rtn)
// {
// agv.TaskSend = rtn ? "Move" + Common.nodeInfo[nodeIdx].Name : "";
// Common.nodeInfo[nodeIdx].AgvName = agv.Name;
// Common.linePlace.Remove(node.Name);
// Common.LogInfo(string.Format("{0}载大料架运往{1}[RFID={2}][{3}],", agv.Name, Common.nodeInfo[nodeIdx].Name, agv.RFID, Common.nodeInfo[nodeIdx].Action));
// return;
// }
// }
// }
// else
// {
// Common.LogInfo(string.Format("{0}载大料架到达{1}[RFID={2}],等待料架清空。。。", agv.Name, agv.Place, agv.RFID));
// return;
// }
//}
//else if (agv.RFID.StartsWith("D"))//小料架
//{
// rtn = Common.server.ReadyEnter(agv.Place);
// if (!rtn) return;
// Common.LogInfo(string.Format("{0}载小料架到达{1}[RFID={2}],服务器发送ReadyEnter信号", agv.Name, agv.Place, agv.RFID));
//}
if (agv.RFID.StartsWith("D") || agv.RFID.StartsWith("C"))//不分大小料架
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}载小料架到达{1}[RFID={2}],服务器发送ReadyEnter信号", agv.Name, agv.Place, agv.RFID));
}
else//其他地方到E区域,则是空车
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyLeave信号", agv.Name, agv.Place));
}
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以进入料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.MayLeave)
{
if (agv.TaskSend == "Enter") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Enter"]);
agv.TaskSend = rtn ? "Enter" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以出去料架", agv.Name, agv.Place));
}
//else if (node.Action == ClientAction.NeedEnter)
//{
// rtn = Common.server.ReadyEnter(agv.Place);
// if (!rtn) return;
// Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
//}
//else if (node.Action == ClientAction.NeedLeave)
//{
// rtn = Common.server.ReadyLeave(agv.Place);
// if (!rtn) return;
// Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyLeave信号", agv.Name, agv.Place));
//}
break;
}
}
/// <summary>
/// 料架进入小车
/// </summary>
/// <param name="idx"></param>
private void StateEnter(int idx)
{
}
/// <summary>
/// 料架进入小车完成
/// </summary>
/// <param name="idx"></param>
private void StateEnterFinish(int idx)
{
bool rtn;
Agv_Info agv = Common.agvInfo[idx];
if (agv.TaskSend != "") return;
int index = FindNode(agv.Place);
if (index == -1) return;
ClientNode node = Common.nodeInfo[index];
switch (agv.Place)
{
case "A6":
if (node.Action == ClientAction.FinishLeave)
{
string nextPlace = agv.NextPlace;
if (nextPlace.Equals(""))
return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + nextPlace]);
if (rtn)
{
agv.NextPlace = "";
node.AgvName = "";
agv.TaskSend = rtn ? "Move" + nextPlace : "";
Common.LogInfo(string.Format("{0}装载满料架,运往{1}", "A6", agv.Name,nextPlace));
}
}
break;
case "E1":
case "E2":
case "E3":
case "E4":
case "E5":
case "E6":
case "E7":
case "E8":
case "E9":
case "E10":
case "E11":
case "E12":
case "E14":
case "E15":
case "E16":
case "E21":
case "E22":
//产线客户端不发FinishLeave
//产线空料架进入小车
//node.AgvName = "";
//node.Action = ClientAction.None;
ResetNodeState(node);
//目前条件:根据需求运料架,需要就运行【后续需要更改为从产线进入小车的料架均为小料架】 s.Action == ClientAction.NeedD || s.Action == ClientAction.NeedEnter
int tarIdx = Common.nodeInfo.FindIndex(s => s.Name == "A5"
&& (s.Action == ClientAction.NeedD || s.Action == ClientAction.NeedEnter || s.Action == ClientAction.NeedC) && s.IsUse);
if (tarIdx == -1)
{
Common.LogInfo(string.Format("{0} 已装载料架,A5不需要料架", agv.Name));//小
}
else
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["MoveA5"]);
if (rtn)
{
agv.NextPlace = "";
node.AgvName = "";
agv.TaskSend = rtn ? "MoveA5" : "";
Common.LogInfo(string.Format("{0} 已装载料架,送往A5", agv.Name));//小
}
break;
}
tarIdx = Common.nodeInfo.FindIndex(s => s.Name == "A6" &&
(s.Action == ClientAction.NeedEnter || s.Action == ClientAction.NeedEnterLeave) && s.IsUse);
if (tarIdx == -1)
{
Common.LogInfo(string.Format("{0} 已装载料架,A6不需要料架", agv.Name));//小
}
else
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["MoveA6"]);
if (rtn)
{
agv.NextPlace = "";
node.AgvName = "";
agv.TaskSend = rtn ? "MoveA6" : "";
Common.LogInfo(string.Format("{0} 已装载料架,送往A6", agv.Name));//小
}
}
break;
}
}
/// <summary>
/// 料架离开小车
/// </summary>
/// <param name="idx">agv索引</param>
private void StateLeave(int idx)
{
Agv_Info agv = Common.agvInfo[idx];
}
/// <summary>
/// 料架离开小车完成
/// </summary>
/// <param name="idx"></param>
private void StateLeaveFinish(int idx)
{
bool rtn;
Agv_Info agv = Common.agvInfo[idx];
if (agv.TaskSend != "") return;
int index = FindNode(agv.Place);
if (index == -1) return;
ClientNode node = Common.nodeInfo[index];
switch (agv.Place)
{
case "A5":
//if (node.Action == ClientAction.FinishEnter)
//{
// Common.LogInfo(string.Format("{0}在{1}完成进入料架", agv.Name, agv.Place));
//}
//break;
case "A6":
if (node.Action == ClientAction.FinishEnter)
{
Common.LogInfo(string.Format("{0}在{1}完成进入料架", agv.Name, agv.Place));
//agv.RFID = "";
ResetAGVState(agv, true);
ResetNodeState(node);
//查询附近是否有空料架要出,没有则去待机位
if (!CheckEmptyShelf(agv))
MoveStandby(agv);
}
break;
case "E1":
case "E2":
case "E3":
case "E4":
case "E5":
case "E6":
case "E7":
case "E8":
case "E9":
case "E10":
case "E11":
case "E12":
case "E14":
case "E15":
case "E16":
case "E21":
case "E22":
/// agv.RFID = "";
// node.AgvName = "";
//node.Action = ClientAction.None;
ResetAGVState(agv, true);
ResetNodeState(node);
//查询附近是否有空料架要出,没有则去待机位
if (!CheckEmptyShelf(agv))
MoveStandby(agv);
break;
}
}
/// <summary>
/// 重置节点状态
/// </summary>
/// <param name="clientNode"></param>
private void ResetNodeState(ClientNode clientNode)
{
clientNode.Action = ClientAction.None;
clientNode.AgvName = "";
clientNode.RFID = "";
}
/// <summary>
/// 重置agv变量信息(RFID)
/// </summary>
/// <param name="agv"></param>
/// <param name="IsNextPlace">是否清除下一目的地</param>
private void ResetAGVState(Agv_Info agv,bool IsNextPlace=false)
{
agv.RFID = "";
if (IsNextPlace)
agv.NextPlace = "";
}
private bool MoveStandby(Agv_Info agv)
{
string log;
//清除当前任务点
if (!agv.Place.Equals(""))
{
int idx = Common.nodeInfo.FindIndex(s => s.Name == agv.Place);
if (idx > -1)
{
Common.nodeInfo[idx].AgvName = "";
}
else
{
log = "清理AgvName,没有找到 " + agv.Name;
Common.LogInfo(log);
}
}
//执行下一个任务
//agv.Place = "";
bool rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["MoveStandby"]); //返回待机区
if (rtn)
{
agv.TaskSend = "";
agv.Place = "";
log = string.Format("{0} MoveStandby", agv.Name);
Common.LogInfo(log);
Common.mir.State_Ready(agv);
}
else
{
agv.TaskSend = "";
log = string.Format("{0} MoveStandby 失败", agv.Name);
//防止上一个任务已执行但返回失败时,删除任务
//Common.mir.Del_Mission(agv);
Common.LogInfo(log);
}
return rtn;
}
/// <summary>
/// 错误状态
/// </summary>
/// <param name="idx"></param>
private void StateError(int idx)
{
Agv_Info agv = Common.agvInfo[idx];
Common.LogInfo(string.Format("{0}在执行任务[{1}]出现错误:{2}", agv.Name, agv.TaskSend, agv.MissionText));
}
/// <summary>
/// 查找节点是否存在以及是否调用
/// </summary>
/// <param name="nodeName"></param>
/// <returns></returns>
private int FindNode(string nodeName)
{
int idx = Common.nodeInfo.FindIndex(s => s.Name.Equals(nodeName) && s.IsUse);
return idx;
}
private bool FindA6Leave(out string nextNode)
{
nextNode = "";
int idx = Common.nodeInfo.FindIndex(s => s.Name.Equals("A6")
&& (s.Action == ClientAction.NeedLeave || s.Action == ClientAction.NeedEnterLeave)
&& s.AgvName == "" && s.IsUse);//&& (s.RFID.StartsWith("C")|| s.RFID.StartsWith("D"))
if (idx == -1) return false;
bool rtn = FindA6Destination(Common.nodeInfo[idx].RFID, out string dest);
if (!rtn) return false;
idx = Common.nodeInfo.FindIndex(s => s.Name.Equals(dest) && s.IsUse);
if (idx == -1)
{
return false;
}
else
{
nextNode = dest;
return true;
}
}
private bool FindA6Destination(string rfid, out string dest)
{
dest = "";
try
{
string url = Common.itsHttp + rfid;
var client = new RestClient(url) { Timeout = -1 };
var request = new RestRequest(Method.GET);
//request.AddHeader("Host", "10.85.17.233");
IRestResponse response = client.Execute(request);
string json = response.Content;
json = json.Replace("\r", "");
json = json.Replace("\n", "");
json = json.Replace(" ", "");
Common.LogInfo("ITS URL: " + url + " Return: " + json);
if (string.IsNullOrWhiteSpace(json)) return false;
List<BoxDestInfo> res = JsonHelper.DeserializeJsonToList<BoxDestInfo>(json);
//JavaScriptSerializer serializer = new JavaScriptSerializer();
if (res == null || res.Count==0)
return false;
if (res[0].id == rfid)
{
if (res[0].location.Equals("Feeder"))
{
res[0].location = "FeederIn";
}
if (Common.agvProductionLine.TryGetValue(res[0].location, out string loc))
{
dest = loc;
Common.LogInfo("出满料架任务:目的地为 "+ loc+" [产线名 "+ res[0].location + "]");
return true;
}
}
//Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
//if (dic == null) return false;
//bool b1 = dic.TryGetValue("id", out object id);
//bool b2 = dic.TryGetValue("location", out object location);
//if (b1 && b2)
//{
// if (id.ToString() == rfid)
// {
// if (Common.agvProductionLine.TryGetValue(location.ToString(), out string loc))
// {
// dest = loc;
// return true;
// }
// }
//}
return false;
}
catch (Exception ex)
{
Common.log.Error("FindA6Destination", ex);
return false;
}
}
public class BoxDestInfo
{
/// <summary>
/// RFID
/// </summary>
public string id { get; set; }
public string SO { get; set; }
public string location { get; set; }
}
}
}