BoatHome.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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.Sorter.Common;
  7. using MECF.Framework.Common.Device.Bases;
  8. using MECF.Framework.Common.Equipment;
  9. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using FurnaceRT.Equipments.Systems;
  16. using static Aitex.Core.RT.Device.Unit.IoAutoShutter;
  17. using FurnaceRT.Devices;
  18. using static Aitex.Core.RT.Device.Unit.IoBoat;
  19. namespace FurnaceRT.Equipments.Boats
  20. {
  21. public class BoatHome : ModuleRoutine, IRoutine
  22. {
  23. enum RoutineStep
  24. {
  25. AutoShutterOpen,
  26. AutoShutterClose,
  27. CheckPrepareMove,
  28. SetReset,
  29. SetServoOn,
  30. BoatZAxisHome,
  31. BoatRAxisHome,
  32. Delay1,
  33. SetBoatRAxisMoveStop,
  34. }
  35. private BoatModule _boatModule;
  36. private int _zAxisTimeout = 0;
  37. private int _rAxisTimeout = 0;
  38. private int _shutterTimeout = 0;
  39. public BoatHome(BoatModule boatModule)
  40. {
  41. _boatModule = boatModule;
  42. Module = boatModule.Module;
  43. Name = "Boat Home";
  44. }
  45. public Result Start(params object[] objs)
  46. {
  47. Reset();
  48. _zAxisTimeout = SC.GetValue<int>($"{Module}.BoatElevatorServo.MotionTimeout");
  49. _rAxisTimeout = SC.GetValue<int>($"{Module}.BoatRotationServo.MotionTimeout");
  50. _shutterTimeout = SC.GetValue<int>($"{Module}.AutoShutter.MotionTimeout");
  51. _boatModule.AutoShutterMoveFailedForInterlock.RetryMessage = (int)BoatModule.MSG.Home;
  52. _boatModule.AutoShutterOpenTimeOut.RetryMessage = (int)BoatModule.MSG.Home;
  53. _boatModule.AutoShutterCloseTimeOut.RetryMessage = (int)BoatModule.MSG.Home;
  54. _boatModule.BoatZAxisHomeFailed.RetryMessage = (int)BoatModule.MSG.Home;
  55. _boatModule.BoatZAxisHomeTimeout.RetryMessage = (int)BoatModule.MSG.Home;
  56. _boatModule.BoatRAxisHomeFailed.RetryMessage = (int)BoatModule.MSG.Home;
  57. _boatModule.BoatRAxisHomeTimeout.RetryMessage = (int)BoatModule.MSG.Home;
  58. Notify($"Start");
  59. return Result.RUN;
  60. }
  61. public void Abort()
  62. {
  63. _boatModule.BoatZAxisStop();
  64. _boatModule.BoatRAxisStop();
  65. }
  66. public override Result Monitor()
  67. {
  68. try
  69. {
  70. PauseRountine(_boatModule.RAxisDevice.IsPause);
  71. if (_boatModule.RAxisDevice.IsPause)
  72. return Result.RUN;
  73. AutoShutterOpen((int)RoutineStep.AutoShutterOpen, true, _shutterTimeout);
  74. CheckPrepareMove((int)RoutineStep.CheckPrepareMove, 2);
  75. SetBoatZAxisMove((int)RoutineStep.BoatZAxisHome, BoatPosition.HomePosition, (int)_zAxisTimeout);
  76. SetBoatRAxisMoveStop((int)RoutineStep.SetBoatRAxisMoveStop);
  77. Delay((int)RoutineStep.Delay1, 2);
  78. SetBoatRAxisHome((int)RoutineStep.BoatRAxisHome, _rAxisTimeout);
  79. AutoShutterOpen((int)RoutineStep.AutoShutterClose, false, _shutterTimeout);
  80. }
  81. catch (RoutineBreakException)
  82. {
  83. return Result.RUN;
  84. }
  85. catch (RoutineFaildException ex)
  86. {
  87. return Result.FAIL;
  88. }
  89. Notify("Finished");
  90. return Result.DONE;
  91. }
  92. private void SetBoatRAxisMoveStop(int id)
  93. {
  94. Tuple<bool, Result> ret = Execute(id, () =>
  95. {
  96. Notify($"Set RAxis boat stop");
  97. _boatModule.RAxisDevice.ServoStop();
  98. return true;
  99. });
  100. if (ret.Item1)
  101. {
  102. if (ret.Item2 == Result.FAIL)
  103. {
  104. throw (new RoutineFaildException());
  105. }
  106. else
  107. throw (new RoutineBreakException());
  108. }
  109. }
  110. private void SetBoatRAxisHome(int id, int timeout)
  111. {
  112. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  113. {
  114. Notify($"Boat RAxis home");
  115. if (!_boatModule.RAxisDevice.SetServoHome())
  116. {
  117. _boatModule.BoatRAxisHomeFailed.Set();
  118. }
  119. return true;
  120. }, () =>
  121. {
  122. if (_boatModule.RAxisDevice.IsError)
  123. return null;
  124. return _boatModule.RAxisDevice.IsHomeDone&& _boatModule.RAxisDevice.IsMoving && _boatModule.RAxisDevice.IsReady;
  125. }, timeout * 1000);
  126. if (ret.Item1)
  127. {
  128. if (ret.Item2 == Result.FAIL)
  129. {
  130. _boatModule.RAxisDevice.ServoStop();
  131. throw (new RoutineFaildException());
  132. }
  133. else if (ret.Item2 == Result.TIMEOUT) //timeout
  134. {
  135. _boatModule.RAxisDevice.ServoStop();
  136. _boatModule.BoatRAxisHomeTimeout.Set($"can not complete in {timeout} seconds");
  137. throw (new RoutineFaildException());
  138. }
  139. else
  140. throw (new RoutineBreakException());
  141. }
  142. }
  143. private void CheckPrepareMove(int id, int timeout)
  144. {
  145. var reason = "";
  146. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  147. {
  148. Notify($"Check boat move enable");
  149. return true;
  150. }, () =>
  151. {
  152. return _boatModule.CheckPrepareMove(out reason);
  153. }, timeout * 2 * 1000);
  154. if (ret.Item1)
  155. {
  156. if (ret.Item2 == Result.FAIL)
  157. {
  158. throw (new RoutineFaildException());
  159. }
  160. else if (ret.Item2 == Result.TIMEOUT) //timeout
  161. {
  162. _boatModule.BoatZAxisMoveFailedForInterlock.Set($"{reason} is not OK, can not complete in {timeout} seconds");
  163. throw (new RoutineFaildException());
  164. }
  165. else
  166. throw (new RoutineBreakException());
  167. }
  168. }
  169. private void SetBoatZAxisMove(int id, BoatPosition position, int timeout)
  170. {
  171. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  172. {
  173. Notify($"Boat ZAxis movet to {position}");
  174. string reason;
  175. if (!_boatModule.ZAxisDevice.SetServoMoveTo(position.ToString(), out reason))
  176. {
  177. //_boatModule.BoatDevice.BoatRAxisMoveFailedForInterlock.Description = reason;
  178. _boatModule.BoatZAxisMoveFailedForInterlock.Set(reason);
  179. }
  180. return true;
  181. }, () =>
  182. {
  183. if (_boatModule.ZAxisDevice.IsError)
  184. return null;
  185. return _boatModule.ZAxisDevice.CheckServoAtPosition(position.ToString());
  186. }, timeout * 2 * 1000);
  187. if (ret.Item1)
  188. {
  189. if (ret.Item2 == Result.FAIL)
  190. {
  191. _boatModule.ZAxisDevice.ServoStop();
  192. throw (new RoutineFaildException());
  193. }
  194. else if (ret.Item2 == Result.TIMEOUT) //timeout
  195. {
  196. _boatModule.ZAxisDevice.ServoStop();
  197. _boatModule.BoatZAxisMoveTimeOut.Set($"can not complete in {timeout} seconds");
  198. throw (new RoutineFaildException());
  199. }
  200. else
  201. throw (new RoutineBreakException());
  202. }
  203. }
  204. private void AutoShutterOpen(int id, bool isOpen, int timeout)
  205. {
  206. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  207. {
  208. Notify($"shutter {(isOpen ? "open" : "close")}");
  209. string reason;
  210. if (!_boatModule.ShutterDevice.SetOpen(isOpen, out reason))
  211. {
  212. _boatModule.ShutterDevice.AutoShutterMoveFailedForInterlock.Set();
  213. return false;
  214. }
  215. return true;
  216. }, () =>
  217. {
  218. return isOpen ? _boatModule.ShutterDevice.OpenCloseStatus == DeviceStatus.Open : _boatModule.ShutterDevice.OpenCloseStatus == DeviceStatus.Close;
  219. }, timeout * 1000);
  220. if (ret.Item1)
  221. {
  222. if (ret.Item2 == Result.FAIL)
  223. {
  224. throw (new RoutineFaildException());
  225. }
  226. else if (ret.Item2 == Result.TIMEOUT) //timeout
  227. {
  228. if (isOpen)
  229. {
  230. _boatModule.ShutterDevice.AutoShutterOpenTimeOut.Set($"can not complete in {timeout} seconds");
  231. }
  232. else
  233. {
  234. _boatModule.ShutterDevice.AutoShutterCloseTimeOut.Set($"can not complete in {timeout} seconds");
  235. }
  236. throw (new RoutineFaildException());
  237. }
  238. else
  239. throw (new RoutineBreakException());
  240. }
  241. }
  242. //private void AutoShutterOpen(int id, bool isOpen, int timeout)
  243. //{
  244. // Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  245. // {
  246. // Notify($"Auto shutter {(isOpen ? "open" : "close")}");
  247. // string reason;
  248. // if (!_boatModule.AutoShutterDevice.SetOpen(isOpen, out reason))
  249. // {
  250. // _boatModule.AutoShutterMoveFailedForInterlock.Set();
  251. // return false;
  252. // }
  253. // return true;
  254. // }, () =>
  255. // {
  256. // return isOpen ? _boatModule.AutoShutterDevice.OpenCloseStatus == ASOpenCloseStatus.Open : _boatModule.AutoShutterDevice.OpenCloseStatus == ASOpenCloseStatus.Close;
  257. // //return true;
  258. // }, timeout * 1000);
  259. // if (ret.Item1)
  260. // {
  261. // if (ret.Item2 == Result.FAIL)
  262. // {
  263. // throw (new RoutineFaildException());
  264. // }
  265. // else if (ret.Item2 == Result.TIMEOUT) //timeout
  266. // {
  267. // if (isOpen)
  268. // {
  269. // //autoShutter.AutoShutterOpenTimeOut.Description = $"can not complete in {timeout} seconds";
  270. // _boatModule.AutoShutterOpenTimeOut.Set($"can not complete in {timeout} seconds");
  271. // }
  272. // else
  273. // {
  274. // //autoShutter.AutoShutterCloseTimeOut.Description = $"can not complete in {timeout} seconds";
  275. // _boatModule.AutoShutterCloseTimeOut.Set($"can not complete in {timeout} seconds");
  276. // }
  277. // throw (new RoutineFaildException());
  278. // }
  279. // else
  280. // throw (new RoutineBreakException());
  281. // }
  282. //}
  283. //private void AutoShutterUp(int id, bool isUp, int timeout)
  284. //{
  285. // Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  286. // {
  287. // Notify($"Auto shutter {(isUp ? "up" : "down")}");
  288. // string reason;
  289. // if (!_boatModule.AutoShutterDevice.SetUp(isUp, out reason))
  290. // {
  291. // _boatModule.AutoShutterMoveFailedForInterlock.Set();
  292. // return false;
  293. // }
  294. // return true;
  295. // }, () =>
  296. // {
  297. // return isUp ? _boatModule.AutoShutterDevice.UpDownStatus == ASUpDownStatus.Up : _boatModule.AutoShutterDevice.UpDownStatus == ASUpDownStatus.Down;
  298. // //return true;
  299. // }, timeout * 1000);
  300. // if (ret.Item1)
  301. // {
  302. // if (ret.Item2 == Result.FAIL)
  303. // {
  304. // throw (new RoutineFaildException());
  305. // }
  306. // else if (ret.Item2 == Result.TIMEOUT) //timeout
  307. // {
  308. // if (isUp)
  309. // {
  310. // //autoShutter.AutoShutterUpTimeOut.Description = $"can not complete in {timeout} seconds";
  311. // _boatModule.AutoShutterUpTimeOut.Set($"can not complete in {timeout} seconds");
  312. // }
  313. // else
  314. // {
  315. // //autoShutter.AutoShutterDownTimeOut.Description = $"can not complete in {timeout} seconds";
  316. // _boatModule.AutoShutterDownTimeOut.Set($"can not complete in {timeout} seconds");
  317. // }
  318. // throw (new RoutineFaildException());
  319. // }
  320. // else
  321. // throw (new RoutineBreakException());
  322. // }
  323. //}
  324. //private void SetReset(int id)
  325. //{
  326. // Tuple<bool, Result> ret = Execute(id, () =>
  327. // {
  328. // Notify($"Set boat reset");
  329. // _boatModule.BoatDevice.SetReset();
  330. // return true;
  331. // });
  332. // if (ret.Item1)
  333. // {
  334. // if (ret.Item2 == Result.FAIL)
  335. // {
  336. // throw (new RoutineFaildException());
  337. // }
  338. // else
  339. // throw (new RoutineBreakException());
  340. // }
  341. //}
  342. //private void SetServoOn(int id)
  343. //{
  344. // Tuple<bool, Result> ret = Execute(id, () =>
  345. // {
  346. // Notify($"Set boat servo on");
  347. // _boatModule.BoatDevice.SetServoOn();
  348. // return true;
  349. // });
  350. // if (ret.Item1)
  351. // {
  352. // if (ret.Item2 == Result.FAIL)
  353. // {
  354. // throw (new RoutineFaildException());
  355. // }
  356. // else
  357. // throw (new RoutineBreakException());
  358. // }
  359. //}
  360. //private void SetZAxisBoatSpeed(int id, float speed)
  361. //{
  362. // Tuple<bool, Result> ret = Execute(id, () =>
  363. // {
  364. // Notify($"Set ZAxis boat speed={speed}");
  365. // _boatModule.BoatDevice.SetZAxisSpeed(speed);
  366. // return true;
  367. // });
  368. // if (ret.Item1)
  369. // {
  370. // if (ret.Item2 == Result.FAIL)
  371. // {
  372. // throw (new RoutineFaildException());
  373. // }
  374. // else
  375. // throw (new RoutineBreakException());
  376. // }
  377. //}
  378. //private void BoatZAxisMoveTo(int id, BoatPosition targetPosition, int timeout)
  379. //{
  380. // Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  381. // {
  382. // Notify($"Boat ZAxis home");
  383. // string reason;
  384. // if (!_boatModule.BoatDevice.ZAxisMoveTo(targetPosition, out reason))
  385. // {
  386. // //_boatModule.BoatDevice.BoatZAxisMoveFailedForInterlock.Description = reason;
  387. // _boatModule.BoatDevice.BoatZAxisHomeFailed.Set(reason);
  388. // return false;
  389. // }
  390. // return true;
  391. // }, () =>
  392. // {
  393. // return _boatModule.BoatDevice.BoatCurrentPosition == targetPosition && !_boatModule.BoatDevice.IsZAxisMoving;
  394. // }, timeout * 1000);
  395. // if (ret.Item1)
  396. // {
  397. // if (ret.Item2 == Result.FAIL)
  398. // {
  399. // throw (new RoutineFaildException());
  400. // }
  401. // else if (ret.Item2 == Result.TIMEOUT) //timeout
  402. // {
  403. // //_boatModule.BoatDevice.BoatZAxisMoveTimeOut.Description = $"can not complete in {timeout} seconds";
  404. // _boatModule.BoatDevice.BoatZAxisHomeTimeout.Set($"can not complete in {timeout} seconds");
  405. // throw (new RoutineFaildException());
  406. // }
  407. // else
  408. // throw (new RoutineBreakException());
  409. // }
  410. //}
  411. //private void SetBoatRAxisMode(int id, BoatRotationMode rotationMode, int timeout)
  412. //{
  413. // Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  414. // {
  415. // Notify($"Boat RAxis home");
  416. // if (!_boatModule.BoatDevice.RAxisRotate(rotationMode, out string reason))
  417. // {
  418. // _boatModule.BoatDevice.BoatRAxisHomeFailed.Set();
  419. // }
  420. // return true;
  421. // }, () =>
  422. // {
  423. // return _boatModule.BoatDevice.IsRAxisAtHome && !_boatModule.BoatDevice.IsRAxisRotating;
  424. // }, timeout * 1000);
  425. // if (ret.Item1)
  426. // {
  427. // if (ret.Item2 == Result.FAIL)
  428. // {
  429. // throw (new RoutineFaildException());
  430. // }
  431. // else if (ret.Item2 == Result.TIMEOUT) //timeout
  432. // {
  433. // //_boatModule.BoatDevice.BoatRAxisHomeTimeout.Description = $"can not complete in {timeout} seconds";
  434. // _boatModule.BoatDevice.BoatRAxisHomeTimeout.Set($"can not complete in {timeout} seconds");
  435. // throw (new RoutineFaildException());
  436. // }
  437. // else
  438. // throw (new RoutineBreakException());
  439. // }
  440. //}
  441. //private void SetBoatRAxisSpeed(int id, float speed)
  442. //{
  443. // Tuple<bool, Result> ret = Execute(id, () =>
  444. // {
  445. // Notify($"Set RAxis boat speed={speed}");
  446. // _boatModule.SetRAxisSpeed(speed);
  447. // return true;
  448. // });
  449. // if (ret.Item1)
  450. // {
  451. // if (ret.Item2 == Result.FAIL)
  452. // {
  453. // throw (new RoutineFaildException());
  454. // }
  455. // else
  456. // throw (new RoutineBreakException());
  457. // }
  458. //}
  459. //private void SetBoatRAxisInterval(int id, float interval)
  460. //{
  461. // Tuple<bool, Result> ret = Execute(id, () =>
  462. // {
  463. // Notify($"Set boat r axis interval={interval}");
  464. // _boatModule.SetRAxisIntervalPosition(interval);
  465. // return true;
  466. // });
  467. // if (ret.Item1)
  468. // {
  469. // if (ret.Item2 == Result.FAIL)
  470. // {
  471. // throw (new RoutineFaildException());
  472. // }
  473. // else
  474. // throw (new RoutineBreakException());
  475. // }
  476. //}
  477. }
  478. }