From e54c5a471c372e314f89c340644ddda98ab063bb Mon Sep 17 00:00:00 2001 From: xcd <834800634@qq.com> Date: 星期三, 19 八月 2020 11:04:22 +0800 Subject: [PATCH] 1. 优化gts板卡回原点操作 2. 添加数据精度配置 3. 添加HalconTool异常捕捉分类和部分非空判断 --- src/Bro.Device.GTSCard/GTSCardDriver.cs | 601 +++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 416 insertions(+), 185 deletions(-) diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs index 5b3eb7f..c430886 100644 --- a/src/Bro.Device.GTSCard/GTSCardDriver.cs +++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs @@ -21,7 +21,7 @@ public class GTSCardDriver : MotionCardBase { // 寮傚父浜嬩欢 - public Action<Exception> OnExceptionRaised; + //public Action<Exception> OnExceptionRaised; public GTSCardInitialConfig IIConfig { @@ -46,6 +46,8 @@ /// 杩愬姩杞寸珛鍗虫殏鍋� /// </summary> Dictionary<int, ManualResetEvent> axisImmediatePauseHandleDict = new Dictionary<int, ManualResetEvent>(); + Dictionary<int, bool> axisImmediatePauseFlag = new Dictionary<int, bool>(); + Dictionary<int, bool> axisPauseResumeFlag = new Dictionary<int, bool>(); //Dictionary<int, CancellationTokenSource> axisMoveCancelDict = new Dictionary<int, CancellationTokenSource>(); @@ -61,7 +63,7 @@ { AxisInfo axisInfo = new AxisInfo(); axisInfo.AxisName = axisSetting.AxisName; - var axisMovingStatus = AxisMovingOptionValues.FirstOrDefault(u => u.AxisIndex == axisSetting.AxisIndex); + var axisMovingStatus = AxisStatusList.FirstOrDefault(u => u.AxisIndex == axisSetting.AxisIndex); axisInfo.AxisLocation = axisMovingStatus == null ? 0 : Convert.ToDouble(axisMovingStatus.CurPosition); axisInfos.Add(axisInfo); @@ -96,6 +98,8 @@ protected override void Start() { AllAxisOn(); + + base.Start(); } protected override void Stop() @@ -116,11 +120,17 @@ { foreach (var operationSet in motionCardOperationConfig.OperationCollection) { + if (operationSet.DelayBefore > 0) + Thread.Sleep(operationSet.DelayBefore); + responseMessage = RunOperationSet(operationSet); if (!responseMessage.Result) { return responseMessage; } + + if (operationSet.DelayAfter > 0) + Thread.Sleep(operationSet.DelayAfter); } } return responseMessage; @@ -137,16 +147,22 @@ { foreach (var preCheck in operationSet.PreCheckIOCollection) { - int timeout = operationSet.PreCheckIOTimeout; + _pauseHandle.Wait(); + IOValue? ioData = null; - while (CurrentState == DeviceState.DSOpen) + if (CurrentState == DeviceState.DSOpen) { - Thread.Sleep(10); - ioData = MonitorValues.FirstOrDefault(u => u.IONum == preCheck.IOItem.IONum && u.IOType == preCheck.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 - timeout -= 10; - if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0)) + int timeout = operationSet.PreCheckIOTimeout; + + while (CurrentState == DeviceState.DSOpen) { - break; + Thread.Sleep(10); + ioData = MonitorValues.FirstOrDefault(u => u.IONum == preCheck.IOItem.IONum && u.IOType == preCheck.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 + timeout -= 10; + if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0)) + { + break; + } } } @@ -162,10 +178,15 @@ // 2.鏉垮崱杩愬姩 if (CurrentState == DeviceState.DSOpen) { - responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps }); - if (!responseMessage.Result) + _pauseHandle.Wait(); + + if (CurrentState == DeviceState.DSOpen) { - return responseMessage; + responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps }); + if (!responseMessage.Result) + { + return responseMessage; + } } } @@ -173,14 +194,23 @@ // 3.IO杈撳嚭 涓嶉渶瑕佽秴鏃� if (CurrentState == DeviceState.DSOpen) { + foreach (var ioOutput in operationSet.IOOutputCollection) { - var ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioOutput.IOItem.IONum && u.IOType == ioOutput.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 - if (ioOutput.CheckValue != ioData) + _pauseHandle.Wait(); + + if (CurrentState == DeviceState.DSOpen) { - responseMessage.Result = false; - responseMessage.Message = $"IO杈撳嚭涓嶉�氳繃锛岄厤缃細{ioOutput.GetDisplayText()}锛屽綋鍓嶅�硷細{ioData}"; - return responseMessage; + WriteOutput((short)ioOutput.IOItem.IONum, ioOutput.CheckValue); + + //var ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioOutput.IOItem.IONum && u.IOType == ioOutput.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 + + //if (ioOutput.CheckValue != ioData) + //{ + // responseMessage.Result = false; + // responseMessage.Message = $"IO杈撳嚭涓嶉�氳繃锛岄厤缃細{ioOutput.GetDisplayText()}锛屽綋鍓嶅�硷細{ioData}"; + // return responseMessage; + //} } } } @@ -217,20 +247,21 @@ #endregion #region ImmediatePause + ManualResetEventSlim _pauseHandle = new ManualResetEventSlim(true); + /// <summary> /// 鍚姩绔嬪嵆鏆傚仠 /// </summary> - public void SetImmediatePause() + public override void SetImmediatePause() { if (!_isResetting) { var immediatePauseAxis = IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled && a.IsImmediatePause).Select(u => u.AxisIndex).ToList(); - + _pauseHandle.Reset(); immediatePauseAxis.ForEach(async axisIndex => { axisImmediatePauseHandleDict[axisIndex].Reset(); - - //axisMoveCancelDict[axisIndex].Cancel(); + axisImmediatePauseFlag[axisIndex] = true; await MoveStop(axisIndex, 0);//鎵�鏈夎酱閮芥殏鍋� }); @@ -240,15 +271,22 @@ /// <summary> /// 鎭㈠绔嬪嵆鏆傚仠 /// </summary> - public void ResetImmediatePause() + public override void ResetImmediatePause(bool isResumeMoving) { var immediatePauseAxis = IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled && a.IsImmediatePause).Select(u => u.AxisIndex).ToList(); - + _pauseHandle.Set(); immediatePauseAxis.ForEach(axisIndex => { - //axisMoveCancelDict[axisIndex] = new CancellationTokenSource(); - + axisImmediatePauseFlag[axisIndex] = false; axisImmediatePauseHandleDict[axisIndex].Set(); + if (isResumeMoving) + { + axisPauseResumeFlag[axisIndex] = true; + } + else + { + axisPauseResumeFlag[axisIndex] = false; + } }); } #endregion @@ -267,7 +305,7 @@ ClearStatus(1, IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled).Count); if (res != (short)GTSRetCode.GRCRunOK) { - throw new Exception("鏉垮崱杞藉叆閰嶇疆鏂囦欢寮傚父锛岄敊璇爜锛�" + res); + throw new ProcessException("鏉垮崱杞藉叆閰嶇疆鏂囦欢寮傚父锛岄敊璇爜锛�" + res); } } @@ -336,15 +374,14 @@ ResponseMessage responseMessage = new ResponseMessage(); if (opConfig is MotionOperationCollection gtsOperationCollection) { - List<Task<bool>> taskList = new List<Task<bool>>(); - foreach (var movingOp in gtsOperationCollection.MovingOps) + List<bool> resultList = new List<bool>(); + Parallel.ForEach(gtsOperationCollection.MovingOps, movingOp => { - var task = SingleAxisMoving(movingOp); - taskList.Add(task); - } - - Task.WaitAll(taskList.ToArray()); - responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult()); + axisImmediatePauseFlag[movingOp.AxisIndex] = false; + axisPauseResumeFlag[movingOp.AxisIndex] = true; + resultList.Add(SingleAxisMoving(movingOp).Result); + }); + responseMessage.Result = resultList.All(u => u == true); if (!responseMessage.Result) { responseMessage.Message = $"鐐逛綅杩愬姩寮傚父"; @@ -366,81 +403,96 @@ resultCode.Add(GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, axisIndex)); resultCode.Add(GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, axisIndex, out trapprm)); trapprm.smoothTime = 1; - trapprm.acc = optionPara.VelocityPara.Acc != 0 ? optionPara.VelocityPara.Acc : 1; - trapprm.dec = optionPara.VelocityPara.Dec != 0 ? optionPara.VelocityPara.Dec : 1; + + if (optionPara.VelocityPara.Acc != 0) + { + trapprm.acc = optionPara.VelocityPara.Acc; + } + + if (optionPara.VelocityPara.Dec != 0) + { + trapprm.dec = optionPara.VelocityPara.Dec; + } resultCode.Add(GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, axisIndex, ref trapprm)); - resultCode.Add(GTSCardAPI.GT_SetVel((short)IConfig.CardNum, axisIndex, optionPara.VelocityPara.Velocity * IConfig.AxisVelocityRatio)); + + if (optionPara.VelocityPara.Velocity != 0) + { + resultCode.Add(GTSCardAPI.GT_SetVel((short)IConfig.CardNum, axisIndex, optionPara.VelocityPara.Velocity * IConfig.AxisVelocityRatio)); + } var resultOK = resultCode.All(u => u == (short)GTSRetCode.GRCRunOK); if (!resultOK) { - throw new Exception("杞�" + optionPara.AxisIndex + "璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛�" + string.Join(",", resultCode)); + throw new ProcessException("杞�" + optionPara.AxisIndex + "璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛�" + string.Join(",", resultCode)); } return resultOK; } + TaskFactory taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning); /// <summary> /// 鍗曚釜杞� 杩愬姩(鐐瑰埌鐐� jog 鍥為浂...锛� /// </summary> /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param> - public override async Task<bool> SingleAxisMoving(MovingOption optionPara) + public override Task<bool> SingleAxisMoving(MovingOption optionPara) { - return await Task.Run(() => - { - axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); + return Task.Run(() => + { bool isSuccessAndStop = false; - try + do { - if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false) + axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); + + if (axisPauseResumeFlag.ContainsKey(optionPara.AxisIndex) && !axisPauseResumeFlag[optionPara.AxisIndex]) + return true; + + try { - string _position = ""; - string motionType = optionPara.MoveMode == EnumHelper.MotionMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString(); - - _position = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff},{optionPara.AxisIndex},{motionType},{GetPosition(optionPara.AxisIndex)},{GetPrfPosition(optionPara.AxisIndex)},{optionPara.Destination},"; - - switch (optionPara.MoveMode) + if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false) { - case MotionMode.Normal: - { - if (_isResetting) - { - LogAsync(DateTime.Now, "澶嶄綅涓惎鍔ㄨ繍鍔ㄥ紓甯�", optionPara.AxisIndex + "鍚姩杩愬姩寮傚父"); - return false; - } + string motionType = optionPara.MoveMode == EnumHelper.MotionMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString(); - if (optionPara.IsAbsolute) + switch (optionPara.MoveMode) + { + case MotionMode.Normal: { - isSuccessAndStop = P2PMoveAbs(optionPara); - } - else - { - isSuccessAndStop = P2PMoveRel(optionPara); - } + if (_isResetting) + { + LogAsync(DateTime.Now, "澶嶄綅涓惎鍔ㄨ繍鍔ㄥ紓甯�", optionPara.AxisIndex + "鍚姩杩愬姩寮傚父"); + return false; + } - } - break; - case MotionMode.FindOri: - { - isSuccessAndStop = GoHome(optionPara); - } - break; - case MotionMode.Jog: - { - isSuccessAndStop = JogMove(optionPara); - } - break; + if (optionPara.IsAbsolute) + { + isSuccessAndStop = P2PMoveAbs(optionPara); + } + //else + //{ + // isSuccessAndStop = P2PMoveRel(optionPara); + //} + + } + break; + case MotionMode.FindOri: + { + //isSuccessAndStop = GoHome(optionPara); + isSuccessAndStop = P2PGoHome(optionPara); + } + break; + case MotionMode.Jog: + { + isSuccessAndStop = JogMove(optionPara); + } + break; + } } - _position += $"{GetPosition(optionPara.AxisIndex)},"; - _position += $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}"; - LogAsync(DateTime.Now, "", _position); } - } - catch (Exception ex) - { - isSuccessAndStop = false; - LogAsync(DateTime.Now, $"杞磠optionPara.AxisIndex}杩愬姩寮傚父", ex.GetExceptionMessage()); - } + catch (Exception ex) + { + isSuccessAndStop = false; + LogAsync(DateTime.Now, $"杞磠optionPara.AxisIndex}杩愬姩寮傚父", ex.GetExceptionMessage()); + } + } while (axisImmediatePauseFlag.ContainsKey(optionPara.AxisIndex) && axisImmediatePauseFlag[optionPara.AxisIndex]); return isSuccessAndStop; }); } @@ -457,9 +509,10 @@ var ret = GTSCardAPI.GT_GetPrfPos((short)IConfig.CardNum, (short)axisNum, out prfpos, 1, out pclock); if (ret != (short)GTSRetCode.GRCRunOK) { - throw new Exception("杞�" + axisNum + "鑾峰彇瑙勫垝浣嶇疆寮傚父锛岄敊璇爜锛�" + ret); + throw new ProcessException("杞�" + axisNum + "鑾峰彇瑙勫垝浣嶇疆寮傚父锛岄敊璇爜锛�" + ret); } - position = prfpos / IConfig.AxisVelocityRatio; + //var AxisRatio = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == axisNum) == null ? 1 : IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == axisNum).AxisRatio; + position = prfpos; return position; } @@ -473,13 +526,14 @@ lock (moveLock) { double position = 0; - int pPos = 0; - var ret = GTSCardAPI.GT_GetPos((short)IConfig.CardNum, (short)axisNum, out pPos); + double pPos = 0; + var ret = GTSCardAPI.GT_GetPrfPos((short)IConfig.CardNum, (short)axisNum, out pPos, 1, out uint pclock); if (ret != (short)GTSRetCode.GRCRunOK) { - throw new Exception("杞�" + axisNum + "鑾峰彇鐩爣浣嶇疆寮傚父锛岄敊璇爜锛�" + ret); + throw new ProcessException("杞�" + axisNum + "鑾峰彇鐩爣浣嶇疆寮傚父锛岄敊璇爜锛�" + ret); } - position = pPos / IConfig.AxisVelocityRatio; + //var AxisRatio = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == axisNum) == null ? 1 : IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == axisNum).AxisRatio; + position = pPos; return position; } } @@ -488,7 +542,7 @@ /// 鑾峰彇瑙勫垝閫熷害 /// </summary> /// <param name="axisNum">Axis number</param> - /// <returns></returns> + /// <returns>閫熷害鑴夊啿</returns> public double GetPrfVelocity(int axisNum) { double prfVel = 0; @@ -496,9 +550,8 @@ var ret = GTSCardAPI.GT_GetPrfVel((short)IConfig.CardNum, (short)axisNum, out prfVel, 1, out pclock); if (ret != (short)GTSRetCode.GRCRunOK) { - throw new Exception("杞�" + axisNum + "鑾峰彇瑙勫垝閫熷害寮傚父锛岄敊璇爜锛�" + ret); + throw new ProcessException("杞�" + axisNum + "鑾峰彇瑙勫垝閫熷害寮傚父锛岄敊璇爜锛�" + ret); } - prfVel = prfVel / IConfig.AxisVelocityRatio; return prfVel; } @@ -506,16 +559,15 @@ /// 鑾峰彇褰撳墠閫熷害 /// </summary> /// <param name="axisNum">Axis number</param> - /// <returns></returns> + /// <returns>閫熷害鑴夊啿</returns> public double GetVelocity(int axisNum) { double vel = 0; var ret = GTSCardAPI.GT_GetVel((short)IConfig.CardNum, (short)axisNum, out vel); if (ret != (short)GTSRetCode.GRCRunOK) { - throw new Exception("杞�" + axisNum + "鑾峰彇褰撳墠閫熷害寮傚父锛岄敊璇爜锛�" + ret); + throw new ProcessException("杞�" + axisNum + "鑾峰彇褰撳墠閫熷害寮傚父锛岄敊璇爜锛�" + ret); } - vel = vel / IConfig.AxisVelocityRatio; return vel; } @@ -554,7 +606,7 @@ catch (Exception ex) { AllMoveStop(true); - OnExceptionRaised?.Invoke(ex); + OnExceptionOccured?.Invoke(DateTime.Now, ex); return false; } } @@ -572,7 +624,7 @@ if (_isResetting) { LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); - throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); + throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); } int repeatTime = 30; @@ -591,7 +643,7 @@ if (CurrentState != EnumHelper.DeviceState.DSOpen) { LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�"); - throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); + throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); } LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination); @@ -600,11 +652,12 @@ int currentPosition = (int)GetPosition(optionPara.AxisIndex); int dPosition = optionPara.Destination + currentPosition; int timeout = optionPara.MovingTimeout; + var AxisRatio = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == optionPara.AxisIndex) == null ? 1 : IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == optionPara.AxisIndex).AxisRatio; while (CurrentState == DeviceState.DSOpen) { //璁剧疆 杩愬姩鍙傛暟 isSuccessSetAxisParam = SetAxisParam(optionPara); - ret = GTSCardAPI.GT_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(dPosition * IConfig.AxisVelocityRatio));// 璁剧疆瑙勫垝浣嶇疆 + ret = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(dPosition));// 璁剧疆鐩殑浣嶇疆 ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杩愬姩 if (ret != (short)GTSRetCode.GRCRunOK) @@ -624,8 +677,8 @@ repeatTime = 1000; do { - isStop = IsStop((short)optionPara.AxisIndex); Thread.Sleep(50); + isStop = IsStop((short)optionPara.AxisIndex); repeatTime--; } while (!isStop && repeatTime > 0); @@ -634,7 +687,7 @@ catch (Exception ex) { AllMoveStop(true); - OnExceptionRaised?.Invoke(ex); + OnExceptionOccured?.Invoke(DateTime.Now, ex); return false; } } @@ -652,7 +705,7 @@ if (_isResetting) { LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); - throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); + throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); } int repeatTime = 30; while (CurrentState != EnumHelper.DeviceState.DSOpen && repeatTime > 0) @@ -669,17 +722,18 @@ if (CurrentState != EnumHelper.DeviceState.DSOpen) { LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�"); - throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); + throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); } LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination); short ret = 0; bool isSuccessSetAxisParam = false; int timeout = optionPara.MovingTimeout; - while (CurrentState == DeviceState.DSOpen&&!_isPause) + //var AxisRatio = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == optionPara.AxisIndex) == null ? 1 : IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == optionPara.AxisIndex).AxisRatio; + while (CurrentState == DeviceState.DSOpen && !_isPause) { //璁剧疆 杩愬姩鍙傛暟 isSuccessSetAxisParam = SetAxisParam(optionPara); - ret = GTSCardAPI.GT_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(optionPara.Destination * IConfig.AxisVelocityRatio));// 璁剧疆瑙勫垝浣嶇疆 + ret = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(optionPara.Destination));// 璁剧疆鐩爣浣嶇疆 ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杩愬姩 if (ret != (short)GTSRetCode.GRCRunOK) @@ -698,8 +752,8 @@ repeatTime = 1000; while (!isStop && repeatTime > 0) { - isStop = IsStop((short)optionPara.AxisIndex); Thread.Sleep(50); + isStop = IsStop((short)optionPara.AxisIndex); repeatTime--; } @@ -708,7 +762,7 @@ catch (Exception ex) { AllMoveStop(true); - OnExceptionRaised?.Invoke(ex); + OnExceptionOccured?.Invoke(DateTime.Now, ex); return false; } } @@ -732,7 +786,7 @@ if (ret != (short)GTSRetCode.GRCRunOK) { LogAsync(DateTime.Now, "杞�" + axisNum + "杩愬姩鍋滄寮傚父", "閿欒鐮侊細" + ret); - throw new Exception("杞�" + axisNum + "杩愬姩鍋滄寮傚父锛岄敊璇爜锛�" + ret); + throw new ProcessException("杞�" + axisNum + "杩愬姩鍋滄寮傚父锛岄敊璇爜锛�" + ret); } else { @@ -741,8 +795,8 @@ int repeatTime = 100; do { - isStop = IsStop((short)axisNum); Thread.Sleep(10); + isStop = IsStop((short)axisNum); repeatTime--; } while (!isStop && repeatTime > 0); @@ -768,54 +822,189 @@ var resultOK = taskList.All(u => u.GetAwaiter().GetResult()); } + ///// <summary> + ///// 鍥炲師鐐� + ///// </summary> + ///// <param name="movingOption">鍗″彿</param> + ///// <param name="axisn">杞村彿</param> + ///// <param name="homests">杞村洖鍘熺偣鐘舵��</param> + //public bool GoHome(MovingOption movingOption) + //{ + // try + // { + // PositionReset(movingOption.AxisIndex, 1); + // GTSCardAPI.THomePrm thomeprm; + // GTSCardAPI.THomeStatus homests; + // // 鍚姩Home鎹曡幏 + // short rtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME); + // // 鍒囨崲鍒扮偣浣嶈繍鍔ㄦā寮� + // rtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex); + // // 璇诲彇鐐逛綅妯″紡杩愬姩鍙傛暟 + // rtn = GTSCardAPI.GT_GetHomePrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out thomeprm); + + // thomeprm.mode = movingOption.GoHomePara.HomeMode;//鍥為浂鏂瑰紡 + // thomeprm.moveDir = movingOption.GoHomePara.HomeDir;//鍥為浂鏂瑰悜 + // thomeprm.edge = movingOption.GoHomePara.Edge; + // thomeprm.velHigh = movingOption.GoHomePara.HighVelocity; + // thomeprm.velLow = movingOption.GoHomePara.LowVelocity; + // thomeprm.acc = movingOption.VelocityPara.Acc; + // thomeprm.dec = movingOption.VelocityPara.Dec; + // thomeprm.searchHomeDistance = movingOption.GoHomePara.SearchHomeDistance;//鎼滅储璺濈 + // thomeprm.homeOffset = movingOption.GoHomePara.HomeOffset; //鍋忕Щ璺濈 + // thomeprm.escapeStep = movingOption.GoHomePara.EscapeStep; + // rtn = GTSCardAPI.GT_GoHome((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref thomeprm); //鍚姩鍥為浂 + + // bool isStop = false; + // int repeatTime = 1000; + // do + // { + // Thread.Sleep(10); + // GTSCardAPI.GT_GetHomeStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out homests); + + // isStop = homests.run == 0; + // if (isStop && homests.error == 0) + // { + // Thread.Sleep(200); + // PositionReset(movingOption.AxisIndex, 1); + // } + // repeatTime--; + // } while (!isStop && repeatTime > 0); + + // return isStop; + // } + // catch (Exception ex) + // { + // AllMoveStop(true); + // OnExceptionOccured?.Invoke(DateTime.Now, ex); + // return false; + // } + //} + /// <summary> - /// 鍥炲師鐐� + /// P2P鏂瑰紡鍥炲師鐐� /// </summary> - /// <param name="movingOption">鍗″彿</param> - /// <param name="axisn">杞村彿</param> - /// <param name="homests">杞村洖鍘熺偣鐘舵��</param> - public bool GoHome(MovingOption movingOption) + /// <param name="movingOption">杩愬姩鍙傛暟</param> + public bool P2PGoHome(MovingOption movingOption) { try { - PositionReset(movingOption.AxisIndex, 1); - GTSCardAPI.THomePrm thomeprm; - GTSCardAPI.THomeStatus homests; - short rtn = GTSCardAPI.GT_GetHomePrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out thomeprm); - thomeprm.mode = movingOption.GoHomePara.HomeMode;//鍥為浂鏂瑰紡 - thomeprm.moveDir = movingOption.GoHomePara.HomeDir;//鍥為浂鏂瑰悜 - thomeprm.edge = movingOption.GoHomePara.Edge; - thomeprm.velHigh = movingOption.GoHomePara.HighVelocity; - thomeprm.velLow = movingOption.GoHomePara.LowVelocity; - thomeprm.acc = movingOption.VelocityPara.Acc; - thomeprm.dec = movingOption.VelocityPara.Dec; - thomeprm.searchHomeDistance = movingOption.GoHomePara.SearchHomeDistance;//鎼滄悳璺濈 - thomeprm.homeOffset = movingOption.GoHomePara.HomeOffset; //鍋忕Щ璺濈 - thomeprm.escapeStep = movingOption.GoHomePara.EscapeStep; - rtn = GTSCardAPI.GT_GoHome((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref thomeprm); //鍚姩鍥為浂 + var goHomePara = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex).GoHomePara; + bool homeDirection = goHomePara.IsHomeDirPositive; + bool isRightLimitReached = false; + short capture = 0; - bool isStop = false; - int repeatTime = 1000; + GTSCardAPI.TTrapPrm trapPrm; + + // 鍒囨崲鍒扮偣浣嶈繍鍔ㄦā寮� + 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; + trapPrm.dec = movingOption.VelocityPara.Dec; + // 璁剧疆鐐逛綅妯″紡杩愬姩鍙傛暟 + sRtn = GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref trapPrm); + // 璁剧疆鐐逛綅妯″紡鐩爣閫熷害锛屽嵆鍥炲師鐐归�熷害 + sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, goHomePara.HighVelocity); + do { - Thread.Sleep(10); - GTSCardAPI.GT_GetHomeStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out homests); + PositionReset(movingOption.AxisIndex, 1); + ClearStatus(movingOption.AxisIndex, 1); - isStop = homests.run == 0; - if (isStop && homests.error == 0) + //鎼滅储璺濈 闃舵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_Update((short)IConfig.CardNum, 1 << (movingOption.AxisIndex - 1)); + + int repeatTime = goHomePara.GoHomeTimeOut * 1000; + + bool isStop = false; + int pos; + uint clk;//鏃堕挓鍙傛暟 + do { - Thread.Sleep(200); - PositionReset(movingOption.AxisIndex, 1); - } - repeatTime--; - } while (!isStop && repeatTime > 0); + Thread.Sleep(IConfig.MonitorInterval * 5); + // 璇诲彇鎹曡幏鐘舵�� + GTSCardAPI.GT_GetCaptureStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out capture, out pos, 1, out clk); + isStop = IsStop((short)movingOption.AxisIndex); + repeatTime -= IConfig.MonitorInterval * 5; + } while (!(isStop || capture == 1 || repeatTime <= 0)); - return isStop; + if (repeatTime <= 0) + { + MoveStop((short)movingOption.AxisIndex, 0); + throw new ProcessException($"杩愬姩杞磠movingOption.AxisIndex} 锛屽洖鍘熺偣瓒呮椂寮傚父"); + } + + var axisStatus = AxisStatusList.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex); + + if (isStop && capture != 1) + { + if (((axisStatus.AxisStatus & 0x20) != 0) || ((axisStatus.AxisStatus & 0x40) != 0)) + { + //movingOption.GoHomePara.HomeDir = (short)(movingOption.GoHomePara.HomeDir == 1 ? -1 : 1); + + //姝i檺浣� + if ((axisStatus.AxisStatus & 0x20) != 0 && !goHomePara.IsCaptureDirPositive) + { + isRightLimitReached = true; + } + + //璐熼檺浣� + if ((axisStatus.AxisStatus & 0x40) != 0 && goHomePara.IsCaptureDirPositive) + { + isRightLimitReached = true; + } + + homeDirection = !homeDirection; + } + + ClearStatus(movingOption.AxisIndex, 1); + } + + if (capture == 1) + { + if (!isRightLimitReached) + { + capture = 0; + ClearStatus(movingOption.AxisIndex, 1); + //GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME); + continue; + } + + //鍏坰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); + // 鍦ㄨ繍鍔ㄧ姸鎬佷笅鏇存柊鐩爣浣嶇疆 + sRtn = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (movingOption.AxisIndex - 1)); + isStop = false; + + repeatTime = 1000; + do + { + Thread.Sleep(20); + isStop = IsStop((short)movingOption.AxisIndex); + repeatTime--; + } while (!isStop && repeatTime > 0); + PositionReset(movingOption.AxisIndex, 1); + return (sRtn == (short)GTSRetCode.GRCRunOK) && isStop; + } + + } while (!(capture == 1 && isRightLimitReached)); + return false; } catch (Exception ex) { AllMoveStop(true); - OnExceptionRaised?.Invoke(ex); + OnExceptionOccured?.Invoke(DateTime.Now, ex); return false; } } @@ -852,13 +1041,13 @@ /// 鎸変綅璁剧疆鏁板瓧 IO 杈撳嚭鐘舵�� /// </summary> /// <param name="index">杈撳嚭鍙�,杩斿洖1-16</param> - /// <param name="value">false琛ㄧず杈撳嚭锛宼rue琛ㄧず鍏抽棴</param> + /// <param name="value">false琛ㄧず鍏筹紝true琛ㄧず寮�锛屾澘鍗¤璁剧疆鍙栧弽</param> public override void WriteOutput(short index, IOValue value) { short outNum = (short)(index % 100 + 1); if ((int)value <= 1) { - GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)value); + GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, IConfig.IsOutputReversed ? (short)(value == IOValue.TRUE ? 0 : 1) : (short)value); } else { @@ -875,7 +1064,7 @@ /// <returns></returns> public bool IsStop(short axisNum) { - int sts = GetAxisStatus(axisNum); + int sts = AxisStatusList.FirstOrDefault(u => u.AxisIndex == axisNum).AxisStatus; if ((sts & 0x400) == 0) return true;//鍋滄杩斿洖true else return false; //杩愯涓繑鍥瀎alse } @@ -929,10 +1118,10 @@ AxisMovingStatus axisMovingStatus = new AxisMovingStatus(); axisMovingStatus.AxisIndex = axisConfig.AxisIndex; axisMovingStatus.AxisName = axisConfig.AxisName; - axisMovingStatus.CurPosition = Convert.ToInt32(GetPosition(axisMovingStatus.AxisIndex)); + //axisMovingStatus.CurPosition = Convert.ToInt32(GetPosition(axisMovingStatus.AxisIndex)); axisMovingStatus.Destination = Convert.ToInt32(GetPrfPosition(axisMovingStatus.AxisIndex)); - axisMovingStatus.CurVelocity = GetVelocity(axisMovingStatus.AxisIndex); - axisMovingStatus.PrfVelocity = GetPrfVelocity(axisMovingStatus.AxisIndex); + //axisMovingStatus.CurVelocity = GetVelocity(axisMovingStatus.AxisIndex); + //axisMovingStatus.PrfVelocity = GetPrfVelocity(axisMovingStatus.AxisIndex); axisMovingStatus.AxisStatus = GetAxisStatus(axisMovingStatus.AxisIndex); axisMovingStatusesList.Add(axisMovingStatus); @@ -954,7 +1143,7 @@ GTSCardAPI.GT_GetDo((short)IConfig.CardNum, GTSCardAPI.MC_GPO, out outValue); //瑙f瀽缁撴灉 - for (var index = 1; index <= 16; index++) + for (var index = 0; index < 16; index++) { IOItem inItem = new IOItem() { @@ -985,28 +1174,39 @@ { if (!IConfig.IsEnableMonitor) return; + var newValues = GetMonitorValues(); var newAxisMovingStatus = GetAxisMovingStatus(); + if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0) continue; - Stopwatch sw = new Stopwatch(); - sw.Start(); + //Stopwatch sw = new Stopwatch(); + //sw.Start(); if (MonitorValues.Count == newValues.Count) { - var tempNew = newValues.DeepSerializeClone();//clone - var tempOld = MonitorValues.DeepSerializeClone(); + //var tempNew = newValues.DeepSerializeClone();//clone + //var tempOld = MonitorValues.DeepSerializeClone(); + var tempNew = new List<IOItem>(newValues);//clone + var tempOld = new List<IOItem>(MonitorValues); MonitorCheckAndInvoke(tempNew, tempOld); } - AxisMovingOptionValues = new List<AxisMovingStatus>(newAxisMovingStatus); - MonitorValues = new List<IOItem>(newValues); - sw.Stop(); - - if (sw.ElapsedMilliseconds > 20) + if (AxisStatusList.Count == newAxisMovingStatus.Count) { - LogAsync(DateTime.Now, $"{this.Name}杞鏃堕棿锛歿sw.ElapsedMilliseconds}", ""); + var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone + var tempOld = new List<AxisMovingStatus>(AxisStatusList); + AxisStatusCheck(tempNew, tempOld); } + + AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus); + MonitorValues = new List<IOItem>(newValues); + //sw.Stop(); + + //if (sw.ElapsedMilliseconds > 20) + //{ + // LogAsync(DateTime.Now, $"{this.Name}杞鏃堕棿锛歿sw.ElapsedMilliseconds}", ""); + //} if (IConfig.MonitorInterval > 0) { @@ -1025,29 +1225,52 @@ }); } + private async void AxisStatusCheck(List<AxisMovingStatus> tempNew, List<AxisMovingStatus> tempOld) + { + await Task.Run(() => + { + foreach (var newSts in tempNew) + { + var oldSts = tempOld.FirstOrDefault(u => u.AxisIndex == newSts.AxisIndex); + if (oldSts != null) + { + if (oldSts.AxisStatus != newSts.AxisStatus) + { + AxisStatusChanged(newSts.AxisIndex, oldSts.AxisStatus, newSts.AxisStatus); + } + + if (((newSts.AxisStatus >> 1 & 1) == 1) && ((oldSts.AxisStatus >> 1 & 1) == 0)) //鍒濇鎶ヨ + { + AxisAlarmRaised(newSts.AxisStatus, $"杞磠newSts.AxisIndex}:{newSts.AxisName}杞翠己鏈嶆姤璀�"); + } + } + } + }); + } + public override void OnMethodInvoked(IAsyncResult ar) { - MotionCardMonitorSet monitorSet = ar.AsyncState as MotionCardMonitorSet; - ProcessResponse resValues = monitorSet.Response; - if (resValues.ResultValue == (int)ReplyValue.IGNORE) - { - return; - } + //MotionCardMonitorSet monitorSet = ar.AsyncState as MotionCardMonitorSet; + //ProcessResponse resValues = monitorSet.Response; + //if (resValues.ResultValue == (int)ReplyValue.IGNORE) + //{ + // return; + //} - Stopwatch sw = new Stopwatch(); - sw.Start(); - // 灏嗘寚瀹欼OItem鍐欏叆鏉垮崱 - foreach (var replyIOData in monitorSet.ReplyIODatas) - { - //鍐欏叆IO杈撳嚭 - if (replyIOData.IOType == IOType.OUTPUT) - { - GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPI, (short)replyIOData.IONum, (short)replyIOData.Value); - } - // in鍙涓嶈兘鍐� - } - sw.Stop(); - LogAsync(DateTime.Now, $"{Name}鍙嶉瀹屾垚锛岃�楁椂{sw.ElapsedMilliseconds}ms", $"{resValues.GetDisplayText()}"); + //Stopwatch sw = new Stopwatch(); + //sw.Start(); + //// 灏嗘寚瀹欼OItem鍐欏叆鏉垮崱 + //foreach (var replyIOData in monitorSet.ReplyIODatas) + //{ + // //鍐欏叆IO杈撳嚭 + // if (replyIOData.IOType == IOType.OUTPUT) + // { + // GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPI, (short)replyIOData.IONum, (short)replyIOData.Value); + // } + // // in鍙涓嶈兘鍐� + //} + //sw.Stop(); + //LogAsync(DateTime.Now, $"{Name}鍙嶉瀹屾垚锛岃�楁椂{sw.ElapsedMilliseconds}ms", $"{resValues.GetDisplayText()}"); } protected void MonitorCheckAndInvoke(List<IOItem> tempNew, List<IOItem> tempOld) @@ -1092,8 +1315,9 @@ //monitorSet.OpConfig.InputPara = monitorSet.InputDataIndex.ConvertAll(index => //{ - // return tempNew[index].Value; + // return tempNew[index].Value == IOValue.TRUE ? 1 : 0; //}).ToList(); + monitorSet.OpConfig.InputPara = new List<int>() { (int)newIOItem.Value }; ExcuteMonitorInvoke(DateTime.Now, monitorSet.InvokeDevice, this, monitorSet); } @@ -1107,15 +1331,22 @@ int axis_sts; var axisSettings = IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled); ClearStatus(1, axisSettings.Count); + + if (AxisStatusList.Count == 0) + { + Thread.Sleep(10); + } + foreach (var axisSetting in axisSettings) { - axis_sts = GetAxisStatus((short)axisSetting.AxisIndex); + //axis_sts = GetAxisStatus((short)axisSetting.AxisIndex); + axis_sts = AxisStatusList.FirstOrDefault(u => u.AxisIndex == axisSetting.AxisIndex)?.AxisStatus ?? 0; if ((axis_sts & 0x200) == 0) { var rst = GTSCardAPI.GT_AxisOn((short)IConfig.CardNum, (short)axisSetting.AxisIndex); } - // 鎵�鏈変綅缃闆� - PositionReset(1, axisSettings.Count); + //// 鎵�鏈変綅缃闆� + //PositionReset(1, axisSettings.Count); // 姝f瀬闄愭姤璀� if ((axis_sts & 0x20) != 0) { -- Gitblit v1.8.0