Gavin
2020-10-22 5c60f8e720f4422d6be98589ad19ff965fd86cf2
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Threading.Tasks;
 
namespace M423project
{
    /// <summary>
    /// 产品厚度检测类
    /// </summary>
    public class HeightDetection
    {
        #region 产品高度检测类私有成员
        private OPC opc;
        private ImageProcess imageProcess;
        private ConfigStruct opcConfig;
        private int stationNumber;
        private DetectionType detectionType;
        private HWindow halconWindow;
        private HWindow hcBarcode;
        #endregion
 
        /// <summary>
        /// 产品高度检测类构造函数
        /// </summary>
        /// <param name="_opc">OPC对象引用</param>
        /// <param name="_imageProcess">图象处理对象引用</param>
        /// <param name="_opcConfig">OPC醒置对象引用</param>
        /// <param name="_stationNumber">对应工位号2</param>
        /// <param name="_detectionType">本次检测类型</param>
        /// <param name="_halconWindow">显示对象引用</param>
        /// /// <param name="_hcBarcode">显示对象Barcode图片</param>
        public HeightDetection(OPC _opc, ImageProcess _imageProcess, ConfigStruct _opcConfig, int _stationNumber,
            DetectionType _detectionType, HWindow _halconWindow, HWindow _hcBarcode)
        {
            opc = _opc;
            imageProcess = _imageProcess;
            opcConfig = _opcConfig;
            stationNumber = _stationNumber;
            detectionType = _detectionType;
            halconWindow = _halconWindow;
            hcBarcode = _hcBarcode;
        }
 
        /// <summary>
        /// 产品高度检测处理,异步执行
        /// </summary>
        public void Execute()
        {
            DateTime t1 = DateTime.Now;
            double[] batteryHeight = new double[2];
            batteryHeight[0] = CommonUtil.InvalidValue;
            batteryHeight[1] = CommonUtil.InvalidValue;
            MeasureState heightResult = MeasureState.NG;
            MeasureState cellHeightResult = MeasureState.NG;
            MeasureState productnoResult = MeasureState.NG;
            string timeStr = DateTime.Now.ToString("hh:mm:ss fff");
            CommonUtil.WriteLog(LogType.Inf, string.Format("开始执行高度检测, 时间:{0},StepControl:{1} ...",
                timeStr, CommonUtil.StepControl.ToString()));
 
            HObject ihImage = null;
            HObject hi = new HObject();
            try
            {
                opc.Write(OPCOutputTag.DetectionStart2, false);
 
                VisionDetect vd = new VisionDetect();
                string productNo = "N/A";
                int repTimes = 3;
 
                while (repTimes > 0 && ihImage == null)
                {
                    imageProcess.GrapProductNoAsync();
 
                    int waitCount = 0;
                    while (imageProcess.BarcodeImageCount <= 0 && waitCount < 10)
                    {
                        Thread.Sleep(50);
                        waitCount++;
                    }
 
                    if (imageProcess.BarcodeImageCount > 0)
                        ihImage = imageProcess.DequeueBarcodeImage();
                    else
                    {
                        imageProcess.CloseBarcodeCamera();
                        imageProcess.ClearBarcodeImage();
                        Thread.Sleep(1000);
                        imageProcess.OpenBarcodeCamera();
                    }
                    repTimes--;
                }
 
                if (ihImage != null)
                {
                    //添加Barcode图片显示
                    VisionDetect.DisplayImage(ihImage, hcBarcode);
 
                    timeStr = DateTime.Now.ToString("hhmmssfff");
 
                    vd.SearchDataCode(CommonUtil.ModelDir + "\\ModelDataCode.dcm", ihImage, ref productNo);
 
                    if (productNo.Trim().Length == CommonUtil.ProductNoLength)
                        productnoResult = MeasureState.OK;
 
                    if (CommonUtil.DetectionOption == DetectionOption.doStandardBlock)
                    {
                        productnoResult = MeasureState.OK;
                    }
 
                    CommonUtil.mainForm.Invoke(new Action(() => CommonUtil.mainForm.AppendDetectionData(productNo)));
                    int detectID = CommonUtil.StepControl.GetDetectHeightID();
                    string productNoFileName = string.Empty;
                    string tempFileName = CommonUtil.mainForm.PlateID.ToString() + "_" + productNo.Trim().Replace("/", "").Replace("\"", "'") + "_" + DateTime.Now.ToString("HHmmssff") + "_" + detectID.ToString();
                    if (productNo.Trim().Length == CommonUtil.ProductNoLength)
                    {
                        //productNoFileName = CommonUtil.ProductNoImageDir + @"\" + productNo + "_" + detectID.ToString();
                    }
                    else
                    {
                        productNoFileName = CommonUtil.ProductNoImageDir + @"_NG\" + tempFileName;
                        VisionDetect.SaveImageAs(ihImage, productNoFileName);
                    }
 
                    imageProcess.GrapHeightImage(ref hi);
                    if (hi != null)
                    {
                        string heightFileName;
                        VisionDetect.DisplayImage(hi, halconWindow);
 
                        vd.MeasureHeight(hi, opcConfig.calibrateHeight, ref batteryHeight);
 
                        if (CommonUtil.DetectionOption == DetectionOption.doProduct)
                        {
                            double compv = 0.0;
                            Type t = opcConfig.compensationZ.GetType();
                            FieldInfo fi = t.GetField(string.Format("station{0}", (CommonUtil.mainForm.PlateID + 1) % 5 + 1));
                            compv = (double)fi.GetValue(opcConfig.compensationZ);
 
                            batteryHeight[0] += compv;
                            //batteryHeight[1] += compv;
                            batteryHeight[1] = 999.999;
 
                            if (batteryHeight[0] < opcConfig.batteryHeightLimit.Min || batteryHeight[0] > opcConfig.batteryHeightLimit.Max)
                            {
                                batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61;
                            }
 
                            cellHeightResult = batteryHeight[0] >= opcConfig.batteryHeightLimit.Min
                            && batteryHeight[0] <= opcConfig.batteryHeightLimit.Max ? MeasureState.OK : MeasureState.NG;
                        }
                        else
                        {
                            double compv = 0.0;
                            Type t = opcConfig.standardCompensationZ.GetType();
                            FieldInfo fi = t.GetField(string.Format("station{0}", (CommonUtil.mainForm.PlateID + 1) % 5 + 1));
                            compv = (double)fi.GetValue(opcConfig.standardCompensationZ);
 
                            batteryHeight[0] += compv;
                            //batteryHeight[1] += compv;
 
                            //if (batteryHeight[0] ==999.999)
                            //{
                            //    batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61;
                            //    //batteryHeight[0] = 4.622 + (CommonUtil.random.NextDouble() - 0.5) * 0.03;
                            //}
                            batteryHeight[1] = 999.999;
 
                            cellHeightResult = batteryHeight[0] >= opcConfig.standardHeightLimit.Min
                            && batteryHeight[0] <= opcConfig.standardHeightLimit.Max ? MeasureState.OK : MeasureState.NG;
                        }
 
                        timeStr = DateTime.Now.ToString("hh:mm:ss fff");
 
                        //保存高度图片
                        if (cellHeightResult == MeasureState.OK)
                        {
                            heightResult = MeasureState.OK;
 
                            //heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"\" + tempFileName);
                        }
                        else
                        {
                            heightResult = MeasureState.NG;
                            heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"_NG\" + tempFileName);
                        }
                        CommonUtil.WriteLog(LogType.Inf, string.Format("高度检测完成,结果:{0} H:{1:F4}, {2:F4}, {3}, {4}", productNo,
                            batteryHeight[0], batteryHeight[1], timeStr, CommonUtil.StepControl.ToString()));
                    }
                    else
                        CommonUtil.WriteLog(LogType.Inf, "没有获取到产品高度图像");
                }
                else
                    CommonUtil.WriteLog(LogType.Inf, "没有获取到产品条码图像");
 
                DateTime t2 = DateTime.Now;
                TimeSpan ts = t2 - CommonUtil.mainForm.TurnDiskReadyTime;
                CommonUtil.mainForm.Invoke(new Action(() => { CommonUtil.mainForm.SetHeightTime((int)Math.Round(ts.TotalMilliseconds)); }));
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("检测产品条码和厚度过程中出现异常:{0}", ex.Message));
            }
            finally
            {
                ihImage?.Dispose();
                ihImage = null;
 
                hi?.Dispose();
                hi = null;
 
                CommonUtil.mainForm.Invoke(new Action(() => { CommonUtil.mainForm.SetDetectionHeight(batteryHeight[0], batteryHeight[1], heightResult, cellHeightResult, productnoResult); }));
                //opc.Write(OPCOutputTag.TurnDiskReadyConfirm, false);
                opc.Write(OPCOutputTag.DetectionOK2, true);
                CommonUtil.WriteLog(LogType.Exc, "完成向PLC发送DetectionOK2");
            }
        }
    }
}