BoatHome.cs 19 KB

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