GalilProfilePositionRoutine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.Routine;
  3. using MECF.Framework.Common.CommonData.PUF;
  4. using MECF.Framework.Common.Routine;
  5. using MECF.Framework.Common.TwinCat;
  6. using CyberX8_Core;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Diagnostics;
  13. namespace CyberX8_RT.Devices.AXIS.Galil
  14. {
  15. public class GalilProfilePositionRoutine : RoutineBase, IRoutine
  16. {
  17. private enum ProfilePositionStep
  18. {
  19. WriteAcceleration,
  20. WriteDeceleration,
  21. WriteSpeed,
  22. WriteTargetPosition,
  23. StartMotion,
  24. Delay,
  25. CheckRunEnd,
  26. End
  27. }
  28. #region 常量
  29. private const string PROFILE_VELOCITY = "ProfileVelocity";
  30. private const string PROFILE_ACCEL = "ProfileAccel";
  31. private const string PROFILE_DECEL = "ProfileDecel";
  32. private const string TARGET_POSITION = "TargetPosition";
  33. private const int MOTION_STOP_CODE = 1;
  34. #endregion
  35. #region 内部变量
  36. private JetAxisBase _axis;
  37. private int _profileVelocity = 0;
  38. private int _profileAcceleration = 0;
  39. private int _profileDeceleration = 0;
  40. private int _targetPosition;
  41. private bool _judgeTorqueLimit = false;
  42. private Stopwatch _stopWatch = new Stopwatch();
  43. #endregion
  44. public GalilProfilePositionRoutine(string module,JetAxisBase axis) : base(module)
  45. {
  46. _axis = axis;
  47. }
  48. public void Abort()
  49. {
  50. Runner.Stop("Manual Abort");
  51. }
  52. public RState Monitor()
  53. {
  54. Runner.Run(ProfilePositionStep.WriteAcceleration, () => { return _axis.WriteAcceleration(_profileAcceleration); },_delay_1ms)
  55. .Run(ProfilePositionStep.WriteDeceleration, () => { return _axis.WriteDeceleration(_profileDeceleration); }, _delay_1ms)
  56. .Run(ProfilePositionStep.WriteSpeed, () => { return _axis.WriteSpeed(_profileVelocity); }, _delay_1ms)
  57. .Run(ProfilePositionStep.WriteTargetPosition, () => { return _axis.WriteAbsolutePosition(_targetPosition); }, _delay_1ms)
  58. .Run(ProfilePositionStep.StartMotion, () =>_axis.WriteStartMotion(), _delay_1ms)
  59. .Delay(ProfilePositionStep.Delay,500)
  60. .WaitWithStopCondition(ProfilePositionStep.CheckRunEnd,CheckRun,CheckRunStop)
  61. .End(ProfilePositionStep.End,NullFun,100);
  62. return Runner.Status;
  63. }
  64. /// <summary>
  65. /// 检验是否正常运行
  66. /// </summary>
  67. /// <returns></returns>
  68. private bool CheckRun()
  69. {
  70. bool isInTargetPosition = _axis.JudgeCurrentPositionIsInTargetPosition(_targetPosition);
  71. if (_judgeTorqueLimit)
  72. {
  73. if(!_axis.IsRun&& isInTargetPosition)
  74. {
  75. return true;
  76. }
  77. return false;
  78. }
  79. else
  80. {
  81. bool isTorqueOver = TorqueIsOverLimit();
  82. //过载同时掉电
  83. if (isTorqueOver && !_axis.IsSwitchOn)
  84. {
  85. return true;
  86. }
  87. if(!_axis.IsRun&&isInTargetPosition)
  88. {
  89. return true;
  90. }
  91. return false;
  92. }
  93. }
  94. /// <summary>
  95. /// Torque是否过载
  96. /// </summary>
  97. /// <returns></returns>
  98. private bool TorqueIsOverLimit()
  99. {
  100. return !_axis.MotionData.TorqueLimited;
  101. }
  102. /// <summary>
  103. /// 检验是否停止
  104. /// </summary>
  105. /// <returns></returns>
  106. private bool CheckRunStop()
  107. {
  108. //没有运动
  109. bool isStop = !_axis.IsRun&&!_axis.JudgeCurrentPositionIsInTargetPosition(_targetPosition);
  110. if (isStop&&_judgeTorqueLimit)
  111. {
  112. ErrorMsg = "position is stop";
  113. return true;
  114. }
  115. if (_judgeTorqueLimit)
  116. {
  117. if (TorqueIsOverLimit())
  118. {
  119. ErrorMsg = "Torque is over limit";
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. public RState Start(params object[] objs)
  126. {
  127. _stopWatch.Reset();
  128. _targetPosition = (int)objs[0];
  129. _profileVelocity = (int)objs[1];
  130. _profileAcceleration = (int)objs[2];
  131. _profileDeceleration = (int)objs[3];
  132. if(objs.Length>=5)
  133. {
  134. _judgeTorqueLimit = (bool)objs[4];
  135. }
  136. return Runner.Start(Module, "ProfilePosition");
  137. }
  138. }
  139. }