BoatMove.cs 15 KB

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