TransporterConflictRoutine.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Routine;
  4. using Aitex.Core.RT.SCCore;
  5. using MECF.Framework.Common.Equipment;
  6. using MECF.Framework.Common.Routine;
  7. using CyberX8_Core;
  8. using CyberX8_RT.Devices.AXIS;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Aitex.Core.Util;
  16. namespace CyberX8_RT.Modules.Transporter
  17. {
  18. public class TransporterConflictRoutine : RoutineBase, IRoutine
  19. {
  20. private enum ConflictStep
  21. {
  22. CheckSafeStatus,
  23. SafeWaitOtherStatus,
  24. ReCheckSafeStatus,
  25. CheckOtherStatus,
  26. OtherGantryWait,
  27. End
  28. }
  29. #region 内部变量
  30. private string _otherModule = "";
  31. private int _transporterMinimumDistance=600;
  32. private TransporterEntity _otherTransporterEntity;
  33. private JetAxisBase _otherGantryAxis;
  34. private double _targetPosition;
  35. private bool _isOtherStartPosition = false;
  36. private bool _isOtherGantryChangedToSafeStatus = false;
  37. /// <summary>
  38. /// 是否为正向运行
  39. /// </summary>
  40. private bool _runPositive = false;
  41. private bool _isConflict = true;
  42. private double _safeDistace = 0;
  43. #endregion
  44. /// <summary>
  45. /// 构造函数
  46. /// </summary>
  47. /// <param name="module"></param>
  48. public TransporterConflictRoutine(string module) : base(module)
  49. {
  50. }
  51. /// <summary>
  52. /// 中止
  53. /// </summary>
  54. public void Abort()
  55. {
  56. Runner.Stop("Manual Abort");
  57. }
  58. /// <summary>
  59. /// 监控
  60. /// </summary>
  61. /// <returns></returns>
  62. public RState Monitor()
  63. {
  64. Runner.Run(ConflictStep.CheckSafeStatus, CheckSafeConflict,_delay_1ms)
  65. .WaitWithStopCondition(ConflictStep.SafeWaitOtherStatus, ConflictWaitOtherStatus, () => { return false; })
  66. .Run(ConflictStep.ReCheckSafeStatus,CheckSafeConflict,_delay_1ms)
  67. .Run(ConflictStep.CheckOtherStatus, CheckOtherStatus,100)
  68. .WaitWithStopCondition(ConflictStep.OtherGantryWait, CheckOtherAxisPositionEndStatus, CheckOtherAxisPositionStopStatus)
  69. .End(ConflictStep.End, NullFun, _delay_1ms);
  70. return Runner.Status;
  71. }
  72. /// <summary>
  73. /// 检验是否存在安全冲突
  74. /// </summary>
  75. /// <returns></returns>
  76. private bool CheckSafeConflict()
  77. {
  78. var result = CheckOtherIsInSafeDistance(_otherGantryAxis.MotionData.MotorPosition);
  79. if(!result.result)
  80. {
  81. _safeDistace = result.safeDistance;
  82. _isConflict = true;
  83. }
  84. return true;
  85. }
  86. /// <summary>
  87. /// 冲突时等待其他Idle状态
  88. /// </summary>
  89. /// <returns></returns>
  90. private bool ConflictWaitOtherStatus()
  91. {
  92. if(_isConflict)
  93. {
  94. return _otherTransporterEntity.IsIdle;
  95. }
  96. return true;
  97. }
  98. /// <summary>
  99. /// 检验另一个Axis状态
  100. /// </summary>
  101. /// <returns></returns>
  102. private bool CheckOtherStatus()
  103. {
  104. if(_isConflict)
  105. {
  106. bool positionResult= _otherTransporterEntity.CheckToPostMessage<TransporterState,TransporterMSG>(eEvent.ERR_TRANSPORTER,
  107. _otherModule,(int)TransporterMSG.GantrySafeMove,_safeDistace);
  108. if(positionResult)
  109. {
  110. _isOtherStartPosition = true;
  111. }
  112. return positionResult;
  113. }
  114. return true;
  115. }
  116. /// <summary>
  117. /// 检验另外Gantry运动情况
  118. /// </summary>
  119. /// <returns></returns>
  120. private bool CheckOtherAxisPositionEndStatus()
  121. {
  122. if(_isOtherStartPosition)
  123. {
  124. if (_otherTransporterEntity.State == (int)TransporterState.GantrySafeMoving)
  125. {
  126. _isOtherGantryChangedToSafeStatus = true;
  127. }
  128. if (_isOtherGantryChangedToSafeStatus)
  129. {
  130. return _otherTransporterEntity.IsIdle;
  131. }
  132. else
  133. {
  134. return false;
  135. }
  136. }
  137. else
  138. {
  139. return true;
  140. }
  141. }
  142. /// <summary>
  143. /// 检验另外Gantry运动停止情况
  144. /// </summary>
  145. /// <returns></returns>
  146. private bool CheckOtherAxisPositionStopStatus()
  147. {
  148. if (_isOtherStartPosition)
  149. {
  150. return _otherTransporterEntity.IsError;
  151. }
  152. else
  153. {
  154. return true;
  155. }
  156. }
  157. /// <summary>
  158. /// 检验对方是否在安全距离
  159. /// </summary>
  160. /// <param name="position"></param>
  161. /// <returns></returns>
  162. private (bool result,double safeDistance) CheckOtherIsInSafeDistance(double position)
  163. {
  164. if (Module.ToString() == ModuleName.Transporter2.ToString())
  165. {
  166. if (_runPositive)
  167. {
  168. double safeDistance = _targetPosition + _transporterMinimumDistance;
  169. if (position <= safeDistance||(Math.Abs(position - safeDistance) <= 10))
  170. {
  171. return (false,safeDistance);
  172. }
  173. }
  174. return (true,0);
  175. }
  176. else
  177. {
  178. if(!_runPositive)
  179. {
  180. double safeDistance = _targetPosition - _transporterMinimumDistance;
  181. if (position >= safeDistance)
  182. {
  183. return (false,safeDistance);
  184. }
  185. }
  186. return (true,0);
  187. }
  188. }
  189. /// <summary>
  190. /// 启动
  191. /// </summary>
  192. /// <param name="objs"></param>
  193. /// <returns></returns>
  194. public RState Start(params object[] objs)
  195. {
  196. if(Module=="Transporter2")
  197. {
  198. _otherModule = "Transporter1";
  199. }
  200. else
  201. {
  202. _otherModule = "Transporter2";
  203. }
  204. _targetPosition = (double)objs[0];
  205. _runPositive = (bool)objs[1];
  206. _isConflict = false;
  207. _isOtherGantryChangedToSafeStatus = false;
  208. _transporterMinimumDistance = SC.GetValue<int>("Transporter.TransporterMinimumDistance");
  209. _otherGantryAxis = DEVICE.GetDevice<JetAxisBase>($"{_otherModule}.Gantry");
  210. _otherTransporterEntity = Singleton<RouteManager>.Instance.GetModule<TransporterEntity>(_otherModule);
  211. Runner.Start(Module, "Transporter Safe Distance");
  212. return RState.Running;
  213. }
  214. }
  215. }