BoatMoveCycleTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. using FurnaceRT.Devices;
  22. namespace FurnaceRT.Equipments.Boats
  23. {
  24. public class BoatMoveCycleTest : ModuleRoutine, IRoutine
  25. {
  26. enum RoutineStep
  27. {
  28. SetBoatSpeed,
  29. SetBoatDirection,
  30. BoatRAxisHome,
  31. BoatRAxisMove,
  32. SetBoatRAxisMoveStop,
  33. SetBoatInterval,
  34. CheckPrepareMove,
  35. AutoShutterOpen,
  36. Loop,
  37. EndLoop,
  38. Delay1,
  39. Delay2,
  40. Delay3,
  41. ZMoveToStart,
  42. ZMoveToEnd,
  43. }
  44. private BoatRotationMode _targetMode;
  45. private float _interval;
  46. private float _rotateTime;
  47. private BoatModule _boatModule;
  48. private int _timeout = 0;
  49. private int _shutterTimeout = 0;
  50. private bool _isRotate;
  51. private BoatRotationDirection _direction;
  52. private Stopwatch _timer = new Stopwatch();
  53. private int _count;
  54. private BoatPosition _positionStart;
  55. private BoatPosition _positionEnd;
  56. public BoatMoveCycleTest(BoatModule boatModule)
  57. {
  58. _boatModule = boatModule;
  59. Module = boatModule.Module;
  60. Name = "MoveTest";
  61. }
  62. public void Init(string positionStart = "HomePosition", string positionEnd = "CapPosition", string direction = "CW")
  63. {
  64. _positionStart = (BoatPosition)Enum.Parse(typeof(BoatPosition), positionStart);
  65. _positionEnd = (BoatPosition)Enum.Parse(typeof(BoatPosition), positionEnd);
  66. _direction = (BoatRotationDirection)Enum.Parse(typeof(BoatRotationDirection), direction);
  67. }
  68. public Result Start(params object[] objs)
  69. {
  70. Reset();
  71. _isRotate = _targetMode != BoatRotationMode.Stop;
  72. _timeout = SC.GetValue<int>($"{Module}.MotionTimeout");
  73. _count = SC.GetValue<int>($"Boat.CycleCount");
  74. _interval = (float)SC.GetValue<double>($"Boat.IntervalTime");
  75. _rotateTime = (float)SC.GetValue<double>($"Boat.BoatRotationServo.RotateTime");
  76. _shutterTimeout = SC.GetValue<int>($"{Module}.AutoShutter.MotionTimeout");
  77. string reason = string.Empty;
  78. if (!_boatModule.CheckPrepareMove(out reason, false))
  79. {
  80. _boatModule.BoatZAxisMoveFailedForInterlock.Set(reason);
  81. return Result.FAIL;
  82. }
  83. Notify($"Start");
  84. return Result.RUN;
  85. }
  86. public void Abort()
  87. {
  88. _boatModule.ZAxisDevice.ServoStop();
  89. _boatModule.RAxisDevice.ServoStop();
  90. }
  91. public override Result Monitor()
  92. {
  93. try
  94. {
  95. PauseRountine(_boatModule.RAxisDevice.IsPause);
  96. if (_boatModule.RAxisDevice.IsPause)
  97. return Result.RUN;
  98. AutoShutterOpen((int)RoutineStep.AutoShutterOpen, true, 30);
  99. SetBoatRAxisMove((int)RoutineStep.BoatRAxisMove, _direction, (int)_rotateTime);
  100. Loop((int)RoutineStep.Loop, _count);
  101. CheckPrepareMove((int)RoutineStep.CheckPrepareMove, 2);
  102. SetBoatZAxisMoveStart((int)RoutineStep.ZMoveToStart, _positionStart, (int)_timeout);
  103. //SetBoatRAxisMoveStop((int)RoutineStep.SetBoatRAxisMoveStop);
  104. Delay((int)RoutineStep.Delay1, 2);
  105. Delay((int)RoutineStep.Delay2, _interval);
  106. //SetBoatRAxisHome((int)RoutineStep.BoatRAxisHome, _timeout);
  107. SetBoatZAxisMoveEnd((int)RoutineStep.ZMoveToEnd, _positionEnd, (int)_timeout);
  108. Delay((int)RoutineStep.Delay3, _interval);
  109. EndLoop((int)RoutineStep.EndLoop);
  110. }
  111. catch (RoutineBreakException)
  112. {
  113. return Result.RUN;
  114. }
  115. catch (RoutineFaildException ex)
  116. {
  117. _boatModule.RAxisDevice.ServoStop();
  118. return Result.FAIL;
  119. }
  120. _boatModule.RAxisDevice.ServoStop();
  121. Notify("Finished");
  122. return Result.DONE;
  123. }
  124. private void AutoShutterOpen(int id, bool isOpen, int timeout)
  125. {
  126. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  127. {
  128. Notify($"shutter {(isOpen ? "open" : "close")}");
  129. string reason;
  130. if (!_boatModule.ShutterDevice.SetOpen(isOpen, out reason))
  131. {
  132. _boatModule.ShutterDevice.AutoShutterMoveFailedForInterlock.Set();
  133. return false;
  134. }
  135. return true;
  136. }, () =>
  137. {
  138. return isOpen ? _boatModule.ShutterDevice.OpenCloseStatus == DeviceStatus.Open : _boatModule.ShutterDevice.OpenCloseStatus == DeviceStatus.Close;
  139. }, timeout * 1000);
  140. if (ret.Item1)
  141. {
  142. if (ret.Item2 == Result.FAIL)
  143. {
  144. throw (new RoutineFaildException());
  145. }
  146. else if (ret.Item2 == Result.TIMEOUT) //timeout
  147. {
  148. if (isOpen)
  149. {
  150. _boatModule.ShutterDevice.AutoShutterOpenTimeOut.Set($"can not complete in {timeout} seconds");
  151. }
  152. else
  153. {
  154. _boatModule.ShutterDevice.AutoShutterCloseTimeOut.Set($"can not complete in {timeout} seconds");
  155. }
  156. throw (new RoutineFaildException());
  157. }
  158. else
  159. throw (new RoutineBreakException());
  160. }
  161. }
  162. private void CheckPrepareMove(int id, int timeout)
  163. {
  164. var reason = "";
  165. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  166. {
  167. Notify($"Check boat move enable");
  168. return true;
  169. }, () =>
  170. {
  171. return _boatModule.CheckPrepareMove(out reason);
  172. }, timeout * 2 * 1000);
  173. if (ret.Item1)
  174. {
  175. if (ret.Item2 == Result.FAIL)
  176. {
  177. throw (new RoutineFaildException());
  178. }
  179. else if (ret.Item2 == Result.TIMEOUT) //timeout
  180. {
  181. _boatModule.BoatZAxisMoveFailedForInterlock.Set($"{reason} is not OK, can not complete in {timeout} seconds");
  182. throw (new RoutineFaildException());
  183. }
  184. else
  185. throw (new RoutineBreakException());
  186. }
  187. }
  188. private void SetBoatZAxisMoveStart(int id, BoatPosition position, int timeout)
  189. {
  190. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  191. {
  192. Notify($"Boat ZAxis movet to {position}");
  193. string reason;
  194. if (!_boatModule.ZAxisDevice.SetServoMoveTo(position.ToString(), out reason))
  195. {
  196. //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason;
  197. _boatModule.BoatZAxisMoveFailedForInterlock.Set(reason);
  198. }
  199. return true;
  200. }, () =>
  201. {
  202. if (_boatModule.ZAxisDevice.IsError)
  203. return null;
  204. return _boatModule.ZAxisDevice.CheckServoAtPosition(position.ToString());
  205. }, timeout * 2 * 1000);
  206. if (ret.Item1)
  207. {
  208. if (ret.Item2 == Result.FAIL)
  209. {
  210. _boatModule.ZAxisDevice.ServoStop();
  211. throw (new RoutineFaildException());
  212. }
  213. else if (ret.Item2 == Result.TIMEOUT) //timeout
  214. {
  215. _boatModule.ZAxisDevice.ServoStop();
  216. _boatModule.BoatZAxisMoveTimeOut.Set($"can not complete in {timeout} seconds");
  217. throw (new RoutineFaildException());
  218. }
  219. else
  220. throw (new RoutineBreakException());
  221. }
  222. }
  223. private void SetBoatZAxisMoveEnd(int id, BoatPosition position, int timeout)
  224. {
  225. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  226. {
  227. Notify($"Boat ZAxis movet to {position}");
  228. string reason;
  229. if (!_boatModule.ZAxisDevice.SetServoMoveTo(position.ToString(), out reason))
  230. {
  231. //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason;
  232. _boatModule.BoatZAxisMoveFailedForInterlock.Set(reason);
  233. }
  234. return true;
  235. }, () =>
  236. {
  237. if (_boatModule.ZAxisDevice.IsError)
  238. return null;
  239. return _boatModule.ZAxisDevice.CheckServoAtPosition(position.ToString());
  240. }, timeout * 2 * 1000);
  241. if (ret.Item1)
  242. {
  243. if (ret.Item2 == Result.FAIL)
  244. {
  245. _boatModule.ZAxisDevice.ServoStop();
  246. throw (new RoutineFaildException());
  247. }
  248. else if (ret.Item2 == Result.TIMEOUT) //timeout
  249. {
  250. _boatModule.ZAxisDevice.ServoStop();
  251. _boatModule.BoatZAxisMoveTimeOut.Set($"can not complete in {timeout} seconds");
  252. throw (new RoutineFaildException());
  253. }
  254. else
  255. throw (new RoutineBreakException());
  256. }
  257. }
  258. private void SetBoatRAxisMoveStop(int id)
  259. {
  260. Tuple<bool, Result> ret = Execute(id, () =>
  261. {
  262. Notify($"Set RAxis boat stop");
  263. _boatModule.RAxisDevice.ServoStop();
  264. return true;
  265. });
  266. if (ret.Item1)
  267. {
  268. if (ret.Item2 == Result.FAIL)
  269. {
  270. throw (new RoutineFaildException());
  271. }
  272. else
  273. throw (new RoutineBreakException());
  274. }
  275. }
  276. private void SetBoatRAxisHome(int id, int timeout)
  277. {
  278. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  279. {
  280. Notify($"Boat RAxis home");
  281. if (!_boatModule.RAxisDevice.SetServoHome())
  282. {
  283. _boatModule.BoatRAxisHomeFailed.Set();
  284. }
  285. return true;
  286. }, () =>
  287. {
  288. if (_boatModule.RAxisDevice.IsError)
  289. return null;
  290. return true;
  291. }, timeout * 1000);
  292. if (ret.Item1)
  293. {
  294. if (ret.Item2 == Result.FAIL)
  295. {
  296. _boatModule.RAxisDevice.ServoStop();
  297. throw (new RoutineFaildException());
  298. }
  299. else if (ret.Item2 == Result.TIMEOUT) //timeout
  300. {
  301. _boatModule.RAxisDevice.ServoStop();
  302. _boatModule.BoatRAxisHomeTimeout.Set($"can not complete in {timeout} seconds");
  303. throw (new RoutineFaildException());
  304. }
  305. else
  306. throw (new RoutineBreakException());
  307. }
  308. }
  309. private void SetBoatRAxisMove(int id, BoatRotationDirection direction, int timeout)
  310. {
  311. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  312. {
  313. Notify($"Boat RAxis {direction}");
  314. _timer.Restart();
  315. string reason;
  316. if (!_boatModule.RAxisDevice.SetServoMoveTo(direction.ToString(), out reason))
  317. {
  318. //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason;
  319. _boatModule.BoatRAxisMoveFailedForInterlock.Set(reason);
  320. }
  321. return true;
  322. }, () =>
  323. {
  324. if (_boatModule.RAxisDevice.IsError)
  325. return null;
  326. return _boatModule.RAxisDevice.IsMoving;
  327. }, timeout * 2 * 1000);
  328. if (ret.Item1)
  329. {
  330. if (ret.Item2 == Result.FAIL)
  331. {
  332. _boatModule.RAxisDevice.ServoStop();
  333. throw (new RoutineFaildException());
  334. }
  335. else if (ret.Item2 == Result.TIMEOUT) //timeout
  336. {
  337. _boatModule.RAxisDevice.ServoStop();
  338. _boatModule.BoatRAxisMoveTimeOut.Set($"can not complete in {timeout} seconds");
  339. throw (new RoutineFaildException());
  340. }
  341. else
  342. throw (new RoutineBreakException());
  343. }
  344. }
  345. }
  346. }