From 18475c9ceff517b7ba292b9f8b90933b0ce3fb78 Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期三, 23 十二月 2020 16:38:53 +0800 Subject: [PATCH] 添加MES日志开关配置和详细数据日志 --- src/Bro.Device.GTSCard/GTSCardDriver.cs | 181 +++++++++++++++++++++++++++++++++++++------- 1 files changed, 150 insertions(+), 31 deletions(-) diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs index 573f480..303ddb4 100644 --- a/src/Bro.Device.GTSCard/GTSCardDriver.cs +++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs @@ -99,6 +99,9 @@ { AllAxisOn(); + MonitorPosition(); + MonitorAxisStatus(); + base.Start(); } @@ -523,7 +526,7 @@ /// <returns></returns> public double GetPosition(int axisNum) { - lock (moveLock) + //lock (moveLock) { double position = 0; double pPos = 0; @@ -643,7 +646,9 @@ if (CurrentState != EnumHelper.DeviceState.DSOpen) { LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�"); - throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); + + return false; + //throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); } LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination); @@ -722,8 +727,11 @@ if (CurrentState != EnumHelper.DeviceState.DSOpen) { LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�"); - throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); + + return false; + //throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); } + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination); short ret = 0; bool isSuccessSetAxisParam = false; @@ -780,7 +788,8 @@ bool isStop = false; if (option == 1) { - StateChange(EnumHelper.DeviceState.DSExcept); + //StateChange(EnumHelper.DeviceState.DSExcept); + LogAsync(DateTime.Now, "鎬ュ仠鍋滄", ""); } var ret = GTSCardAPI.GT_Stop((short)IConfig.CardNum, 1 << (axisNum - 1), option); if (ret != (short)GTSRetCode.GRCRunOK) @@ -888,17 +897,17 @@ { try { + Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; + var goHomePara = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex).GoHomePara; bool homeDirection = goHomePara.IsHomeDirPositive; bool isRightLimitReached = false; short capture = 0; GTSCardAPI.TTrapPrm trapPrm; - //鎼滅储璺濈 闃舵1 - // 鍚姩Home鎹曡幏 - short sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME); + // 鍒囨崲鍒扮偣浣嶈繍鍔ㄦā寮� - sRtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex); + short sRtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex); // 璇诲彇鐐逛綅妯″紡杩愬姩鍙傛暟 sRtn = GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out trapPrm); trapPrm.acc = movingOption.VelocityPara.Acc; @@ -906,30 +915,56 @@ // 璁剧疆鐐逛綅妯″紡杩愬姩鍙傛暟 sRtn = GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref trapPrm); // 璁剧疆鐐逛綅妯″紡鐩爣閫熷害锛屽嵆鍥炲師鐐归�熷害 - sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, goHomePara.HighVelocity); + sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, goHomePara.HomeVelocity); do { PositionReset(movingOption.AxisIndex, 1); ClearStatus(movingOption.AxisIndex, 1); + //LogAsync(DateTime.Now, $"{Name}浣嶇疆缃浂", ""); + + if (goHomePara.IsCaptureMode) + { + //鎼滅储璺濈 闃舵1 + // 鍚姩Home鎹曡幏 + sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME); + } + // 璁剧疆鐐逛綅妯″紡鐩爣浣嶇疆锛屽嵆鍘熺偣鎼滅储璺濈 - sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, homeDirection ? 99999999 : -99999999); + sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, homeDirection ? 999999999 : -999999999); // 鍚姩杩愬姩 sRtn = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (movingOption.AxisIndex - 1)); int repeatTime = goHomePara.GoHomeTimeOut * 1000; bool isStop = false; - int pos; + int pos = 0; uint clk;//鏃堕挓鍙傛暟 + + int checkInterval = IConfig.MonitorInterval; + if (IConfig.MonitorInterval <= 0) + { + checkInterval = 10; + } + do { - Thread.Sleep(IConfig.MonitorInterval * 5); - // 璇诲彇鎹曡幏鐘舵�� - GTSCardAPI.GT_GetCaptureStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out capture, out pos, 1, out clk); + Thread.Sleep(checkInterval); + + if (goHomePara.IsCaptureMode) + { + // 璇诲彇鎹曡幏鐘舵�� + GTSCardAPI.GT_GetCaptureStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out capture, out pos, 1, out clk); + } + else + { + GTSCardAPI.GT_GetDi((short)IConfig.CardNum, GTSCardAPI.MC_HOME, out int pValue); + capture = (short)((pValue & (1 << (movingOption.AxisIndex - 1))) == 0 ? 1 : 0); + //LogAsync(DateTime.Now, $"鍘熺偣鐘舵�亄pValue}锛岃酱{movingOption.AxisIndex}鍘熺偣{capture}", ""); + } isStop = IsStop((short)movingOption.AxisIndex); - repeatTime -= IConfig.MonitorInterval * 5; + repeatTime -= checkInterval; } while (!(isStop || capture == 1 || repeatTime <= 0)); if (repeatTime <= 0) @@ -940,20 +975,22 @@ var axisStatus = AxisStatusList.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex); + //if (isStop) + //{ + // LogAsync(DateTime.Now, $"杞磠movingOption.AxisIndex}澶嶄綅涓仠姝�", $"Capture鐘舵�亄capture}"); + //} + if (isStop && capture != 1) { - ClearStatus((short)movingOption.AxisIndex, 1); - if (((axisStatus.AxisStatus & 0x20) != 0) || ((axisStatus.AxisStatus & 0x40) != 0)) { - //movingOption.GoHomePara.HomeDir = (short)(movingOption.GoHomePara.HomeDir == 1 ? -1 : 1); + capture = 0; //姝i檺浣� if ((axisStatus.AxisStatus & 0x20) != 0 && !goHomePara.IsCaptureDirPositive) { isRightLimitReached = true; } - //璐熼檺浣� if ((axisStatus.AxisStatus & 0x40) != 0 && goHomePara.IsCaptureDirPositive) @@ -962,7 +999,11 @@ } homeDirection = !homeDirection; + + LogAsync(DateTime.Now, $"杞磠movingOption.AxisIndex}鏋侀檺浣嶇疆鎹㈠悜", ""); } + + ClearStatus(movingOption.AxisIndex, 1); } if (capture == 1) @@ -970,6 +1011,7 @@ if (!isRightLimitReached) { capture = 0; + ClearStatus(movingOption.AxisIndex, 1); //GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME); continue; } @@ -977,6 +1019,7 @@ //鍏坰top MoveStop((short)movingOption.AxisIndex, 0); ClearStatus((short)movingOption.AxisIndex, 1); + //宸茬粡鎹曡幏鍒癏ome鎵嶅彲浠ュ洖闆� 闃舵2 // 杩愬姩鍒�"鎹曡幏浣嶇疆+鍋忕Щ閲�" sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, pos + goHomePara.HomeOffset); @@ -992,14 +1035,18 @@ repeatTime--; } while (!isStop && repeatTime > 0); PositionReset(movingOption.AxisIndex, 1); + LogAsync(DateTime.Now, $"杞磠movingOption.AxisIndex}澶嶄綅瀹屾垚锛屼綅缃竻闆�", ""); + return (sRtn == (short)GTSRetCode.GRCRunOK) && isStop; } } while (!(capture == 1 && isRightLimitReached)); + return false; } catch (Exception ex) { + LogAsync(DateTime.Now, $"{Name}鍥炲師鐐瑰紓甯�", ex.GetExceptionMessage()); AllMoveStop(true); OnExceptionOccured?.Invoke(DateTime.Now, ex); return false; @@ -1073,7 +1120,7 @@ /// <returns></returns> public override int GetAxisStatus(int axisNum) { - lock (moveLock) + //lock (moveLock) { int sts = 0; uint pclock = 0; @@ -1096,7 +1143,7 @@ /// <returns></returns> public override bool PositionReset(int startAxisIndex, int count) { - lock (moveLock) + //lock (moveLock) { var rtn = GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)startAxisIndex, (short)count); return rtn == (short)GTSRetCode.GRCRunOK; @@ -1127,7 +1174,6 @@ return axisMovingStatusesList; } - public List<IOItem> GetMonitorValues() { @@ -1161,6 +1207,79 @@ return result; } + SpinWait _positionWait = new SpinWait(); + private async void MonitorPosition() + { + await Task.Run(() => + { + while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit) + { + try + { + if (!IConfig.IsEnableMonitor) + return; + + AxisStatusList.ForEach(a => + { + int curPosition = (int)GetPrfPosition(a.AxisIndex); + + if (a.CurPosition != curPosition) + { + a.CurPosition = curPosition; + AxisPositionChanged(a.AxisIndex, curPosition); + } + }); + + _positionWait.SpinOnce(); + } + catch (Exception ex) + { + if (CurrentState == DeviceState.DSOpen) + { + LogAsync(DateTime.Now, $"{this.Name}鐩戝惉杞翠俊鎭紓甯�", ex.GetExceptionMessage()); + } + } + } + }); + } + + SpinWait _statusWait = new SpinWait(); + private async void MonitorAxisStatus() + { + await Task.Run(() => + { + while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit) + { + try + { + if (!IConfig.IsEnableMonitor) + return; + + AxisStatusList.ForEach(a => + { + int curStatus = GetAxisStatus(a.AxisIndex); + + if (a.AxisStatus != curStatus) + { + int temp = a.AxisStatus; + a.AxisStatus = curStatus; + AxisStatusChanged(a.AxisIndex, temp, curStatus); + } + }); + + _statusWait.SpinOnce(); + } + catch (Exception ex) + { + if (CurrentState == DeviceState.DSOpen) + { + LogAsync(DateTime.Now, $"{this.Name}鐩戝惉杞翠俊鎭紓甯�", ex.GetExceptionMessage()); + } + } + } + }); + } + public async override void Monitor() { await Task.Run(() => @@ -1173,9 +1292,9 @@ return; var newValues = GetMonitorValues(); - var newAxisMovingStatus = GetAxisMovingStatus(); + //var newAxisMovingStatus = GetAxisMovingStatus(); - if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0) + if (newValues == null || newValues.Count == 0) continue; //Stopwatch sw = new Stopwatch(); @@ -1189,14 +1308,14 @@ MonitorCheckAndInvoke(tempNew, tempOld); } - if (AxisStatusList.Count == newAxisMovingStatus.Count) - { - var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone - var tempOld = new List<AxisMovingStatus>(AxisStatusList); - AxisStatusCheck(tempNew, tempOld); - } + //if (AxisStatusList.Count == newAxisMovingStatus.Count) + //{ + // var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone + // var tempOld = new List<AxisMovingStatus>(AxisStatusList); + // AxisStatusCheck(tempNew, tempOld); + //} - AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus); + //AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus); MonitorValues = new List<IOItem>(newValues); //sw.Stop(); -- Gitblit v1.8.0