SRDHomeRoutine.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. //Check LoadDI
  120. if (!systemFacilities.LoaderDiEnable)
  121. {
  122. LOG.WriteLog(eEvent.ERR_SRD, Module, "Load DI is Disable");
  123. return false;
  124. }
  125. return true;
  126. }
  127. /// <summary>
  128. /// Rotation Home
  129. /// </summary>
  130. /// <returns></returns>
  131. private bool RotationAxisHome()
  132. {
  133. return _rotationAxis.Home();
  134. }
  135. /// <summary>
  136. /// 检验Rotation Home状态
  137. /// </summary>
  138. /// <returns></returns>
  139. private bool CheckRotationHome()
  140. {
  141. return _rotationAxis.IsHomed&&_rotationAxis.Status==RState.End;
  142. }
  143. /// <summary>
  144. /// LiftUpOn
  145. /// </summary>
  146. /// <param name="param"></param>
  147. /// <returns></returns>
  148. private bool LiftUpOn()
  149. {
  150. bool result = _common.LiftUpOnAction("", null);
  151. if (!result)
  152. {
  153. NotifyError(eEvent.ERR_SRD, "Lift Up On Action is failed", 0);
  154. return result;
  155. }
  156. return true;
  157. }
  158. /// <summary>
  159. /// 检验LiftUpOnEnd状态
  160. /// </summary>
  161. /// <param name="param"></param>
  162. /// <returns></returns>
  163. private bool CheckLiftUpOnEndStatus()
  164. {
  165. return _common.Status == RState.End;
  166. }
  167. /// <summary>
  168. /// 检验LiftUpOnStop状态
  169. /// </summary>
  170. /// <param name="param"></param>
  171. /// <returns></returns>
  172. private bool CheckLiftUpOnStopStatus()
  173. {
  174. if (_common.Status == RState.Failed || _common.Status == RState.Timeout)
  175. {
  176. NotifyError(eEvent.ERR_SRD, "Check LiftUpOn is failed", 0);
  177. return true;
  178. }
  179. return false;
  180. }
  181. /// <summary>
  182. /// Flippers Out
  183. /// </summary>
  184. /// <param name="param"></param>
  185. /// <returns></returns>
  186. private bool FlippersOut(int size)
  187. {
  188. bool result = false;
  189. object[] objects = new object[1];
  190. objects[0] = size;
  191. result = _common.FlipperOutAction("", objects);
  192. if (!result)
  193. {
  194. NotifyError(eEvent.ERR_SRD, $"FlipperOut Action is failed", 0);
  195. return result;
  196. }
  197. return true;
  198. }
  199. /// <summary>
  200. /// 检验FlippersOut结束状态
  201. /// </summary>
  202. /// <param name="param"></param>
  203. /// <returns></returns>
  204. private bool CheckFlippersOutEndStatus()
  205. {
  206. return _common.Status == RState.End;
  207. }
  208. /// <summary>
  209. /// 检验FlippersOut结束状态
  210. /// </summary>
  211. /// <param name="param"></param>
  212. /// <returns></returns>
  213. private bool CheckFlippersOutStopStatus()
  214. {
  215. if (_common.Status == RState.Failed || _common.Status == RState.Timeout)
  216. {
  217. NotifyError(eEvent.ERR_SRD, $"Check FlipperOut Action is failed", 0);
  218. return true;
  219. }
  220. return false;
  221. }
  222. /// <summary>
  223. /// 启动
  224. /// </summary>
  225. /// <param name="objs"></param>
  226. /// <returns></returns>
  227. public RState Start(params object[] objs)
  228. {
  229. _rotationAxis = DEVICE.GetDevice<JetAxisBase>($"{Module}.Rotation");
  230. _common = DEVICE.GetDevice<SrdCommonDevice>($"{Module}.Common");
  231. _doorCloseRoutine = new SrdCommonDoorCloseRoutine(Module);
  232. return Runner.Start(Module, "Home");
  233. }
  234. }
  235. }