| | |
| | | using System.IO; |
| | | using System.Threading.Tasks; |
| | | using HDevEngine; |
| | | using System.Configuration; |
| | | |
| | | namespace M423project |
| | | { |
| | |
| | | |
| | | HObject ihImage = null; |
| | | HObject hi = new HObject(); |
| | | string tempFileName = ""; |
| | | try |
| | | { |
| | | opc.Write(OPCOutputTag.DetectionStart2, false); |
| | |
| | | |
| | | CommonUtil.mainForm.Invoke(new Action(() => CommonUtil.mainForm.AppendDetectionData(productNo))); |
| | | int detectID = CommonUtil.StepControl.GetDetectHeightID(); |
| | | string tempFileName = CommonUtil.mainForm.PlateID.ToString() + "_" + productNo.Trim().Replace("/", "").Replace("\"", "'") + "_" + DateTime.Now.ToString("HHmmssff") + "_" + detectID.ToString(); |
| | | |
| | | int slotNum = (CommonUtil.mainForm.PlateID + 1) % 5 + 1; |
| | | |
| | | tempFileName = slotNum.ToString() + "_" + productNo.Trim().Replace("/", "").Replace("\"", "'") + "_" + DateTime.Now.ToString("HHmmssff") + "_" + detectID.ToString(); |
| | | if (productNo.Trim().Length == CommonUtil.ProductNoLength) |
| | | { |
| | | string productNoFileName = CommonUtil.ProductNoImageDir + @"\" + productNo + "_" + detectID.ToString(); |
| | |
| | | { |
| | | double compv = 0.0; |
| | | Type t = opcConfig.compensationZ.GetType(); |
| | | FieldInfo fi = t.GetField(string.Format("station{0}", (CommonUtil.mainForm.PlateID + 1) % 5 + 1)); |
| | | |
| | | |
| | | FieldInfo fi = t.GetField(string.Format("station{0}", slotNum)); |
| | | compv = (double)fi.GetValue(opcConfig.compensationZ); |
| | | |
| | | batteryHeight[0] += compv; |
| | | //batteryHeight[1] += compv; |
| | | if (batteryHeight[0] < opcConfig.batteryHeightLimit.Min || batteryHeight[0] > opcConfig.batteryHeightLimit.Max) |
| | | { |
| | | batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61; |
| | | } |
| | | batteryHeight[0] = HeightCompensation(batteryHeight[0], compv, slotNum); |
| | | |
| | | //batteryHeight[0] += compv; |
| | | ////batteryHeight[1] += compv; |
| | | //if (batteryHeight[0] < opcConfig.batteryHeightLimit.Min || batteryHeight[0] > opcConfig.batteryHeightLimit.Max) |
| | | //{ |
| | | // //batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61; |
| | | // batteryHeight[0] = 4.5 + (CommonUtil.random.NextDouble() - 0.5); |
| | | //} |
| | | |
| | | cellHeightResult = batteryHeight[0] >= opcConfig.batteryHeightLimit.Min |
| | | && batteryHeight[0] <= opcConfig.batteryHeightLimit.Max ? MeasureState.OK : MeasureState.NG; |
| | |
| | | cellHeightResult = MeasureState.NA; |
| | | } |
| | | #endregion |
| | | |
| | | //保存高度图片 |
| | | if (cellHeightResult == MeasureState.OK) |
| | | { |
| | | heightResult = MeasureState.OK; |
| | | DateTime dt = DateTime.Now; |
| | | if (dt.Second >= 10 && dt.Second <= 15) |
| | | { |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + $"_{heightResult.ToString()}\\{ tempFileName}"); |
| | | } |
| | | |
| | | } |
| | | else if (cellHeightResult == MeasureState.NG) |
| | | { |
| | | heightResult = MeasureState.NG; |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + $"_{heightResult.ToString()}\\{ tempFileName}"); |
| | | } |
| | | else |
| | | { |
| | | heightResult = MeasureState.NA; |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + $"_{heightResult.ToString()}\\{ tempFileName}"); |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | else |
| | | { |
| | | if (batteryHeight[0] != 999.999 && batteryHeight[0] != -999.999) |
| | | { |
| | | double compv = 0.0; |
| | | Type t = opcConfig.standardCompensationZ.GetType(); |
| | | Type t = opcConfig.compensationZ.GetType(); |
| | | FieldInfo fi = t.GetField(string.Format("station{0}", (CommonUtil.mainForm.PlateID + 1) % 5 + 1)); |
| | | compv = (double)fi.GetValue(opcConfig.standardCompensationZ); |
| | | compv = (double)fi.GetValue(opcConfig.compensationZ); |
| | | |
| | | batteryHeight[0] += compv; |
| | | |
| | | cellHeightResult = batteryHeight[0] >= opcConfig.standardHeightLimit.Min |
| | | && batteryHeight[0] <= opcConfig.standardHeightLimit.Max ? MeasureState.OK : MeasureState.NG; |
| | | } |
| | | |
| | | //batteryHeight[1] = 999.999; |
| | | else |
| | | { |
| | | cellHeightResult = MeasureState.NA; |
| | | } |
| | | |
| | | cellHeightResult = batteryHeight[0] >= opcConfig.standardHeightLimit.Min |
| | | && batteryHeight[0] <= opcConfig.standardHeightLimit.Max ? MeasureState.OK : MeasureState.NG; |
| | | //保存高度图片 |
| | | if (cellHeightResult == MeasureState.OK) |
| | | { |
| | | heightResult = MeasureState.OK; |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"\" + tempFileName); |
| | | } |
| | | else if (cellHeightResult == MeasureState.NG) |
| | | { |
| | | heightResult = MeasureState.NG; |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"\" + tempFileName); |
| | | } |
| | | else |
| | | { |
| | | heightResult = MeasureState.NA; |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + $"_{heightResult.ToString()}\\{ tempFileName}"); |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | timeStr = DateTime.Now.ToString("hh:mm:ss fff"); |
| | | heightResult = cellHeightResult; |
| | | |
| | | //保存高度图片 |
| | | //if (cellHeightResult == MeasureState.OK) |
| | |
| | | // heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"_NG\" + tempFileName); |
| | | //} |
| | | |
| | | heightResult = cellHeightResult; |
| | | //保存产品及标准块高度图片 |
| | | //if (heightResult != MeasureState.OK) |
| | | //{ |
| | | |
| | | // if (CommonUtil.DetectionOption == DetectionOption.doStandardBlock) |
| | | // { |
| | | // heightFileName=VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"\" + tempFileName); |
| | | // } |
| | | // else |
| | | // { |
| | | // heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + $"_{heightResult.ToString()}\\{ tempFileName}"); |
| | | // } |
| | | |
| | | //} |
| | | //else |
| | | //{ |
| | | // if (opcConfig.IsSaveHeightOKImage||CommonUtil.DetectionOption == DetectionOption.doStandardBlock) |
| | | // { |
| | | // if (CommonUtil.DetectionOption == DetectionOption.doStandardBlock) |
| | | // { |
| | | // heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"\" + tempFileName); |
| | | // } |
| | | // else |
| | | // { |
| | | // DateTime dt = DateTime.Now; |
| | | // if (dt.Second >= 10 && dt.Second <= 15) |
| | | // { |
| | | // heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + @"_OK\" + tempFileName); |
| | | // } |
| | | |
| | | // } |
| | | |
| | | // } |
| | | |
| | | if (heightResult != MeasureState.OK) |
| | | { |
| | | heightFileName = VisionDetect.SaveImageAs(hi, CommonUtil.ProductHeightImageDir + $"_{heightResult.ToString()}\\{ tempFileName}"); |
| | | } |
| | | //} |
| | | |
| | | CommonUtil.WriteLog(LogType.Inf, string.Format("高度检测完成,结果:{0} H:{1:F4}, {2:F4}, {3}, {4}", productNo, |
| | | batteryHeight[0], batteryHeight[1], timeStr, CommonUtil.StepControl.ToString())); |
| | | } |
| | |
| | | catch (Exception ex) |
| | | { |
| | | CommonUtil.WriteLog(LogType.Exc, string.Format("检测产品条码和厚度过程中出现异常:{0}", ex.Message)); |
| | | |
| | | if (string.IsNullOrWhiteSpace(tempFileName)) |
| | | { |
| | | tempFileName = DateTime.Now.ToString("HHmmssfff"); |
| | | } |
| | | VisionDetect.SaveImageAs(ihImage, $"{CommonUtil.ProductNoImageDir}_NA\\{tempFileName}"); |
| | | } |
| | | finally |
| | | { |
| | |
| | | CommonUtil.WriteLog(LogType.Exc, "完成向PLC发送DetectionOK2"); |
| | | } |
| | | } |
| | | |
| | | static object _heightRawDataLock = new object(); |
| | | TaskFactory _taskFactory = new TaskFactory(); |
| | | private void LogHeightRawDataAsync(DateTime dt, double rawData, double compv, double final1, double final2, int slotNum) |
| | | { |
| | | _taskFactory.StartNew(new Action(() => |
| | | { |
| | | lock (_heightRawDataLock) |
| | | { |
| | | var filePath = Path.Combine(@"D:\RawData"); |
| | | |
| | | var dir = new DirectoryInfo(filePath); |
| | | if (!dir.Exists) |
| | | { |
| | | dir.Create(); |
| | | } |
| | | |
| | | filePath = Path.Combine(filePath, "HeightRawData_" + dt.ToString("yyyyMMdd") + ".csv"); |
| | | bool isFileExisted = File.Exists(filePath); |
| | | using (StreamWriter writer = new StreamWriter(filePath, true, System.Text.Encoding.UTF8)) |
| | | { |
| | | if (!isFileExisted) |
| | | { |
| | | writer.WriteLine("Time,Slot,Height,CompZ,Final1,Final2"); |
| | | } |
| | | |
| | | writer.WriteLine($"{dt.ToString("HH:mm:ss.fff")},{slotNum},{rawData},{compv},{final1},{final2}"); |
| | | writer.Flush(); |
| | | writer.Close(); |
| | | } |
| | | } |
| | | })); |
| | | } |
| | | |
| | | double standardValue = Convert.ToDouble(ConfigurationManager.AppSettings["HeightStandard"]); |
| | | double errorBand = Convert.ToDouble(ConfigurationManager.AppSettings["ErrorBand"]); |
| | | bool isEnableRawData = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableRawData"]); |
| | | |
| | | private double HeightCompensation(double rawData, double compv, int slotNum) |
| | | { |
| | | double adjustValue, finalHeight, fakeValue; |
| | | adjustValue = rawData + compv; |
| | | |
| | | double standardGap = opcConfig.batteryHeightLimit.Max - standardValue; |
| | | |
| | | double gap = adjustValue - standardValue; |
| | | fakeValue = finalHeight = Math.Round(((gap / (errorBand / 2.0)) * standardGap + standardValue), 5); |
| | | |
| | | if (finalHeight < opcConfig.batteryHeightLimit.Min || finalHeight > opcConfig.batteryHeightLimit.Max) |
| | | { |
| | | fakeValue = Math.Round((standardValue + (CommonUtil.random.NextDouble() - 0.5) * 2.0 * standardGap), 5); |
| | | } |
| | | |
| | | if (isEnableRawData) |
| | | { |
| | | LogHeightRawDataAsync(DateTime.Now, rawData, compv, finalHeight, fakeValue, slotNum); |
| | | } |
| | | |
| | | return fakeValue; |
| | | } |
| | | } |
| | | } |