BoatMove.cs 15 KB

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