From 5918194fccdb2a2303e713b8d2f3335243b9e2ef Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期二, 07 七月 2020 14:53:13 +0800
Subject: [PATCH] 板卡驱动

---
 src/Bro.Device.GTSCard/GTSCardDriver.cs         |   98 ++++++++++++++++++++++++++++++++++++++++---------
 src/Bro.Common.Model/Model/MotionCardRelated.cs |    8 ++--
 2 files changed, 84 insertions(+), 22 deletions(-)

diff --git a/src/Bro.Common.Model/Model/MotionCardRelated.cs b/src/Bro.Common.Model/Model/MotionCardRelated.cs
index c97e847..6342b0a 100644
--- a/src/Bro.Common.Model/Model/MotionCardRelated.cs
+++ b/src/Bro.Common.Model/Model/MotionCardRelated.cs
@@ -84,10 +84,10 @@
         //[Description("IsUseWarning锛氭槸鍚﹀惎鐢ㄦ姤璀�")]
         //public bool IsUseWarning { get; set; } = false;
 
-        //[Category("鏆傚仠閰嶇疆")]
-        //[DisplayName("鏄惁鍚敤绔嬪嵆鏆傚仠")]
-        //[Description("IsImmediatePause锛氭槸鍚﹀惎鐢ㄧ珛鍗虫殏鍋�")]
-        //public bool IsImmediatePause { get; set; } = false;
+        [Category("鏆傚仠閰嶇疆")]
+        [DisplayName("鏄惁鍚敤绔嬪嵆鏆傚仠")]
+        [Description("IsImmediatePause锛氭槸鍚﹀惎鐢ㄧ珛鍗虫殏鍋�")]
+        public bool IsImmediatePause { get; set; } = false;
 
         //[Category("鏆傚仠閰嶇疆")]
         //[DisplayName("杞翠綅缃浜庡尯闂村唴鏃堕�傜敤鐨勬殏鍋滄搷浣�")]
diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index c2a4783..c2cc8e6 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -37,6 +37,13 @@
         /// </summary>
         bool _isResetting = false;
 
+        /// <summary>
+        /// 杩愬姩杞寸珛鍗虫殏鍋�
+        /// </summary>
+        Dictionary<int, ManualResetEvent> axisImmediatePauseHandleDict = new Dictionary<int, ManualResetEvent>();
+
+        Dictionary<int, CancellationTokenSource> axisMoveCancelDict = new Dictionary<int, CancellationTokenSource>();
+
         public void SetResetFlag(bool isReset)
         {
             _isResetting = isReset;
@@ -52,16 +59,24 @@
         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());
+
+            axisMoveCancelDict.Values.ToList().ForEach(c =>
+            {
+                c = new CancellationTokenSource();
+            });
         }
 
+        ManualResetEvent _pauseHandle = new ManualResetEvent(true);
         protected override void Pause()
         {
-            throw new NotImplementedException();
+
         }
 
         protected override void Resume()
         {
-            throw new NotImplementedException();
+
         }
 
         protected override void Start()
@@ -83,13 +98,16 @@
         public override ResponseMessage Run(IOperationConfig config)
         {
             ResponseMessage responseMessage = new ResponseMessage();
-            var motionCardOperationConfig = config as MotionCardOperationConfigBase;
-            foreach (var operationSet in motionCardOperationConfig.OperationCollection)
+            if (config is MotionCardOperationConfigBase motionCardOperationConfig)
             {
-                responseMessage = RunOperationSet(operationSet);
-                if (!responseMessage.Result)
+                _pauseHandle.WaitOne();
+                foreach (var operationSet in motionCardOperationConfig.OperationCollection)
                 {
-                    return responseMessage;
+                    responseMessage = RunOperationSet(operationSet);
+                    if (!responseMessage.Result)
+                    {
+                        return responseMessage;
+                    }
                 }
             }
             return responseMessage;
@@ -185,6 +203,43 @@
         }
         #endregion
 
+        #region ImmediatePause
+        /// <summary>
+        /// 鍚姩绔嬪嵆鏆傚仠
+        /// </summary>
+        public void SetImmediatePause()
+        {
+            if (!_isResetting)
+            {
+                var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList();
+
+                immediatePauseAxis.ForEach(async axisIndex =>
+                {
+                    axisImmediatePauseHandleDict[axisIndex].Reset();
+
+                    axisMoveCancelDict[axisIndex].Cancel();
+
+                    await MoveStop(axisIndex, 0);
+                });
+            }
+        }
+
+        /// <summary>
+        /// 鎭㈠绔嬪嵆鏆傚仠
+        /// </summary>
+        public void ResetImmediatePause()
+        {
+            var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList();
+
+            immediatePauseAxis.ForEach(axisIndex =>
+            {
+                axisMoveCancelDict[axisIndex] = new CancellationTokenSource();
+
+                axisImmediatePauseHandleDict[axisIndex].Set();
+            });
+        }
+        #endregion
+
         #region GTSCard
 
         /// <summary>
@@ -266,19 +321,22 @@
         public override ResponseMessage MoveToPoint(IOperationConfig opConfig)
         {
             ResponseMessage responseMessage = new ResponseMessage();
-            var gtsOperationCollection = opConfig as MotionOperationCollection;
-            List<Task<bool>> taskList = new List<Task<bool>>();
-            foreach (var movingOp in gtsOperationCollection.MovingOps)
+            if (opConfig is MotionOperationCollection gtsOperationCollection)
             {
-                var task = SingleAxisMoving(movingOp);
-                taskList.Add(task);
-            }
+                _pauseHandle.WaitOne();
+                List<Task<bool>> taskList = new List<Task<bool>>();
+                foreach (var movingOp in gtsOperationCollection.MovingOps)
+                {
+                    var task = SingleAxisMoving(movingOp);
+                    taskList.Add(task);
+                }
 
-            Task.WaitAll(taskList.ToArray());
-            responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult());
-            if (!responseMessage.Result)
-            {
-                responseMessage.Message = $"鐐逛綅杩愬姩寮傚父";
+                Task.WaitAll(taskList.ToArray());
+                responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult());
+                if (!responseMessage.Result)
+                {
+                    responseMessage.Message = $"鐐逛綅杩愬姩寮傚父";
+                }
             }
             return responseMessage;
         }
@@ -322,6 +380,8 @@
                 {
                     if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false)
                     {
+                        axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
+
                         string _position = "";
                         string motionType = optionPara.MoveMode == EnumHelper.MotionMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString();
 
@@ -575,6 +635,8 @@
         {
             try
             {
+                axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne();
+
                 if (_isResetting)
                 {
                     LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩");

--
Gitblit v1.8.0