BoatMove.cs 15 KB

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