GalilLipselHomeRoutine.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.Routine;
  3. using MECF.Framework.Common.CommonData.PUF;
  4. using MECF.Framework.Common.Routine;
  5. using CyberX8_Core;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using MECF.Framework.Common.Utilities;
  12. namespace CyberX8_RT.Devices.AXIS.GalilLipsel
  13. {
  14. public class GalilLipselHomeRoutine : RoutineBase, IRoutine
  15. {
  16. private enum HomeStep
  17. {
  18. HomingAcceleration,
  19. HomingDeceleration,
  20. HomingSpeed,
  21. ReversePrPosition,
  22. StartMotion,
  23. CheckMotion,
  24. StopMotion,
  25. CheckStopMotion,
  26. ForwardMotion,
  27. StartForwardMotion,
  28. CheckForwardMotion,
  29. DPDelay,
  30. DP,
  31. WaitDP,
  32. End
  33. }
  34. #region 常量
  35. private const int MOTION_STOP_CODE = 1;
  36. private const int ST_STOP_CODE = 4;
  37. private const int PR_POSITION = -410000;
  38. private const int RIGHT_PR_POSITION = 50000;
  39. #endregion
  40. #region 内部变量
  41. private JetAxisBase _axis;
  42. private int _timeout = 5000;
  43. private double _motionPosition = 0;
  44. private GalilLipselStopPositionRoutine _stopRoutine;
  45. private DateTime _stayTime=DateTime.Now;
  46. private bool _isStay = false;
  47. private int _homingSpeed = 0;
  48. private int _homingAcceleration = 0;
  49. private int _homingDeceleration = 0;
  50. private int _homingOffset = 0;
  51. #endregion
  52. /// <summary>
  53. /// 构造函数
  54. /// </summary>
  55. /// <param name="module"></param>
  56. /// <param name="axis"></param>
  57. public GalilLipselHomeRoutine(string module,JetAxisBase axis) : base(module)
  58. {
  59. _axis = axis;
  60. _stopRoutine=new GalilLipselStopPositionRoutine(module, axis);
  61. }
  62. public void Abort()
  63. {
  64. Runner.Stop("Manual Abort");
  65. }
  66. public RState Monitor()
  67. {
  68. Runner.Run(HomeStep.HomingAcceleration, () => { return _axis.WriteAcceleration(_homingAcceleration); }, _delay_1ms)
  69. .Run(HomeStep.HomingDeceleration, () => { return _axis.WriteDeceleration(_homingDeceleration); }, _delay_1ms)
  70. .Run(HomeStep.HomingSpeed, () => { return _axis.WriteSpeed(_homingSpeed); }, _delay_1ms)
  71. .Run(HomeStep.ReversePrPosition, () => { return _axis.WriteReferencePosition(PR_POSITION); }, _delay_1ms)
  72. .Run(HomeStep.StartMotion, () => { return _axis.WriteStartMotion(); }, _delay_1ms)
  73. .WaitWithStopCondition(HomeStep.CheckMotion, CheckMotion,CheckErrorOrWarning,_timeout)
  74. .Run(HomeStep.StopMotion, () => { return _stopRoutine.Start() == RState.Running; }, _delay_1ms)
  75. .WaitWithStopCondition(HomeStep.CheckStopMotion, () => { return CommonFunction.CheckRoutineEndState(_stopRoutine); },
  76. () => { return CommonFunction.CheckRoutineStopState(_stopRoutine); },_delay_5s)
  77. .Run(HomeStep.ForwardMotion, () => { return _axis.WriteReferencePosition(RIGHT_PR_POSITION); }, _delay_1ms)
  78. .Run(HomeStep.StartForwardMotion, () => { return _axis.WriteStartMotion(); }, _delay_1ms)
  79. .WaitWithStopCondition(HomeStep.CheckForwardMotion, CheckRightMotionEnd, CheckErrorOrWarning, _timeout)
  80. .Delay(HomeStep.DPDelay, 1000)
  81. .Run(HomeStep.DP, () => { return _axis.WriteDP(_homingOffset); }, _delay_1ms)
  82. .Wait(HomeStep.WaitDP, () => { return Math.Round(Math.Abs(_axis.MotionData.MotorPosition - _homingOffset / _axis.ScaleFactor), 2) <= _axis.ToleranceDefault; }, _delay_1s)
  83. .End(HomeStep.End,NullFun,100);
  84. return Runner.Status;
  85. }
  86. /// <summary>
  87. /// 检验运动(一直运动至motionposition不再变小为止)
  88. /// </summary>
  89. /// <returns></returns>
  90. private bool CheckMotion()
  91. {
  92. if (_motionPosition == 0)
  93. {
  94. _motionPosition = _axis.MotionData.MotorPosition;
  95. return false;
  96. }
  97. if(_axis.MotionData.MotorPosition<_motionPosition)
  98. {
  99. _motionPosition = _axis.MotionData.MotorPosition;
  100. _isStay = false;
  101. return false;
  102. }
  103. else if (_axis.MotionData.MotorPosition == _motionPosition)
  104. {
  105. if (!_isStay)
  106. {
  107. _isStay = true;
  108. _stayTime = DateTime.Now;
  109. }
  110. else
  111. {
  112. if (DateTime.Now.Subtract(_stayTime).TotalMilliseconds >= 1000)
  113. {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. return true;
  120. }
  121. /// <summary>
  122. /// 检验是否运动至右边位置结束
  123. /// </summary>
  124. /// <returns></returns>
  125. private bool CheckRightMotionEnd()
  126. {
  127. return _axis.MotionData.StopCode == MOTION_STOP_CODE && !_axis.IsRun;
  128. }
  129. /// <summary>
  130. /// 检验是否出错或告警
  131. /// </summary>
  132. /// <returns></returns>
  133. private bool CheckErrorOrWarning()
  134. {
  135. //byte stopCode = _axis.MotionData.StopCode;
  136. //if (stopCode != 0 && stopCode != MOTION_STOP_CODE)
  137. //{
  138. // LOG.WriteLog(eEvent.ERR_AXIS, Module, $"axis home stopcode is {stopCode}");
  139. // return true;
  140. //}
  141. return false;
  142. }
  143. public RState Start(params object[] objs)
  144. {
  145. _timeout = (int)objs[0];
  146. _homingAcceleration=(int)objs[1];
  147. _homingDeceleration= (int)objs[2];
  148. _homingSpeed=(int)objs[3];
  149. _homingOffset = (int)objs[4];
  150. _motionPosition = 0;
  151. _isStay = false;
  152. return Runner.Start(Module, "Home");
  153. }
  154. }
  155. }