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");
|
}
|
}
|
}
|
}
|