From 5918194fccdb2a2303e713b8d2f3335243b9e2ef Mon Sep 17 00:00:00 2001 From: wells.liu <wells.liu@broconcentric.com> Date: 星期二, 07 七月 2020 14:53:13 +0800 Subject: [PATCH] 板卡驱动 --- src/Bro.Device.GTSCard/GTSCardDriver.cs | 283 ++++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 200 insertions(+), 83 deletions(-) diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs index b8e668e..c2cc8e6 100644 --- a/src/Bro.Device.GTSCard/GTSCardDriver.cs +++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs @@ -37,6 +37,13 @@ /// </summary> bool _isResetting = false; + /// <summary> + /// 杩愬姩杞寸珛鍗虫殏鍋� + /// </summary> + Dictionary<int, ManualResetEvent> axisImmediatePauseHandleDict = new Dictionary<int, ManualResetEvent>(); + + Dictionary<int, CancellationTokenSource> axisMoveCancelDict = new Dictionary<int, CancellationTokenSource>(); + public void SetResetFlag(bool isReset) { _isResetting = isReset; @@ -52,16 +59,24 @@ protected override void Init() { InitialMotionCard(); + axisImmediatePauseHandleDict = IConfig.AxisSettings.ToDictionary(a => a.AxisIndex, a => new ManualResetEvent(true)); + axisMoveCancelDict = IConfig.AxisSettings.ToDictionary(a => a.AxisIndex, a => new CancellationTokenSource()); + + axisMoveCancelDict.Values.ToList().ForEach(c => + { + c = new CancellationTokenSource(); + }); } + ManualResetEvent _pauseHandle = new ManualResetEvent(true); protected override void Pause() { - throw new NotImplementedException(); + } protected override void Resume() { - throw new NotImplementedException(); + } protected override void Start() @@ -76,27 +91,47 @@ } /// <summary> - /// 璁惧 杩愯 + /// 璁惧 杩愯锛堟墽琛� 鏉垮崱绯诲垪鐨勬搷浣滅殑 闆嗗悎锛� /// </summary> /// <param name="config"></param> /// <returns></returns> public override ResponseMessage Run(IOperationConfig config) { ResponseMessage responseMessage = new ResponseMessage(); - var opConfig = config as GTSCardOperationConfig; + if (config is MotionCardOperationConfigBase motionCardOperationConfig) + { + _pauseHandle.WaitOne(); + foreach (var operationSet in motionCardOperationConfig.OperationCollection) + { + responseMessage = RunOperationSet(operationSet); + if (!responseMessage.Result) + { + return responseMessage; + } + } + } + return responseMessage; + } + + /// <summary> + /// 鎵ц 涓�涓郴鍒楃殑鎿嶄綔 + /// </summary> + private ResponseMessage RunOperationSet(MotionCardOperationSet operationSet) + { + ResponseMessage responseMessage = new ResponseMessage(); // 1.棰勬鏌� if (CurrentState == DeviceState.DSOpen) { - foreach (var preCheck in opConfig.PreCheckIOCollection) + foreach (var preCheck in operationSet.PreCheckIOCollection) { - int timeout = opConfig.PreCheckIOTimeout; - int? ioData = null; + int timeout = operationSet.PreCheckIOTimeout; + IOValue? ioData = null; while (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 || (opConfig.PreCheckIOTimeout > 0 && timeout < 0)) + if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0)) { break; } @@ -114,8 +149,7 @@ // 2.鏉垮崱杩愬姩 if (CurrentState == DeviceState.DSOpen) { - responseMessage = MoveToPoint(opConfig); - + responseMessage = MoveToPoint(operationSet.MotionOperationCollection); if (!responseMessage.Result) { return responseMessage; @@ -126,7 +160,7 @@ // 3.IO杈撳嚭 涓嶉渶瑕佽秴鏃� if (CurrentState == DeviceState.DSOpen) { - foreach (var ioOutput in opConfig.IOOutputCollection) + 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) @@ -141,16 +175,16 @@ // 4.IO纭 if (CurrentState == DeviceState.DSOpen) { - foreach (var ioConfirm in opConfig.IOConfirmCollection) + foreach (var ioConfirm in operationSet.IOConfirmCollection) { - int timeout = opConfig.IOConfirmTimeout; - int? ioData = null; + int timeout = operationSet.IOConfirmTimeout; + IOValue? ioData = null; while (CurrentState == DeviceState.DSOpen) { Thread.Sleep(10); ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioConfirm.IOItem.IONum && u.IOType == ioConfirm.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 timeout -= 10; - if (ioConfirm.CheckValue == ioData || (opConfig.IOConfirmTimeout > 0 && timeout < 0)) + if (ioConfirm.CheckValue == ioData || (operationSet.IOConfirmTimeout > 0 && timeout < 0)) { break; } @@ -166,6 +200,43 @@ } return responseMessage; + } + #endregion + + #region ImmediatePause + /// <summary> + /// 鍚姩绔嬪嵆鏆傚仠 + /// </summary> + public void SetImmediatePause() + { + if (!_isResetting) + { + var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList(); + + immediatePauseAxis.ForEach(async axisIndex => + { + axisImmediatePauseHandleDict[axisIndex].Reset(); + + axisMoveCancelDict[axisIndex].Cancel(); + + await MoveStop(axisIndex, 0); + }); + } + } + + /// <summary> + /// 鎭㈠绔嬪嵆鏆傚仠 + /// </summary> + public void ResetImmediatePause() + { + var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList(); + + immediatePauseAxis.ForEach(axisIndex => + { + axisMoveCancelDict[axisIndex] = new CancellationTokenSource(); + + axisImmediatePauseHandleDict[axisIndex].Set(); + }); } #endregion @@ -250,24 +321,22 @@ public override ResponseMessage MoveToPoint(IOperationConfig opConfig) { ResponseMessage responseMessage = new ResponseMessage(); - var gtsOperationConfig = opConfig as GTSCardOperationConfig; - List<Task<bool>> taskList = new List<Task<bool>>(); - //TaskFactory factory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None); - // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱杩愬姩缁撴潫 - foreach (var movingOp in gtsOperationConfig.MovingOps) + if (opConfig is MotionOperationCollection gtsOperationCollection) { - //var task = factory.StartNew<bool>((op) => - //{ - // return SingleAxisMoving(op as MovingOption); - //}, movingOp); - var task = SingleAxisMoving(movingOp); - taskList.Add(task); - } - Task.WaitAll(taskList.ToArray()); - responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult()); - if (!responseMessage.Result) - { - responseMessage.Message = $"鐐逛綅杩愬姩寮傚父"; + _pauseHandle.WaitOne(); + List<Task<bool>> taskList = new List<Task<bool>>(); + foreach (var movingOp in gtsOperationCollection.MovingOps) + { + var task = SingleAxisMoving(movingOp); + taskList.Add(task); + } + + Task.WaitAll(taskList.ToArray()); + responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult()); + if (!responseMessage.Result) + { + responseMessage.Message = $"鐐逛綅杩愬姩寮傚父"; + } } return responseMessage; } @@ -311,14 +380,16 @@ { if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false) { + axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); + string _position = ""; - string motionType = optionPara.MoveMode == EnumHelper.MotorMoveMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString(); + 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) { - case MotorMoveMode.Normal: + case MotionMode.Normal: { if (_isResetting) { @@ -337,12 +408,12 @@ } break; - case MotorMoveMode.FindOri: + case MotionMode.FindOri: { isSuccessAndStop = GoHome(optionPara); } break; - case MotorMoveMode.Jog: + case MotionMode.Jog: { isSuccessAndStop = JogMove(optionPara); } @@ -402,6 +473,41 @@ } /// <summary> + /// 鑾峰彇瑙勫垝閫熷害 + /// </summary> + /// <param name="axisNum">Axis number</param> + /// <returns></returns> + public double GetPrfVelocity(int axisNum) + { + double prfVel = 0; + uint pclock = 0; + 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); + } + prfVel = prfVel / IConfig.AxisVelocityRatio; + return prfVel; + } + + /// <summary> + /// 鑾峰彇褰撳墠閫熷害 + /// </summary> + /// <param name="axisNum">Axis number</param> + /// <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); + } + vel = vel / IConfig.AxisVelocityRatio; + return vel; + } + + /// <summary> /// Set Single Axis Do Jog Move /// </summary> /// <param name="axisNum">AxisNo</param> @@ -426,7 +532,7 @@ if (ret != (short)GTSRetCode.GRCRunOK) { - LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime); + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "JogMove寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime); Thread.Sleep(10); } repeatTime--; @@ -439,29 +545,6 @@ OnExceptionRaised?.Invoke(ex); return false; } - - } - - /// <summary> - /// Set Single Axis Do stop Jog Move - /// </summary> - /// <param name="axisNum">AxisNo</param> - /// <returns></returns> - public bool StopJog(int axisNum) - { - //鍋滄杩愬姩 - MoveStop(axisNum, 0); - //杩愬姩寮�濮嬪悗 妫�鏌ヨ繍鍔ㄦ槸鍚﹀仠姝� - bool isStop = false; - int repeatTime = 1000; - do - { - isStop = IsStop((short)IConfig.CardNum, (short)axisNum); - Thread.Sleep(50); - repeatTime--; - } while (!isStop && repeatTime > 0); - - return isStop; } /// <summary> @@ -529,7 +612,7 @@ repeatTime = 1000; do { - isStop = IsStop((short)IConfig.CardNum, (short)optionPara.AxisIndex); + isStop = IsStop((short)optionPara.AxisIndex); Thread.Sleep(50); repeatTime--; } while (!isStop && repeatTime > 0); @@ -552,6 +635,8 @@ { try { + axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); + if (_isResetting) { LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); @@ -601,7 +686,7 @@ repeatTime = 1000; do { - isStop = IsStop((short)IConfig.CardNum, (short)optionPara.AxisIndex); + isStop = IsStop((short)optionPara.AxisIndex); Thread.Sleep(50); repeatTime--; } while (!isStop && repeatTime > 0); @@ -644,7 +729,7 @@ int repeatTime = 100; do { - isStop = IsStop((short)IConfig.CardNum, (short)axisNum); + isStop = IsStop((short)axisNum); Thread.Sleep(10); repeatTime--; } while (!isStop && repeatTime > 0); @@ -756,16 +841,17 @@ /// </summary> /// <param name="index">杈撳嚭鍙�,杩斿洖1-16</param> /// <param name="value">false琛ㄧず杈撳嚭锛宼rue琛ㄧず鍏抽棴</param> - public void WriteOut(short index, bool value) + public override void WriteOutput(short index, IOValue value) { short outNum = (short)(index % 100 + 1); - if (value) + if ((int)value <= 1) { - GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, 0); + GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)value); } else { - GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, 1); + var currentValue = (int)MonitorValues.FirstOrDefault(u => u.IONum == outNum && u.IOType == IOType.OUTPUT).Value; + GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)(currentValue == 1 ? 0 : 1)); } } @@ -775,23 +861,53 @@ /// <param name="cardNum">鏉垮崱鍙�</param> /// <param name="axisNum">杞村彿</param> /// <returns></returns> - public bool IsStop(short cardNum, short axisNum) + public bool IsStop(short axisNum) + { + int sts = GetAxisStatus(axisNum); + if ((sts & 0x400) == 0) return true;//鍋滄杩斿洖true + else return false; //杩愯涓繑鍥瀎alse + } + + /// <summary> + /// 璇诲彇杞寸姸鎬� + /// </summary> + /// <param name="cardNum">鏉垮崱鍙�</param> + /// <param name="axisNum">杞村彿</param> + /// <returns></returns> + public int GetAxisStatus(int axisNum) { lock (moveLock) { int sts = 0; uint pclock = 0; - GTSCardAPI.GT_GetSts(cardNum, axisNum, out sts, 1, out pclock); - if ((sts & 0x400) == 0) return true;//鍋滄杩斿洖true - else return false; //杩愯涓繑鍥瀎alse + GTSCardAPI.GT_GetSts((short)IConfig.CardNum, (short)axisNum, out sts, 1, out pclock); + return sts; } } - #endregion #region IMonitor - public List<IOItem> MonitorValues { get; set; } = new List<IOItem>(); + public List<AxisMovingStatus> GetAxisMovingStatus() + { + List<AxisMovingStatus> axisMovingStatusesList = new List<AxisMovingStatus>(); + foreach (var axisConfig in IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled)) + { + AxisMovingStatus axisMovingStatus = new AxisMovingStatus(); + axisMovingStatus.AxisIndex = axisConfig.AxisIndex; + axisMovingStatus.AxisName = axisConfig.AxisName; + 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.AxisStatus = GetAxisStatus(axisMovingStatus.AxisIndex); + + axisMovingStatusesList.Add(axisMovingStatus); + + } + + return axisMovingStatusesList; + } public List<IOItem> GetMonitorValues() @@ -810,13 +926,13 @@ IOItem inItem = new IOItem() { IONum = index, - Value = (inValue & (1 << index)) == 0 ? 1 : 0, + Value = (inValue & (1 << index)) == 0 ? IOValue.TRUE : IOValue.FALSE, IOType = IOType.INPUT }; IOItem outItem = new IOItem() { IONum = index, - Value = (outValue & (1 << index)) == 0 ? 1 : 0, + Value = (outValue & (1 << index)) == 0 ? IOValue.TRUE : IOValue.FALSE, IOType = IOType.OUTPUT }; result.Add(inItem); @@ -837,7 +953,8 @@ if (!IConfig.IsEnableMonitor) return; var newValues = GetMonitorValues(); - if (newValues == null || newValues.Count == 0) + var newAxisMovingStatus = GetAxisMovingStatus(); + if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0) continue; Stopwatch sw = new Stopwatch(); @@ -848,6 +965,8 @@ var tempOld = MonitorValues.DeepSerializeClone(); MonitorCheckAndInvoke(tempNew, tempOld); } + + AxisMovingOptionValues = new List<AxisMovingStatus>(newAxisMovingStatus); MonitorValues = new List<IOItem>(newValues); sw.Stop(); @@ -873,7 +992,7 @@ }); } - private void OnMethodInvoked(IAsyncResult ar) + public override void OnMethodInvoked(IAsyncResult ar) { MotionCardMonitorSet monitorSet = ar.AsyncState as MotionCardMonitorSet; ProcessResponse resValues = monitorSet.Response; @@ -905,13 +1024,12 @@ { MotionCardWarningSet warningSet = wSet as MotionCardWarningSet; - bool isOn = ((tempNew.FirstOrDefault(u => u.IONum == warningSet.TriggerIndex && u.IOType == warningSet.WarningIOModel)?.Value >> warningSet.TriggerIndex) & 1) == (warningSet.TriggerValue ? 1 : 0); + bool isOn = (((int)((tempNew.FirstOrDefault(u => u.IONum == warningSet.TriggerIndex && u.IOType == warningSet.WarningIOModel)?.Value)) >> warningSet.TriggerIndex) & 1) == (warningSet.TriggerValue ? 1 : 0); if (warningSet.CurrentStatus != isOn) { warningSet.CurrentStatus = isOn; warningSet.TriggerTime = DateTime.Now; - warningSet.WarningDescription = $"璀︽姤锛歿warningSet.Name}-瑙﹀彂绱㈠紩锛歿warningSet.TriggerIndex}-{warningSet.WarningIOModel.GetEnumDescription()}:{warningSet.WarningCode}"; SaveAlarmCSVAsync(DateTime.Now, this.Name, warningSet); ExcuteMonitorAlarm(DateTime.Now, this, warningSet); } @@ -932,7 +1050,7 @@ if (newIOItem?.Value != oldIOItem?.Value) { - if (monitorSet.TriggerValue == -999 || newIOItem.Value == monitorSet.TriggerValue) + if (monitorSet.TriggerValue == -999 || (int)newIOItem.Value == monitorSet.TriggerValue) { if (monitorSet.OpConfig == null) { @@ -944,7 +1062,7 @@ // return tempNew[index].Value; //}).ToList(); - ExcuteMonitorInvok(DateTime.Now, monitorSet.InvokeDevice, this, monitorSet, OnMethodInvoked); + ExcuteMonitorInvoke(DateTime.Now, monitorSet.InvokeDevice, this, monitorSet); } } }); @@ -954,12 +1072,11 @@ public override void ResetAlarm() { int axis_sts; - uint clk; var axisSettings = IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled); GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)axisSettings.Count); foreach (var axisSetting in axisSettings) { - GTSCardAPI.GT_GetSts((short)IConfig.CardNum, (short)axisSetting.AxisIndex, out axis_sts, 1, out clk); + axis_sts = GetAxisStatus((short)axisSetting.AxisIndex); if ((axis_sts & 0x200) == 0) { var rst = GTSCardAPI.GT_AxisOn((short)IConfig.CardNum, (short)axisSetting.AxisIndex); -- Gitblit v1.8.0