领胜LDS 键盘AOI检测项目
xcd
2020-09-30 8287e5b91dff39a8c88f37004040a25ac5f3f488
src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -99,6 +99,9 @@
        {
            AllAxisOn();
            MonitorPosition();
            MonitorAxisStatus();
            base.Start();
        }
@@ -894,11 +897,9 @@
                short capture = 0;
                GTSCardAPI.TTrapPrm trapPrm;
                //搜索距离 阶段1
                // 启动Home捕获
                short sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
                // 切换到点位运动模式
                sRtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex);
                short sRtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex);
                // 读取点位模式运动参数
                sRtn = GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out trapPrm);
                trapPrm.acc = movingOption.VelocityPara.Acc;
@@ -906,12 +907,19 @@
                // 设置点位模式运动参数
                sRtn = GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref trapPrm);
                // 设置点位模式目标速度,即回原点速度
                sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, goHomePara.HighVelocity);
                sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, goHomePara.HomeVelocity);
                do
                {
                    PositionReset(movingOption.AxisIndex, 1);
                    ClearStatus(movingOption.AxisIndex, 1);
                    if (goHomePara.IsCaptureMode)
                    {
                        //搜索距离 阶段1
                        // 启动Home捕获
                        sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
                    }
                    // 设置点位模式目标位置,即原点搜索距离
                    sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, homeDirection ? 99999999 : -99999999);
@@ -921,15 +929,32 @@
                    int repeatTime = goHomePara.GoHomeTimeOut * 1000;
                    bool isStop = false;
                    int pos;
                    int pos = 0;
                    uint clk;//时钟参数
                    int checkInterval = IConfig.MonitorInterval;
                    if (IConfig.MonitorInterval <= 0)
                    {
                        checkInterval = 10;
                    }
                    do
                    {
                        Thread.Sleep(IConfig.MonitorInterval * 5);
                        // 读取捕获状态
                        GTSCardAPI.GT_GetCaptureStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out capture, out pos, 1, out clk);
                        Thread.Sleep(checkInterval * 2);
                        if (goHomePara.IsCaptureMode)
                        {
                            // 读取捕获状态
                            GTSCardAPI.GT_GetCaptureStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out capture, out pos, 1, out clk);
                        }
                        else
                        {
                            GTSCardAPI.GT_GetDi((short)IConfig.CardNum, GTSCardAPI.MC_HOME, out int pValue);
                            capture = (short)((pValue & (1 << (movingOption.AxisIndex - 1))) == 0 ? 1 : 0);
                        }
                        isStop = IsStop((short)movingOption.AxisIndex);
                        repeatTime -= IConfig.MonitorInterval * 5;
                        repeatTime -= checkInterval * 2;
                    } while (!(isStop || capture == 1 || repeatTime <= 0));
                    if (repeatTime <= 0)
@@ -942,8 +967,6 @@
                    if (isStop && capture != 1)
                    {
                        ClearStatus((short)movingOption.AxisIndex, 1);
                        if (((axisStatus.AxisStatus & 0x20) != 0) || ((axisStatus.AxisStatus & 0x40) != 0))
                        {
                            //movingOption.GoHomePara.HomeDir = (short)(movingOption.GoHomePara.HomeDir == 1 ? -1 : 1);
@@ -954,7 +977,6 @@
                                isRightLimitReached = true;
                            }
                            //负限位
                            if ((axisStatus.AxisStatus & 0x40) != 0 && goHomePara.IsCaptureDirPositive)
                            {
@@ -963,6 +985,8 @@
                            homeDirection = !homeDirection;
                        }
                        ClearStatus(movingOption.AxisIndex, 1);
                    }
                    if (capture == 1)
@@ -970,6 +994,7 @@
                        if (!isRightLimitReached)
                        {
                            capture = 0;
                            ClearStatus(movingOption.AxisIndex, 1);
                            //GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
                            continue;
                        }
@@ -977,6 +1002,7 @@
                        //先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);
@@ -1128,7 +1154,6 @@
            return axisMovingStatusesList;
        }
        public List<IOItem> GetMonitorValues()
        {
            var result = new List<IOItem>();
@@ -1161,6 +1186,79 @@
            return result;
        }
        SpinWait _positionWait = new SpinWait();
        private async void MonitorPosition()
        {
            await Task.Run(() =>
            {
                while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit)
                {
                    try
                    {
                        if (!IConfig.IsEnableMonitor)
                            return;
                        AxisStatusList.ForEach(a =>
                        {
                            int curPosition = (int)GetPrfPosition(a.AxisIndex);
                            if (a.CurPosition != curPosition)
                            {
                                a.CurPosition = curPosition;
                                AxisPositionChanged(a.AxisIndex, curPosition);
                            }
                        });
                        _positionWait.SpinOnce();
                    }
                    catch (Exception ex)
                    {
                        if (CurrentState == DeviceState.DSOpen)
                        {
                            LogAsync(DateTime.Now, $"{this.Name}监听轴信息异常", ex.GetExceptionMessage());
                        }
                    }
                }
            });
        }
        SpinWait _statusWait = new SpinWait();
        private async void MonitorAxisStatus()
        {
            await Task.Run(() =>
            {
                while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit)
                {
                    try
                    {
                        if (!IConfig.IsEnableMonitor)
                            return;
                        AxisStatusList.ForEach(a =>
                        {
                            int curStatus = GetAxisStatus(a.AxisIndex);
                            if (a.AxisStatus != curStatus)
                            {
                                int temp = a.AxisStatus;
                                a.AxisStatus = curStatus;
                                AxisStatusChanged(a.AxisIndex, temp, curStatus);
                            }
                        });
                        _statusWait.SpinOnce();
                    }
                    catch (Exception ex)
                    {
                        if (CurrentState == DeviceState.DSOpen)
                        {
                            LogAsync(DateTime.Now, $"{this.Name}监听轴信息异常", ex.GetExceptionMessage());
                        }
                    }
                }
            });
        }
        public async override void Monitor()
        {
            await Task.Run(() =>
@@ -1173,9 +1271,9 @@
                            return;
                        var newValues = GetMonitorValues();
                        var newAxisMovingStatus = GetAxisMovingStatus();
                        //var newAxisMovingStatus = GetAxisMovingStatus();
                        if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0)
                        if (newValues == null || newValues.Count == 0)
                            continue;
                        //Stopwatch sw = new Stopwatch();
@@ -1189,14 +1287,14 @@
                            MonitorCheckAndInvoke(tempNew, tempOld);
                        }
                        if (AxisStatusList.Count == newAxisMovingStatus.Count)
                        {
                            var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone
                            var tempOld = new List<AxisMovingStatus>(AxisStatusList);
                            AxisStatusCheck(tempNew, tempOld);
                        }
                        //if (AxisStatusList.Count == newAxisMovingStatus.Count)
                        //{
                        //    var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone
                        //    var tempOld = new List<AxisMovingStatus>(AxisStatusList);
                        //    AxisStatusCheck(tempNew, tempOld);
                        //}
                        AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus);
                        //AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus);
                        MonitorValues = new List<IOItem>(newValues);
                        //sw.Stop();