OutTrayLineBean.cs
23.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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 出料皮带线,皮带线5=NG线,NG料在内侧,紧急出料在外侧
/// </summary>
public class OutTrayLineBean
{
private bool NgInReverseSide = true;
public int MaxPosition = 5;
private int equipBeanId = 301;
private string Line3_TrayCheck = "";
//出口定位信号
private string ExitLocation_Check = "";
//出口料盘检测信号
private string ExitTray_Check = "";
//出口料盘检测信号
private string LineRun_Do = "";
//最后一盘料的位置,>5时可置为0,0表示当前皮带线无料盘
public int LastTrayPosition = 0;
private string DLine_Brush_Do = "";
private int burshDelayS = ConfigAppSettings.GetIntValue(Setting_Init.Brush_Delay_Seconds);
private DateTime BrushStopTime = DateTime.Now;
private string Name = "";
private LineTurnBean lineRun = null;
private string cameraName = "";
public ReelCheckParam reelParam = null;
internal LineMoveInfo MoveInfo = null;
public DischargeLine equipBean
{
get { return LineManager.Line.DisLineMap[equipBeanId]; }
}
internal bool Line3CanRun = true;
public int Id = 0;
public OutTrayLineBean(int id, string name, string exitLocationS, string exittrayS, string lineRundo, string line3checkDi, int dId, string brushDo,string cName)
{
this.Id = id;
this.equipBeanId = dId;
this.Name = name;
MoveInfo = new LineMoveInfo(Id, name);
this.ExitLocation_Check = exitLocationS;
this.ExitTray_Check = exittrayS;
this.LineRun_Do = lineRundo;
this.Line3_TrayCheck = line3checkDi;
this.DLine_Brush_Do = brushDo;
lineRun = new LineTurnBean(dId, LineRun_Do, ExitLocation_Check);
this.cameraName = cName;
}
public void StopRun()
{
MoveInfo.EndMove();
lineRun.StopLineRun();
Line3CanRun = true;
}
#region 出库处理
// private string posId = "";
public bool StartOut(InOutParam moveParam)
{
if (LineTurnIsStop() && MoveInfo.MoveType.Equals(LineMoveType.None) && (equipBean.runStatus.Equals(LineRunStatus.Runing) || equipBean.runStatus.Equals(LineRunStatus.Busy)))
{
int needEmptyP = 1;
if (moveParam.InStoreNg)
{
needEmptyP = 2;
}
if (LastTrayPosition <= needEmptyP)
{
LogUtil.error(Name + " 启动送料【" + moveParam.ToStr() + "】 失败,入口有料", Id * 1000 + 32);
return false;
}
//MoveInfo.MoveParam = moveParam;
MoveInfo.NewMove(LineMoveType.OutStore, moveParam);
posId = "[" + moveParam.PosId + "][" + moveParam.WareCode + "][" + moveParam.rfid + "]";
if (moveParam.InStoreNg)
{
posId = moveParam.rfid + "NG料" + "[" + moveParam.WareCode + "]";
}
else if (moveParam.urgentReel)
{
posId = "[" + moveParam.PosId + "]紧急出料" + "[" + moveParam.WareCode + "]";
}
MoveInfo.NextMoveStep(LineMoveStep.DLO_01_NGBack);
LogUtil.info(Name + "开始送料【" + moveParam.ToStr() + "】:" + MoveInfo.SLog + "NG气缸后退");
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(300));
if (equipBeanId.Equals(302))
{
if (equipBean.CylinderIsOk(IO_Type.NGCylinder_Before, IO_Type.NGCylinder_After))
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_02_WaitEnteryNoTray);
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "--- 等待入口无料-- ");
}
else
{
equipBean.CylinderMove(MoveInfo, IO_Type.NGCylinder_Before, IO_Type.NGCylinder_After);
}
}
else
{
if (LastTrayPosition > needEmptyP)
{
WaitReviceTray();
}
else
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_02_WaitEnteryNoTray);
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "--- 等待入口 " + needEmptyP + " 无料-- ");
}
}
return true;
}
else
{
LogUtil.error(Name + " 启动送料【" + moveParam.ToStr() + "】 失败", Id * 1000 + 31);
return false;
}
}
private void WaitReviceTray()
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_04_CanReviceTray);
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "--- 等待移栽放料盘-- ");
LastTrayPosition = 1;
if (equipBean.IsDebug)
{
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(3000));
}
}
string posId = "";
internal void OutStoreProcess()
{
if (MoveInfo.IsInWait)
{
equipBean.CheckWait(MoveInfo);
}
if (MoveInfo.IsInWait)
{
return;
}
if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_01_NGBack))
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_02_WaitEnteryNoTray);
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "--- 等待入口无料-- ");
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_02_WaitEnteryNoTray))
{
int needEmptyP = 1;
if (MoveInfo.MoveParam.InStoreNg)
{
needEmptyP = 2;
}
if (LastTrayPosition > needEmptyP)
{
WaitReviceTray();
}
else if (MoveInfo.IsTimeOut(180))
{
LogUtil.error(Name + "等待入口" + needEmptyP + "无料超时" + FormUtil.GetSpanStr(MoveInfo.StepSpan()), Id * 1000 + 33);
}
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_04_CanReviceTray))
{
if (equipBean.IsDebug)
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_05_TrayIsOk);
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "调试中,更改为已放料盘");
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
}
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_05_TrayIsOk))
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_06_WaitTime);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
//判断ng气缸是否需要前进
if (MoveInfo.MoveParam.InStoreNg && NgInReverseSide)
{
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "盘已放,等待2000,NG气缸前进");
equipBean.CylinderMove(MoveInfo, IO_Type.NGCylinder_After, IO_Type.NGCylinder_Before);
}
else
{
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "盘已放,等待2000");
}
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_06_WaitTime))
{
if (equipBean.IOValue(ExitTray_Check).Equals(IO_VALUE.HIGH))
{
//皮带线不转,并且入口无信号
if (equipBean.Line3TurnIsStop() && equipBean.IOValue(Line3_TrayCheck).Equals(IO_VALUE.LOW))
{
Line3CanRun = false;
if (Line3_TrayCheck.Equals(IO_Type.EntryTray_Check3))
{
equipBean.Line3LastTrayP = 1;
}
}
else
{
return;
}
}
MoveInfo.NextMoveStep(LineMoveStep.DLO_07_LineRun);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + " [" + LastTrayPosition + "],转动到下个工位");
if (equipBean.IOValue(ExitTray_Check).Equals(IO_VALUE.HIGH))
{
StartBrush();
}
lineRun.StartLineRun(LineEndProcess);
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_07_LineRun))
{
if (LineTurnIsStop())
{
if (MoveInfo.MoveParam.InStoreNg && NgInReverseSide)
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_09_NGAfter);
LastTrayPosition++;
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + " [" + LastTrayPosition + "],NG气缸后退");
equipBean.CylinderMove(MoveInfo, IO_Type.NGCylinder_Before, IO_Type.NGCylinder_After);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
}
else if (MoveInfo.MoveParam.urgentReel || MoveInfo.MoveParam.InStoreNg)
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_08_NGBefore);
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + "NG气缸前进");
equipBean.CylinderMove(MoveInfo, IO_Type.NGCylinder_After, IO_Type.NGCylinder_Before);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
}
else
{
MoveInfo.EndMove();
LogUtil.info(Name + "送料【" + posId + "】结束");
}
}
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_08_NGBefore))
{
MoveInfo.NextMoveStep(LineMoveStep.DLO_09_NGAfter);
LastTrayPosition++;
LogUtil.info(Name + "送料【" + posId + "】:" + MoveInfo.SLog + " [" + LastTrayPosition + "],NG气缸后退");
equipBean.CylinderMove(MoveInfo, IO_Type.NGCylinder_Before, IO_Type.NGCylinder_After);
}
else if (MoveInfo.MoveStep.Equals(LineMoveStep.DLO_09_NGAfter))
{
MoveInfo.EndMove();
LogUtil.info(Name + "送料【" + posId + "】:结束");
}
}
private bool LineTurnIsStop()
{
if (lineRun.InTurn.Equals(false))
{
if (equipBean.IOValue(LineRun_Do).Equals(IO_VALUE.LOW))
{
if (lineRun.CanStart(500))
{
return true;
}
}
else
{
equipBean.IOMove(LineRun_Do, IO_VALUE.LOW);
}
}
return false;
}
/// <summary>
/// 转动结束,最后一盘料的位置+1
/// </summary>
/// <param name="result"></param>
public void LineEndProcess(bool result)
{
if (result && LastTrayPosition > 0)
{
LastTrayPosition++;
}
if (!Line3CanRun)
{
equipBean.Line3Turn.LastEndTime = DateTime.Now;
}
Line3CanRun = true;
//equipBean.IOMove(DLine_Brush_Do, IO_VALUE.HIGH);
//BrushStopTime = DateTime.Now.AddSeconds(burshDelayS);
}
/// <summary>
/// 此皮带线是否有盘。重启会重置为空
/// </summary>
/// <returns></returns>
internal bool HasTray()
{
if (LastTrayPosition > 0 &&
LastTrayPosition <= MaxPosition * 2)
{
return true;
}
if (equipBean.IOValue(ExitTray_Check).Equals(IO_VALUE.HIGH))
{
if (LastTrayPosition > MaxPosition)
{
LastTrayPosition = MaxPosition;
}
return true;
}
return false;
}
internal bool IsInTurn()
{
return lineRun.InTurn;
}
#endregion
public void TimerPro()
{
//皮带线1出口无料
if (IsInTurn().Equals(false) && HasTray() && lineRun.CanStart())
{
bool exitHasTray = false;
bool line3HasTray = false;
string imgName = "";
//如果有监控相机
if (!cameraName.Equals(""))
{
int[] results = ReelCheckUtil.ReelCheck(equipBean.DeviceID, cameraName,reelParam,out imgName);
if (results != null && results.Length == 2)
{
exitHasTray = (results[0] == 1);
line3HasTray = (results[1] == 1);
if (exitHasTray && equipBean.IOValue(ExitTray_Check).Equals(IO_VALUE.LOW))
{
LogUtil.error(Name + "出口信号检测无料,视觉检测有料,认为出口有料");
}
if (line3HasTray && equipBean.IOValue(Line3_TrayCheck).Equals(IO_VALUE.LOW))
{
LogUtil.error(Name + ",长皮带线" + Line3_TrayCheck + "=LOW,视觉检测有料,认为长皮带线有料");
}
}
}
if (MoveInfo.MoveType.Equals(LineMoveType.None) &&
equipBean.IOValue(ExitTray_Check).Equals(IO_VALUE.LOW) && (!exitHasTray))
{
//皮带线1出口无料
//StartBrush();
lineRun.StartLineRun(LineEndProcess);
}
else if (MoveInfo.MoveStep <= LineMoveStep.DLO_02_WaitEnteryNoTray && MoveInfo.IsInWait.Equals(false) &&
(equipBean.IOValue(ExitTray_Check).Equals(IO_VALUE.HIGH) || exitHasTray))
{
//皮带线1出口有料,皮带3入库无料,且在停止状态
if (equipBean.Line3TurnIsStop() && equipBean.IOValue(Line3_TrayCheck).Equals(IO_VALUE.LOW) && (!line3HasTray))
{
Line3CanRun = false;
LogUtil.debug(Name + "出口有料,且" + Line3_TrayCheck + "=LOW,转动一个工位,[" + LineRun_Do + "] [" + ExitLocation_Check + "]");
if (!cameraName.Equals(""))
{
LogUtil.info(Name + "出口有料,长皮带线无料,转动一个工位,"+ exitHasTray + ","+ line3HasTray + ",imgName= "+ imgName + "");
}
StartBrush();
lineRun.StartLineRun(LineEndProcess);
}
}
}
if (DateTime.Now <= BrushStopTime)
//if (DateTime.Now <= BrushStopTime || equipBean.IOValue(LineRun_Do).Equals(IO_VALUE.HIGH))
{
equipBean.CheckAndMove(DLine_Brush_Do, IO_VALUE.HIGH);
}
else
{
equipBean.CheckAndMove(DLine_Brush_Do, IO_VALUE.LOW);
}
}
public void StartBrush()
{
BrushStopTime = DateTime.Now.AddSeconds(burshDelayS + 8);
equipBean.IOMove(DLine_Brush_Do, IO_VALUE.HIGH);
}
}
public class LineTurnBean
{
public delegate void TurnEndDel(bool result);
public TurnEndDel turnEnd = null;
public bool InTurn = false;
private string MoveDO = "";
private string CheckDI = "";
private int subType = 0;
internal DateTime LastEndTime = DateTime.Now;
private bool WaitCheckDI_High = false;
private bool CheckDIHigh = false;
public bool CanStart(int totalMS = 1000)
{
if (IOManager.IOValue(MoveDO, subType).Equals(IO_VALUE.LOW))
{
TimeSpan span = DateTime.Now - LastEndTime;
if (span.TotalMilliseconds > totalMS)
{
return true;
}
}
return false;
}
public LineTurnBean(int subType, string moveDO = "", string checkDI = "")
{
this.subType = subType;
this.MoveDO = moveDO;
this.CheckDI = checkDI;
if (!moveDO.Equals("") && (!checkDI.Equals("")))
{
//添加事件
AIOBOXManager.AddDIEvent(checkDI, subType, DIChangePro);
}
}
public void DIChangePro(string ioIP, ushort index, IO_VALUE oldValue, IO_VALUE newValue)
{
if (WaitCheckDI_High && InTurn && (!CheckDIHigh) && oldValue.Equals(IO_VALUE.LOW) && newValue.Equals(IO_VALUE.HIGH))
{
ConfigIO config = IOManager.GetDI(CheckDI, subType);
if (config.IO_IP.Equals(ioIP) && config.GetIOAddr().Equals(index))
{
LogUtil.debug("收到DIChangeDelegate[" + ioIP + "][" + index + "][" + oldValue + "][" + newValue + "],更新CheckDIHigh=true ");
CheckDIHigh = true;
}
}
}
public void StopLineRun()
{
if (InTurn)
{
WaitCheckDI_High = false;
CheckDIHigh = false;
InTurn = false;
}
}
public void StartLineRun(TurnEndDel endProcess, int timeOutMS = 30000)
{
StartLineRun(MoveDO, CheckDI, endProcess, timeOutMS);
}
private int canWhileCount = 3;
public void StartLineRun(string moveDO, string checkDI, TurnEndDel endProcess, int timeOutMS = 30000)
{
MoveDO = moveDO;
CheckDI = checkDI;
InTurn = true;
this.turnEnd = endProcess;
Task.Factory.StartNew(delegate
{
canWhileCount = 3;
LineRunAndWait(timeOutMS);
});
}
private string LineRunAndWait(int timeOutMS = 30000)
{
//MoveDO = moveDO;
//CheckDI = checkDI;
string logName = "LineTurn[" + subType + "][" + MoveDO + "][" + CheckDI + "] ";
LogUtil.debug("开始转动:" + logName);
IOManager.IOMove(MoveDO, IO_VALUE.HIGH, subType);
InTurn = true;
DateTime startTime = DateTime.Now;
string result = "";
bool isStop = false;
WaitCheckDI_High = false;
CheckDIHigh = false;
//先等待信号灭一次,然后再亮时停止
while (true)
{
Thread.Sleep(20);
TimeSpan span = DateTime.Now - startTime;
if (span.TotalSeconds > 1 && IOManager.IOValue(MoveDO, subType).Equals(IO_VALUE.LOW))
{
LogUtil.error(logName + "转动,已等待[" + FormUtil.GetSpanStr(span) + "],重写DO");
IOManager.IOMove(MoveDO, IO_VALUE.HIGH, subType);
}
if (!InTurn)
{
isStop = true;
result = "手动停止";
}
else if (span.TotalMilliseconds > timeOutMS)
{
isStop = true;
result = "转动超时";
}
//判断Buzy及位置是否结束
else if (IOManager.IOValue(CheckDI, subType).Equals(IO_VALUE.LOW))
{
isStop = true;
}
if (isStop)
{
if (result.Equals(""))
{
LogUtil.debug(logName + "验证" + CheckDI + "=Low结束:" + result);
}
else
{
LogUtil.info(logName + " 验证" + CheckDI + "=Low结束:" + result);
}
break;
}
}
result = "";
isStop = false;
WaitCheckDI_High = true;
CheckDIHigh = false;
while (true)
{
Thread.Sleep(10);
TimeSpan span = DateTime.Now - startTime;
bool isRestart = false;
if (span.TotalSeconds > 2 && IOManager.IOValue(MoveDO, subType).Equals(IO_VALUE.LOW))
{
LogUtil.error(logName + " 转动已等待[" + FormUtil.GetSpanStr(span) + "],重写DO");
IOManager.IOMove(MoveDO, IO_VALUE.HIGH, subType);
}
if (!InTurn)
{
isStop = true;
result = "手动停止";
}
else if (span.TotalMilliseconds > timeOutMS)
{
isStop = true;
result = "转动超时";
}
//判断Buzy及位置是否结束
else if (IOManager.IOValue(CheckDI, subType).Equals(IO_VALUE.HIGH) || CheckDIHigh)
{
if (canWhileCount > 0 && span.TotalMilliseconds < 800)
{
isRestart = true;
canWhileCount--;
LogUtil.info(logName + " 异常停止,重新开始转动: 耗时 [" + FormUtil.GetSpanStr(span) + "][" + canWhileCount + "]");
return LineRunAndWait(timeOutMS);
}
isStop = true;
}
if (isStop)
{
if (result.Equals(""))
{
//正常4-5秒
if (span.TotalSeconds >= 1 && span.TotalSeconds <= 6)
{
LogUtil.debug(logName + "停止运动: 耗时 [" + FormUtil.GetSpanStr(span) + "][" + canWhileCount + "]");
}
else
{
LogUtil.info(logName + "停止运动: 耗时 [" + FormUtil.GetSpanStr(span) + "][" + canWhileCount + "]");
}
}
else
{
LogUtil.info(logName + " 停止运动:" + result + " " + MoveDO);
}
LastEndTime = DateTime.Now;
IOManager.IOMove(MoveDO, IO_VALUE.LOW, subType);
InTurn = false;
WaitCheckDI_High = false;
CheckDIHigh = false;
if (!isRestart)
{
turnEnd?.Invoke(result.Equals(""));
}
return result;
}
}
}
}
}