领胜LDS 键盘AOI检测项目
wells.liu
2020-07-16 81480d62561dcb75a1fc968814ace1368fe5181b
src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -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>();
@@ -145,8 +147,13 @@
            {
                foreach (var preCheck in operationSet.PreCheckIOCollection)
                {
                    int timeout = operationSet.PreCheckIOTimeout;
                    _pauseHandle.Wait();
                    IOValue? ioData = null;
                    if (CurrentState == DeviceState.DSOpen)
                    {
                        int timeout = operationSet.PreCheckIOTimeout;
                    while (CurrentState == DeviceState.DSOpen)
                    {
                        Thread.Sleep(10);
@@ -155,6 +162,7 @@
                        if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0))
                        {
                            break;
                            }
                        }
                    }
@@ -170,10 +178,15 @@
            // 2.板卡运动
            if (CurrentState == DeviceState.DSOpen)
            {
                _pauseHandle.Wait();
                if (CurrentState == DeviceState.DSOpen)
                {
                responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps });
                if (!responseMessage.Result)
                {
                    return responseMessage;
                    }
                }
            }
@@ -181,7 +194,12 @@
            // 3.IO输出 不需要超时
            if (CurrentState == DeviceState.DSOpen)
            {
                foreach (var ioOutput in operationSet.IOOutputCollection)
                {
                    _pauseHandle.Wait();
                    if (CurrentState == DeviceState.DSOpen)
                {
                    WriteOutput((short)ioOutput.IOItem.IONum, ioOutput.CheckValue);
@@ -193,6 +211,7 @@
                    //    responseMessage.Message = $"IO输出不通过,配置:{ioOutput.GetDisplayText()},当前值:{ioData}";
                    //    return responseMessage;
                    //}
                    }
                }
            }
@@ -228,6 +247,8 @@
        #endregion
        #region ImmediatePause
        ManualResetEventSlim _pauseHandle = new ManualResetEventSlim(true);
        /// <summary>
        /// 启动立即暂停
        /// </summary>
@@ -236,12 +257,11 @@
            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);//所有轴都暂停
                });
@@ -251,15 +271,22 @@
        /// <summary>
        /// 恢复立即暂停
        /// </summary>
        public override 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
@@ -347,19 +374,11 @@
            ResponseMessage responseMessage = new ResponseMessage();
            if (opConfig is MotionOperationCollection gtsOperationCollection)
            {
                //List<Task<bool>> taskList = new List<Task<bool>>();
                //foreach (var movingOp in gtsOperationCollection.MovingOps)
                //{
                //    var task = SingleAxisMoving(movingOp);
                //    taskList.Add(task);
                //    task.Start();
                //}
                //Task.WaitAll(taskList.ToArray());
                //responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult());
                List<bool> resultList = new List<bool>();
                Parallel.ForEach(gtsOperationCollection.MovingOps, movingOp =>
                {
                    axisImmediatePauseFlag[movingOp.AxisIndex] = false;
                    axisPauseResumeFlag[movingOp.AxisIndex] = true;
                    resultList.Add(SingleAxisMoving(movingOp).Result);
                });
                responseMessage.Result = resultList.All(u => u == true);
@@ -419,16 +438,19 @@
        {
            return Task.Run(() =>
             {
                 axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
                 bool isSuccessAndStop = false;
                do
                {
                    axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
                    if (!axisPauseResumeFlag[optionPara.AxisIndex])
                        return true;
                 try
                 {
                     if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false)
                     {
                         // 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)
                         {
@@ -444,10 +466,10 @@
                                     {
                                         isSuccessAndStop = P2PMoveAbs(optionPara);
                                     }
                                     else
                                     {
                                         isSuccessAndStop = P2PMoveRel(optionPara);
                                     }
                                        //else
                                        //{
                                        //    isSuccessAndStop = P2PMoveRel(optionPara);
                                        //}
                                 }
                                 break;
@@ -463,9 +485,6 @@
                                 }
                                 break;
                         }
                         //_position += $"{GetPosition(optionPara.AxisIndex)},";
                         //_position += $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}";
                         //LogAsync(DateTime.Now, "", _position);
                     }
                 }
                 catch (Exception ex)
@@ -473,6 +492,7 @@
                     isSuccessAndStop = false;
                     LogAsync(DateTime.Now, $"轴{optionPara.AxisIndex}运动异常", ex.GetExceptionMessage());
                 }
                } while (axisImmediatePauseFlag[optionPara.AxisIndex]);
                 return isSuccessAndStop;
             });
        }
@@ -868,6 +888,10 @@
        {
            try
            {
                var goHomePara = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex).GoHomePara;
                bool homeDirection = goHomePara.IsHomeDirPositive;
                bool isRightLimitReached = false;
                StartCapture:
                PositionReset(movingOption.AxisIndex, 1);
@@ -887,24 +911,24 @@
                // 设置点位模式运动参数
                sRtn = GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref trapPrm);
                // 设置点位模式目标速度,即回原点速度
                sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, movingOption.GoHomePara.HighVelocity);
                sRtn = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)movingOption.AxisIndex, goHomePara.HighVelocity);
                // 设置点位模式目标位置,即原点搜索距离
                sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, movingOption.GoHomePara.HomeDir == 1 ? 99999999 : -99999999);
                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 = 1000;
                int repeatTime = goHomePara.GoHomeTimeOut * 1000;
                short capture;
                int pos;
                uint clk;//时钟参数
                do
                {
                    Thread.Sleep(20);
                    Thread.Sleep(IConfig.MonitorInterval);
                    // 读取捕获状态
                    GTSCardAPI.GT_GetCaptureStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out capture, out pos, 1, out clk);
                    isStop = IsStop((short)movingOption.AxisIndex);
                    repeatTime--;
                    repeatTime -= IConfig.MonitorInterval;
                } while (!(isStop || capture == 1 || repeatTime <= 0));
                if (repeatTime <= 0)
@@ -921,7 +945,22 @@
                    if (((axisStatus.AxisStatus & 0x20) != 0) || ((axisStatus.AxisStatus & 0x40) != 0))
                    {
                        movingOption.GoHomePara.HomeDir = (short)(movingOption.GoHomePara.HomeDir == 1 ? -1 : 1);
                        //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;
                        goto StartCapture;
                    }
@@ -930,12 +969,18 @@
                if (capture == 1)
                {
                    if (!isRightLimitReached)
                    {
                        capture = 0;
                        goto StartCapture;
                    }
                    //先stop
                    MoveStop((short)movingOption.AxisIndex, 0);
                    ClearStatus((short)movingOption.AxisIndex, 1);
                    //已经捕获到Home才可以回零 阶段2
                    // 运动到"捕获位置+偏移量"
                    sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, pos + movingOption.GoHomePara.HomeOffset);
                    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;
@@ -1282,10 +1327,16 @@
            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 = AxisStatusList.FirstOrDefault(u => u.AxisIndex == axisSetting.AxisIndex).AxisStatus;
                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);