BoatMove.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. CheckSensor,
  35. SetBoatInterval,
  36. CheckPrepareMove,
  37. Loop,
  38. EndLoop,
  39. Delay1,
  40. Delay2,
  41. Delay3,
  42. ZMoveToStart,
  43. ZMoveToEnd,
  44. ZMove,
  45. AutoShutterOpen,
  46. AutoShutterClose,
  47. }
  48. private BoatModule _boatModule;
  49. private int _timeout = 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. _timeout = SC.GetValue<int>($"{Module}.MotionTimeout");
  87. _shutterTimeout = SC.GetValue<int>($"{Module}.AutoShutter.MotionTimeout");
  88. var isBeforeShutter = new List<string>() { "boatload", "boatcap2", "boatunload", "boatloaderhome", };
  89. if (!string.IsNullOrEmpty(_command) && isBeforeShutter.Contains(_command))
  90. {
  91. string reason = "";
  92. if (!_boatModule.CheckPrepareMove(out reason, false))
  93. {
  94. _boatModule.BoatZAxisMoveFailedForInterlock.Set(reason);
  95. //return Result.FAIL;
  96. }
  97. }
  98. Notify($"Start");
  99. return Result.RUN;
  100. }
  101. public void Abort()
  102. {
  103. if (_boatModule.CheckPrepareMove(out _, false))
  104. {
  105. _boatModule.ZAxisDevice.ServoStop();
  106. _boatModule.RAxisDevice.ServoStop();
  107. }
  108. }
  109. public override Result Monitor()
  110. {
  111. try
  112. {
  113. PauseRountine(_boatModule.RAxisDevice.IsPause);
  114. if (_boatModule.RAxisDevice.IsPause)
  115. return Result.RUN;
  116. if (_boatModule.RAxisDevice.IsError || _boatModule.ZAxisDevice.IsError)
  117. return Result.FAIL;
  118. switch (_command)
  119. {
  120. case "boatload":
  121. case "boatcap2":
  122. AutoShutterOpen((int)RoutineStep.AutoShutterOpen, true, _shutterTimeout);
  123. //CheckPrepareMove((int)RoutineStep.CheckPrepareMove, 2);
  124. var position = (BoatPosition)Enum.Parse(typeof(BoatPosition), _position);
  125. SetBoatZAxisMove((int)RoutineStep.ZMove, position, _speed, (int)_timeout);
  126. break;
  127. case "boatunload":
  128. case "boatloaderhome":
  129. AutoShutterOpen((int)RoutineStep.AutoShutterOpen, true, _shutterTimeout);
  130. //CheckPrepareMove((int)RoutineStep.CheckPrepareMove, 2);
  131. position = (BoatPosition)Enum.Parse(typeof(BoatPosition), _position);
  132. SetBoatZAxisMove((int)RoutineStep.ZMove, position, _speed, (int)_timeout);
  133. AutoShutterOpen((int)RoutineStep.AutoShutterClose, false, _shutterTimeout);
  134. break;
  135. case "boatrotate":
  136. var direction = (BoatRotationDirection)Enum.Parse(typeof(BoatRotationDirection), _position);
  137. SetBoatRAxisMove((int)RoutineStep.BoatRAxisMove, direction, _speed, _timeout);
  138. break;
  139. case "boatrotatestop"://r home
  140. SetBoatRAxisMoveStop((int)RoutineStep.SetBoatRAxisMoveStop);
  141. CheckBoatMoveSensor((int)RoutineStep.CheckSensor, _timeout);
  142. Delay((int)RoutineStep.Delay1, 2);
  143. SetBoatRAxisHome((int)RoutineStep.BoatRAxisHome, _timeout);
  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 CheckBoatMoveSensor(int id, int timeout)
  299. {
  300. var reason = "";
  301. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  302. {
  303. Notify($"Check boat move Sensor");
  304. return true;
  305. }, () =>
  306. {
  307. if (!_boatModule.RAxisDevice.IsMoving)
  308. {
  309. return true;
  310. }
  311. return false; ;
  312. }, timeout * 1000);
  313. if (ret.Item1)
  314. {
  315. if (ret.Item2 == Result.FAIL)
  316. {
  317. throw (new RoutineFaildException());
  318. }
  319. else if (ret.Item2 == Result.TIMEOUT) //timeout
  320. {
  321. throw (new RoutineFaildException());
  322. }
  323. else
  324. throw (new RoutineBreakException());
  325. }
  326. }
  327. private void SetBoatRAxisHome(int id, int timeout)
  328. {
  329. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  330. {
  331. Notify($"Boat RAxis home");
  332. if (!_boatModule.RAxisDevice.SetServoHome())
  333. {
  334. _boatModule.BoatRAxisHomeFailed.Set();
  335. }
  336. return true;
  337. }, () =>
  338. {
  339. if (_boatModule.RAxisDevice.IsError)
  340. return null;
  341. return _boatModule.RAxisDevice.IsHomeDone && _boatModule.RAxisDevice.IsReady;
  342. }, timeout * 1000);
  343. if (ret.Item1)
  344. {
  345. if (ret.Item2 == Result.FAIL)
  346. {
  347. _boatModule.RAxisDevice.ServoStop();
  348. throw (new RoutineFaildException());
  349. }
  350. else if (ret.Item2 == Result.TIMEOUT) //timeout
  351. {
  352. _boatModule.RAxisDevice.ServoStop();
  353. _boatModule.BoatRAxisHomeTimeout.Set($"can not complete in {timeout} seconds");
  354. throw (new RoutineFaildException());
  355. }
  356. else
  357. throw (new RoutineBreakException());
  358. }
  359. }
  360. private void SetBoatRAxisMove(int id, BoatRotationDirection direction, float speed, int timeout)
  361. {
  362. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  363. {
  364. Notify($"Boat RAxis {direction}");
  365. string reason;
  366. if (!_boatModule.RAxisDevice.SetServoMoveTo(direction.ToString(), out reason, speed))
  367. {
  368. //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason;
  369. _boatModule.BoatRAxisMoveFailedForInterlock.Set(reason);
  370. }
  371. return true;
  372. }, () =>
  373. {
  374. if (_boatModule.RAxisDevice.IsError)
  375. return null;
  376. return _boatModule.RAxisDevice.IsMoving;
  377. }, timeout * 2 * 1000);
  378. if (ret.Item1)
  379. {
  380. if (ret.Item2 == Result.FAIL)
  381. {
  382. _boatModule.RAxisDevice.ServoStop();
  383. throw (new RoutineFaildException());
  384. }
  385. else if (ret.Item2 == Result.TIMEOUT) //timeout
  386. {
  387. _boatModule.RAxisDevice.ServoStop();
  388. _boatModule.BoatRAxisMoveTimeOut.Set($"can not complete in {timeout} seconds");
  389. throw (new RoutineFaildException());
  390. }
  391. else
  392. throw (new RoutineBreakException());
  393. }
  394. }
  395. }
  396. }