BoatRAxisMoveCycleTest.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Device.Unit;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Routine;
  5. using Aitex.Core.RT.SCCore;
  6. using Aitex.Core.Util;
  7. using Aitex.Sorter.Common;
  8. using MECF.Framework.Common.Alarms;
  9. using MECF.Framework.Common.Device.Bases;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.SubstrateTrackings;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using FurnaceRT.Equipments.Systems;
  19. using static Aitex.Core.RT.Device.Unit.IoBoat;
  20. using System.Diagnostics;
  21. namespace FurnaceRT.Equipments.Boats
  22. {
  23. public class BoatRAxisMoveCycleTest : ModuleRoutine, IRoutine
  24. {
  25. enum RoutineStep
  26. {
  27. SetBoatSpeed,
  28. SetBoatDirection,
  29. BoatRAxisHome,
  30. BoatRAxisMove,
  31. SetBoatRAxisMoveStop,
  32. SetBoatInterval,
  33. Loop,
  34. EndLoop,
  35. Delay1,
  36. Delay2,
  37. Delay3,
  38. }
  39. private BoatRotationMode _targetMode;
  40. private float _speed;
  41. private float _interval;
  42. private float _rotateTime;
  43. private BoatModule _boatModule;
  44. private int _timeout = 0;
  45. private bool _isRotate;
  46. private BoatRotationDirection _direction;
  47. private Stopwatch _timer = new Stopwatch();
  48. private int _count;
  49. public BoatRAxisMoveCycleTest(BoatModule boatModule)
  50. {
  51. _boatModule = boatModule;
  52. Module = boatModule.Module;
  53. Name = "RAxisMoveTest";
  54. }
  55. public void Init(string direction = "CW")
  56. {
  57. _direction = (BoatRotationDirection)Enum.Parse(typeof(BoatRotationDirection), direction);
  58. }
  59. public Result Start(params object[] objs)
  60. {
  61. Reset();
  62. _isRotate = _targetMode != BoatRotationMode.Stop;
  63. _timeout = SC.GetValue<int>($"{Module}.MotionTimeout");
  64. _count = SC.GetValue<int>($"Boat.BoatRotationServo.CycleCount");
  65. _speed = (float)SC.GetValue<double>($"Boat.BoatRotationServo.MoveSpeed");
  66. _interval = (float)SC.GetValue<double>($"Boat.BoatRotationServo.IntervalTime");
  67. _rotateTime = (float)SC.GetValue<double>($"Boat.BoatRotationServo.RotateTime");
  68. Notify($"Start");
  69. return Result.RUN;
  70. }
  71. public void Abort()
  72. {
  73. _boatModule.RAxisDevice.ServoStop();
  74. }
  75. public override Result Monitor()
  76. {
  77. try
  78. {
  79. PauseRountine(_boatModule.RAxisDevice.IsPause);
  80. if (_boatModule.RAxisDevice.IsPause)
  81. return Result.RUN;
  82. if (_boatModule.RAxisDevice.IsError)
  83. return Result.FAIL;
  84. Loop((int)RoutineStep.Loop, _count);
  85. SetBoatRAxisMove((int)RoutineStep.BoatRAxisMove, _direction, (int)_rotateTime);
  86. SetBoatRAxisMoveStop((int)RoutineStep.SetBoatRAxisMoveStop);
  87. Delay((int)RoutineStep.Delay1, 2);
  88. Delay((int)RoutineStep.Delay2, _interval);
  89. SetBoatRAxisHome((int)RoutineStep.BoatRAxisHome, _timeout);
  90. Delay((int)RoutineStep.Delay3, _interval);
  91. EndLoop((int)RoutineStep.EndLoop);
  92. }
  93. catch (RoutineBreakException)
  94. {
  95. return Result.RUN;
  96. }
  97. catch (RoutineFaildException ex)
  98. {
  99. return Result.FAIL;
  100. }
  101. Notify("Finished");
  102. return Result.DONE;
  103. }
  104. private void SetBoatRAxisMoveStop(int id)
  105. {
  106. Tuple<bool, Result> ret = Execute(id, () =>
  107. {
  108. Notify($"Set RAxis boat stop");
  109. _boatModule.RAxisDevice.ServoStop();
  110. return true;
  111. });
  112. if (ret.Item1)
  113. {
  114. if (ret.Item2 == Result.FAIL)
  115. {
  116. throw (new RoutineFaildException());
  117. }
  118. else
  119. throw (new RoutineBreakException());
  120. }
  121. }
  122. private void SetBoatRAxisHome(int id, int timeout)
  123. {
  124. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  125. {
  126. Notify($"Boat RAxis home");
  127. if (!_boatModule.RAxisDevice.SetServoHome())
  128. {
  129. _boatModule.BoatRAxisHomeFailed.Set();
  130. }
  131. return true;
  132. }, () =>
  133. {
  134. if (_boatModule.RAxisDevice.IsError)
  135. return null;
  136. return _boatModule.RAxisDevice.IsHomeDone && _boatModule.RAxisDevice.IsReady;
  137. }, timeout * 1000);
  138. if (ret.Item1)
  139. {
  140. if (ret.Item2 == Result.FAIL)
  141. {
  142. _boatModule.RAxisDevice.ServoStop();
  143. throw (new RoutineFaildException());
  144. }
  145. else if (ret.Item2 == Result.TIMEOUT) //timeout
  146. {
  147. _boatModule.RAxisDevice.ServoStop();
  148. _boatModule.BoatRAxisHomeTimeout.Set($"can not complete in {timeout} seconds");
  149. throw (new RoutineFaildException());
  150. }
  151. else
  152. throw (new RoutineBreakException());
  153. }
  154. }
  155. private void SetBoatRAxisMove(int id, BoatRotationDirection direction, int timeout)
  156. {
  157. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  158. {
  159. Notify($"Boat RAxis {direction}");
  160. _timer.Restart();
  161. string reason;
  162. if (!_boatModule.RAxisDevice.SetServoMoveTo(direction.ToString(), out reason))
  163. {
  164. //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason;
  165. _boatModule.BoatRAxisMoveFailedForInterlock.Set(reason);
  166. }
  167. return true;
  168. }, () =>
  169. {
  170. if (_boatModule.RAxisDevice.IsError)
  171. return null;
  172. return _boatModule.RAxisDevice.IsMoving && _timer.ElapsedMilliseconds > timeout * 1000;
  173. }, timeout * 2 * 1000);
  174. if (ret.Item1)
  175. {
  176. if (ret.Item2 == Result.FAIL)
  177. {
  178. _boatModule.RAxisDevice.ServoStop();
  179. throw (new RoutineFaildException());
  180. }
  181. else if (ret.Item2 == Result.TIMEOUT) //timeout
  182. {
  183. _boatModule.RAxisDevice.ServoStop();
  184. _boatModule.BoatRAxisMoveTimeOut.Set($"can not complete in {timeout} seconds");
  185. throw (new RoutineFaildException());
  186. }
  187. else
  188. throw (new RoutineBreakException());
  189. }
  190. }
  191. }
  192. }