领胜LDS 键盘AOI检测项目
xcd
2020-07-11 56d73b9710766e7acab2e084a6af322175c78f1b
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
        {
@@ -61,7 +61,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);
@@ -162,7 +162,7 @@
            // 2.板卡运动
            if (CurrentState == DeviceState.DSOpen)
            {
                responseMessage = MoveToPoint(operationSet.MotionOperationCollection);
                responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps });
                if (!responseMessage.Result)
                {
                    return responseMessage;
@@ -220,7 +220,7 @@
        /// <summary>
        /// 启动立即暂停
        /// </summary>
        public void SetImmediatePause()
        public override void SetImmediatePause()
        {
            if (!_isResetting)
            {
@@ -240,7 +240,7 @@
        /// <summary>
        /// 恢复立即暂停
        /// </summary>
        public void ResetImmediatePause()
        public override void ResetImmediatePause()
        {
            var immediatePauseAxis = IConfig.AxisSettings.FindAll(a => a.IsAxisEnabled && a.IsImmediatePause).Select(u => u.AxisIndex).ToList();
@@ -362,14 +362,15 @@
        {
            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));
            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));
            var resultOK = resultCode.All(u => u == (short)GTSRetCode.GRCRunOK);
            if (!resultOK)
@@ -386,7 +387,7 @@
        public override async Task<bool> SingleAxisMoving(MovingOption optionPara)
        {
            return await Task.Run(() =>
            {
            {
                axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
                bool isSuccessAndStop = false;
                try
@@ -458,7 +459,8 @@
            {
                throw new Exception("轴" + 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 / AxisRatio;
            return position;
        }
@@ -478,7 +480,8 @@
                {
                    throw new Exception("轴" + 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 / AxisRatio;
                return position;
            }
        }
@@ -487,7 +490,7 @@
        /// 获取规划速度
        /// </summary>
        /// <param name="axisNum">Axis number</param>
        /// <returns></returns>
        /// <returns>速度脉冲</returns>
        public double GetPrfVelocity(int axisNum)
        {
            double prfVel = 0;
@@ -497,7 +500,6 @@
            {
                throw new Exception("轴" + axisNum + "获取规划速度异常,错误码:" + ret);
            }
            prfVel = prfVel / IConfig.AxisVelocityRatio;
            return prfVel;
        }
@@ -505,7 +507,7 @@
        /// 获取当前速度
        /// </summary>
        /// <param name="axisNum">Axis number</param>
        /// <returns></returns>
        /// <returns>速度脉冲</returns>
        public double GetVelocity(int axisNum)
        {
            double vel = 0;
@@ -514,7 +516,6 @@
            {
                throw new Exception("轴" + axisNum + "获取当前速度异常,错误码:" + ret);
            }
            vel = vel / IConfig.AxisVelocityRatio;
            return vel;
        }
@@ -553,7 +554,7 @@
            catch (Exception ex)
            {
                AllMoveStop(true);
                OnExceptionRaised?.Invoke(ex);
                OnExceptionOccured?.Invoke(DateTime.Now, ex);
                return false;
            }
        }
@@ -599,11 +600,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_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(dPosition * AxisRatio));// 设置规划位置
                    ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//更新运动
                    if (ret != (short)GTSRetCode.GRCRunOK)
@@ -633,7 +635,7 @@
            catch (Exception ex)
            {
                AllMoveStop(true);
                OnExceptionRaised?.Invoke(ex);
                OnExceptionOccured?.Invoke(DateTime.Now, ex);
                return false;
            }
        }
@@ -674,11 +676,12 @@
                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_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(optionPara.Destination * AxisRatio));// 设置规划位置
                    ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//更新运动
                    if (ret != (short)GTSRetCode.GRCRunOK)
@@ -707,7 +710,7 @@
            catch (Exception ex)
            {
                AllMoveStop(true);
                OnExceptionRaised?.Invoke(ex);
                OnExceptionOccured?.Invoke(DateTime.Now, ex);
                return false;
            }
        }
@@ -814,7 +817,7 @@
            catch (Exception ex)
            {
                AllMoveStop(true);
                OnExceptionRaised?.Invoke(ex);
                OnExceptionOccured?.Invoke(DateTime.Now, ex);
                return false;
            }
        }
@@ -854,7 +857,7 @@
        /// <param name="value">false表示输出,true表示关闭</param>
        public override void WriteOutput(short index, IOValue value)
        {
            short outNum = (short)(index % 100 + 1);
            short outNum = (short)(index % 100);
            if ((int)value <= 1)
            {
                GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)value);
@@ -874,7 +877,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;              //运行中返回false
        }
@@ -984,8 +987,10 @@
                    {
                        if (!IConfig.IsEnableMonitor)
                            return;
                        var newValues = GetMonitorValues();
                        var newAxisMovingStatus = GetAxisMovingStatus();
                        if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0)
                            continue;
@@ -993,12 +998,21 @@
                        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);
                        if (AxisStatusList.Count == newAxisMovingStatus.Count)
                        {
                            var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone
                            var tempOld = new List<AxisMovingStatus>(AxisStatusList);
                            AxisStatusCheck(tempNew, tempOld);
                        }
                        AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus);
                        MonitorValues = new List<IOItem>(newValues);
                        sw.Stop();
@@ -1024,29 +1038,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();
            // 将指定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)
@@ -1089,10 +1126,10 @@
                            monitorSet.OpConfig = new OperationConfigBase();
                        }
                        //monitorSet.OpConfig.InputPara = monitorSet.InputDataIndex.ConvertAll(index =>
                        //{
                        //    return tempNew[index].Value;
                        //}).ToList();
                        monitorSet.OpConfig.InputPara = monitorSet.InputDataIndex.ConvertAll(index =>
                        {
                            return tempNew[index].Value == IOValue.TRUE ? 1 : 0;
                        }).ToList();
                        ExcuteMonitorInvoke(DateTime.Now, monitorSet.InvokeDevice, this, monitorSet);
                    }
@@ -1108,7 +1145,8 @@
            ClearStatus(1, axisSettings.Count);
            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;
                if ((axis_sts & 0x200) == 0)
                {
                    var rst = GTSCardAPI.GT_AxisOn((short)IConfig.CardNum, (short)axisSetting.AxisIndex);