XRayMachine.XRay.cs
21.4 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
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
using crc = OnlineStore.CodeResourceControl;
partial class XRayMachine
{
public HashSet<string> pnlst;
/// <summary>
/// 特殊料重复点料次数
/// </summary>
public int ptrycount;
/// <summary>
/// 数量为0重试次数
/// </summary>
public int ptryerrcount=3;
public int relmove;
const string xraydir = @"\Image\Xray";
Task GetResultTask = null;
void WorkProcess()
{
if (CheckWait(MoveInfo))
return;
switch (MoveInfo.MoveStep)
{
case MoveStep.XRay_01_LocationDown:
MoveInfo.StopwatchReset();
MoveInfo.NextMoveStep(MoveStep.XRay_02_RunIn);
RobotManage.ProductivityCount++;
MoveInfo.log($"定位气缸下降,打开点料机入口门");
CylinderMove(MoveInfo, IO_XRay_Type.Location_Cylinder_Down, IO_XRay_Type.Location_Cylinder_Up, IO_VALUE.LOW);
CylinderMove(null, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.HIGH);
break;
case MoveStep.XRay_02_RunIn:
MoveInfo.NextMoveStep(MoveStep.XRay_03_CloseDoor);
MoveInfo.log($"入料伺服相对运转");
//Line_Entry_Axis.RelMove(ResetMoveInfo, Config.Line_Entry_Relative, Config.Line_Entry_Relative_speed);
Line_Entry_Axis.SpeedMove(Config.Line_Entry_Relative_speed);
Line_In_Axis.RelMove(MoveInfo, Config.Line_In_Relative, Config.Line_In_Relative_speed);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
//MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_XRay_Type.Tray_Inside_Check, IO_VALUE.HIGH));
break;
case MoveStep.XRay_03_CloseDoor:
MoveInfo.NextMoveStep(MoveStep.XRay_04_OpenXray);
MoveInfo.log($"关闭点料机入口门");
Line_Entry_Axis.SuddenStop();
CylinderMove(MoveInfo, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.LOW);
CylinderMove(MoveInfo, IO_XRay_Type.Location_Cylinder_Down, IO_XRay_Type.Location_Cylinder_Up, IO_VALUE.HIGH);
IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.HIGH);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
break;
case MoveStep.XRay_04_OpenXray:
MoveInfo.NextMoveStep(MoveStep.XRay_05_GetImage);
RobotManage.XRay.Start();
MoveInfo.log($"打开X光");
var pps = MoveInfo.ReelParam.PN.Split('-');
var pn = pps[0];
if (pnlst.Contains(pn)) {
MoveInfo.NextMoveStep(MoveStep.XRay_Extra_01);
MoveInfo.log($"判断为需要多次拍照的料盘,PN:{pn}");
countlist.Clear();
}
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(ConfigHelper.Config.Get("xray_light_delay", 1000)));
break;
case MoveStep.XRay_05_GetImage:
if (GrabImage())
{
MoveInfo.NextMoveStep(MoveStep.XRay_08_OpenOutDoor);
MoveInfo.log($"获取图像,开始点料 IsRayOpen:{RobotManage.XRay.IsRayOpen}");
GetResultTask = Task.Run(()=> { GetCountResult(); });
}
else {
MoveInfo.NextMoveStep(MoveStep.XRay_08_OpenOutDoor);
//Msg.add("图像平板获取图像失败",MsgLevel.warning);
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "获取图像失败";
MoveInfo.ReelParam.logresult();
}
MoveInfo.log($"关闭X光");
RobotManage.XRay.Stop();
IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
MoveInfo.StopwatchLog(false, "准备送出");
//MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(500));
break;
case MoveStep.XRay_08_OpenOutDoor:
MoveInfo.NextMoveStep(MoveStep.XRay_09_SentToLabelStop);
CylinderMove(MoveInfo, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.HIGH);
MoveInfo.log($"打开出口门");
break;
case MoveStep.XRay_09_SentToLabelStop:
if (RobotManage.labelMachine.TrayCanIN())
{
MoveInfo.NextMoveStep(MoveStep.XRay_10_CloseOutDoor);
MoveInfo.log($"打开出口门,送出到贴标机构");
Line_In_Axis.SpeedMove(Config.Line_In_Relative_speed*2);
RobotManage.Line1.LineRun("xray", 999, "XRay_09_SentToLabelStop");
MoveInfo.StopwatchLog(false, "开始送出");
}
else if (MoveInfo.IsTimeOut(30)) {
Msg.add(crc.GetString("wait_xx_free", "等待{0}空闲", RobotManage.labelMachine.DeviceNameShow), MsgLevel.warning);
MoveInfo.log("等待贴标机构入口空闲");
}
break;
case MoveStep.XRay_10_CloseOutDoor:
if (RobotManage.labelMachine.Tray_Check.Equals(IO_VALUE.HIGH))
{
//RobotManage.labelMachine.InLineRunControl(IO_VALUE.LOW);
RobotManage.Line1.LineStop("xray", "XRay_10_CloseOutDoor");
MoveInfo.NextMoveStep(MoveStep.XRay_11_GetCoutResult);
MoveInfo.log($"关闭出口门,停止X光机内线体");
Line_In_Axis.SuddenStop();
CylinderMove(null, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.LOW);
MoveInfo.StopwatchLog(false, "到达阻挡");
}
else if (MoveInfo.IsTimeOut(10))
{
Msg.add(crc.GetString("wait_goto_xx", "等待料盘到达{0}",RobotManage.labelMachine.DeviceNameShow), MsgLevel.warning);
MoveInfo.log("等待料盘到达贴标线入口");
}
break;
case MoveStep.XRay_11_GetCoutResult:
if (GetResultTask == null || GetResultTask.IsCompleted)
{
RobotManage.labelMachine.preReelParam = MoveInfo.ReelParam;
MoveInfo.NextMoveStep(MoveStep.XRay_End);
MoveInfo.log($"获得点料结果 QTY:{MoveInfo.ReelParam.QTY}");
MoveInfo.StopwatchLog(false, "获得点料结果");
}
else if (MoveInfo.IsTimeOut(30)) {
MoveInfo.NextMoveStep(MoveStep.XRay_End);
MoveInfo.log("获取点料结果超时");
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "获取点料结果超时";
MoveInfo.ReelParam.logresult();
RobotManage.labelMachine.preReelParam = MoveInfo.ReelParam;
}
break;
case MoveStep.XRay_End:
MoveInfo.log($"送出到贴标机构");
MoveInfo.StopwatchLog();
MoveInfo.EndMove();
break;
case MoveStep.XRay_Extra_01:
MoveInfo.NextMoveStep(MoveStep.XRay_Extra_02);
if (GrabImage())
{
MoveInfo.log($"获取图像,开始点料 IsRayOpen:{RobotManage.XRay.IsRayOpen}");
var c = GetCountResult(true);
countlist.Add(c);
MoveInfo.log($"获取点料数量:{c}, 第:{countlist.Count}次");
}
else
{
MoveInfo.StopwatchLog(false, "获取图像失败");
countlist.Add(-1);
}
relmove = ConfigHelper.Config.Get<int>("wiston_relmove", 50);
if (countlist.Count % 2 == 0)
relmove = relmove / 2 * -1;
Line_In_Axis.RelMove(MoveInfo, relmove, Config.Line_In_Relative_speed);
MoveInfo.StopwatchLog(false, $"轻微移动皮带,重试X光点料,relmove:{relmove}");
break;
case MoveStep.XRay_Extra_02:
var zerocount = countlist.Where((x) => x == 0).Count();
var selectcount = countlist.Where((x) => x > 0 && x <= ConfigHelper.Config.Get<int>("wiston_smaxcount", 10500)).ToList();
var hasnumcount = selectcount.Count();
var rightcount = 0;
try
{
selectcount.Sort();
if (hasnumcount % 2 == 1 && hasnumcount >= 5)
{
var m1 = selectcount.ElementAt(hasnumcount / 2 - 1);
var m2 = selectcount.ElementAt(hasnumcount / 2);
var m3 = selectcount.ElementAt(hasnumcount / 2 + 1);
if ((double)m1 / m2 >= 0.988d && (double)m2 / m3 >= 0.988d)
{
rightcount = m2;
}
if (zerocount > ptryerrcount || hasnumcount >= ptrycount + 2)
{
rightcount = m2;
}
MoveInfo.log($"阶段数据:{string.Join(",", countlist)}. 有效数据:{string.Join(",", selectcount)},m1:{m1},m2:{m2},m3:{m3},m1/m2:{(double)m1 / m2:0.000},m2/m3:{(double)m2 / m3:0.000}");
}
else
{
if (zerocount > ptryerrcount)
{
rightcount = selectcount.ElementAt(hasnumcount / 2);
MoveInfo.log($"错误数据过多,结束点料,rightcount:{rightcount}");
}
MoveInfo.log($"阶段数据:{string.Join(",", countlist)}. 有效数据:{string.Join(",", selectcount)}");
}
}
catch (Exception e){
MoveInfo.log(e.ToString());
if (selectcount.Count > 0)
rightcount = selectcount[0];
else {
MoveInfo.NextMoveStep(MoveStep.XRay_Extra_03);
MoveInfo.log($"错误数据过多,结束点料,无有效数据");
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "无有效数据";
MoveInfo.ReelParam.logresult();
}
}
if (rightcount==0)
{
MoveInfo.NextMoveStep(MoveStep.XRay_Extra_01);
MoveInfo.log($"开始第{countlist.Count+1}次");
}
else
{
MoveInfo.NextMoveStep(MoveStep.XRay_Extra_03);
MoveInfo.log($"获取到选定数量{rightcount}, 分别数量:{string.Join(",", selectcount)}, 总数据:{countlist}");
MoveInfo.ReelParam.QTY = rightcount;
var cl = ServerConn.inputCounterDataByXRayMachine(MoveInfo.ReelParam.WareCode, rightcount);
if (cl == null)
{
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "点料数量上传失败";
MoveInfo.ReelParam.logresult();
}
}
break;
case MoveStep.XRay_Extra_03:
MoveInfo.NextMoveStep(MoveStep.XRay_08_OpenOutDoor);
MoveInfo.log($"关闭X光");
RobotManage.XRay.Stop();
IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
MoveInfo.StopwatchLog(false, "准备送出");
break;
}
}
List<int> countlist = new List<int>();
public string xrayImagePath;
[HandleProcessCorruptedStateExceptions]
public bool GrabImage()
{
Bitmap bmp = null;
try
{
int xplate_getimage_retry_times = ConfigHelper.Config.Get("xplate_getimage_retry_times",5);
int xplate_open_retry_times = ConfigHelper.Config.Get("xplate_open_retry_times", 3);
for (int ii = 1; ii <= xplate_getimage_retry_times; ii++) {
bool rtn = RobotManage.xrayImage.GetImage(1);
if (!rtn)
{
if (ii == xplate_getimage_retry_times)
return false;
for (int jj = 1; jj <= xplate_open_retry_times; jj++)
{
bool closeR = RobotManage.xrayImage.Close();
if (!closeR)
MoveInfo.log($"xray plate Close ii={ii}, times={jj}, result={closeR}");
Thread.Sleep(500 * jj + (jj - 1) * 1000);
rtn = RobotManage.xrayImage.Open();
Thread.Sleep(500);
if (!rtn)
{
MoveInfo.log($"xray plate open failed ii={ii}, times={jj}");
if (jj == xplate_open_retry_times)
return false;
continue;
}
else
break;
}
Thread.Sleep(1000 * ii+(ii-1)*2000);
}
else
break;
}
MoveInfo.log("get image from plate scusses");
//RobotManage.xrayImage.WindowWidth = 30000;
//RobotManage.xrayImage.WindowLevel = 15000;
bmp = RobotManage.xrayImage.Get48bImage();
Directory.CreateDirectory(xraydir);
var n = DateTime.Now;
string backDir = Path.Combine(xraydir, "back", n.Year.ToString() + "-" + n.Month.ToString(), n.Day.ToString(), n.Hour.ToString());
Directory.CreateDirectory(backDir);
xrayImagePath = Path.Combine(backDir, MoveInfo.ReelParam.GetImgName());
bmp.Save(xrayImagePath, System.Drawing.Imaging.ImageFormat.Png);
// File.Copy(xrayImagePath, backDir);
MoveInfo.log($"save xray pic success. current file:{xrayImagePath}");
return true;
}
catch (Exception ex)
{
MoveInfo.log(ex.ToString());
return false;
}
finally
{
if (bmp != null)
bmp.Dispose();
bmp = null;
//StepImage?.Invoke(bmp);
}
}
[HandleProcessCorruptedStateExceptions]
public int GetCountResult(bool dontlog = false)
{
//返回的数量是string类型,count = "4000,3000,3500,2000"
int count = 0;
//resfile = "";
//tpDstImg = new API.EyemImage();
try
{
int[] countStr = new int[4];
int result = 0;
int ShrinkOffset = ConfigHelper.Config.Get<int>("ShrinkOffset",100);
var pps = MoveInfo.ReelParam.PN.Split('-');
var type = Pn_Algo_Match.MatchPN(pps[0],out _,out double? finetuning);
MoveInfo.log($"匹配到算法.{pps[0]}={type}, finetuning={finetuning}");
if (finetuning != null)
{
XrayImage.setFineTuning = finetuning.Value;
}
else
{
XrayImage.setFineTuning = ConfigHelper.Config.Get("setFineTuningDefault", 0.7);
}
if (type.StartsWith("IP_Template_PARTS"))
{
if (!File.Exists(type.Substring(17 + 1)))
{
MoveInfo.log($"模版文件丢失.{type.Substring(17 + 1)}");
type = "auto";
}
}
Bitmap dst=null;
if (type == "auto")
{
result = XrayImage.GetLocalCount(xrayImagePath, ShrinkOffset, out countStr, out dst);
MoveInfo.log("GetCountResult " + type + " 调用 GetLocalCount 【" + xrayImagePath + "】,返回【" + result + "】,结果【" + string.Join(",",countStr) + "】");
}
else if (type.StartsWith("IP_Template_PARTS"))
{
string template = "";
if (type.Length > 18)
{
template = type.Substring(17 + 1);
}
result = XrayImage.GetLocalCountTemplate(xrayImagePath, ShrinkOffset, template, out countStr, out dst);
MoveInfo.log("GetCountResult " + template + " 调用 GetLocalCountTemplate 【" + xrayImagePath + "】,返回【" + result + "】,结果【" + string.Join(",", countStr) + "】");
}
else
{
result = XrayImage.GetLocalCountIrregular(xrayImagePath, ShrinkOffset, type.ToString(), out countStr, out dst);
MoveInfo.log("GetCountResult " + type + " 调用 GetLocalCountIrregular 【" + xrayImagePath + "】,返回【" + result + "】,结果【" + string.Join(",", countStr) + "】");
}
if (dst != null)
{
dst.Save("ResOut\\" + Path.GetFileNameWithoutExtension(xrayImagePath) + "-Mark.png");
dst.Dispose();
}
if (result != 0)
{
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "点料失败";
MoveInfo.ReelParam.logresult();
return count;
}
if (countStr != null)
{
//string[] array = countStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
count = (from a in countStr where a > 0 select a).FirstOrDefault();
}
else
{
count = 0;
}
if (result == 0 && count > 0)
{
MoveInfo.ReelParam.QTY = count;
string resfilename = Path.GetFileNameWithoutExtension(xrayImagePath) + "-Mark.png";
string root = Path.Combine(Application.StartupPath, "ResOut");
var n = DateTime.Now;
string destdir = Path.Combine(root, n.Year.ToString() + "-" + n.Month.ToString(), n.Day.ToString(), n.Hour.ToString());
Directory.CreateDirectory(destdir);
string resfile = Path.Combine(destdir, resfilename);
if (File.Exists(resfile))
File.Delete(resfile);
Directory.Move(Path.Combine(root, resfilename), resfile);
if (!RobotManage.offlinemode && !dontlog)
{
var cl = ServerConn.inputCounterDataByXRayMachine(MoveInfo.ReelParam.WareCode, count);
if (cl == null)
{
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "点料数量上传失败";
MoveInfo.ReelParam.logresult();
}
}
}
else {
MoveInfo.ReelParam.IsNg = true;
MoveInfo.ReelParam.NgMsg = "点料结果为0";
MoveInfo.ReelParam.logresult();
}
}
catch (Exception ex)
{
MoveInfo.log("GetCountResult 出错: " + ex.ToString());
}
return count;
}
}
}