SRDRotationRoutine.cs 4.9 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 (!_rotationAxis.IsSwitchOn)
  98. {
  99. LOG.WriteLog(eEvent.ERR_SRD, _module, $"{_module}.Rotation is switchoff");
  100. return false;
  101. }
  102. if (!_rotationAxis.IsHomed)
  103. {
  104. LOG.WriteLog(eEvent.ERR_SRD, _module, $"{_module}.Rotation is not homed");
  105. return false;
  106. }
  107. _rotationProviderAxis = BeckhoffAxisProviderManager.Instance.GetAxisProvider($"{_module}.Rotation");
  108. if (_rotationProviderAxis == null)
  109. {
  110. LOG.WriteLog(eEvent.ERR_SRD, _module, $"{_module}.Rotation Provider is not exist");
  111. return false;
  112. }
  113. if (!_srdCommon.RotationInterLock())
  114. {
  115. return false;
  116. }
  117. double _scale = _rotationProviderAxis.ScaleFactor;
  118. //获取当前Position
  119. double currentPosition = _rotationAxis.MotionData.MotorPosition;
  120. //转成角速度(deg/s)
  121. double DPSspeed = BeckhoffVelocityUtil.ConvertVelocityToDegPerSecondByRPM((int)_speed);
  122. int rotationSpeed = (int)Math.Round(_scale * DPSspeed, 0);
  123. int targetPosition = (int)Math.Round(_scale * (_time * DPSspeed + currentPosition), 0);
  124. LOG.WriteLog(eEvent.INFO_SRD, _module, "Start Rotation");
  125. return _rotationAxis.ProfilePosition(targetPosition, rotationSpeed * SPEED_RATIO, 0, 0);
  126. }
  127. /// <summary>
  128. /// 检验移动状态
  129. /// </summary>
  130. /// <returns></returns>
  131. private bool CheckRotationStatus()
  132. {
  133. return _rotationAxis.Status == RState.End;
  134. }
  135. /// <summary>
  136. /// 检验是否还在运动
  137. /// </summary>
  138. /// <returns></returns>
  139. private bool CheckRotationRunStop()
  140. {
  141. return _rotationAxis.Status == RState.Failed;
  142. }
  143. public RState Start(params object[] objs)
  144. {
  145. _rotationAxis = (JetAxisBase)objs[0];
  146. _time = (double)objs[1];
  147. _speed = (double)objs[2];
  148. Runner.Start(Module, Name);
  149. return RState.Running;
  150. }
  151. }
  152. }