From 81480d62561dcb75a1fc968814ace1368fe5181b Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期四, 16 七月 2020 11:57:08 +0800
Subject: [PATCH] Merge branch 'master' of http://gitblit.broconcentric.com:8088/r/M071

---
 src/Bro.Device.GTSCard/GTSCardDriver.cs |  245 +++++++++++++++++++++++++++++-------------------
 1 files changed, 148 insertions(+), 97 deletions(-)

diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index a917333..d87e661 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/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,16 +147,22 @@
             {
                 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 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
-                        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 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
+                            timeout -= 10;
+                            if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0))
+                            {
+                                break;
+                            }
                         }
                     }
 
@@ -170,10 +178,15 @@
             // 2.鏉垮崱杩愬姩
             if (CurrentState == DeviceState.DSOpen)
             {
-                responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps });
-                if (!responseMessage.Result)
+                _pauseHandle.Wait();
+
+                if (CurrentState == DeviceState.DSOpen)
                 {
-                    return responseMessage;
+                    responseMessage = MoveToPoint(new MotionOperationCollection() { MovingOps = operationSet.MovingOps });
+                    if (!responseMessage.Result)
+                    {
+                        return responseMessage;
+                    }
                 }
             }
 
@@ -181,18 +194,24 @@
             // 3.IO杈撳嚭 涓嶉渶瑕佽秴鏃�
             if (CurrentState == DeviceState.DSOpen)
             {
+
                 foreach (var ioOutput in operationSet.IOOutputCollection)
                 {
-                    WriteOutput((short)ioOutput.IOItem.IONum, ioOutput.CheckValue);
+                    _pauseHandle.Wait();
 
-                    //var ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioOutput.IOItem.IONum && u.IOType == ioOutput.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
+                    if (CurrentState == DeviceState.DSOpen)
+                    {
+                        WriteOutput((short)ioOutput.IOItem.IONum, ioOutput.CheckValue);
 
-                    //if (ioOutput.CheckValue != ioData)
-                    //{
-                    //    responseMessage.Result = false;
-                    //    responseMessage.Message = $"IO杈撳嚭涓嶉�氳繃锛岄厤缃細{ioOutput.GetDisplayText()}锛屽綋鍓嶅�硷細{ioData}";
-                    //    return responseMessage;
-                    //}
+                        //var ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioOutput.IOItem.IONum && u.IOType == ioOutput.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
+
+                        //if (ioOutput.CheckValue != ioData)
+                        //{
+                        //    responseMessage.Result = false;
+                        //    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);
@@ -418,63 +437,64 @@
         public override Task<bool> SingleAxisMoving(MovingOption optionPara)
         {
             return Task.Run(() =>
-             {
-                 axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
-                 bool isSuccessAndStop = false;
-                 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();
+            {
+                bool isSuccessAndStop = false;
+                do
+                {
+                    axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
 
-                         // _position = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff},{optionPara.AxisIndex},{motionType},{GetPosition(optionPara.AxisIndex)},{GetPrfPosition(optionPara.AxisIndex)},{optionPara.Destination},";
+                    if (!axisPauseResumeFlag[optionPara.AxisIndex])
+                        return true;
 
-                         switch (optionPara.MoveMode)
-                         {
-                             case MotionMode.Normal:
-                                 {
-                                     if (_isResetting)
-                                     {
-                                         LogAsync(DateTime.Now, "澶嶄綅涓惎鍔ㄨ繍鍔ㄥ紓甯�", optionPara.AxisIndex + "鍚姩杩愬姩寮傚父");
-                                         return false;
-                                     }
+                    try
+                    {
+                        if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false)
+                        {
+                            string motionType = optionPara.MoveMode == EnumHelper.MotionMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString();
 
-                                     if (optionPara.IsAbsolute)
-                                     {
-                                         isSuccessAndStop = P2PMoveAbs(optionPara);
-                                     }
-                                     else
-                                     {
-                                         isSuccessAndStop = P2PMoveRel(optionPara);
-                                     }
+                            switch (optionPara.MoveMode)
+                            {
+                                case MotionMode.Normal:
+                                    {
+                                        if (_isResetting)
+                                        {
+                                            LogAsync(DateTime.Now, "澶嶄綅涓惎鍔ㄨ繍鍔ㄥ紓甯�", optionPara.AxisIndex + "鍚姩杩愬姩寮傚父");
+                                            return false;
+                                        }
 
-                                 }
-                                 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());
-                 }
-                 return isSuccessAndStop;
-             });
+                                        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;
+                            }
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        isSuccessAndStop = false;
+                        LogAsync(DateTime.Now, $"杞磠optionPara.AxisIndex}杩愬姩寮傚父", ex.GetExceptionMessage());
+                    }
+                } while (axisImmediatePauseFlag[optionPara.AxisIndex]);
+                return isSuccessAndStop;
+            });
         }
 
         /// <summary>
@@ -868,7 +888,11 @@
         {
             try
             {
-                StartCapture:
+                var goHomePara = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex).GoHomePara;
+                bool homeDirection = goHomePara.IsHomeDirPositive;
+                bool isRightLimitReached = false;
+
+            StartCapture:
 
                 PositionReset(movingOption.AxisIndex, 1);
                 ClearStatus(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);
+
+                        //姝i檺浣�
+                        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;
+                    }
+
                     //鍏坰top
                     MoveStop((short)movingOption.AxisIndex, 0);
                     ClearStatus((short)movingOption.AxisIndex, 1);
                     //宸茬粡鎹曡幏鍒癏ome鎵嶅彲浠ュ洖闆� 闃舵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);

--
Gitblit v1.8.0