SRDRotationRoutine.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Fsm;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.Routine;
  5. using MECF.Framework.Common.Beckhoff.AxisProvider;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.RecipeCenter;
  8. using MECF.Framework.Common.Utilities;
  9. using CyberX8_Core;
  10. using CyberX8_RT.Devices.AXIS;
  11. using CyberX8_RT.Devices.SRD;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Runtime.InteropServices;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace CyberX8_RT.Modules.SRD
  20. {
  21. internal class SRDRotationRoutine : ModuleRoutineBase, IRoutine
  22. {
  23. private enum RotationStep
  24. {
  25. Rotation,
  26. Wait,
  27. End
  28. }
  29. #region 常量
  30. /// <summary>
  31. /// ROTATION电机转速比例
  32. /// </summary>
  33. private const int SPEED_RATIO = 10;
  34. #endregion
  35. #region 内部变量
  36. /// <summary>
  37. /// 模块名称
  38. /// </summary>
  39. private string _module;
  40. /// <summary>
  41. /// Rotation Axis
  42. /// </summary>
  43. private JetAxisBase _rotationAxis;
  44. /// <summary>
  45. /// 转速
  46. /// </summary>
  47. private double _speed;
  48. /// <summary>
  49. /// 时间
  50. /// </summary>
  51. private double _time;
  52. /// <summary>
  53. /// 超时时间
  54. /// </summary>
  55. private int _timeOut = 6000;
  56. /// <summary>
  57. /// SRD rotation Provider对象
  58. /// </summary>
  59. private BeckhoffProviderAxis _rotationProviderAxis;
  60. /// <summary>
  61. /// SRD Common
  62. /// </summary>
  63. private SrdCommonDevice _srdCommon;
  64. #endregion
  65. #region 属性
  66. /// <summary>
  67. /// 当前状态机
  68. /// </summary>
  69. public string CurrentStateMachine
  70. {
  71. get { return Runner.CurrentStep.ToString(); }
  72. }
  73. #endregion
  74. public SRDRotationRoutine(ModuleName module, JetAxisBase rotationAxis) : base(module)
  75. {
  76. Name = "StartRotation";
  77. _module = module.ToString();
  78. _srdCommon = DEVICE.GetDevice<SrdCommonDevice>($"{module}.Common");
  79. }
  80. public void Abort()
  81. {
  82. Runner.Stop("Manual Abort");
  83. }
  84. public RState Monitor()
  85. {
  86. Runner.Run(RotationStep.Rotation, StartRotation, NullFun, _timeOut)
  87. .WaitWithStopCondition(RotationStep.Wait, CheckRotationStatus, CheckRotationRunStop)
  88. .End(RotationStep.End, NullFun);
  89. return Runner.Status;
  90. }
  91. /// <summary>
  92. /// 开始旋转
  93. /// </summary>
  94. /// <returns></returns>
  95. public bool StartRotation()
  96. {
  97. if (_srdCommon.CommonData.DoorOpened)
  98. {
  99. LOG.WriteLog(eEvent.ERR_SRD, _module, "Door is not closed. Can't do rotation");
  100. return false;
  101. }
  102. if (!_rotationAxis.IsSwitchOn)
  103. {
  104. LOG.WriteLog(eEvent.ERR_SRD, _module, $"{_module}.Rotation is switchoff");
  105. return false;
  106. }
  107. if (!_rotationAxis.IsHomed)
  108. {
  109. LOG.WriteLog(eEvent.ERR_SRD, _module, $"{_module}.Rotation is not homed");
  110. return false;
  111. }
  112. _rotationProviderAxis = BeckhoffAxisProviderManager.Instance.GetAxisProvider($"{_module}.Rotation");
  113. if (_rotationProviderAxis == null)
  114. {
  115. LOG.WriteLog(eEvent.ERR_SRD, _module, $"{_module}.Rotation Provider is not exist");
  116. return false;
  117. }
  118. double _scale = _rotationProviderAxis.ScaleFactor;
  119. //获取当前Position
  120. double currentPosition = _rotationAxis.MotionData.MotorPosition;
  121. //转成角速度(deg/s)
  122. double DPSspeed = BeckhoffVelocityUtil.ConvertVelocityToDegPerSecondByRPM((int)_speed);
  123. int rotationSpeed = (int)Math.Round(_scale * DPSspeed, 0);
  124. int targetPosition = (int)Math.Round(_scale * (_time * DPSspeed + currentPosition), 0);
  125. LOG.WriteLog(eEvent.INFO_SRD, _module, "Start Rotation");
  126. return _rotationAxis.ProfilePosition(targetPosition, rotationSpeed * SPEED_RATIO, 0, 0);
  127. }
  128. /// <summary>
  129. /// 检验移动状态
  130. /// </summary>
  131. /// <returns></returns>
  132. private bool CheckRotationStatus()
  133. {
  134. return _rotationAxis.Status == RState.End;
  135. }
  136. /// <summary>
  137. /// 检验是否还在运动
  138. /// </summary>
  139. /// <returns></returns>
  140. private bool CheckRotationRunStop()
  141. {
  142. return _rotationAxis.Status == RState.Failed;
  143. }
  144. public RState Start(params object[] objs)
  145. {
  146. _rotationAxis = (JetAxisBase)objs[0];
  147. _time = (double)objs[1];
  148. _speed = (double)objs[2];
  149. Runner.Start(Module, Name);
  150. return RState.Running;
  151. }
  152. }
  153. }