From 8bd46da0c9fbfe39a65d75b1d85692d8783f5905 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期三, 15 九月 2021 10:50:54 +0800
Subject: [PATCH] 现场修改

---
 src/Bro.Device.GTSCard/GTSCardDriver.cs |  149 ++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 108 insertions(+), 41 deletions(-)

diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index a721273..14b4c25 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -312,7 +312,6 @@
             }
         }
 
-
         public override bool AllAxisOn()
         {
             List<Task<bool>> taskList = new List<Task<bool>>(); ;
@@ -387,7 +386,7 @@
                 responseMessage.Result = resultList.All(u => u == true);
                 if (!responseMessage.Result)
                 {
-                    responseMessage.Message = $"鐐逛綅杩愬姩寮傚父";
+                    responseMessage.Message = $"鐐逛綅杩愬姩寮傚父,杩愬姩缁撴灉锛歿string.Join(" ", resultList.Select(u => u ? "1" : "0"))}";
                 }
             }
             return responseMessage;
@@ -400,24 +399,28 @@
         /// <returns></returns>
         private bool SetAxisParam(MovingOption optionPara)
         {
-            List<short> resultCode = new List<short>() { 0 };
+            List<short> resultCode = new List<short>();
             GTSCardAPI.TTrapPrm trapprm = new GTSCardAPI.TTrapPrm();
             short axisIndex = short.Parse(optionPara.AxisIndexStr);
             resultCode.Add(GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, axisIndex));
-            resultCode.Add(GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, axisIndex, out trapprm));
-            trapprm.smoothTime = 1;
 
-            if (optionPara.VelocityPara.Acc != 0)
+            if (optionPara.VelocityPara.Acc != 0 || optionPara.VelocityPara.Dec != 0)
             {
-                trapprm.acc = optionPara.VelocityPara.Acc;
-            }
+                resultCode.Add(GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, axisIndex, out trapprm));
+                trapprm.smoothTime = 1;
 
-            if (optionPara.VelocityPara.Dec != 0)
-            {
-                trapprm.dec = optionPara.VelocityPara.Dec;
-            }
+                if (optionPara.VelocityPara.Acc != 0)
+                {
+                    trapprm.acc = optionPara.VelocityPara.Acc;
+                }
 
-            resultCode.Add(GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, axisIndex, ref trapprm));
+                if (optionPara.VelocityPara.Dec != 0)
+                {
+                    trapprm.dec = optionPara.VelocityPara.Dec;
+                }
+
+                resultCode.Add(GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, axisIndex, ref trapprm));
+            }
 
             if (optionPara.VelocityPara.Velocity != 0)
             {
@@ -427,9 +430,10 @@
             var resultOK = resultCode.All(u => u == (short)GTSRetCode.GRCRunOK);
             if (!resultOK)
             {
-                throw new ProcessException("杞�" + optionPara.AxisIndex + "璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛�" + string.Join(",", resultCode));
+                //throw new ProcessException("杞�" + optionPara.AxisIndex + "璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛�" + string.Join(",", resultCode));
+                LogAsync(DateTime.Now, $"杞磠optionPara.AxisIndex}璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛歿string.Join(",", resultCode)}", "");
             }
-            return resultOK;
+            return true;
         }
 
         TaskFactory taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);
@@ -526,7 +530,7 @@
         /// <returns></returns>
         public double GetPosition(int axisNum)
         {
-            lock (moveLock)
+            //lock (moveLock)
             {
                 double position = 0;
                 double pPos = 0;
@@ -646,7 +650,9 @@
                 if (CurrentState != EnumHelper.DeviceState.DSOpen)
                 {
                     LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�");
-                    throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null);
+
+                    return false;
+                    //throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null);
                 }
 
                 LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination);
@@ -725,8 +731,11 @@
                 if (CurrentState != EnumHelper.DeviceState.DSOpen)
                 {
                     LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�");
-                    throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null);
+
+                    return false;
+                    //throw new ProcessException("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null);
                 }
+
                 LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination);
                 short ret = 0;
                 bool isSuccessSetAxisParam = false;
@@ -760,7 +769,14 @@
                     repeatTime--;
                 }
 
-                return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+                if (repeatTime <= 0)
+                {
+                    LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move鏈仠姝�", "");
+                    return false;
+                }
+
+                //return (ret == (short)GTSRetCode.GRCRunOK) && isStop;
+                return true;
             }
             catch (Exception ex)
             {
@@ -783,7 +799,8 @@
                 bool isStop = false;
                 if (option == 1)
                 {
-                    StateChange(EnumHelper.DeviceState.DSExcept);
+                    //StateChange(EnumHelper.DeviceState.DSExcept);
+                    LogAsync(DateTime.Now, "鎬ュ仠鍋滄", "");
                 }
                 var ret = GTSCardAPI.GT_Stop((short)IConfig.CardNum, 1 << (axisNum - 1), option);
                 if (ret != (short)GTSRetCode.GRCRunOK)
@@ -891,6 +908,8 @@
         {
             try
             {
+                Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
+
                 var goHomePara = IConfig.AxisSettings.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex).GoHomePara;
                 bool homeDirection = goHomePara.IsHomeDirPositive;
                 bool isRightLimitReached = false;
@@ -907,34 +926,56 @@
                 // 璁剧疆鐐逛綅妯″紡杩愬姩鍙傛暟
                 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);
 
-                    //鎼滅储璺濈 闃舵1
-                    // 鍚姩Home鎹曡幏
-                    sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
+                    //LogAsync(DateTime.Now, $"{Name}浣嶇疆缃浂", "");
+
+                    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);
+                    sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, homeDirection ? 999999999 : -999999999);
                     // 鍚姩杩愬姩
                     sRtn = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (movingOption.AxisIndex - 1));
 
                     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);
+
+                        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);
+                            //LogAsync(DateTime.Now, $"鍘熺偣鐘舵�亄pValue}锛岃酱{movingOption.AxisIndex}鍘熺偣{capture}", "");
+                        }
                         isStop = IsStop((short)movingOption.AxisIndex);
-                        repeatTime -= IConfig.MonitorInterval * 5;
+                        repeatTime -= checkInterval;
                     } while (!(isStop || capture == 1 || repeatTime <= 0));
 
                     if (repeatTime <= 0)
@@ -945,25 +986,40 @@
 
                     var axisStatus = AxisStatusList.FirstOrDefault(u => u.AxisIndex == movingOption.AxisIndex);
 
+                    //if (isStop)
+                    //{
+                    //    LogAsync(DateTime.Now, $"杞磠movingOption.AxisIndex}澶嶄綅涓仠姝�", $"Capture鐘舵�亄capture}");
+                    //}
+
                     if (isStop && capture != 1)
                     {
                         if (((axisStatus.AxisStatus & 0x20) != 0) || ((axisStatus.AxisStatus & 0x40) != 0))
                         {
-                            //movingOption.GoHomePara.HomeDir = (short)(movingOption.GoHomePara.HomeDir == 1 ? -1 : 1);
+                            capture = 0;
 
                             //姝i檺浣�
-                            if ((axisStatus.AxisStatus & 0x20) != 0 && !goHomePara.IsCaptureDirPositive)
+                            if ((axisStatus.AxisStatus & 0x20) != 0)
                             {
-                                isRightLimitReached = true;
+                                homeDirection = false;
+
+                                if (!goHomePara.IsCaptureDirPositive)
+                                {
+                                    isRightLimitReached = true;
+                                }
                             }
 
                             //璐熼檺浣�
-                            if ((axisStatus.AxisStatus & 0x40) != 0 && goHomePara.IsCaptureDirPositive)
+                            if ((axisStatus.AxisStatus & 0x40) != 0)
                             {
-                                isRightLimitReached = true;
+                                homeDirection = true;
+
+                                if (goHomePara.IsCaptureDirPositive)
+                                {
+                                    isRightLimitReached = true;
+                                }
                             }
 
-                            homeDirection = !homeDirection;
+                            LogAsync(DateTime.Now, $"杞磠movingOption.AxisIndex}鏋侀檺浣嶇疆鎹㈠悜", "");
                         }
 
                         ClearStatus(movingOption.AxisIndex, 1);
@@ -998,17 +1054,25 @@
                             repeatTime--;
                         } while (!isStop && repeatTime > 0);
                         PositionReset(movingOption.AxisIndex, 1);
+                        LogAsync(DateTime.Now, $"杞磠movingOption.AxisIndex}澶嶄綅瀹屾垚锛屼綅缃竻闆�", "");
+
                         return (sRtn == (short)GTSRetCode.GRCRunOK) && isStop;
                     }
 
                 } while (!(capture == 1 && isRightLimitReached));
+
                 return false;
             }
             catch (Exception ex)
             {
+                LogAsync(DateTime.Now, $"{Name}鍥炲師鐐瑰紓甯�", ex.GetExceptionMessage());
                 AllMoveStop(true);
                 OnExceptionOccured?.Invoke(DateTime.Now, ex);
                 return false;
+            }
+            finally
+            { 
+            
             }
         }
 
@@ -1079,7 +1143,7 @@
         /// <returns></returns>
         public override int GetAxisStatus(int axisNum)
         {
-            lock (moveLock)
+            //lock (moveLock)
             {
                 int sts = 0;
                 uint pclock = 0;
@@ -1102,7 +1166,7 @@
         /// <returns></returns>
         public override bool PositionReset(int startAxisIndex, int count)
         {
-            lock (moveLock)
+            //lock (moveLock)
             {
                 var rtn = GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)startAxisIndex, (short)count);
                 return rtn == (short)GTSRetCode.GRCRunOK;
@@ -1189,7 +1253,9 @@
                             }
                         });
 
-                        _positionWait.SpinOnce();
+                        Task.Delay(100).Wait();
+
+                        //_positionWait.SpinOnce();
                     }
                     catch (Exception ex)
                     {
@@ -1226,7 +1292,8 @@
                             }
                         });
 
-                        _statusWait.SpinOnce();
+                        Thread.Sleep(10);
+                        //Task.Delay(10).Wait();
                     }
                     catch (Exception ex)
                     {
@@ -1253,7 +1320,7 @@
                         var newValues = GetMonitorValues();
                         //var newAxisMovingStatus = GetAxisMovingStatus();
 
-                        if (newValues == null || newValues.Count == 0 )
+                        if (newValues == null || newValues.Count == 0)
                             continue;
 
                         //Stopwatch sw = new Stopwatch();

--
Gitblit v1.8.0