PathAStar.cs
25.5 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
using OfficeOpenXml.Drawing.Chart;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Common;
namespace RCS.WinClient.Service
{
public class PathAStar
{
/// <summary>
/// 生成的路径
/// </summary>
List<Base_Point> OpenList = new List<Base_Point>();
/// <summary>
///
/// </summary>
List<Base_Point> CloseList = new List<Base_Point>();
/// <summary>
///
/// </summary>
List<Base_Point> SurPointsList = new List<Base_Point>();
public const int CONER = 25;
public const int LINE = 3;
public const int MUTEX_LINE = 4;
public const int TOEND = 3;
/// <summary>
/// 寻找路径
/// </summary>
/// <param name="listApoint">可用点列表</param>
/// <param name="beginPoint">起点</param>
/// <param name="endPoint">终点</param>
/// <param name="actionType">AGV动作</param>
/// <param name="AgvDirection">车头方向</param>
/// <param name="clear">是否清除</param>
/// <returns></returns>
public Base_Point GetPathPoint(List<Base_Point> listApoint, Base_Point beginPoint, Base_Point endPoint, EnumMsg.ActionType actionType, EnumMsg.Direction AgvDirection, bool clear)
{
if (beginPoint == null || endPoint == null)
{
throw new Exception(" 查找路径失败,起点终点不能为空");
}
OpenList.Add(beginPoint);
if (clear)
{
CloseList.Clear();
}
int x = 0;
while (OpenList.Count != 0)
{
x++;
//按照F值从小到大排序
//将此点从Open表剔除,并加入Close表中
OpenList.Sort();
var currentPoint = OpenList[0];
OpenList.RemoveAt(0);
CloseList.Add(currentPoint);
List<Base_Point> aroundPoints;
//如果是起点,就需要特别处理
aroundPoints = GetAroundPoint(listApoint, currentPoint);
if (aroundPoints.Count == 0 && OpenList.Count == 0)
{
//查找规划失败后距离终点最近的点
var tempList = CloseList.OrderBy(x => x.XLength - endPoint.XLength + x.YLength - endPoint.YLength);
var nearestPoint = tempList.FirstOrDefault();
if (nearestPoint == beginPoint)
{
nearestPoint = tempList.LastOrDefault();
}
App.ExFile.MessageBusinessErr("GetPathPoint", $"起点为{beginPoint.Barcode}的agv,路线规划失败,最接近终点的点为{nearestPoint?.Barcode},此点为预估点请结合实际排查");
return currentPoint;
}
foreach (var judgePoint in aroundPoints)
{
if (OpenList.Contains(judgePoint))
{
var G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint, beginPoint);
if (G <= judgePoint.Value_G)
{
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = G;
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
}
}
else
{
//如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint, beginPoint);
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
OpenList.Add(judgePoint);
if (judgePoint.Barcode == endPoint.Barcode)
{
return judgePoint;
}
}
}
}
return OpenList.Last();
}
/// <summary>
/// 寻找路径
/// </summary>
/// <param name="listApoint">可用点列表</param>
/// <param name="beginPoint">起点</param>
/// <param name="endPoint">终点</param>
/// <param name="actionType">AGV动作</param>
/// <param name="AgvDirection">车头方向</param>
/// <param name="clear">是否清除</param>
/// <returns></returns>
public Base_Point GetPathPoint(List<Base_Point> listApoint, Base_Point beginPoint, Base_Point endPoint, Base_Agv agv, bool isClear)
{
if (beginPoint == null || endPoint == null)
{
throw new Exception(" 查找路径失败,起点终点不能为空");
}
OpenList.Add(beginPoint);
if (isClear)
{
CloseList.Clear();
}
int x = 0;
while (OpenList.Count != 0)
{
x++;
//按照F值从小到大排序
//将此点从Open表剔除,并加入Close表中
OpenList.Sort();
var currentPoint = OpenList[0];
OpenList.RemoveAt(0);
CloseList.Add(currentPoint);
List<Base_Point> aroundPoints;
aroundPoints = GetAroundPoint(listApoint, currentPoint);
if (aroundPoints.Count == 0 && OpenList.Count == 0)
{
//查找规划失败后距离终点最近的点
var tempList = CloseList.OrderBy(x => x.XLength - endPoint.XLength + x.YLength - endPoint.YLength);
var nearestPoint = tempList.FirstOrDefault();
if (nearestPoint == beginPoint)
{
nearestPoint = tempList.LastOrDefault();
}
App.ExFile.MessageBusinessErr("GetPathPoint", $"起点为{beginPoint.Barcode}的agv{agv.AgvNo},路线规划失败,最接近终点的点为{nearestPoint?.Barcode},此点为预估点请结合实际排查");
return currentPoint;
}
foreach (var judgePoint in aroundPoints)
{
if (OpenList.Contains(judgePoint))
{
var G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint, beginPoint);
if (G <= judgePoint.Value_G)
{
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = G;
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
}
}
else
{
//如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint, beginPoint);
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
OpenList.Add(judgePoint);
if (judgePoint.Barcode == endPoint.Barcode)
{
return judgePoint;
}
}
}
}
return OpenList.Last();
}
/// <summary>
/// 寻找四周能走的点
/// </summary>
/// <param name="listApoint">可用点集合</param>
/// <param name="currentPoint"></param>
/// <returns></returns>
public List<Base_Point> GetAroundPoint(List<Base_Point> listApoint, Base_Point currentPoint)
{
var listPoint = new List<Base_Point>();
//找到当前点的X+方向点,判断是否在可用点里面,再判断X+的点是否可走
var xPos = currentPoint.XPosPoint;
if (currentPoint.IsXPos && xPos != null && listApoint.Contains(xPos))
{
if (CanReach(xPos, currentPoint, EnumMsg.Direction.X正方向))
{
listPoint.Add(xPos);
}
}
var xNeg = currentPoint.XNegPoint;
if (currentPoint.IsXNeg && xNeg != null && listApoint.Contains(xNeg))
{
if (CanReach(xNeg, currentPoint, EnumMsg.Direction.X负方向))
{
listPoint.Add(xNeg);
}
}
var yPos = currentPoint.YPosPoint;
if (currentPoint.IsYPos && yPos != null && listApoint.Contains(yPos))
{
if (CanReach(yPos, currentPoint, EnumMsg.Direction.Y正方向))
{
listPoint.Add(yPos);
}
}
var yNeg = currentPoint.YNegPoint;
if (currentPoint.IsYNeg && yNeg != null && listApoint.Contains(yNeg))
{
if (CanReach(yNeg, currentPoint, EnumMsg.Direction.Y负方向))
{
listPoint.Add(yNeg);
}
}
return listPoint;
}
/// <summary>
/// 获取起点能去的点位,普通行走只能往车头行走,倒退行走只能往车尾行走
/// </summary>
/// <param name="currentPoint"></param>
/// <returns></returns>
public List<Base_Point> GetBeginAroundPoint(List<Base_Point> listApoint, Base_Point beginPoint, Base_Point endPoint, EnumMsg.ActionType actionType, EnumMsg.Direction AgvDirection)
{
var listPoint = new List<Base_Point>();
//站台倒车会撞站台,充电桩倒车会撞墙
if (actionType == EnumMsg.ActionType.倒车行走 && beginPoint.PointType == EnumMsg.PointType.站台点)
{
return listPoint;
}
var nextPoint = GetDirectionPoint(beginPoint, actionType, AgvDirection);
if (nextPoint != null && listApoint.Contains(nextPoint))
{
//如果是去【站台附属点、充点电】就不管 方向是否通行,是否有方向锁。其他的就需要判断。因为金利隆项目,是环形单向,去站台附属点、充点电的倒退,是没有方向的,所以强行加上。
var pointTypes = new EnumMsg.PointType[] { EnumMsg.PointType.站台附属点, EnumMsg.PointType.充电点 };
//获取从起点到下个点的方向(不用车头方向,是因为倒退行走的方向不是车头方向)
var flag = App.Config_KeyValueDic.TryGetValue(beginPoint.Barcode, out var res);
var pointDircition = GetDircition(nextPoint, beginPoint);
if (pointTypes.Contains(endPoint.PointType)
|| nextPoint.Barcode.In("3510", "4656")
|| flag && res!.DoubleValue == int.Parse(endPoint.Barcode)
|| CanReach(nextPoint, beginPoint, pointDircition))
{
listPoint.Add(nextPoint);
}
}
return listPoint;
}
/// <summary>
/// 能否选择此点
/// </summary>
/// <param name="judgePoint">目标点</param>
/// <param name="currentPoint">当前点</param>
/// <param name="flag">从当前点到目标点的方向</param>
/// <returns></returns>
public bool CanReach(Base_Point judgePoint, Base_Point currentPoint, EnumMsg.Direction pathDirection)
{
if (judgePoint == null)
{
return false;
}
//关掉的点,排除
if (CloseList.Exists(a => a.Barcode == judgePoint.Barcode))
{
return false;
}
if (App.AgvList.Count == 0)
{
return false;
}
//面对面行驶路径生成的安全距离:4个车头距离 + 300的雷达距离
var safeDistance = App.AgvList[0].HeadLength * 4 + 300;
switch (pathDirection)
{
case EnumMsg.Direction.X正方向:
//判断当前点的固定方向是否允许X+方向行走
if (!currentPoint.IsXPos)
{
return false;
}
//对于起点的X正方向的安全距离内,若临时方向存在X -,则不能选择此点
if (App.PointList.Any(x => x.IntY == currentPoint.IntY && x.IntX > currentPoint.IntX && x.XLength <= currentPoint.XLength + safeDistance
&& x.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.X负方向)))
{
return false;
}
//对于X+的四周点,若临时方向存在X-,则不能选择此点
if (judgePoint.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.X负方向))
{
return false;
}
break;
case EnumMsg.Direction.X负方向:
if (!currentPoint.IsXNeg)
{
return false;
}
if (App.PointList.Any(x => x.IntY == currentPoint.IntY && x.IntX < currentPoint.IntX && x.XLength >= currentPoint.XLength - safeDistance
&& x.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.X正方向)))
{
return false;
}
if (judgePoint.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.X正方向))
{
return false;
}
break;
case EnumMsg.Direction.Y正方向:
if (!currentPoint.IsYPos)
{
return false;
}
if (App.PointList.Any(x => x.IntX == currentPoint.IntX
&& x.IntY > currentPoint.IntY
&& x.XLength <= currentPoint.YLength + safeDistance
&& x.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.Y负方向)))
{
return false;
}
if (judgePoint.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.Y负方向))
{
return false;
}
break;
case EnumMsg.Direction.Y负方向:
if (!currentPoint.IsYNeg)
{
return false;
}
if (App.PointList.Any(x => x.IntX == currentPoint.IntX && x.IntY < currentPoint.IntY && x.XLength >= currentPoint.YLength - safeDistance
&& x.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.Y正方向)))
{
return false;
}
if (judgePoint.TmpDirectionList.Exists(a => a.PathDirection == EnumMsg.Direction.Y正方向))
{
return false;
}
break;
default:
throw new Exception("创建路径失败:找不到此点的方向。");
}
return true;
}
/// <summary>
/// 获取从【当前点】到【要判断的点】的行走方向(必须是相邻的两个点)
/// </summary>
/// <param name="judgePoint">要判断的点</param>
/// <param name="currentPoint">当前点</param>
/// <returns></returns>
public EnumMsg.Direction GetDircition(Base_Point judgePoint, Base_Point currentPoint)
{
if (judgePoint.IntX == currentPoint.IntX)
{
if (currentPoint.YPosPoint != null && judgePoint.IntY == currentPoint.YPosPoint.IntY)
{
return EnumMsg.Direction.Y正方向;
}
else if (currentPoint.YNegPoint != null && judgePoint.IntY == currentPoint.YNegPoint.IntY)
{
return EnumMsg.Direction.Y负方向;
}
}
if (judgePoint.IntY == currentPoint.IntY)
{
if (currentPoint.XPosPoint != null && judgePoint.IntX == currentPoint.XPosPoint.IntX)
{
return EnumMsg.Direction.X正方向;
}
else if (currentPoint.XNegPoint != null && judgePoint.IntX == currentPoint.XNegPoint.IntX)
{
return EnumMsg.Direction.X负方向;
}
}
throw new Exception("无法找到此两点的路径方向:" + judgePoint.Barcode + "-" + currentPoint.Barcode);
}
/// <summary>
/// G值权重的计算
/// </summary>
/// <param name="judgePoint"></param>
/// <param name="currentPoint"></param>
/// <returns></returns>
public double ConditionG(Base_Point judgePoint, Base_Point currentPoint, Base_Point beginPoint)
{
double value = LINE;
if (currentPoint.ParentPoint != null)
{
Base_Point parentPoint = currentPoint.ParentPoint;
//如果三点在一个直线上,则等于4,否则等于8
var isXLine = parentPoint.IntX == currentPoint.IntX && judgePoint.IntX == parentPoint.IntX;
var isYLine = parentPoint.IntY == currentPoint.IntY && judgePoint.IntY == parentPoint.IntY;
if (isXLine || isYLine || judgePoint.ParentPoint == beginPoint)
{
value = LINE;
//如果是关键路段,则权重加大
if (judgePoint.RegionName.Contains("Mutex"))
{
value = MUTEX_LINE;
}
}
else
{
value = CONER;
}
}
//返回值加上将要行走的小车数量
return value + judgePoint.TmpDirectionList.Count + judgePoint.XPosGValue;
}
public double ConditionG(Base_Point judgePoint, Base_Point currentPoint, Base_Point beginPoint, bool flag)
{
double value = LINE;
Base_Point parentPoint;
//获取判断点基于当前点的行进方向
string direction;
if (judgePoint.IntX == currentPoint.ParentPoint.IntX)
{
direction = "Y";
}
else
{
direction = "X";
}
if (currentPoint.ParentPoint != null)
{
parentPoint = currentPoint.ParentPoint;
//如果三点在一个直线上,则等于4,否则等于8
var isXLine = parentPoint.IntX == currentPoint.IntX && judgePoint.IntX == parentPoint.IntX;
var isYLine = parentPoint.IntY == currentPoint.IntY && judgePoint.IntY == parentPoint.IntY;
if (isXLine || isYLine || judgePoint.ParentPoint == beginPoint)
{
value = LINE;
//如果是关键路段,则权重加大
if (judgePoint.RegionName.Contains("Mutex"))
{
value = MUTEX_LINE;
}
}
else
{
value = CONER;
}
}
if (App.StationList.Any(x => x.PreBarcode == judgePoint.Barcode))
{
value += 10000;
}
//优先走同向车道,避免道路占用
var sameDirectionCount = currentPoint.TmpDirectionList.Count(x => x.PathDirection.ToString().StartsWith(direction));
//返回值加上将要行走的小车数量
return value + judgePoint.TmpDirectionList.Count - sameDirectionCount;
}
/// <summary>
/// H值权重的计算
/// </summary>
/// <param name="judgePoint"></param>
/// <param name="endPoint"></param>
/// <returns></returns>
public double CalcH(Base_Point judgePoint, Base_Point endPoint)
{
//查找距离目标点的距离(X+Y)*4
//int dtX = Math.Abs(judgePoint.IntX - endPoint.IntX);
//int dtY = Math.Abs(judgePoint.IntY - endPoint.IntY);
int dtX = Math.Abs(judgePoint.XLength - endPoint.XLength);
int dtY = Math.Abs(judgePoint.YLength - endPoint.YLength);
return (dtX + dtY) * TOEND;
//return (dtX + dtY) * 0;
}
/// <summary>
/// 根据动作和和车头方向,获取下一个点位
/// </summary>
/// <param name="currentPoint">当前点</param>
/// <param name="actionType">动作</param>
/// <param name="AgvDirection">车头方向</param>
/// <returns></returns>
public Base_Point? GetDirectionPoint(Base_Point currentPoint, EnumMsg.ActionType actionType, EnumMsg.Direction AgvDirection)
{
Base_Point? directionPoint = null;
switch (AgvDirection)
{
case EnumMsg.Direction.X正方向:
directionPoint = actionType == EnumMsg.ActionType.倒车行走 ? currentPoint.XNegPoint : currentPoint.XPosPoint;
break;
case EnumMsg.Direction.Y正方向:
directionPoint = actionType == EnumMsg.ActionType.倒车行走 ? currentPoint.YNegPoint : currentPoint.YPosPoint;
break;
case EnumMsg.Direction.X负方向:
directionPoint = actionType == EnumMsg.ActionType.倒车行走 ? currentPoint.XPosPoint : currentPoint.XNegPoint;
break;
case EnumMsg.Direction.Y负方向:
directionPoint = actionType == EnumMsg.ActionType.倒车行走 ? currentPoint.YPosPoint : currentPoint.YNegPoint;
break;
}
return directionPoint;
}
/// <summary>
/// 寻找路径
/// </summary>
/// <param name="listApoint">可用的点</param>
/// <param name="beginPoint">起点</param>
/// <param name="endPoint">终点</param>
/// <param name="agv"></param>
/// <returns></returns>
public Base_Point DemoGetPathPoint(List<Base_Point> listApoint, Base_Point beginPoint, Base_Point endPoint, Base_Agv agv)
{
if (beginPoint == null || endPoint == null)
throw new Exception(" 查找路径失败,起点终点不能为空");
OpenList.Add(beginPoint);
int x = 0;
while (OpenList.Count != 0)
{
x++;
//按照F值从小到大排序
//将此点从Open表剔除,并加入Close表中
OpenList.Sort();
Base_Point currentPoint = OpenList[0];
OpenList.RemoveAt(0);
CloseList.Add(currentPoint);
//找出它相邻的点
List<Base_Point> aroundPoints = GetAroundPoint(listApoint, currentPoint);
if (aroundPoints.Count == 0 && OpenList.Count == 0) return currentPoint;
foreach (Base_Point judgePoint in aroundPoints)
{
if (OpenList.Contains(judgePoint))
{
var G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint, beginPoint);
if (G <= judgePoint.Value_G)
{
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = G;
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
}
}
else
{
//如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint, beginPoint);
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
OpenList.Add(judgePoint);
if (judgePoint.Barcode == endPoint.Barcode)
return judgePoint;
}
}
}
return OpenList.Last();
}
}
}