From 328d4212fdacd1d6b3a907d680f7b26fb06cf888 Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期二, 30 六月 2020 16:23:11 +0800
Subject: [PATCH] Merge branch 'master' of http://gitblit.broconcentric.com:8088/r/M071

---
 src/Bro.Device.GTSCard/GTSCardDriver.cs |  732 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 626 insertions(+), 106 deletions(-)

diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index a51429e..e96d00c 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -1,27 +1,59 @@
 锘縰sing Bro.Common.Base;
+using Bro.Common.Helper;
 using Bro.Common.Interface;
 using Bro.Common.Model;
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Drawing;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
+using static Bro.Common.Helper.EnumHelper;
 
 namespace Bro.Device.GTSCard
 {
-    public class GTSCardDriver : DeviceBase,IMonitor, IMotion
+    [Device("GTSCard", "鍥洪珮鏉垮崱", EnumHelper.DeviceAttributeType.Device)]
+    public class GTSCardDriver : DeviceBase, IMonitor, IMotion
     {
         public event Action<DateTime, string, IDevice, MonitorSet> OnMonitorInvoke;
         public event Action<DateTime, IDevice, WarningSet> OnMonitorAlarm;
+
+        public delegate bool OnAxisStartToCheckDelegate(int axisIndex, int startPosition, int endPosition);
+        // 寮傚父浜嬩欢
+        public Action<Exception> OnExceptionRaised;
+
+        public GTSCardInitialConfig IConfig
+        {
+            get
+            {
+                return InitialConfig as GTSCardInitialConfig;
+            }
+        }
+
+        static object moveLock = new object();
+        /// <summary>
+        /// 鏄惁澶嶄綅鏍囧織
+        /// </summary>
+        bool _isResetting = false;
+
+        public void SetResetFlag(bool isReset)
+        {
+            _isResetting = isReset;
+        }
 
         public List<AxisInfo> GetCurrentAxisInfo(params string[] axisName)
         {
             throw new NotImplementedException();
         }
 
+        #region DeviceBase
+
         protected override void Init()
         {
-            throw new NotImplementedException();
+            InitialMotionCard();
         }
 
         protected override void Pause()
@@ -36,50 +68,552 @@
 
         protected override void Start()
         {
-            throw new NotImplementedException();
+            AllAxisOn();
         }
 
         protected override void Stop()
         {
-            throw new NotImplementedException();
+            AllMoveStop();
+            AllAxisOff();
+        }
+        #endregion
+
+        #region GTSCard
+
+        /// <summary>
+        /// Load Motion Card parameter from file
+        /// </summary>
+        /// <param name="fileName">Invalid Parameter</param>
+        /// <returns></returns>
+        public void InitialMotionCard()
+        {
+            var res = GTSCardAPI.GT_Open((short)IConfig.CardNum, 0, 1); //鎵撳紑杩愬姩鎺у埗鍣ㄣ�傚弬鏁板繀椤讳负锛�0,1锛夛紝涓嶈兘淇敼銆�     
+            res += GTSCardAPI.GT_LoadConfig((short)IConfig.CardNum, IConfig.InitialConfigFilePath);
+            res += GTSCardAPI.GT_ClrSts(0, 1, 8);
+            if (res != (short)GTSRetCode.GRCRunOK)
+            {
+                throw new Exception("鏉垮崱杞藉叆閰嶇疆鏂囦欢寮傚父锛岄敊璇爜锛�" + res);
+            }
+        }
+
+        public bool AllAxisOn()
+        {
+            List<Task<bool>> taskList = new List<Task<bool>>(); ;
+            // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱寮�鍚�
+            IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum =>
+            {
+                var task = AxisOnAsync((short)IConfig.CardNum, (short)axisNum.AxisIndex);
+                taskList.Add(task);
+            });
+            Task.WaitAll(taskList.ToArray());
+            var resultOK = taskList.All(u => u.GetAwaiter().GetResult());
+            return resultOK;
+        }
+
+        public bool AllAxisOff()
+        {
+            List<Task<bool>> taskList = new List<Task<bool>>(); ;
+            // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱鍏抽棴
+            IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum =>
+            {
+                var task = AxisOffAsync((short)IConfig.CardNum, (short)axisNum.AxisIndex);
+                taskList.Add(task);
+            });
+            Task.WaitAll(taskList.ToArray());
+            var resultOK = taskList.All(u => u.GetAwaiter().GetResult());
+            return resultOK;
         }
 
         /// <summary>
-        /// 鐐逛綅鍒扮偣浣� 杩愬姩
+        /// 鍗曚釜杞村紑鍚�
         /// </summary>
-        /// <param name="cardNum">鍗″彿</param>
-        /// <param name="axisNum">杞村彿</param>
-        /// <param name="prfPosition">瑙勫垝浣嶇疆,鍗曚綅姣背</param>
-        /// <param name="prfVelocity">瑙勫垝閫熷害,鍗曚綅绫虫瘡绉�</param>
-        public void P2P(short cardNum, short axisNum, int prfPosition, int prfVelocity)
+        /// <returns></returns>
+        public async Task<bool> AxisOnAsync(short cardNum, short axisNum)
         {
-            GTSCardAPI.TTrapPrm trapprm;
-            GTSCardAPI.GT_PrfTrap(cardNum, axisNum);
-            GTSCardAPI.GT_GetTrapPrm(cardNum, axisNum, out trapprm);
-            trapprm.acc = GTSCardParameter.P2PAcc;
-            trapprm.dec = GTSCardParameter.P2PDec;
+            return await Task.Run(() =>
+            {
+                var ret = GTSCardAPI.GT_AxisOn(cardNum, axisNum);
+                return ret == (short)GTSRetCode.GRCRunOK;
+            });
+        }
+
+        /// <summary>
+        /// 鍗曚釜杞村叧闂�
+        /// </summary>
+        /// <returns></returns>
+        public async Task<bool> AxisOffAsync(short cardNum, short axisNum)
+        {
+            return await Task.Run(() =>
+            {
+                var ret = GTSCardAPI.GT_AxisOff(cardNum, axisNum);
+                return ret == (short)GTSRetCode.GRCRunOK;
+            });
+        }
+
+        /// <summary>
+        /// 鐐逛綅鍒扮偣浣嶈繍鍔�
+        /// </summary>
+        /// <param name="item">杩愬姩瀵硅薄</param>
+        /// <returns>杩愬姩鎺у埗+鍋滄鍒ゆ柇</returns>
+        public bool MoveToPoint(IOperationConfig opConfig)
+        {
+            bool resultOK = false;
+            var gtsOperationConfig = opConfig as GTSCardOperationConfig;
+            List<Task<bool>> taskList = new List<Task<bool>>();
+            //TaskFactory factory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
+            // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱杩愬姩缁撴潫
+            foreach (var movingOp in gtsOperationConfig.MovingOps)
+            {
+                //var task = factory.StartNew<bool>((op) =>
+                //{
+                //    return SingleAxisMoving(op as MovingOption);
+                //}, movingOp);
+                var task = SingleAxisMoving(movingOp);
+                taskList.Add(task);
+            }
+            Task.WaitAll(taskList.ToArray());
+            resultOK = taskList.All(u => u.GetAwaiter().GetResult());
+
+            return resultOK;
+        }
+
+        /// <summary>
+        /// 鐐瑰埌鐐硅繍鍔ㄨ缃弬鏁�
+        /// </summary>
+        /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param>
+        /// <returns></returns>
+        private bool SetAxisParam(MovingOption optionPara)
+        {
+            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));
             trapprm.smoothTime = 1;
-            GTSCardAPI.GT_SetTrapPrm(cardNum, axisNum, ref trapprm);
-            GTSCardAPI.GT_SetPos(cardNum, axisNum, prfPosition * GTSCardParameter.Dangliang);
-            GTSCardAPI.GT_SetVel(cardNum, axisNum, prfVelocity * GTSCardParameter.Dangliang);
-            GTSCardAPI.GT_Update(cardNum, 1 << (axisNum - 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));
+
+            var resultOK = resultCode.All(u => u == (short)GTSRetCode.GRCRunOK);
+            if (!resultOK)
+            {
+                throw new Exception("杞�" + optionPara.AxisIndex + "璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛�" + string.Join(",", resultCode));
+            }
+            return resultOK;
         }
 
         /// <summary>
-        /// Jog杩愬姩
+        /// 鍗曚釜杞� 杩愬姩(鐐瑰埌鐐� jog 鍥為浂...锛�
         /// </summary>
-        /// <param name="cardNum"></param>
-        /// <param name="axisNum"></param>
-        /// <param name="velocity">瑙勫垝閫熷害锛屽崟浣嶇背姣忕</param>
-        public void Jog(short cardNum, short axisNum, double velocity)
+        /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param>
+        public async Task<bool> SingleAxisMoving(MovingOption optionPara)
         {
-            GTSCardAPI.TJogPrm jogprm = new GTSCardAPI.TJogPrm();
-            short rtn = GTSCardAPI.GT_PrfJog(cardNum, axisNum);
-            jogprm.acc = 1;
-            jogprm.dec = 1;
-            GTSCardAPI.GT_SetJogPrm(cardNum, axisNum, ref jogprm);//璁剧疆jog杩愬姩鍙傛暟
-            GTSCardAPI.GT_SetVel(cardNum, axisNum, velocity);//璁剧疆鐩爣閫熷害
-            GTSCardAPI.GT_Update(cardNum, 1 << (axisNum - 1));//鏇存柊杞磋繍鍔�
+            return await Task.Run(() =>
+            {
+                bool isSuccessAndStop = false;
+                if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false)
+                {
+                    string _position = "";
+                    string motionType = optionPara.MoveMode == EnumHelper.MotorMoveMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString();
+
+                    _position = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")},{optionPara.AxisIndex},{motionType},{GetPosition(optionPara.AxisIndex).ToString()},{GetPrfPosition(optionPara.AxisIndex).ToString()},{optionPara.Destination},";
+
+                    switch (optionPara.MoveMode)
+                    {
+                        case MotorMoveMode.Normal:
+                            {
+                                if (_isResetting)
+                                {
+                                    LogAsync(DateTime.Now, "澶嶄綅涓惎鍔ㄨ繍鍔ㄥ紓甯�", optionPara.AxisIndex + "鍚姩杩愬姩寮傚父");
+                                    return false;
+                                }
+
+                                if (optionPara.IsAbsolute)
+                                {
+                                    isSuccessAndStop = P2PMoveAbs(optionPara);
+                                }
+                                else
+                                {
+                                    isSuccessAndStop = P2PMoveRel(optionPara);
+                                }
+
+                            }
+                            break;
+                        case MotorMoveMode.FindOri:
+                            {
+                                isSuccessAndStop = GoHome(optionPara);
+                            }
+                            break;
+                        case MotorMoveMode.Jog:
+                            {
+                                isSuccessAndStop = JogMove(optionPara);
+                            }
+                            break;
+                    }
+                    _position += $"{GetPosition(optionPara.AxisIndex)},";
+                    _position += $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}";
+                    LogAsync(DateTime.Now, "", _position);
+                }
+                return isSuccessAndStop;
+            });
+        }
+
+        /// <summary>
+        /// 鑾峰彇瑙勫垝浣嶇疆锛堣鍘荤殑浣嶇疆锛�
+        /// </summary>
+        /// <param name="axisNum">Axis number</param>
+        /// <returns></returns>
+        public double GetPrfPosition(int axisNum)
+        {
+            lock (moveLock)
+            {
+                double position = 0;
+                double prfpos = 0; uint pclock = 0;
+                var ret = GTSCardAPI.GT_GetPrfPos((short)IConfig.CardNum, (short)axisNum, out prfpos, 1, out pclock);
+                if (ret != (short)GTSRetCode.GRCRunOK)
+                {
+                    throw new Exception("杞�" + axisNum + "鑾峰彇瑙勫垝浣嶇疆寮傚父锛岄敊璇爜锛�" + ret);
+                }
+                position = prfpos / IConfig.AxisVelocityRatio;
+                return position;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇鐩墠褰撳墠浣嶇疆
+        /// </summary>
+        /// <param name="axisNum">Axis number</param>
+        /// <returns></returns>
+        public double GetPosition(int axisNum)
+        {
+            lock (moveLock)
+            {
+                double position = 0;
+                int pPos = 0;
+                var ret = GTSCardAPI.GT_GetPos((short)IConfig.CardNum, (short)axisNum, out pPos);
+                if (ret != (short)GTSRetCode.GRCRunOK)
+                {
+                    throw new Exception("杞�" + axisNum + "鑾峰彇鐩爣浣嶇疆寮傚父锛岄敊璇爜锛�" + ret);
+                }
+                position = pPos / IConfig.AxisVelocityRatio;
+                return position;
+            }
+        }
+
+
+        /// <summary>
+        /// Set Single Axis Do Jog Move  
+        /// </summary>
+        /// <param name="axisNum">AxisNo</param>
+        /// <param name="nDirection">Motion Direction 0: Negative, 1: Positive</param>
+        /// <param name="nMaxVel">max velocity</param>
+        /// <returns></returns>
+        public bool JogMove(MovingOption optionPara)
+        {
+            try
+            {
+                GTSCardAPI.TJogPrm jogprm = new GTSCardAPI.TJogPrm();
+                short ret = 0;
+                int repeatTime = 100;
+                do
+                {
+                    ret = GTSCardAPI.GT_PrfJog((short)IConfig.CardNum, (short)optionPara.AxisIndex);
+                    jogprm.acc = optionPara.VelocityPara.Acc;
+                    jogprm.dec = optionPara.VelocityPara.Dec;
+                    ret = GTSCardAPI.GT_SetJogPrm((short)IConfig.CardNum, (short)optionPara.AxisIndex, ref jogprm);//璁剧疆jog杩愬姩鍙傛暟
+                    ret = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)optionPara.AxisIndex, optionPara.VelocityPara.Velocity);//璁剧疆鐩爣閫熷害
+                    ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杞磋繍鍔�
+
+                    if (ret != (short)GTSRetCode.GRCRunOK)
+                    {
+                        LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime);
+                        Thread.Sleep(50);
+                    }
+                    repeatTime--;
+                } while (ret != (short)GTSRetCode.GRCRunOK && repeatTime > 0);
+                return (ret == (short)GTSRetCode.GRCRunOK);
+            }
+            catch (Exception ex)
+            {
+                AllMoveStop(true);
+                OnExceptionRaised?.Invoke(ex);
+                return false;
+            }
+
+        }
+
+        /// <summary>
+        /// Set Single Axis Do stop Jog Move  
+        /// </summary>
+        /// <param name="axisNum">AxisNo</param>
+        /// <returns></returns>
+        public bool StopJog(int axisNum)
+        {
+            //鍋滄杩愬姩
+            MoveStop(axisNum, 0);
+            //杩愬姩寮�濮嬪悗 妫�鏌ヨ繍鍔ㄦ槸鍚﹀仠姝�
+            bool isStop = false;
+            int repeatTime = 1000;
+            do
+            {
+                isStop = IsStop((short)IConfig.CardNum, (short)axisNum);
+                Thread.Sleep(50);
+                repeatTime--;
+            } while (!isStop && repeatTime > 0);
+
+            return isStop;
+        }
+
+        /// <summary>
+        /// 鐩稿浣嶇疆杩愬姩 
+        /// </summary>
+        /// <param name="axisNum">AxisNo</param>
+        /// <param name="nDistance">run distance</param>
+        /// <returns></returns>
+        public bool P2PMoveRel(MovingOption optionPara)
+        {
+            try
+            {
+                if (_isResetting)
+                {
+                    LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩");
+                    throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩");
+                }
+
+                int repeatTime = 30;
+                while (CurrentState != EnumHelper.DeviceState.DSOpen && repeatTime > 0)
+                {
+                    Thread.Sleep(10);
+                    repeatTime--;
+                }
+
+                if (CurrentState == EnumHelper.DeviceState.DSExcept)
+                {
+                    LogAsync(DateTime.Now, "鏉垮崱寮傚父鐘舵��", "杞�" + optionPara.AxisIndex + "璇曞浘寮傚父鐘舵�佽繍鍔�");
+                    return false;
+                }
+
+                if (CurrentState != EnumHelper.DeviceState.DSOpen)
+                {
+                    LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�");
+                    throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null);
+                }
+
+                LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination);
+                short ret = 0;
+                repeatTime = 100;
+                bool isSuccessSetAxisParam = false;
+                int currentPosition = (int)GetPosition(optionPara.AxisIndex);
+                int dPosition = optionPara.Destination + currentPosition;
+                do
+                {
+                    //璁剧疆 杩愬姩鍙傛暟
+                    isSuccessSetAxisParam = SetAxisParam(optionPara);
+                    ret = GTSCardAPI.GT_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(dPosition * IConfig.AxisVelocityRatio));// 璁剧疆瑙勫垝浣嶇疆
+                    ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杩愬姩
+
+                    if (ret != (short)GTSRetCode.GRCRunOK)
+                    {
+                        LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime);
+                        Thread.Sleep(50);
+                    }
+                    repeatTime--;
+                } while (ret != (short)GTSRetCode.GRCRunOK && !isSuccessSetAxisParam && repeatTime > 0);
+
+                //杩愬姩寮�濮嬪悗 妫�鏌ヨ繍鍔ㄦ槸鍚﹀仠姝�
+                bool isStop = false;
+                repeatTime = 1000;
+                do
+                {
+                    isStop = IsStop((short)IConfig.CardNum, (short)optionPara.AxisIndex);
+                    Thread.Sleep(50);
+                    repeatTime--;
+                } while (!isStop && repeatTime > 0);
+
+                return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+            }
+            catch (Exception ex)
+            {
+                AllMoveStop(true);
+                OnExceptionRaised?.Invoke(ex);
+                return false;
+            }
+        }
+
+        /// <summary>
+        ///  缁濆浣嶇疆杩愬姩
+        /// </summary>
+        /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param>
+        public bool P2PMoveAbs(MovingOption optionPara)
+        {
+            try
+            {
+                if (_isResetting)
+                {
+                    LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩");
+                    throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩");
+                }
+                int repeatTime = 30;
+                while (CurrentState != EnumHelper.DeviceState.DSOpen && repeatTime > 0)
+                {
+                    Thread.Sleep(10);
+                    repeatTime--;
+                }
+                if (CurrentState == EnumHelper.DeviceState.DSExcept)
+                {
+                    LogAsync(DateTime.Now, "鏉垮崱寮傚父鐘舵��", "杞�" + optionPara.AxisIndex + "璇曞浘寮傚父鐘舵�佽繍鍔�");
+                    return false;
+                }
+
+                if (CurrentState != EnumHelper.DeviceState.DSOpen)
+                {
+                    LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�");
+                    throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null);
+                }
+                LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination);
+                short ret = 0;
+                repeatTime = 100;
+                bool isSuccessSetAxisParam = false;
+                do
+                {
+                    //璁剧疆 杩愬姩鍙傛暟
+                    isSuccessSetAxisParam = SetAxisParam(optionPara);
+                    ret = GTSCardAPI.GT_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(optionPara.Destination * IConfig.AxisVelocityRatio));// 璁剧疆瑙勫垝浣嶇疆
+                    ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杩愬姩
+
+                    if (ret != (short)GTSRetCode.GRCRunOK)
+                    {
+                        LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime);
+                        Thread.Sleep(50);
+                    }
+                    repeatTime--;
+                } while (ret != (short)GTSRetCode.GRCRunOK && !isSuccessSetAxisParam && repeatTime > 0);
+
+                bool isStop = false;
+                repeatTime = 1000;
+                do
+                {
+                    isStop = IsStop((short)IConfig.CardNum, (short)optionPara.AxisIndex);
+                    Thread.Sleep(50);
+                    repeatTime--;
+                } while (!isStop && repeatTime > 0);
+
+                return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+            }
+            catch (Exception ex)
+            {
+                AllMoveStop(true);
+                OnExceptionRaised?.Invoke(ex);
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 杩愬姩鍋滄
+        /// </summary>
+        /// <param name="axisNum">axisNo</param>
+        /// <param name="option">0琛ㄧず骞虫粦鍋滄锛�1琛ㄧず绱ф�ュ仠姝�</param>
+        /// <returns></returns>
+        public async Task<bool> MoveStop(int axisNum, int option)
+        {
+            return await Task.Run(() =>
+            {
+                bool isStop = false;
+                if (option == 1)
+                {
+                    StateChange(EnumHelper.DeviceState.DSExcept);
+                }
+                var ret = GTSCardAPI.GT_Stop((short)IConfig.CardNum, 1 << (axisNum - 1), option);
+                if (ret != (short)GTSRetCode.GRCRunOK)
+                {
+                    LogAsync(DateTime.Now, "杞�" + axisNum + "杩愬姩鍋滄寮傚父", "閿欒鐮侊細" + ret);
+                    throw new Exception("杞�" + axisNum + "杩愬姩鍋滄寮傚父锛岄敊璇爜锛�" + ret);
+                }
+                else
+                {
+                    LogAsync(DateTime.Now, "杞�" + axisNum + "杩愬姩鍋滄", "");
+                }
+                int repeatTime = 100;
+                do
+                {
+                    isStop = IsStop((short)IConfig.CardNum, (short)axisNum);
+                    Thread.Sleep(10);
+                    repeatTime--;
+                } while (!isStop && repeatTime > 0);
+
+                return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+            });
+        }
+
+        /// <summary>
+        /// 鎵�鏈夊紑鍚殑杞村仠姝�
+        /// </summary>
+        /// <param name="emergencyStop"></param>
+        public void AllMoveStop(bool emergencyStop = false)
+        {
+            int option = emergencyStop ? 1 : 0;
+            List<Task<bool>> taskList = new List<Task<bool>>(); ;
+            // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱杩愬姩缁撴潫
+            IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum =>
+            {
+                var task = MoveStop(axisNum.AxisIndex, option);
+                taskList.Add(task);
+            });
+            Task.WaitAll(taskList.ToArray());
+            var resultOK = taskList.All(u => u.GetAwaiter().GetResult());
+        }
+
+        /// <summary>
+        /// 鍥炲師鐐�
+        /// </summary>
+        /// <param name="movingOption">鍗″彿</param>
+        /// <param name="axisn">杞村彿</param>
+        /// <param name="homests">杞村洖鍘熺偣鐘舵��</param>
+        public bool GoHome(MovingOption movingOption)
+        {
+            try
+            {
+                GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, 1);
+                GTSCardAPI.THomePrm thomeprm;
+                GTSCardAPI.THomeStatus homests;
+                short rtn = GTSCardAPI.GT_GetHomePrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out thomeprm);
+                thomeprm.mode = movingOption.GoHomePara.HomeMode;//鍥為浂鏂瑰紡
+                thomeprm.moveDir = movingOption.GoHomePara.HomeDir;//鍥為浂鏂瑰悜
+                thomeprm.edge = movingOption.GoHomePara.Edge;
+                thomeprm.velHigh = movingOption.GoHomePara.HighVelocity;
+                thomeprm.velLow = movingOption.GoHomePara.LowVelocity;
+                thomeprm.acc = movingOption.VelocityPara.Acc;
+                thomeprm.dec = movingOption.VelocityPara.Dec;
+                thomeprm.searchHomeDistance = movingOption.GoHomePara.SearchHomeDistance;//鎼滄悳璺濈
+                thomeprm.homeOffset = movingOption.GoHomePara.HomeOffset;  //鍋忕Щ璺濈
+                thomeprm.escapeStep = movingOption.GoHomePara.EscapeStep;
+                rtn = GTSCardAPI.GT_GoHome((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref thomeprm);  //鍚姩鍥為浂
+
+                bool isStop = false;
+                int repeatTime = 1000;
+                do
+                {
+                    Thread.Sleep(10);
+                    GTSCardAPI.GT_GetHomeStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out homests);
+
+                    isStop = homests.run == 0;
+                    if (isStop && homests.error == 0)
+                    {
+                        Thread.Sleep(200);
+                        GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, 1);
+                    }
+                    repeatTime--;
+                } while (!isStop && repeatTime > 0);
+
+                return isStop;
+            }
+            catch (Exception ex)
+            {
+                AllMoveStop(true);
+                OnExceptionRaised?.Invoke(ex);
+                return false;
+            }
         }
 
         /// <summary>
@@ -99,36 +633,20 @@
         /// <summary>
         /// 杈撳嚭
         /// </summary>
-        /// <param name="cardNumo">鍗″彿</param>
+        /// <param name="cardNum">鍗″彿</param>
         /// <param name="index">杈撳嚭鍙�,杩斿洖1-16</param>
-        /// <param name="value">0琛ㄧず杈撳嚭锛�1琛ㄧず鍏抽棴</param>
-        public void WriteOut(short cardNumo, short index, bool value)
+        /// <param name="value">false琛ㄧず杈撳嚭锛宼rue琛ㄧず鍏抽棴</param>
+        public void WriteOut(short cardNum, short index, bool value)
         {
             short outNum = (short)(index % 100 + 1);
-            switch (value)
+            if (value)
             {
-                case true:
-                    {
-                        GTSCardAPI.GT_SetDoBit(cardNumo, GTSCardAPI.MC_GPO, outNum, 0);//鎸変綅杈撳嚭锛�0琛ㄧず杈撳嚭锛�1琛ㄧず鍏抽棴
-                    }
-                    break;
-                case false:
-                    {
-                        GTSCardAPI.GT_SetDoBit(cardNumo, GTSCardAPI.MC_GPO, outNum, 1);//鎸変綅杈撳嚭锛�0琛ㄧず杈撳嚭锛�1琛ㄧず鍏抽棴
-                    }
-                    break;
+                GTSCardAPI.GT_SetDoBit(cardNum, GTSCardAPI.MC_GPO, outNum, 0);
             }
-        }
-
-        /// <summary>
-        /// 鍋滄 鏌愪釜杞�
-        /// </summary>
-        /// <param name="cardNum"></param>
-        /// <param name="axisNum"></param>
-        /// <param name="option">0琛ㄧず骞虫粦鍋滄锛�1琛ㄧず绱ф�ュ仠姝�</param>
-        public void Stop(short cardNum, short axisNum, short option)
-        {
-            GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), option);
+            else
+            {
+                GTSCardAPI.GT_SetDoBit(cardNum, GTSCardAPI.MC_GPO, outNum, 1);
+            }
         }
 
         /// <summary>
@@ -137,34 +655,15 @@
         /// <param name="cardNum"></param>
         /// <param name="axisNum">杞村彿</param>
         /// <param name="value">鍋滄鏂瑰紡锛宖alse琛ㄧず骞虫粦鍋滄锛宼rue琛ㄧず绱ф�ュ仠姝�</param>
-        public void Stop(short cardNum, short axisNum, bool value)
+        public void Stop(short cardNum, short axisNum, bool emergencyStop)
         {
-            switch (value)
+            if (emergencyStop)
             {
-                case false:
-                    {
-                        GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), 0);
-                    }
-                    break;
-                case true:
-                    {
-                        GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), 1 << (axisNum - 1));
-
-                    }
-                    break;
+                GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), 1 << (axisNum - 1));
             }
-        }
-
-        /// <summary>
-        /// 鍋滄 鍏ㄩ儴杞�
-        /// </summary>
-        /// <param name="cardNum"></param>
-        /// <param name="value">鍋滄鏂瑰紡锛宖alse琛ㄧず骞虫粦鍋滄锛宼rue琛ㄧず绱ф�ュ仠姝�</param>
-        public void StopAll(short cardNum, bool value)
-        {
-            for (short i = 1; i <= GTSCardParameter.AxisCount; i++)
+            else
             {
-                Stop(cardNum, i, value);
+                GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), 0);
             }
         }
 
@@ -190,47 +689,28 @@
         /// <summary>
         /// 璇诲彇IO杈撳嚭鐘舵��
         /// </summary>
-        /// <param name="cardNumo"></param>
+        /// <param name="cardNum"></param>
         /// <param name="index"></param>
         /// <returns></returns>
-        public bool GetDoSts(short cardNumo, short index)
+        public bool GetDoSts(short cardNum, short index)
         {
             short outNum = 0;
             int outSts;
             outNum = (short)(index % 100);
-            GTSCardAPI.GT_GetDo(cardNumo, GTSCardAPI.MC_GPO, out outSts);
+            GTSCardAPI.GT_GetDo(cardNum, GTSCardAPI.MC_GPO, out outSts);
             if ((outSts & (1 << outNum)) == 0) return true;
             else return false;
-        }
-
-        static object lockObj = new object();
-      
-
-        /// <summary>
-        /// 璇诲彇褰撳墠鍊�
-        /// </summary>
-        /// <param name="cardNum">鍗″彿</param>
-        /// <param name="axisNum">杞村彿</param>
-        /// <returns>杩斿洖褰撳墠鍊硷紝鍗曚綅姣背</returns>
-        public double GetPosMM(short cardNum, short axisNum)
-        {
-            lock (lockObj)
-            {
-                double prfpos = 0; uint pclock = 0;
-                GTSCardAPI.GT_GetPrfPos(cardNum, axisNum, out prfpos, 1, out pclock);
-                return prfpos / GTSCardParameter.Dangliang;
-            }
         }
 
         /// <summary>
         /// 璇诲彇杞寸姸鎬侊紝鍒ゆ柇鐢垫満鏄惁鍋滄
         /// </summary>
-        /// <param name="cardNum"></param>
-        /// <param name="axisNum"></param>
+        /// <param name="cardNum">鏉垮崱鍙�</param>
+        /// <param name="axisNum">杞村彿</param>
         /// <returns></returns>
         public bool IsStop(short cardNum, short axisNum)
         {
-            lock (lockObj)
+            lock (moveLock)
             {
                 int sts = 0;
                 uint pclock = 0;
@@ -240,6 +720,8 @@
             }
         }
 
+        #endregion
+
         public void Monitor()
         {
             throw new NotImplementedException();
@@ -247,7 +729,45 @@
 
         public void ResetAlarm()
         {
-            throw new NotImplementedException();
+            int axis_sts;
+            uint clk;
+            var axisSettings = IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled);
+            GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)axisSettings.Count);
+            foreach (var axisSetting in axisSettings)
+            {
+                GTSCardAPI.GT_GetSts((short)IConfig.CardNum, (short)axisSetting.AxisIndex, out axis_sts, 1, out clk);
+                if ((axis_sts & 0x200) == 0)
+                {
+                    var rst = GTSCardAPI.GT_AxisOn((short)IConfig.CardNum, (short)axisSetting.AxisIndex);
+                }
+                // 浣嶇疆璇烽浂
+                GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, 1, (short)axisSettings.Count);
+                // 姝f瀬闄愭姤璀�
+                if ((axis_sts & 0x20) != 0)
+                {
+                    // 璐熷悜绉诲姩
+                    MovingOption movingOption = new MovingOption();
+                    movingOption.AxisIndex = (short)axisSetting.AxisIndex;
+                    movingOption.Destination = -50; // 璐熷悜绉诲姩
+                    movingOption.VelocityPara.Velocity = 50;
+                    P2PMoveAbs(movingOption);
+                }
+
+                // 璐熸瀬闄愭姤璀�
+                if ((axis_sts & 0x40) != 0)
+                {
+                    // 姝e悜绉诲姩
+                    MovingOption movingOption = new MovingOption();
+                    movingOption.AxisIndex = (short)axisSetting.AxisIndex;
+                    movingOption.Destination = 50; // 璐熷悜绉诲姩
+                    movingOption.VelocityPara.Velocity = 50;
+                    P2PMoveAbs(movingOption);
+                }
+            }
+
+            // 娓呴櫎鐘舵��
+            GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled).Count);
         }
+
     }
 }

--
Gitblit v1.8.0