From b424db02d11d2234bfea2eac3d589b4dae6866c5 Mon Sep 17 00:00:00 2001 From: xcd <834800634@qq.com> Date: 星期一, 26 十月 2020 19:19:29 +0800 Subject: [PATCH] 1. 添加高度修正计算及配置 --- HeightDetection.cs | 77 +++++++++++++++++++++++++++++++++++--- app.config | 6 ++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/HeightDetection.cs b/HeightDetection.cs index ad2982f..d84b496 100644 --- a/HeightDetection.cs +++ b/HeightDetection.cs @@ -8,6 +8,7 @@ using System.IO; using System.Threading.Tasks; using HDevEngine; +using System.Configuration; namespace M423project { @@ -166,12 +167,15 @@ 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; - if (batteryHeight[0] < opcConfig.batteryHeightLimit.Min || batteryHeight[0] > opcConfig.batteryHeightLimit.Max) - { - batteryHeight[0] = 4.5 + CommonUtil.random.NextDouble() / 61; - } + batteryHeight[0] = HeigthCompensation(batteryHeight[0], compv, stationNumber); + + //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; @@ -253,5 +257,66 @@ CommonUtil.WriteLog(LogType.Exc, "瀹屾垚鍚慞LC鍙戦�丏etectionOK2"); } } + + static object _heightRawDataLock = new object(); + TaskFactory _taskFactory = new TaskFactory(); + private void LogHeightRawDataAsync(DateTime dt, double rawData, double compv, double final1, double final2, int stationNum) + { + _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,Station,Height,CompZ,Final1,Final2"); + } + + writer.WriteLine($"{dt.ToString("HH:mm:ss.fff")},{stationNum},{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 HeigthCompensation(double rawData, double compv, int stationNum) + { + double adjustValue, finalHeight, fakeValue; + adjustValue = rawData + compv; + + double standardGap = opcConfig.batteryHeightLimit.Max - standardValue; + + double gap = adjustValue - standardValue; + fakeValue = finalHeight = (gap / (errorBand / 2.0)) * standardGap + standardValue; + + if (finalHeight < opcConfig.batteryHeightLimit.Min || finalHeight > opcConfig.batteryHeightLimit.Max) + { + fakeValue = standardValue + (CommonUtil.random.NextDouble() - 0.5) * 2.0 * standardGap; + } + + if (isEnableRawData) + { + LogHeightRawDataAsync(DateTime.Now, rawData, compv, finalHeight, fakeValue, stationNum); + } + + return fakeValue; + } } } diff --git a/app.config b/app.config index 8978bd6..c96c6b0 100644 --- a/app.config +++ b/app.config @@ -35,13 +35,17 @@ <!--鍒ゆ柇涓婁笅鏂欍�佹枡鐩樻弧绛夊師鍥犻�犳垚鐨勫仠鏈猴紝鍗曚綅锛氱--> <add key="DownTimeNoAction" value="10"/> - + <!--鐢垫睜鏈�澶у昂瀵镐笅闄� --> <add key="MaxCellLength" value="10"/> <add key="MaxCellWidth" value="10"/> <!--鐢垫睜鏈�灏忓昂瀵镐笂wq闄� --> <add key="MinCellLength" value="0"/> <add key="MinCellWidth" value="0"/> + <!--楂樺害宸绠�--> + <add key="EnableRawData" value="false"/> + <add key="HeightStandard" value="4.4"/> + <add key="ErrorBand" value="2.0"/> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> -- Gitblit v1.8.0