SRDHomeRoutine.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Routine;
  4. using MECF.Framework.Common.Routine;
  5. using MECF.Framework.Common.Utilities;
  6. using CyberX8_Core;
  7. using CyberX8_RT.Devices.AXIS;
  8. using CyberX8_RT.Devices.Facilities;
  9. using CyberX8_RT.Devices.SRD;
  10. using CyberX8_RT.Devices.Safety;
  11. namespace CyberX8_RT.Modules.Transporter
  12. {
  13. public class SRDHomeRoutine : RoutineBase, IRoutine
  14. {
  15. private enum HomeAllStep
  16. {
  17. //CheckSafety,
  18. CheckPreCondition,
  19. CloseDoor,
  20. CheckDoorClosed,
  21. HomeSRDRotation,
  22. CheckRotationHome,
  23. RotationHome,
  24. RotationHomeWait,
  25. LiftUpOn,
  26. //FlippersOut100,
  27. FlippersOut150,
  28. FlippersOut200,
  29. OpenDoor,
  30. CheckDoorOpened,
  31. End
  32. }
  33. #region 内部变量
  34. private JetAxisBase _rotationAxis;
  35. private SrdCommonDevice _common;
  36. private SrdCommonDoorCloseRoutine _doorCloseRoutine;
  37. private string _module;
  38. #endregion
  39. #region 属性
  40. /// <summary>
  41. /// 当前子状态机
  42. /// </summary>
  43. public string CurrentStateMachine
  44. {
  45. get { return Runner.CurrentStep.ToString(); }
  46. }
  47. #endregion
  48. public SRDHomeRoutine(string module) : base(module)
  49. {
  50. _module = module;
  51. }
  52. public void Abort()
  53. {
  54. Runner.Stop("Manual Abort");
  55. }
  56. public RState Monitor()
  57. {
  58. Runner//.Run(HomeAllStep.CheckSafety,CheckSafety,_delay_1ms)
  59. .Run(HomeAllStep.CheckPreCondition, CheckPreCondition,NullFun,_delay_1ms)
  60. .Run(HomeAllStep.CloseDoor, () => { return _doorCloseRoutine.Start(true) == RState.Running; }, NullFun, _delay_1ms)
  61. .WaitWithStopCondition(HomeAllStep.CheckDoorClosed, () => CommonFunction.CheckRoutineEndState(_doorCloseRoutine), () => CommonFunction.CheckRoutineStopState(_doorCloseRoutine))
  62. .Run(HomeAllStep.HomeSRDRotation, RotationAxisHome,_delay_1ms)
  63. .WaitWithStopCondition(HomeAllStep.CheckRotationHome, () => { return _rotationAxis.IsHomed && _rotationAxis.Status == RState.End; }, () => { return _rotationAxis.Status == RState.Failed; })
  64. .Run(HomeAllStep.RotationHome, () => { return _rotationAxis.PositionStation("Home",true); }, NullFun, 100)
  65. .WaitWithStopCondition(HomeAllStep.RotationHomeWait, () => { return _rotationAxis.Status == RState.End; }, () => { return _rotationAxis.Status == RState.Failed; })
  66. .Run(HomeAllStep.LiftUpOn, LiftUpOn, CheckLiftUpOnEndStatus, CheckLiftUpOnStopStatus)
  67. //.Run(HomeAllStep.FlippersOut100, () => { return FlippersOut(100); }, CheckFlippersOutEndStatus, CheckFlippersOutStopStatus)
  68. .Run(HomeAllStep.FlippersOut150, () => { return FlippersOut(150); }, CheckFlippersOutEndStatus, CheckFlippersOutStopStatus)
  69. .Run(HomeAllStep.FlippersOut200, () => { return FlippersOut(200); }, CheckFlippersOutEndStatus, CheckFlippersOutStopStatus)
  70. .Run(HomeAllStep.OpenDoor, () => { return _doorCloseRoutine.Start(false) == RState.Running; },NullFun,_delay_1ms)
  71. .WaitWithStopCondition(HomeAllStep.CheckDoorOpened,()=>CommonFunction.CheckRoutineEndState(_doorCloseRoutine),()=>CommonFunction.CheckRoutineStopState(_doorCloseRoutine))
  72. .End(HomeAllStep.End,NullFun);
  73. return Runner.Status;
  74. }
  75. /// <summary>
  76. /// 检验Safety
  77. /// </summary>
  78. /// <returns></returns>
  79. private bool CheckSafety()
  80. {
  81. SafetyDevice safetyDevice = DEVICE.GetDevice<SafetyDevice>("Safety");
  82. if (safetyDevice == null)
  83. {
  84. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Safety device is null");
  85. return false;
  86. }
  87. if (safetyDevice.SafetyData.SrdCommErr)
  88. {
  89. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Twincat SRD Communication status is error");
  90. return false;
  91. }
  92. if (safetyDevice.SafetyData.SrdFunctionBlockErr)
  93. {
  94. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Twincat SRD function block status is error");
  95. return false;
  96. }
  97. return true;
  98. }
  99. /// <summary>
  100. /// 检验前置条件
  101. /// </summary>
  102. /// <returns></returns>
  103. private bool CheckPreCondition()
  104. {
  105. //检查电机谁否开启
  106. if(!_rotationAxis.IsSwitchOn)
  107. {
  108. LOG.WriteLog(eEvent.ERR_SRD, Module, $"{_module}.Rotation is switchoff");
  109. return false;
  110. }
  111. //检查CDA与N2是否开启
  112. SystemFacilities systemFacilities = DEVICE.GetDevice<SystemFacilities>("System.Facilities");
  113. var result = systemFacilities.CheckCDAN2();
  114. if (!result.result)
  115. {
  116. LOG.WriteLog(eEvent.ERR_SRD, Module, $"CDA or N2 is not enabled");
  117. return false;
  118. }
  119. return true;
  120. }
  121. /// <summary>
  122. /// Rotation Home
  123. /// </summary>
  124. /// <returns></returns>
  125. private bool RotationAxisHome()
  126. {
  127. return _rotationAxis.Home();
  128. }
  129. /// <summary>
  130. /// 检验Rotation Home状态
  131. /// </summary>
  132. /// <returns></returns>
  133. private bool CheckRotationHome()
  134. {
  135. return _rotationAxis.IsHomed&&_rotationAxis.Status==RState.End;
  136. }
  137. /// <summary>
  138. /// LiftUpOn
  139. /// </summary>
  140. /// <param name="param"></param>
  141. /// <returns></returns>
  142. private bool LiftUpOn()
  143. {
  144. bool result = _common.LiftUpOnAction("", null);
  145. if (!result)
  146. {
  147. NotifyError(eEvent.ERR_SRD, "Lift Up On Action is failed", 0);
  148. return result;
  149. }
  150. return true;
  151. }
  152. /// <summary>
  153. /// 检验LiftUpOnEnd状态
  154. /// </summary>
  155. /// <param name="param"></param>
  156. /// <returns></returns>
  157. private bool CheckLiftUpOnEndStatus()
  158. {
  159. return _common.Status == RState.End;
  160. }
  161. /// <summary>
  162. /// 检验LiftUpOnStop状态
  163. /// </summary>
  164. /// <param name="param"></param>
  165. /// <returns></returns>
  166. private bool CheckLiftUpOnStopStatus()
  167. {
  168. if (_common.Status == RState.Failed || _common.Status == RState.Timeout)
  169. {
  170. NotifyError(eEvent.ERR_SRD, "Check LiftUpOn is failed", 0);
  171. return true;
  172. }
  173. return false;
  174. }
  175. /// <summary>
  176. /// Flippers Out
  177. /// </summary>
  178. /// <param name="param"></param>
  179. /// <returns></returns>
  180. private bool FlippersOut(int size)
  181. {
  182. bool result = false;
  183. object[] objects = new object[1];
  184. objects[0] = size;
  185. result = _common.FlipperOutAction("", objects);
  186. if (!result)
  187. {
  188. NotifyError(eEvent.ERR_SRD, $"FlipperOut Action is failed", 0);
  189. return result;
  190. }
  191. return true;
  192. }
  193. /// <summary>
  194. /// 检验FlippersOut结束状态
  195. /// </summary>
  196. /// <param name="param"></param>
  197. /// <returns></returns>
  198. private bool CheckFlippersOutEndStatus()
  199. {
  200. return _common.Status == RState.End;
  201. }
  202. /// <summary>
  203. /// 检验FlippersOut结束状态
  204. /// </summary>
  205. /// <param name="param"></param>
  206. /// <returns></returns>
  207. private bool CheckFlippersOutStopStatus()
  208. {
  209. if (_common.Status == RState.Failed || _common.Status == RState.Timeout)
  210. {
  211. NotifyError(eEvent.ERR_SRD, $"Check FlipperOut Action is failed", 0);
  212. return true;
  213. }
  214. return false;
  215. }
  216. /// <summary>
  217. /// 启动
  218. /// </summary>
  219. /// <param name="objs"></param>
  220. /// <returns></returns>
  221. public RState Start(params object[] objs)
  222. {
  223. _rotationAxis = DEVICE.GetDevice<JetAxisBase>($"{Module}.Rotation");
  224. _common = DEVICE.GetDevice<SrdCommonDevice>($"{Module}.Common");
  225. _doorCloseRoutine = new SrdCommonDoorCloseRoutine(Module);
  226. return Runner.Start(Module, "Home");
  227. }
  228. }
  229. }