GetPath.cs
28.2 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
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Common;
namespace RCS.WinClient.Service
{
public class GetPath
{
/// <summary>
/// 为子任务分配路径
/// </summary>
public static void AssignePath()
{
foreach (Base_Agv agv in App.AgvList.FindAll(a => a.IsEnable))
{
try
{
var task = agv.AgvTask;
if (string.IsNullOrEmpty(agv.Barcode))
continue;
List<Base_PathPoint> insertPath = new List<Base_PathPoint>();
BaseDal<Base_SubTask> _sonTask = new BaseDal<Base_SubTask>();
BaseDal<Base_PathPoint> _pathPointDb = new BaseDal<Base_PathPoint>();
if (agv.HeightState == 0 || agv.AgvState == 0 || agv.AgvState == EnumMsg.State.离线) continue;
//if (task != null && task.SubTaskList.Count > 0) App.ExFile.MessageLog("AssignePath", $"主任务【{task.TaskNo}】子任务【{task.SubTaskList[0].SubTaskNo}】子任务状态【{task.SubTaskList[0].SubTaskState}】");
if (task == null || task.SubTaskList.Count == 0 || task.SubTaskList[0].SubTaskState > EnumMsg.SubTaskState.重建路径中) continue;
List<Base_PathPoint> pathPointList = GetPathList(agv);
var sonTask = task.SubTaskList[0];
if (pathPointList == null)
{
if (sonTask.SubTaskNo != App.AvoidSubTaskNo)
{
var updateresult0 = _sonTask.Update(it => it.SubTaskNo == sonTask.SubTaskNo, it => new Base_SubTask()
{
SubTaskState = EnumMsg.SubTaskState.建立路径失败
});
if (!updateresult0.Success) continue;
}
sonTask.SubTaskState = EnumMsg.SubTaskState.建立路径失败;
continue;
}
for (int i = 1; i <= pathPointList.Count; i++)
{
//拐点进行判断
Base_PathPoint pathPoint = pathPointList[i - 1];
//if (pathPoint.IsCorner)
//{
if (pathPoint.Point.PointType == EnumMsg.PointType.整体旋转点)
{
pathPoint.PathPointType = EnumMsg.PointType.整体旋转点;
}
//else if (pathPointList[i - 2].Point.PointType == EnumMsg.PointType.整体旋转点)
//{
//pathPointList[i - 2].pathPointType = EnumMsg.PointType.托盘旋转点;
//}
//else if (pathPointList[i] != null && pathPointList[i].Point.PointType == EnumMsg.PointType.整体旋转点)
//{
//pathPointList[i].pathPointType = EnumMsg.PointType.托盘旋转点;
//}
//}
}
for (int i = 1; i <= pathPointList.Count; i++)
{
var path = pathPointList[i - 1];
path.SubTaskNo = sonTask.SubTaskNo;
path.Barcode = path.Point.Barcode;
path.AreaType = path.Point.AreaType;
path.SerialNo = i;
insertPath.Add(path);
//var insPath = new Base_PathPoint()
//{
// AreaType = path.Point.AreaType,
// SubTaskNo = sonTask.SubTaskNo,
// Barcode = path.Point.Barcode,
// SerialNo = i,
// PathDirection = path.PathDirection,
// IsCorner = path.IsCorner,
// PathPointType = path.PathPointType,
// PathTim = path.PathTim
//};
}
if (sonTask.SubTaskNo != App.AvoidSubTaskNo)
{
_sonTask.BeginTran();
var insertresult = _pathPointDb.Insert(insertPath);
var updateresult1 = _sonTask.Update(it => it.SubTaskNo == sonTask.SubTaskNo, it => new Base_SubTask()
{
SubTaskState = EnumMsg.SubTaskState.已建立路径
});
if (!updateresult1.Success || !insertresult.Success)
{
_sonTask.RollbackTran();
return;
}
else
{
_sonTask.CommitTran();
}
}
sonTask.PathPointList = pathPointList;
sonTask.SubTaskState = EnumMsg.SubTaskState.已建立路径;
agv.PathPointList = sonTask.PathPointList.ToList();
}
catch (Exception ex)
{
App.ExFile.MessageError("AssignePath", $"原因:{ex.Message},\n 堆栈:{ex.StackTrace}");
}
}
}
/// <summary>
/// 生成路径
/// </summary>
/// <returns>路径集合</returns>
public static List<Base_PathPoint> GetPathList(Base_Agv agv)
{
try
{
Base_SubTask sonTask = agv.AgvTask.SubTaskList[0];
Base_Point startPoint = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
Base_Point endPoint = sonTask.EndPoint;
////20210422判断是否有去往终点的AGV(有两辆车去同一个点位的上下站台的情况,所以不能限制,否则卡死两个车)
//var result = App.TaskList.Where(a => a.TaskAgvNo != "" && a.TaskAgvNo != agv.AgvNo).ToList().SelectMany(b => b.SubTaskList).ToList();
//var result1 = result.Exists(a => a.SubTaskState != EnumMsg.SubTaskState.初始化 && a.EndPoint == endPoint);
//if (result1) return null;
////如果是行走任务要生成路径,要格外加判断,因为点位密集,要避免终点附近有其他行走子任务
//if (sonTask.SubTaskType == EnumMsg.AgvTaskType.行走 && startPoint.YLength != endPoint.YLength)
//{
// // 如果startPoint.YLength < endPoint.YLength,说明路线是X正方向行走的
// if (startPoint.YLength < endPoint.YLength)
// {
// //找到最近的有反向方向锁的点位
// var directionPoint = App.PointList.Where(x => x.IntX > startPoint.IntX && x.IntY == endPoint.IntY &&
// x.TmpDirectionList.Exists(y => y.PathDirection == EnumMsg.AgvOri.X负方向))
// .OrderBy(x => x.IntX)
// .FirstOrDefault();
// if (directionPoint != null)
// {
// //如果当前子任务的终点 + 车身长度 > 有反向方向锁的点位(因为终点是没有方向锁的,所以用directionPoint.XNegPoint),就不能生成路径
// if (endPoint.YLength + agv.YLength > directionPoint.XNegPoint.YLength)
// {
// sonTask.SubTaskState = EnumMsg.SubTaskState.建立路径失败;
// agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, "创建行走路径的时候,终点和其他路径的方向锁冲突,要等待其他任务完成!");
// return null;
// }
// }
// }
// // 如果startPoint.YLength > endPoint.YLength,说明路线是X负方向行走的
// if (startPoint.YLength > endPoint.YLength)
// {
// if (App.PointList.Exists(x => x.IntX <= endPoint.IntX && x.IntY == endPoint.IntY &&
// x.YLength > endPoint.YLength - agv.YLength - agv.YLength &&
// x.TmpDirectionList.Exists(y => y.PathDirection == EnumMsg.AgvOri.X正方向)))
// {
// sonTask.SubTaskState = EnumMsg.SubTaskState.建立路径失败;
// agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, "创建行走路径的时候,终点和其他路径的方向锁冲突,要等待其他任务完成!");
// return null;
// }
// }
//}
//判断是否在同一区域
List<Base_Point> listPoints = ComnMethod.GetAreaPoint(startPoint, endPoint);
startPoint = listPoints.First();
endPoint = listPoints.LastOrDefault();
EnumMsg.AgvTaskType taskType = sonTask.SubTaskType;
switch (taskType)
{
case EnumMsg.AgvTaskType.行走:
if (startPoint == endPoint)
return new List<Base_PathPoint>() { ComnMethod.GetPathPoint(endPoint) };
PathAStar pathAStar = new PathAStar();
List<Base_Point> listApoint = GetCanUseApoint(agv);
ClearParentPoint();
Base_Point Parent = pathAStar.GetPathPoint(listApoint, startPoint, endPoint, agv, 2);
if (Parent == null || Parent != null && Parent != endPoint)
{
sonTask.SubTaskState = EnumMsg.SubTaskState.建立路径失败;
return null;
}
//如果有多个路径点,就要检查终点附近,有没有相反的方向锁
if (Parent.ParentPoint != null && !CheckEndPoint(Parent.ParentPoint, Parent, agv))
{
sonTask.SubTaskState = EnumMsg.SubTaskState.建立路径失败;
return null;
}
List<Base_PathPoint> listPathPoint = GetPathPointList(Parent, sonTask);
//获取路径方点的方向
GetPathDirection(listPathPoint);
//获取路径点的拐点
bool isResult = GetPathCorner(listPathPoint, startPoint);
//锁定临时方向锁
OriLock(listPathPoint, agv);
return listPathPoint;
case EnumMsg.AgvTaskType.上升到顶:
case EnumMsg.AgvTaskType.下降到底:
case EnumMsg.AgvTaskType.升降到指定高度:
case EnumMsg.AgvTaskType.升降到指定位置:
case EnumMsg.AgvTaskType.充电:
case EnumMsg.AgvTaskType.取消充电:
case EnumMsg.AgvTaskType.等待:
case EnumMsg.AgvTaskType.上层接料:
case EnumMsg.AgvTaskType.上层下料:
case EnumMsg.AgvTaskType.下层接料:
case EnumMsg.AgvTaskType.下层下料:
case EnumMsg.AgvTaskType.上层接料下层接料:
case EnumMsg.AgvTaskType.上层接料下层下料:
case EnumMsg.AgvTaskType.下层接料下层下料:
case EnumMsg.AgvTaskType.下层下料下层下料:
return new List<Base_PathPoint>() { ComnMethod.GetPathPoint(endPoint) };
default:
throw new Exception("创建路径时无此任务类型:" + taskType.ToString());
}
}
catch (Exception ex)
{
throw new Exception($"GetPathList:{ex.Message},\n 堆栈:{ex.StackTrace}");
}
}
/// <summary>
/// 清除点的父类属性
/// </summary>
public static void ClearParentPoint()
{
foreach (Base_Point point in App.PointList)
{
if (point.ParentPoint != null)
point.ParentPoint = null;
}
}
/// <summary>
///获取可用的路径点
/// </summary>
private static List<Base_Point> GetCanUseApoint(Base_Agv agv)
{
Base_SubTask sonTask = agv.AgvTask.SubTaskList[0];
Base_Point startPoint = sonTask.StartPoint;
var curPoint = App.PointList.Find(a => a.Barcode == agv.Barcode);
if (sonTask.SubTaskState == EnumMsg.SubTaskState.重建路径中)
{
startPoint = App.PointList.Find(a => a.Barcode == agv.Barcode);
}
Base_Point endPoint = sonTask.EndPoint;
//获取可用点
List<Base_Point> listApoint = new List<Base_Point>();
foreach (Base_Point point in App.PointList)
{
if (point != startPoint && point != endPoint)
{
//起点周围点被锁定,且车的位置也在此点,则不建路径
//解决异常重建时,路径依旧堵死的问题
//if (point == startPoint.xNegPoint && point.lockedAgv != null && point.lockedAgv.strBarcode == point.strBarcode) continue;
//if (point == startPoint.xPosPoint && point.lockedAgv != null && point.lockedAgv.strBarcode == point.strBarcode) continue;
//if (point == startPoint.yNegPoint && point.lockedAgv != null && point.lockedAgv.strBarcode == point.strBarcode) continue;
//if (point == startPoint.yPosPoint && point.lockedAgv != null && point.lockedAgv.strBarcode == point.strBarcode) continue;
//以下点类型不使用
if (point.PointType == EnumMsg.PointType.死点) continue;
//禁用的点位不去
if (!point.IsEnable) continue;
//对站台点进行判断
//if (point.PointType == EnumMsg.PointType.站台点
// && agv.HeightState != EnumMsg.HeightState.低位)
//{
// var endSt = App.StationList.Find(a => a.Barcode == agv.AgvTask.EndBarcode);
// if (endSt == null || point.Barcode != endSt.PreBarcode)
// {
// continue;
// }
//}
//if (agv.HeightState != EnumMsg.HeightState.低位)
//{
// if ((curPoint.IntX == 160 && curPoint.IntY == 947)
// || (curPoint.IntX == 160 && curPoint.IntY == 969))
// {
// }
// else
// {
// if (point.IntX == 160 && point.IntY == 947) continue;
// if (point.IntX == 160 && point.IntY == 953) continue;
// if (point.IntX == 160 && point.IntY == 963) continue;
// if (point.IntX == 160 && point.IntY == 969) continue;
// }
//}
//if (point.pointType == EnumMsg.PointType.站台点
// && !App.StationList.Find(a => a.strBarcode == point.strBarcode).strStationNo.Contains("Empty")) continue;
//被占用,不使用
//if (point.isOccupy) continue;
//不包含agv的区域则不选择
if (point.AreaType != agv.CurrMap) continue;
}
listApoint.Add(point);
}
return listApoint;
}
/// <summary>
/// 路径点添加至路径集合
/// </summary>
/// <param name="Parent"></param>
/// <param name="type">任务预览/派发路径</param>
/// <returns></returns>
public static List<Base_PathPoint> GetPathPointList(Base_Point Parent, Base_SubTask subTask)
{
List<Base_PathPoint> listPathPoint = new List<Base_PathPoint>();
while (Parent != null)
{
listPathPoint.Add(ComnMethod.GetPathPoint(Parent));
Parent = Parent.ParentPoint;
}
listPathPoint.Reverse();
for (int i = 0; i < listPathPoint.Count; i++)
{
listPathPoint[i].SerialNo = i + 1;
listPathPoint[i].AreaType = listPathPoint[i].Point.AreaType;
listPathPoint[i].Barcode = listPathPoint[i].Point.Barcode;
if (subTask != null)
{
listPathPoint[i].SubTaskNo = subTask.SubTaskNo;
}
}
return listPathPoint;
}
#region 获取路径方向
/// <summary>
/// 获取路径方向
/// </summary>
/// <param name="pathList">路径集合</param>
public static void GetPathDirection(List<Base_PathPoint> pathList)
{
//终点不比较
for (int i = 0; i < pathList.Count - 1; i++)
{
Base_Point point = pathList[i].Point;
Base_Point nextPoint = pathList[i + 1].Point;
PathAStar pathAStar = new PathAStar();
EnumMsg.AgvOri pathDirection = pathAStar.GetDircition(nextPoint, point);
pathList[i].PathDirection = pathDirection;
}
}
#endregion 获取路径方向
#region 获取路径拐点
/// <summary>
/// 获取路径拐点
/// </summary>
/// <param name="pathList">路径点</param>
public static bool GetPathCorner(List<Base_PathPoint> pathList, Base_Point beginPoint)
{
try
{
if (beginPoint == null)
throw new Exception("获取路径拐点失败,起点终点不能为空");
for (int i = 0; i < pathList.Count - 1; i++)
{
//当前路径点
Base_PathPoint currentPoint = pathList[i];
if (currentPoint.Point == beginPoint)
{
//currentPoint.isCorner = true;
continue;
}
//上个路径点
Base_PathPoint beforePoint = pathList[i - 1];
if (currentPoint.PathDirection != beforePoint.PathDirection)
{
currentPoint.IsCorner = true;
}
else
{
currentPoint.IsCorner = false;
}
}
return true;
}
catch (Exception ex)
{
throw new Exception($"GetPathCorner:{ex.Message},\n 堆栈:{ex.StackTrace}");
}
}
#endregion 获取路径拐点
#region 锁定路径方向
/// <summary>
/// 锁定路径方向(最后一个点位,也就是终点不加方向所)
/// </summary>
/// <param name="pathList">从起点到终点的路径集合</param>
public static void OriLock(List<Base_PathPoint> pathList, Base_Agv agv)
{
//终点不比较
// int dir0 = 0;
for (int i = 0; i < pathList.Count - 1; i++)
{
Base_Point point = pathList[i].Point;
Base_Point nextPoint = pathList[i + 1].Point;
if (point == nextPoint || point.RegionName.Contains("Rotote") ||
point.RegionName.Contains("Mutex") || point.RegionName.Contains("Area"))
continue;
//高位发送目标点的防撞属性
if (agv.HeightState == EnumMsg.HeightState.高位)
{
var task = agv.AgvTask;
int flag = 0;
//临时根据任务条码判断防撞类型
if (!string.IsNullOrEmpty(task.TaskPallet) && task.TaskGroup == EnumMsg.TaskGroup.RCS单步任务.ToString() && int.TryParse(task.TaskPallet, out flag))
{
pathList[i].PathTim = int.Parse(task.TaskPallet);
}
else
{
var endStation = App.StationList.Find(a => a.Barcode == agv.AgvTask.EndPoint.Barcode);
if (endStation != null)
{
pathList[i].PathTim = endStation.Tim;
}
}
}
//if (nextPoint.PointType == EnumMsg.PointType.站台点 && point.PointType == EnumMsg.PointType.站台附属点)
//{
// continue;
//}
PathAStar pathAStar = new PathAStar();
EnumMsg.AgvOri pathDirection = pathAStar.GetDircition(nextPoint, point);
pathList[i].PathDirection = pathDirection;
TmpDirection td = new TmpDirection();
td.PathDirection = pathDirection;
td.AgvNo = agv.AgvNo;
point.TmpDirectionList.Add(td);
}
}
#endregion 锁定路径方向
#region Demo查找路径方法
/// <summary>
/// 清除点的父类属性
/// </summary>
public static void ClearDemoParentPoint()
{
foreach (Base_Point point in App.DemoPointList)
{
if (point.ParentPoint != null)
point.ParentPoint = null;
}
}
#endregion Demo查找路径方法
/// <summary>
/// 检查 终点 + 车身长度的距离内,是否有其他反向方向锁。如果有反向方向锁,说明有其他车来过来,那么去就会撞,或是卡住。
/// </summary>
/// <param name="lastPoint"></param>
/// <param name="endPoint"></param>
public static bool CheckEndPoint(Base_Point lastPoint, Base_Point endPoint, Base_Agv agv)
{
if (lastPoint.IntX == endPoint.IntX)
{
//如果最后的行走方向是Y正方向
if (lastPoint.YPosPoint != null && lastPoint.YPosPoint.IntY == endPoint.IntY)
{
//找到最近的有反向方向锁的点位
var directionPoint = App.PointList.Where(x => x.IntY > endPoint.IntY && x.IntX == endPoint.IntX &&
x.TmpDirectionList.Exists(y => y.PathDirection == EnumMsg.AgvOri.X负方向))
.OrderBy(x => x.IntY)
.FirstOrDefault();
if (directionPoint != null)
{
//如果当前子任务的终点 + 车身长度 > 有反向方向锁的点位(因为终点是没有方向锁的,所以用directionPoint.YNegPoint),就不能生成路径
if (endPoint.XLength + agv.Length > directionPoint.YNegPoint.XLength)
{
agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, "创建行走路径的时候,终点附近有其他路径的方向锁,要等待其他任务完成!");
return false;
}
}
return true;
}
//如果最后的行走方向是Y负方向
if (lastPoint.YNegPoint != null && lastPoint.YNegPoint.IntY == endPoint.IntY)
{
//找到最近的有反向方向锁的点位
var directionPoint = App.PointList.Where(x => x.IntY < endPoint.IntY && x.IntX == endPoint.IntX &&
x.TmpDirectionList.Exists(y => y.PathDirection == EnumMsg.AgvOri.X正方向))
.OrderByDescending(x => x.IntY)
.FirstOrDefault();
if (directionPoint != null)
{
//如果当前子任务的终点 - 车身长度 < 有反向方向锁的点位(因为终点是没有方向锁的,所以用directionPoint.YNegPoint),就不能生成路径
if (endPoint.XLength - agv.Length < directionPoint.YPosPoint.XLength)
{
agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, "创建行走路径的时候,终点附近有其他路径的方向锁,要等待其他任务完成!");
return false;
}
}
return true;
}
}
if (lastPoint.IntY == endPoint.IntY)
{
//如果最后的行走方向是X正方向
if (lastPoint.XPosPoint != null && lastPoint.XPosPoint.IntX == endPoint.IntX)
{
//找到最近的有反向方向锁的点位
var directionPoint = App.PointList.Where(x => x.IntX > endPoint.IntX && x.IntY == endPoint.IntY &&
x.TmpDirectionList.Exists(y => y.PathDirection == EnumMsg.AgvOri.X负方向))
.OrderBy(x => x.IntX)
.FirstOrDefault();
if (directionPoint != null)
{
//如果当前子任务的终点 + 车身长度 > 有反向方向锁的点位(因为终点是没有方向锁的,所以用directionPoint.XNegPoint),就不能生成路径
if (endPoint.YLength + agv.Length >= directionPoint.XNegPoint.YLength)
{
agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, "创建行走路径的时候,终点附近有其他路径的方向锁,要等待其他任务完成!");
return false;
}
}
return true;
}
//如果最后的行走方向是X负方向
if (lastPoint.XNegPoint != null && lastPoint.XNegPoint.IntX == endPoint.IntX)
{
//找到最近的有反向方向锁的点位
var directionPoint = App.PointList.Where(x => x.IntX < endPoint.IntX && x.IntY == endPoint.IntY &&
x.TmpDirectionList.Exists(y => y.PathDirection == EnumMsg.AgvOri.X正方向))
.OrderByDescending(x => x.IntX)
.FirstOrDefault();
if (directionPoint != null)
{
//如果当前子任务的终点 - 车身长度 < 有反向方向锁的点位(因为终点是没有方向锁的,所以用directionPoint.XNegPoint),就不能生成路径
if (endPoint.YLength - agv.Length <= directionPoint.XPosPoint.YLength)
{
agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, "创建行走路径的时候,终点附近有其他路径的方向锁,要等待其他任务完成!");
return false;
}
}
return true;
}
}
return false;
}
}
}