| | |
| | | public class GTSCardDriver : MotionCardBase |
| | | { |
| | | // 异常事件 |
| | | public Action<Exception> OnExceptionRaised; |
| | | //public Action<Exception> OnExceptionRaised; |
| | | |
| | | public GTSCardInitialConfig IIConfig |
| | | { |
| | |
| | | bool _isResetting = false; |
| | | |
| | | /// <summary> |
| | | /// 是否暂停中 |
| | | /// </summary> |
| | | bool _isPause = false; |
| | | |
| | | /// <summary> |
| | | /// 运动轴立即暂停 |
| | | /// </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>(); |
| | | //Dictionary<int, CancellationTokenSource> axisMoveCancelDict = new Dictionary<int, CancellationTokenSource>(); |
| | | |
| | | public void SetResetFlag(bool isReset) |
| | | { |
| | |
| | | |
| | | public override List<AxisInfo> GetCurrentAxisInfo(params string[] axisName) |
| | | { |
| | | throw new NotImplementedException(); |
| | | List<AxisInfo> axisInfos = new List<AxisInfo>(); |
| | | IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled).ForEach(axisSetting => |
| | | { |
| | | AxisInfo axisInfo = new AxisInfo(); |
| | | axisInfo.AxisName = axisSetting.AxisName; |
| | | var axisMovingStatus = AxisStatusList.FirstOrDefault(u => u.AxisIndex == axisSetting.AxisIndex); |
| | | axisInfo.AxisLocation = axisMovingStatus == null ? 0 : Convert.ToDouble(axisMovingStatus.CurPosition); |
| | | |
| | | axisInfos.Add(axisInfo); |
| | | }); |
| | | return axisInfos; |
| | | } |
| | | |
| | | #region DeviceBase |
| | |
| | | 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()); |
| | | axisImmediatePauseHandleDict = IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled).ToDictionary(a => a.AxisIndex, a => new ManualResetEvent(true)); |
| | | //axisMoveCancelDict = IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled).ToDictionary(a => a.AxisIndex, a => new CancellationTokenSource()); |
| | | |
| | | axisMoveCancelDict.Values.ToList().ForEach(c => |
| | | { |
| | | c = new CancellationTokenSource(); |
| | | }); |
| | | //axisMoveCancelDict.Values.ToList().ForEach(c => |
| | | //{ |
| | | // c = new CancellationTokenSource(); |
| | | //}); |
| | | } |
| | | |
| | | ManualResetEvent _pauseHandle = new ManualResetEvent(true); |
| | | protected override void Pause() |
| | | { |
| | | |
| | |
| | | protected override void Start() |
| | | { |
| | | AllAxisOn(); |
| | | |
| | | base.Start(); |
| | | } |
| | | |
| | | protected override void Stop() |
| | |
| | | ResponseMessage responseMessage = new ResponseMessage(); |
| | | if (config is MotionCardOperationConfigBase motionCardOperationConfig) |
| | | { |
| | | _pauseHandle.WaitOne(); |
| | | 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; |
| | |
| | | { |
| | | 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 是开、关 从MonitorValues 获取 |
| | | 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 是开、关 从MonitorValues 获取 |
| | | timeout -= 10; |
| | | if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0)) |
| | | { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | // 2.板卡运动 |
| | | if (CurrentState == DeviceState.DSOpen) |
| | | { |
| | | responseMessage = MoveToPoint(operationSet.MotionOperationCollection); |
| | | if (!responseMessage.Result) |
| | | _pauseHandle.Wait(); |
| | | |
| | | if (CurrentState == DeviceState.DSOpen) |
| | | { |
| | | return responseMessage; |
| | | responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps }); |
| | | if (!responseMessage.Result) |
| | | { |
| | | return responseMessage; |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | // 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 是开、关 从MonitorValues 获取 |
| | | 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 是开、关 从MonitorValues 获取 |
| | | |
| | | //if (ioOutput.CheckValue != ioData) |
| | | //{ |
| | | // responseMessage.Result = false; |
| | | // responseMessage.Message = $"IO输出不通过,配置:{ioOutput.GetDisplayText()},当前值:{ioData}"; |
| | | // return responseMessage; |
| | | //} |
| | | } |
| | | } |
| | | } |
| | |
| | | #endregion |
| | | |
| | | #region ImmediatePause |
| | | ManualResetEventSlim _pauseHandle = new ManualResetEventSlim(true); |
| | | |
| | | /// <summary> |
| | | /// 启动立即暂停 |
| | | /// </summary> |
| | | public void SetImmediatePause() |
| | | public override void SetImmediatePause() |
| | | { |
| | | if (!_isResetting) |
| | | { |
| | | var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList(); |
| | | |
| | | var immediatePauseAxis = IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled && a.IsImmediatePause).Select(u => u.AxisIndex).ToList(); |
| | | _pauseHandle.Reset(); |
| | | immediatePauseAxis.ForEach(async axisIndex => |
| | | { |
| | | axisImmediatePauseHandleDict[axisIndex].Reset(); |
| | | axisImmediatePauseFlag[axisIndex] = true; |
| | | |
| | | axisMoveCancelDict[axisIndex].Cancel(); |
| | | |
| | | await MoveStop(axisIndex, 0); |
| | | await MoveStop(axisIndex, 0);//所有轴都暂停 |
| | | }); |
| | | } |
| | | } |
| | |
| | | /// <summary> |
| | | /// 恢复立即暂停 |
| | | /// </summary> |
| | | public void ResetImmediatePause() |
| | | public override void ResetImmediatePause(bool isResumeMoving) |
| | | { |
| | | var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList(); |
| | | |
| | | 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 |
| | |
| | | { |
| | | var res = GTSCardAPI.GT_Open((short)IConfig.CardNum, 0, 1); //打开运动控制器。参数必须为(0,1),不能修改。 |
| | | res += GTSCardAPI.GT_LoadConfig((short)IConfig.CardNum, IConfig.InitialConfigFilePath); |
| | | res += GTSCardAPI.GT_ClrSts(0, 1, 8); |
| | | ClearStatus(1, IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled).Count); |
| | | if (res != (short)GTSRetCode.GRCRunOK) |
| | | { |
| | | throw new Exception("板卡载入配置文件异常,错误码:" + res); |
| | | throw new ProcessException("板卡载入配置文件异常,错误码:" + res); |
| | | } |
| | | } |
| | | |
| | | |
| | | public bool AllAxisOn() |
| | | public override bool AllAxisOn() |
| | | { |
| | | List<Task<bool>> taskList = new List<Task<bool>>(); ; |
| | | // 如果是多个轴的运动 等每个轴开启 |
| | | IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum => |
| | | IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled).ForEach(axisNum => |
| | | { |
| | | var task = AxisOnAsync((short)IConfig.CardNum, (short)axisNum.AxisIndex); |
| | | var task = AxisOnAsync(axisNum.AxisIndex); |
| | | taskList.Add(task); |
| | | }); |
| | | Task.WaitAll(taskList.ToArray()); |
| | |
| | | return resultOK; |
| | | } |
| | | |
| | | public bool AllAxisOff() |
| | | public override bool AllAxisOff() |
| | | { |
| | | List<Task<bool>> taskList = new List<Task<bool>>(); ; |
| | | // 如果是多个轴的运动 等每个轴关闭 |
| | | IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum => |
| | | IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled).ForEach(axisNum => |
| | | { |
| | | var task = AxisOffAsync((short)IConfig.CardNum, (short)axisNum.AxisIndex); |
| | | var task = AxisOffAsync(axisNum.AxisIndex); |
| | | taskList.Add(task); |
| | | }); |
| | | Task.WaitAll(taskList.ToArray()); |
| | |
| | | /// 单个轴开启 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public async Task<bool> AxisOnAsync(short cardNum, short axisNum) |
| | | public override async Task<bool> AxisOnAsync(int axisNum) |
| | | { |
| | | return await Task.Run(() => |
| | | { |
| | | var ret = GTSCardAPI.GT_AxisOn(cardNum, axisNum); |
| | | var ret = GTSCardAPI.GT_AxisOn((short)IConfig.CardNum, (short)axisNum); |
| | | return ret == (short)GTSRetCode.GRCRunOK; |
| | | }); |
| | | } |
| | |
| | | /// 单个轴关闭 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public async Task<bool> AxisOffAsync(short cardNum, short axisNum) |
| | | public override async Task<bool> AxisOffAsync(int axisNum) |
| | | { |
| | | return await Task.Run(() => |
| | | { |
| | | var ret = GTSCardAPI.GT_AxisOff(cardNum, axisNum); |
| | | var ret = GTSCardAPI.GT_AxisOff((short)IConfig.CardNum, (short)axisNum); |
| | | return ret == (short)GTSRetCode.GRCRunOK; |
| | | }); |
| | | } |
| | |
| | | ResponseMessage responseMessage = new ResponseMessage(); |
| | | if (opConfig is MotionOperationCollection gtsOperationCollection) |
| | | { |
| | | _pauseHandle.WaitOne(); |
| | | 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 = $"点位运动异常"; |
| | |
| | | { |
| | | List<short> resultCode = new List<short>() { 0 }; |
| | | GTSCardAPI.TTrapPrm trapprm = new GTSCardAPI.TTrapPrm(); |
| | | resultCode.Add(GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)optionPara.AxisIndex)); |
| | | resultCode.Add(GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, (short)optionPara.AxisIndex, out trapprm)); |
| | | short axisIndex = short.Parse(optionPara.AxisIndexStr); |
| | | 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; |
| | | |
| | | resultCode.Add(GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, (short)optionPara.AxisIndex, ref trapprm)); |
| | | resultCode.Add(GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)optionPara.AxisIndex, optionPara.VelocityPara.Velocity * IConfig.AxisVelocityRatio)); |
| | | 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)); |
| | | |
| | | 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 async Task<bool> SingleAxisMoving(MovingOption optionPara) |
| | | public override Task<bool> SingleAxisMoving(MovingOption optionPara) |
| | | { |
| | | return await Task.Run(() => |
| | | 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 |
| | | { |
| | | axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); |
| | | |
| | | 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; |
| | | }); |
| | | } |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | /// 获取规划速度 |
| | | /// </summary> |
| | | /// <param name="axisNum">Axis number</param> |
| | | /// <returns></returns> |
| | | /// <returns>速度脉冲</returns> |
| | | public double GetPrfVelocity(int axisNum) |
| | | { |
| | | double prfVel = 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); |
| | | throw new ProcessException("轴" + axisNum + "获取规划速度异常,错误码:" + ret); |
| | | } |
| | | prfVel = prfVel / IConfig.AxisVelocityRatio; |
| | | return prfVel; |
| | | } |
| | | |
| | |
| | | /// 获取当前速度 |
| | | /// </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; |
| | | } |
| | | |
| | |
| | | catch (Exception ex) |
| | | { |
| | | AllMoveStop(true); |
| | | OnExceptionRaised?.Invoke(ex); |
| | | OnExceptionOccured?.Invoke(DateTime.Now, ex); |
| | | return false; |
| | | } |
| | | } |
| | |
| | | if (_isResetting) |
| | | { |
| | | LogAsync(DateTime.Now, "复位过程异常", "轴" + optionPara.AxisIndex + "试图在复位过程中运动"); |
| | | throw new Exception("轴" + optionPara.AxisIndex + "试图在复位过程中运动"); |
| | | throw new ProcessException("轴" + optionPara.AxisIndex + "试图在复位过程中运动"); |
| | | } |
| | | |
| | | int repeatTime = 30; |
| | |
| | | if (CurrentState != EnumHelper.DeviceState.DSOpen) |
| | | { |
| | | LogAsync(DateTime.Now, "非正常状态异常", "轴" + optionPara.AxisIndex + "试图在非正常状态运动"); |
| | | throw new Exception("轴" + optionPara.AxisIndex + "试图在非正常状态运动", null); |
| | | throw new ProcessException("轴" + optionPara.AxisIndex + "试图在非正常状态运动", null); |
| | | } |
| | | |
| | | LogAsync(DateTime.Now, "轴" + optionPara.AxisIndex + "开始运动", "目标坐标:" + optionPara.Destination); |
| | |
| | | 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) |
| | |
| | | repeatTime = 1000; |
| | | do |
| | | { |
| | | isStop = IsStop((short)optionPara.AxisIndex); |
| | | Thread.Sleep(50); |
| | | isStop = IsStop((short)optionPara.AxisIndex); |
| | | repeatTime--; |
| | | } while (!isStop && repeatTime > 0); |
| | | |
| | |
| | | catch (Exception ex) |
| | | { |
| | | AllMoveStop(true); |
| | | OnExceptionRaised?.Invoke(ex); |
| | | OnExceptionOccured?.Invoke(DateTime.Now, ex); |
| | | return false; |
| | | } |
| | | } |
| | |
| | | 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) |
| | |
| | | if (CurrentState != EnumHelper.DeviceState.DSOpen) |
| | | { |
| | | LogAsync(DateTime.Now, "非正常状态异常", "轴" + optionPara.AxisIndex + "试图在非正常状态运动"); |
| | | throw new Exception("轴" + optionPara.AxisIndex + "试图在非正常状态运动", null); |
| | | throw new ProcessException("轴" + optionPara.AxisIndex + "试图在非正常状态运动", null); |
| | | } |
| | | LogAsync(DateTime.Now, "轴" + optionPara.AxisIndex + "开始运动", "目标坐标:" + optionPara.Destination); |
| | | short ret = 0; |
| | | bool isSuccessSetAxisParam = false; |
| | | int timeout = optionPara.MovingTimeout; |
| | | while (CurrentState == DeviceState.DSOpen) |
| | | //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) |
| | |
| | | |
| | | bool isStop = false; |
| | | repeatTime = 1000; |
| | | do |
| | | while (!isStop && repeatTime > 0) |
| | | { |
| | | isStop = IsStop((short)optionPara.AxisIndex); |
| | | Thread.Sleep(50); |
| | | isStop = IsStop((short)optionPara.AxisIndex); |
| | | repeatTime--; |
| | | } while (!isStop && repeatTime > 0); |
| | | } |
| | | |
| | | return (ret == (short)GTSRetCode.GRCRunOK) && isStop; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | AllMoveStop(true); |
| | | OnExceptionRaised?.Invoke(ex); |
| | | OnExceptionOccured?.Invoke(DateTime.Now, ex); |
| | | return false; |
| | | } |
| | | } |
| | |
| | | /// <param name="axisNum">axisNo</param> |
| | | /// <param name="option">0表示平滑停止,1表示紧急停止</param> |
| | | /// <returns></returns> |
| | | public async Task<bool> MoveStop(int axisNum, int option) |
| | | public override async Task<bool> MoveStop(int axisNum, int option) |
| | | { |
| | | return await Task.Run(() => |
| | | { |
| | |
| | | if (ret != (short)GTSRetCode.GRCRunOK) |
| | | { |
| | | LogAsync(DateTime.Now, "轴" + axisNum + "运动停止异常", "错误码:" + ret); |
| | | throw new Exception("轴" + axisNum + "运动停止异常,错误码:" + ret); |
| | | throw new ProcessException("轴" + axisNum + "运动停止异常,错误码:" + ret); |
| | | } |
| | | else |
| | | { |
| | |
| | | int repeatTime = 100; |
| | | do |
| | | { |
| | | isStop = IsStop((short)axisNum); |
| | | Thread.Sleep(10); |
| | | isStop = IsStop((short)axisNum); |
| | | repeatTime--; |
| | | } while (!isStop && repeatTime > 0); |
| | | |
| | |
| | | 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 |
| | | { |
| | | GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)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); |
| | | GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)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); |
| | | |
| | | //正限位 |
| | | 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; |
| | | } |
| | | |
| | | //先stop |
| | | MoveStop((short)movingOption.AxisIndex, 0); |
| | | ClearStatus((short)movingOption.AxisIndex, 1); |
| | | |
| | | //已经捕获到Home才可以回零 阶段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; |
| | | } |
| | | } |
| | |
| | | /// 按位设置数字 IO 输出状态 |
| | | /// </summary> |
| | | /// <param name="index">输出口,返回1-16</param> |
| | | /// <param name="value">false表示输出,true表示关闭</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 |
| | | { |
| | |
| | | /// <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; //运行中返回false |
| | | } |
| | |
| | | /// <summary> |
| | | /// 读取轴状态 |
| | | /// </summary> |
| | | /// <param name="cardNum">板卡号</param> |
| | | /// <param name="axisNum">轴号</param> |
| | | /// <returns></returns> |
| | | public int GetAxisStatus(int axisNum) |
| | | public override int GetAxisStatus(int axisNum) |
| | | { |
| | | lock (moveLock) |
| | | { |
| | |
| | | return sts; |
| | | } |
| | | } |
| | | |
| | | public override bool ClearStatus(int startAxisIndex, int count) |
| | | { |
| | | var rtn = GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, (short)startAxisIndex, (short)count); |
| | | return rtn == (short)GTSRetCode.GRCRunOK; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 位置回零 |
| | | /// </summary> |
| | | /// <param name="startAxisIndex"></param> |
| | | /// <param name="count"></param> |
| | | /// <returns></returns> |
| | | public override bool PositionReset(int startAxisIndex, int count) |
| | | { |
| | | lock (moveLock) |
| | | { |
| | | var rtn = GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)startAxisIndex, (short)count); |
| | | return rtn == (short)GTSRetCode.GRCRunOK; |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region IMonitor |
| | |
| | | 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); |
| | |
| | | GTSCardAPI.GT_GetDo((short)IConfig.CardNum, GTSCardAPI.MC_GPO, out outValue); |
| | | |
| | | //解析结果 |
| | | for (var index = 1; index <= 16; index++) |
| | | for (var index = 0; index < 16; index++) |
| | | { |
| | | IOItem inItem = new IOItem() |
| | | { |
| | |
| | | { |
| | | 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) |
| | | { |
| | |
| | | }); |
| | | } |
| | | |
| | | 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(); |
| | | // 将指定IOItem写入板卡 |
| | | 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(); |
| | | //// 将指定IOItem写入板卡 |
| | | //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) |
| | |
| | | |
| | | //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); |
| | | } |
| | |
| | | { |
| | | int axis_sts; |
| | | var axisSettings = IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled); |
| | | GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)axisSettings.Count); |
| | | 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); |
| | | } |
| | | // 位置请零 |
| | | GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, 1, (short)axisSettings.Count); |
| | | //// 所有位置请零 |
| | | //PositionReset(1, axisSettings.Count); |
| | | // 正极限报警 |
| | | if ((axis_sts & 0x20) != 0) |
| | | { |
| | |
| | | } |
| | | |
| | | // 清除状态 |
| | | GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled).Count); |
| | | ClearStatus(1, axisSettings.Count); |
| | | } |
| | | |
| | | object _alarmLock = new object(); |