SRDHomeRoutine.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Routine;
  4. using MECF.Framework.Common.Equipment;
  5. using MECF.Framework.Common.Routine;
  6. using MECF.Framework.Common.Utilities;
  7. using CyberX8_Core;
  8. using CyberX8_RT.Devices.AXIS;
  9. using CyberX8_RT.Devices.Facilities;
  10. using CyberX8_RT.Devices.PUF;
  11. using CyberX8_RT.Devices.SRD;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using CyberX8_RT.Devices.Safety;
  18. namespace CyberX8_RT.Modules.Transporter
  19. {
  20. public class SRDHomeRoutine : RoutineBase, IRoutine
  21. {
  22. private enum HomeAllStep
  23. {
  24. CheckSafety,
  25. CheckPreCondition,
  26. CloseDoor,
  27. CheckDoorClosed,
  28. HomeSRDArm,
  29. CheckArmHome,
  30. ArmHome,
  31. ArmHomeWait,
  32. HomeSRDRotation,
  33. RotationHome,
  34. RotationHomeWait,
  35. CheckRotationHome,
  36. OpenDoor,
  37. CheckDoorOpened,
  38. End
  39. }
  40. #region 内部变量
  41. private JetAxisBase _armAxis;
  42. private JetAxisBase _rotationAxis;
  43. private SrdCommonDevice _common;
  44. private SrdCommonDoorCloseRoutine _doorCloseRoutine;
  45. private string _module;
  46. #endregion
  47. #region 属性
  48. /// <summary>
  49. /// 当前子状态机
  50. /// </summary>
  51. public string CurrentStateMachine
  52. {
  53. get { return Runner.CurrentStep.ToString(); }
  54. }
  55. #endregion
  56. public SRDHomeRoutine(string module) : base(module)
  57. {
  58. _module = module;
  59. }
  60. public void Abort()
  61. {
  62. Runner.Stop("Manual Abort");
  63. }
  64. public RState Monitor()
  65. {
  66. Runner.Run(HomeAllStep.CheckSafety,CheckSafety,_delay_1ms)
  67. .Run(HomeAllStep.CheckPreCondition, CheckPreCondition,NullFun,_delay_1ms)
  68. .Run(HomeAllStep.CloseDoor, () => { return _doorCloseRoutine.Start(true) == RState.Running; }, NullFun, _delay_1ms)
  69. .WaitWithStopCondition(HomeAllStep.CheckDoorClosed, () => CommonFunction.CheckRoutineEndState(_doorCloseRoutine), () => CommonFunction.CheckRoutineStopState(_doorCloseRoutine))
  70. .Run(HomeAllStep.HomeSRDArm, ArmAxisHome, _delay_1ms)
  71. .WaitWithStopCondition(HomeAllStep.CheckArmHome, () => { return _armAxis.IsHomed && _armAxis.Status == RState.End; }, () => { return _armAxis.Status == RState.Failed; })
  72. .Run(HomeAllStep.HomeSRDRotation, RotationAxisHome,_delay_1ms)
  73. .WaitWithStopCondition(HomeAllStep.CheckRotationHome, () => { return _rotationAxis.IsHomed && _rotationAxis.Status == RState.End; }, () => { return _rotationAxis.Status == RState.Failed; })
  74. .Run(HomeAllStep.ArmHome, () => { return _armAxis.PositionStation("Home", true); }, NullFun, 100)
  75. .WaitWithStopCondition(HomeAllStep.ArmHomeWait, () => { return _armAxis.Status == RState.End; }, () => { return _armAxis.Status == RState.Failed; })
  76. .Run(HomeAllStep.RotationHome, () => { return _rotationAxis.PositionStation("Home",true); }, NullFun, 100)
  77. .WaitWithStopCondition(HomeAllStep.RotationHomeWait, () => { return _rotationAxis.Status == RState.End; }, () => { return _rotationAxis.Status == RState.Failed; })
  78. .Run(HomeAllStep.OpenDoor, () => { return _doorCloseRoutine.Start(false) == RState.Running; },NullFun,_delay_1ms)
  79. .WaitWithStopCondition(HomeAllStep.CheckDoorOpened,()=>CommonFunction.CheckRoutineEndState(_doorCloseRoutine),()=>CommonFunction.CheckRoutineStopState(_doorCloseRoutine))
  80. .End(HomeAllStep.End,NullFun);
  81. return Runner.Status;
  82. }
  83. /// <summary>
  84. /// 检验Safety
  85. /// </summary>
  86. /// <returns></returns>
  87. private bool CheckSafety()
  88. {
  89. SafetyDevice safetyDevice = DEVICE.GetDevice<SafetyDevice>("Safety");
  90. if (safetyDevice == null)
  91. {
  92. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Safety device is null");
  93. return false;
  94. }
  95. if (safetyDevice.SafetyData.TwincatState != 8)
  96. {
  97. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Twincat Status is not OP status");
  98. return false;
  99. }
  100. if (safetyDevice.SafetyData.SrdCommErr)
  101. {
  102. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Twincat SRD Communication status is error");
  103. return false;
  104. }
  105. if (safetyDevice.SafetyData.SrdFunctionBlockErr)
  106. {
  107. LOG.WriteLog(eEvent.ERR_SRD, Module.ToString(), "Twincat SRD function block status is error");
  108. return false;
  109. }
  110. return true;
  111. }
  112. /// <summary>
  113. /// 检验前置条件
  114. /// </summary>
  115. /// <returns></returns>
  116. private bool CheckPreCondition()
  117. {
  118. //检查电机谁否开启
  119. if(! _armAxis.IsSwitchOn)
  120. {
  121. LOG.WriteLog(eEvent.ERR_SRD, Module, $"{_module}.Arm is switchoff");
  122. return false;
  123. }
  124. if(!_rotationAxis.IsSwitchOn)
  125. {
  126. LOG.WriteLog(eEvent.ERR_SRD, Module, $"{_module}.Rotation is switchoff");
  127. return false;
  128. }
  129. //检查CDA与N2是否开启
  130. SystemFacilities systemFacilities = DEVICE.GetDevice<SystemFacilities>("System.Facilities");
  131. var result = systemFacilities.CheckCDAN2();
  132. if (!result.result)
  133. {
  134. LOG.WriteLog(eEvent.ERR_SRD, Module, $"CDA or N2 is not enabled");
  135. return false;
  136. }
  137. return true;
  138. }
  139. /// <summary>
  140. /// Rotation Home
  141. /// </summary>
  142. /// <returns></returns>
  143. private bool RotationAxisHome()
  144. {
  145. return _rotationAxis.Home();
  146. }
  147. /// <summary>
  148. /// 检验Rotation Home状态
  149. /// </summary>
  150. /// <returns></returns>
  151. private bool CheckRotationHome()
  152. {
  153. return _rotationAxis.IsHomed&&_rotationAxis.Status==RState.End;
  154. }
  155. /// <summary>
  156. /// Arm Home
  157. /// </summary>
  158. /// <returns></returns>
  159. private bool ArmAxisHome()
  160. {
  161. return _armAxis.Home();
  162. }
  163. /// <summary>
  164. /// 启动
  165. /// </summary>
  166. /// <param name="objs"></param>
  167. /// <returns></returns>
  168. public RState Start(params object[] objs)
  169. {
  170. _armAxis = DEVICE.GetDevice<JetAxisBase>($"{Module}.Arm");
  171. _rotationAxis = DEVICE.GetDevice<JetAxisBase>($"{Module}.Rotation");
  172. _common = DEVICE.GetDevice<SrdCommonDevice>($"{Module}.Common");
  173. _doorCloseRoutine = new SrdCommonDoorCloseRoutine(Module);
  174. return Runner.Start(Module, "Home");
  175. }
  176. }
  177. }