PathManger.cs
25.8 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
using CtuDeviceLib;
using log4net;
using Mushiny;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace DeviceLibrary
{
public class PathManger
{
static readonly ILog log = Common.LOGGER;
/// <summary>
/// 计算移动路径
/// </summary>
/// <param name="ctu">需要规划路径的CTU</param>
/// <param name="end">终点</param>
/// <param name="pointCodes">返回可路过的点</param>
/// <param name="errmsg">路径计算失败的原因</param>
/// <returns></returns>
public static bool PathCalc(CTU ctu, uint end, out List<PointCode> pointCodes, out string errmsg, List<uint> barrierPoints = null)
{
var start = ctu.CurLandMark;
pointCodes = new List<PointCode>();
errmsg = "";
var startPoint = LandMarks.GetPointCode(start);
var endPoint = LandMarks.GetPointCode(end);
if (start == end)
{
pointCodes.Add(startPoint);
return true;
}
if (startPoint == null)
{
errmsg = $"起始点【{start}】未查询到,无法规划";
return false;
}
if (endPoint == null)
{
errmsg = $"终点【{end}】未查询到,无法规划";
return false;
}
var startAp = new AStartPoint(startPoint);
var endAp = new AStartPoint(endPoint);
AStart aStartAlgo = new AStart();
var parent = aStartAlgo.FindPath(startAp, endAp, barrierPoints);
if (parent == null)
{
errmsg = $"规划失败";
return false;
}
while (parent != null)
{
pointCodes.Add(parent.Landmark);
parent = parent.ParentPoint;
}
pointCodes.Reverse();
return true; ;
}
/// <summary>
/// 获取两点间的最短距离
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static int GetPToPDis(uint p1, uint p2)
{
if (p1 == p2) return 0;
var startPoint = LandMarks.GetPointCode(p1);
var endPoint = LandMarks.GetPointCode(p2);
if (startPoint == null || endPoint == null) return int.MaxValue;
var start = new AStartPoint(startPoint);
var end = new AStartPoint(endPoint);
if (start == null || end == null) return int.MaxValue;
AStart aStartAlgo = new AStart();
var asp = aStartAlgo.FindPath(start, end);
if (asp != null)
{
return asp.F;
}
return int.MaxValue;
}
/// <summary>
/// 获取两点间的运行地码数量
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static int GetPToPCount(uint p1, uint p2)
{
if (p1 == p2) return 0;
var startPoint = LandMarks.GetPointCode(p1);
var endPoint = LandMarks.GetPointCode(p2);
if (startPoint == null || endPoint == null) return int.MaxValue;
var start = new AStartPoint(startPoint);
var end = new AStartPoint(endPoint);
if (start == null || end == null) return int.MaxValue;
AStart aStartAlgo = new AStart();
var asp = aStartAlgo.FindPath(start, end);
if (asp != null)
{
return asp.F;
}
return int.MaxValue;
}
/// <summary>
/// 获取当前ctu最近的出料分拣机入口
/// </summary>
/// <param name="ctuPos"></param>
/// <returns></returns>
public static bool GetNearOutLineIn(uint ctuPos, out string linename)
{
linename = "";
var outLineIns = RobotManager.GetOutLineIns();
if (outLineIns == null || outLineIns.Count == 0) return false;
int minDis = int.MaxValue;
foreach (var item in outLineIns)
{
var posInfo = PosInfos.GetPosInfoByName(item);
if (posInfo == null) continue;
var dis = GetPToPDis(ctuPos, posInfo.PointCode);
if (dis < minDis)
{
minDis = dis;
linename = item;
}
}
return !string.IsNullOrEmpty(linename);
}
/// <summary>
/// 查找距离给定路径3个点的损失最小(距离目的地最近)的避让点
/// </summary>
/// <param name="currentPoint">当前CTU的路径点</param>
/// <param name="destination">当前CTU的目标点</param>
/// <param name="usedPathPointCodes">对向CTU的路径点</param>
/// <returns></returns>
public static uint FindNoConflitPoint(uint CurLandMark, uint destination, List<PointCode> usedPathPointCodes)
{
//所有不在路径上的第一个点
var firstNoConflitPoints = new List<PointCode>();
var allreadyCalculatePoints = new List<uint>();
var waitCalculatePoints = new List<PointCode>();
waitCalculatePoints.Add(LandMarks.GetPointCode(CurLandMark));
while (waitCalculatePoints.Count != 0)
{
var nextWaitCalculatePoints = new List<PointCode>();
foreach (var waitPoint in waitCalculatePoints)
{
allreadyCalculatePoints.Add(waitPoint.Id);
if (usedPathPointCodes.Contains(waitPoint))
{
//还在路径上,继续找四周的点
if (waitPoint.Above != null && !allreadyCalculatePoints.Contains(waitPoint.Above.Id))
{
PointCode Above = LandMarks.GetPointCode(waitPoint.Above.Id);
nextWaitCalculatePoints.Add(Above);
}
if (waitPoint.Below != null && !allreadyCalculatePoints.Contains(waitPoint.Below.Id))
{
PointCode Below = LandMarks.GetPointCode(waitPoint.Below.Id);
nextWaitCalculatePoints.Add(Below);
}
if (waitPoint.Left != null && !allreadyCalculatePoints.Contains(waitPoint.Left.Id))
{
PointCode Left = LandMarks.GetPointCode(waitPoint.Left.Id);
nextWaitCalculatePoints.Add(Left);
}
if (waitPoint.Right != null && !allreadyCalculatePoints.Contains(waitPoint.Right.Id))
{
PointCode Right = LandMarks.GetPointCode(waitPoint.Right.Id);
nextWaitCalculatePoints.Add(Right);
}
}
else
{
//不在路径上,加入到不冲突的点
firstNoConflitPoints.Add(waitPoint);
}
}
waitCalculatePoints = nextWaitCalculatePoints;
}
var noConflitPoints = firstNoConflitPoints;
//扩展到距离路径3个点的所有点
for (int i = 0; i < 3; i++)
{
var expandNoConflitPoints = new List<PointCode>();
foreach (var noConflitPoint in noConflitPoints)
{
if (noConflitPoint.Above != null && !allreadyCalculatePoints.Contains(noConflitPoint.Above.Id))
{
PointCode Above = LandMarks.GetPointCode(noConflitPoint.Above.Id);
expandNoConflitPoints.Add(Above);
}
if (noConflitPoint.Below != null && !allreadyCalculatePoints.Contains(noConflitPoint.Below.Id))
{
PointCode Below = LandMarks.GetPointCode(noConflitPoint.Below.Id);
expandNoConflitPoints.Add(Below);
}
if (noConflitPoint.Left != null && !allreadyCalculatePoints.Contains(noConflitPoint.Left.Id))
{
PointCode Left = LandMarks.GetPointCode(noConflitPoint.Left.Id);
expandNoConflitPoints.Add(Left);
}
if (noConflitPoint.Right != null && !allreadyCalculatePoints.Contains(noConflitPoint.Right.Id))
{
PointCode Right = LandMarks.GetPointCode(noConflitPoint.Right.Id);
expandNoConflitPoints.Add(Right);
}
}
noConflitPoints = expandNoConflitPoints;
}
//查找到目的地路径最近的点
List<PointCode> nearstNoConflitPathCodes = new List<PointCode>();
var endAp = new AStartPoint(LandMarks.GetPointCode(destination));
uint noConflitPointId = 0;
foreach (var secondNoConflitPoint in noConflitPoints)
{
var startAp = new AStartPoint(secondNoConflitPoint);
AStart aStartAlgo = new AStart();
var parent = aStartAlgo.FindPath(startAp, endAp);
var pathCodes = new List<PointCode>();
while (parent != null)
{
pathCodes.Add(parent.Landmark);
parent = parent.ParentPoint;
}
if (pathCodes.Count > 0)
{
if (nearstNoConflitPathCodes.Count == 0 || pathCodes.Count <= nearstNoConflitPathCodes.Count)
{
nearstNoConflitPathCodes = pathCodes;
noConflitPointId = secondNoConflitPoint.Id;
}
}
}
return noConflitPointId;
}
/// <summary>
/// 查找该ctu最近的待机点
/// </summary>
/// <param name="ctu"></param>
/// <param name="ctuStandbies"></param>
/// <param name="ctuStandby"></param>
/// <returns></returns>
public static bool GetNearStand(CTU ctu, out CtuStandby ctuStandby)
{
ctuStandby = null;
List<CtuStandby> ctuStandbies = CtuStandbys.GetIdleStandbies(ctu);
if (ctuStandbies == null || ctuStandbies.Count == 0) return false;
Dictionary<int, CtuStandby> dis = new Dictionary<int, CtuStandby>();
ctuStandbies.ForEach(s =>
{
var dstInfo = PosInfos.GetPosInfoByName(s.Name);
if (dstInfo != null)
{
int d = GetPToPDis(ctu.CurLandMark, dstInfo.PointCode);
dis[d] = s;
}
});
if (dis.Count > 0)
{
var minKey = dis.Keys.Min();
ctuStandby = dis[minKey];
return ctuStandby != null;
}
return ctuStandby != null;
}
/// <summary>
/// 获取指定类型附近的限制区域
/// </summary>
/// <param name="ctu"></param>
/// <param name="usedPoints"></param>
/// <returns></returns>
public static List<PointCode> GetNearArea(List<PointCode> usedPoints, LandmarkType specifiedType)
{
int len = ConfigAppSettings.GetIntValue("CtuSelfSemiLength", 990, "CTU旋转半径");
var limitArea = new List<PointCode>();
if (usedPoints.Count == 0) return limitArea;
foreach (var ppc in usedPoints)
{
if (ppc.Type != specifiedType) continue;
int sumLen = 0;
var neighboor = ppc.Above;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Above;
}
}
sumLen = 0;
neighboor = ppc.Right;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Right;
}
}
sumLen = 0;
neighboor = ppc.Below;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Below;
}
}
sumLen = 0;
neighboor = ppc.Left;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Left;
}
}
}
return limitArea;
}
public static List<PointCode> GetNearArea(uint pointCode, LandmarkType specifiedType = LandmarkType.None)
{
int len = ConfigAppSettings.GetIntValue("CtuSelfSemiLength", 990, "CTU旋转半径");
var limitArea = new List<PointCode>();
var ppc = LandMarks.GetPointCode(pointCode);
if (ppc == null) return limitArea;
//foreach (var ppc in usedPoints)
{
if (specifiedType != LandmarkType.None)
{
if (ppc.Type != specifiedType) return limitArea;
}
int sumLen = 0;
var neighboor = ppc.Above;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Above;
}
}
sumLen = 0;
neighboor = ppc.Right;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Right;
}
}
sumLen = 0;
neighboor = ppc.Below;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Below;
}
}
sumLen = 0;
neighboor = ppc.Left;
while (neighboor != null)
{
var dis = neighboor.Distance;
var id = neighboor.Id;
sumLen += dis;
var code = LandMarks.GetPointCode(id);
if (code != null)
limitArea.Add(code);
if (sumLen > len)
{
break;
}
else
{
var tmpPP = LandMarks.GetPointCode(id);
neighboor = tmpPP.Left;
}
}
}
return limitArea;
}
}
public class AStart
{
static ILog log = Mushiny.Common.LOGGER;
List<AStartPoint> CloseList;
List<AStartPoint> OpenList;
public const int OBLIQUE = 14;
public const int STEP = 10;
public AStart()
{
OpenList = new List<AStartPoint>();
CloseList = new List<AStartPoint>();
}
public AStartPoint FindPath(AStartPoint start, AStartPoint end, List<uint> barrierPoints = null)
{
OpenList = new List<AStartPoint>();
CloseList = new List<AStartPoint>();
OpenList.Add(start);
while (OpenList.Count != 0)
{
//找出F值最小的点
var tempStart = OpenList.MinPoint();
OpenList.Remove(tempStart);
CloseList.Add(tempStart);
//找出它相邻的点
var surroundPoints = SurrroundPoints(tempStart, barrierPoints);
foreach (AStartPoint point in surroundPoints)
{
if (OpenList.Exists(point))
//计算G值, 如果比原来的大, 就什么都不做, 否则设置它的父节点为当前点,并更新G和F
FoundPoint(tempStart, point);
else if (!CloseList.Exists(point))
//如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
NotFoundPoint(tempStart, end, point);
}
if (OpenList.Get(end) != null)
return OpenList.Get(end);
}
return OpenList.Get(end);
}
private void FoundPoint(AStartPoint tempStart, AStartPoint point)
{
var G = CalcG(tempStart, point);
if (G < point.G)
{
point.ParentPoint = tempStart;
point.G = G;
point.CalcF();
}
log.Debug($"寻路算法-在开始列表:{point.Id}");
}
private void NotFoundPoint(AStartPoint tempStart, AStartPoint end, AStartPoint point)
{
point.ParentPoint = tempStart;
point.G = CalcG(tempStart, point);
point.H = CalcH(end, point);
point.CalcF();
OpenList.Add(point);
log.Debug($"寻路算法-{tempStart.Id}->{point.Id}不在开始列表,{point.Id} G=【{point.G}】");
}
private int CalcG(AStartPoint start, AStartPoint point)
{
var dis = start.Landmark.GetDis(point.Id);
// int G = (Math.Abs(point.X - start.X) + Math.Abs(point.Y - start.Y)) == 2 ? STEP : OBLIQUE;
int parentG = point.ParentPoint != null ? point.ParentPoint.G : 0;
return dis + parentG;
}
private int CalcH(AStartPoint end, AStartPoint point)
{
int step = Math.Abs(point.X - end.X) + Math.Abs(point.Y - end.Y);
return step * STEP;
}
/// <summary>
/// 获取某个点周围可以到达的点
/// </summary>
/// <param name="point"></param>
/// <param name="barrierPoints">不可走的点</param>
/// <returns></returns>
public List<AStartPoint> SurrroundPoints(AStartPoint point, List<uint> barrierPoints)
{
var surroundPoints = new List<AStartPoint>();
if (point.Landmark.Above != null)
{
var pid = point.Landmark.Above.Id;
if (barrierPoints == null || !barrierPoints.Contains(pid))
{
var p = new AStartPoint(LandMarks.GetPointCode(pid));
// if (!barrierPoints.HasBarrier(p) && PathWays.IsTwoWay(point.Id, point.Landmark.Above.Id))
surroundPoints.Add(p);
}
}
if (point.Landmark.Right != null)
{
var pid = point.Landmark.Right.Id;
if (barrierPoints == null || !barrierPoints.Contains(pid))
{
var p = new AStartPoint(LandMarks.GetPointCode(pid));
// if (!barrierPoints.HasBarrier(p) && PathWays.IsTwoWay(point.Id, point.Landmark.Right.Id))
surroundPoints.Add(p);
}
}
if (point.Landmark.Below != null)
{
var pid = point.Landmark.Below.Id;
if (barrierPoints == null || !barrierPoints.Contains(pid))
{
var p = new AStartPoint(LandMarks.GetPointCode(pid));
// if (!barrierPoints.HasBarrier(p) && PathWays.IsTwoWay(point.Id, point.Landmark.Below.Id))
surroundPoints.Add(p);
}
}
if (point.Landmark.Left != null)
{
var pid = point.Landmark.Left.Id;
if (barrierPoints == null || !barrierPoints.Contains(pid))
{
var p = new AStartPoint(LandMarks.GetPointCode(pid));
//if (!barrierPoints.HasBarrier(p) && PathWays.IsTwoWay(point.Id, point.Landmark.Left.Id))
surroundPoints.Add(p);
}
}
return surroundPoints;
}
}
public class AStartPoint
{
public uint Id { get { return Landmark.Id; } }
public AStartPoint ParentPoint { get; set; }
public int F { get; set; } //F=G+H
public int G { get; set; }
public int H { get; set; }
public int X { get { return Landmark.X; } }
public int Y { get { return Landmark.Y; } }
public PointCode Landmark { get; set; }
public AStartPoint(PointCode landmark)
{
Landmark = landmark;
}
public void CalcF()
{
this.F = this.G + this.H;
}
}
//对 List<Point> 的一些扩展方法
public static class ListHelper
{
public static bool Exists(this List<AStartPoint> points, AStartPoint point)
{
if (points == null || points.Count == 0) return false;
foreach (AStartPoint p in points)
if ((p.X == point.X) && (p.Y == point.Y))
return true;
return false;
}
/// <summary>
/// 是否有障碍
/// </summary>
/// <param name="points"></param>
/// <param name="point"></param>
/// <returns></returns>
public static bool HasBarrier(this List<AStartPoint> points, AStartPoint point)
{
if (point == null || point.Landmark == null)
{
return true;
}
if (points == null || points.Count == 0) return false;
foreach (AStartPoint p in points)
if (p.Landmark != null && p.Id == point.Landmark.Id)
return true;
return false;
}
public static AStartPoint MinPoint(this List<AStartPoint> points)
{
points = points.OrderBy(p => p.F).ToList();
return points[0];
}
public static AStartPoint Get(this List<AStartPoint> points, AStartPoint point)
{
foreach (AStartPoint p in points)
if ((p.X == point.X) && (p.Y == point.Y))
return p;
return null;
}
public static void Remove(this List<AStartPoint> points, int x, int y)
{
foreach (AStartPoint point in points)
{
if (point.X == x && point.Y == y)
points.Remove(point);
}
}
}
}