SETMEntity.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Fsm;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.Util;
  7. using Aitex.Sorter.Common;
  8. using MECF.Framework.Common.Equipment;
  9. using MECF.Framework.Common.SubstrateTrackings;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using Venus_Core;
  18. using Venus_RT.Devices;
  19. using Venus_RT.Devices.PreAligner;
  20. using Venus_RT.Devices.VCE;
  21. using Venus_RT.Modules.PMs;
  22. namespace Venus_RT.Modules.TM.VenusEntity
  23. {
  24. public class SETMEntity : Entity, IModuleEntity
  25. {
  26. public enum STATE
  27. {
  28. Unknown,
  29. Init,
  30. Initializing,
  31. InitializingRB,
  32. Idle,
  33. Error,
  34. Pumping,
  35. Venting,
  36. Purging,
  37. Leakchecking,
  38. Picking,
  39. Placing,
  40. Swaping,
  41. PMPicking,
  42. PMPlacing,
  43. PMSwaping,
  44. Aligning,
  45. Mapping,
  46. Extending,
  47. Retracting,
  48. Swapping,
  49. Gotoing,
  50. ControllingPressure,
  51. }
  52. public enum MSG
  53. {
  54. Home,
  55. RobotHome,
  56. Online,
  57. Offline,
  58. Pump,
  59. Vent,
  60. Purge,
  61. CyclePurge,
  62. LeakCheck,
  63. Pick,
  64. Place,
  65. Swap,
  66. DoublePick,
  67. DoublePlace,
  68. DoubleSwap,
  69. PMPick,
  70. PMPlace,
  71. PMSwap,
  72. Extend,
  73. Retract,
  74. TMCycle,
  75. ControlPressure,
  76. Error,
  77. Abort,
  78. AbortControlPressure,
  79. Align,
  80. CreateJob,
  81. StartJob,
  82. Goto,
  83. }
  84. #region 公开变量
  85. public bool IsIdle
  86. {
  87. get { return fsm.State == (int)STATE.Idle; }
  88. }
  89. public bool IsError
  90. {
  91. get { return fsm.State == (int)STATE.Error; }
  92. }
  93. public bool IsInit
  94. {
  95. get { return fsm.State == (int)STATE.Unknown || fsm.State == (int)STATE.Init; }
  96. }
  97. public bool IsBusy
  98. {
  99. get { return !IsInit && !IsError && !IsIdle; }
  100. }
  101. public bool VCEIsATM => _tm.IsVCEATM;
  102. public bool TMIsATM => _tm.IsTMATM;
  103. public bool TMIsVAC => _tm.IsTMVAC;
  104. public bool VCEIsVAC => _tm.IsVCEVAC;
  105. public bool IsPMASlitDoorClosed => _tm.PMASlitDoorClosed;
  106. public bool IsPMBSlitDoorClosed => _tm.PMBSlitDoorClosed;
  107. public bool IsPMCSlitDoorClosed => _tm.PMCSlitDoorClosed;
  108. public bool IsVCESlitDoorClosed => _tm.VCESlitDoorClosed;
  109. public RState RobotStatus
  110. {
  111. get
  112. {
  113. if (_robot.Status != RState.Running)
  114. {
  115. if (_robotWatch.ElapsedMilliseconds < 100)
  116. return RState.Running;
  117. else
  118. return _robot.Status;
  119. }
  120. else
  121. return RState.Running;
  122. }
  123. }
  124. public bool IsOnline { get; internal set; }
  125. //public bool IsTMVac => _tm.IsTMVac;
  126. //public bool IsTMATM => _tm.IsTMATM;
  127. #endregion
  128. #region 私有变量
  129. private readonly HongHuTM _tm;
  130. private readonly ITransferRobot _robot;
  131. private readonly IPreAlign _vpa;
  132. private readonly SEMFHomeRoutine _homeRoutine;
  133. private readonly SEMFPickRoutine _pickRoutine;
  134. private readonly SEMFPlaceRoutine _placeRoutine;
  135. private readonly SEMFVentRoutine _ventRoutine;
  136. private readonly SEMFPumpRoutine _pumpRoutine;
  137. private readonly SEMFPMPickRoutine _pickpmRoutine;
  138. private readonly SEMFPMPlaceRoutine _placepmRoutine;
  139. private readonly SEMFSwapRoutine _swaproutine;
  140. private readonly SEMFPMSwapRoutine _pmswaproutine;
  141. private readonly SEMFPMRetractRoutine _pmRetractRoutine;
  142. private readonly SEMFPMExtendRoutine _pmExtendRoutine;
  143. //private readonly
  144. private readonly Stopwatch _robotWatch = new Stopwatch();
  145. #endregion
  146. public SETMEntity()
  147. {
  148. _tm = DEVICE.GetDevice<HongHuTM>("SETM");
  149. if(ModuleHelper.IsInstalled(ModuleName.TMRobot))
  150. _robot = new HongHuVR();
  151. _vpa = new HongHuVPA(ModuleName.VPA);
  152. _robotWatch = new Stopwatch();
  153. _homeRoutine = new SEMFHomeRoutine(_tm,_robot, _vpa);
  154. _pickRoutine = new SEMFPickRoutine(_tm,_robot, _vpa);
  155. _placeRoutine = new SEMFPlaceRoutine(_tm, _robot, _vpa);
  156. _pumpRoutine = new SEMFPumpRoutine(_tm, ModuleName.SETM);
  157. _ventRoutine = new SEMFVentRoutine(_tm, ModuleName.SETM);
  158. _pickpmRoutine = new SEMFPMPickRoutine(_tm, _robot);
  159. _placepmRoutine = new SEMFPMPlaceRoutine(_tm, _robot);
  160. _swaproutine = new SEMFSwapRoutine(_tm, _robot);
  161. _pmswaproutine = new SEMFPMSwapRoutine(_tm, _robot);
  162. _pmExtendRoutine = new SEMFPMExtendRoutine(_tm, _robot, _vpa);
  163. _pmRetractRoutine = new SEMFPMRetractRoutine(_tm, _robot, _vpa);
  164. InitFsmMap();
  165. }
  166. protected override bool Init()
  167. {
  168. DATA.Subscribe($"SETM.FsmState", () => ((STATE)fsm.State).ToString());
  169. DATA.Subscribe("SETM.RobotMoveAction", () => _robot.TMRobotMoveInfo, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  170. DATA.Subscribe("SETM.RobotMoveAction.ArmTarget", () => _robot.TMRobotMoveInfo.ArmTarget.ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  171. DATA.Subscribe("SETM.RobotMoveAction.BladeTarget", () => _robot.TMRobotMoveInfo.BladeTarget, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  172. DATA.Subscribe("SETM.RobotMoveAction.RobotAction", () => _robot.TMRobotMoveInfo.Action.ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  173. OP.Subscribe("SETM.Goto", (cmd, args) => RobotGoto(args));
  174. OP.Subscribe("SETM.Home", (cmd, args) => { PostMsg(MSG.Home); return true; });
  175. OP.Subscribe("SETM.Pick", (cmd, args) => { PostMsg(MSG.Pick, args); return true; });
  176. OP.Subscribe("SETM.Place", (cmd, args) => { PostMsg(MSG.Place, args); return true; });
  177. OP.Subscribe("SETM.Extend", (cmd, args) => { PostMsg(MSG.Extend, args); return true; });
  178. OP.Subscribe("SETM.Retract", (cmd, args) => { PostMsg(MSG.Retract, args); return true; });
  179. OP.Subscribe("SETM.PMPick", (cmd, args) => { PostMsg(MSG.PMPick, args); return true; });
  180. OP.Subscribe("SETM.PMPlace", (cmd, args) => { PostMsg(MSG.PMPlace, args); return true; });
  181. OP.Subscribe("SETM.PumpDown", (cmd, args) => { PostMsg(MSG.Pump); return true; });
  182. OP.Subscribe("SETM.Vent", (cmd, args) => { PostMsg(MSG.Vent); return true; });
  183. return true;
  184. }
  185. private void InitFsmMap()
  186. {
  187. fsm = new StateMachine<SETMEntity>("SETM", (int)STATE.Init, 50);
  188. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  189. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  190. AnyStateTransition(MSG.Offline, fnOffline, FSM_STATE.SAME);
  191. AnyStateTransition(MSG.Home, fnHome, STATE.Initializing);
  192. //Home
  193. Transition(STATE.Initializing, FSM_MSG.TIMER, fnHomeTimeout, STATE.Idle);
  194. Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  195. //Pick
  196. Transition(STATE.Idle, MSG.Pick, fnStartPick, STATE.Picking);
  197. Transition(STATE.Picking, FSM_MSG.TIMER, fnPickTimeout, STATE.Idle);
  198. Transition(STATE.Picking, MSG.Abort, fnAbortPick, STATE.Idle);
  199. //Place
  200. Transition(STATE.Idle, MSG.Place, fnStartPlace, STATE.Placing);
  201. Transition(STATE.Placing, FSM_MSG.TIMER, fnPlaceTimeout, STATE.Idle);
  202. Transition(STATE.Placing, MSG.Abort, fnAbortPlace, STATE.Idle);
  203. //Pump
  204. Transition(STATE.Idle, MSG.Pump, fnStartPump, STATE.Pumping);
  205. Transition(STATE.Pumping, FSM_MSG.TIMER, fnPumpTimeout, STATE.Idle);
  206. Transition(STATE.Pumping, MSG.Abort, fnAbortPump, STATE.Idle);
  207. //Vent
  208. Transition(STATE.Idle, MSG.Vent, fnStartVent, STATE.Venting);
  209. Transition(STATE.Venting, FSM_MSG.TIMER, fnVentTimeout, STATE.Idle);
  210. Transition(STATE.Venting, MSG.Abort, fnAbortVent, STATE.Idle);
  211. //PMPick
  212. Transition(STATE.Idle, MSG.PMPick, fnStartPMPick, STATE.PMPicking);
  213. Transition(STATE.PMPicking, FSM_MSG.TIMER, fnPMPickTimeout, STATE.Idle);
  214. Transition(STATE.PMPicking, MSG.Abort, fnAbortPMPick, STATE.Idle);
  215. //PMPlace
  216. Transition(STATE.Idle, MSG.PMPlace, fnStartPMPlace, STATE.PMPlacing);
  217. Transition(STATE.PMPlacing, FSM_MSG.TIMER, fnPMPlaceTimeout, STATE.Idle);
  218. Transition(STATE.PMPlacing, MSG.Abort, fnAbortPMPlace, STATE.Idle);
  219. //PA align
  220. Transition(STATE.Idle, MSG.Align, fnStartAlign, STATE.Aligning);
  221. Transition(STATE.Aligning, FSM_MSG.TIMER, fnAlignTimeout, STATE.Idle);
  222. Transition(STATE.Aligning, MSG.Abort, fnAbortAlign, STATE.Idle);
  223. //Swap
  224. Transition(STATE.Idle, MSG.Swap, fnStartSwap, STATE.Swapping);
  225. Transition(STATE.Swapping, FSM_MSG.TIMER, fnSwapTimeout, STATE.Idle);
  226. Transition(STATE.Swapping, MSG.Abort, fnAbortSwap, STATE.Idle);
  227. //PM Swap
  228. Transition(STATE.Idle, MSG.PMSwap, fnStartPMSwap, STATE.PMSwaping);
  229. Transition(STATE.PMSwaping, FSM_MSG.TIMER, fnPMSwapTimeout, STATE.Idle);
  230. Transition(STATE.PMSwaping, MSG.Abort, fnAbortPMSwap, STATE.Idle);
  231. //Extend
  232. Transition(STATE.Idle, MSG.Extend, FnStartExtend, STATE.Extending);
  233. Transition(STATE.Extending, FSM_MSG.TIMER, FnExtend, STATE.Idle);
  234. Transition(STATE.Extending, MSG.Abort, FnAbortExtend, STATE.Idle);
  235. //Retract
  236. Transition(STATE.Idle, MSG.Retract, FnStartRetract, STATE.Retracting);
  237. Transition(STATE.Retracting, FSM_MSG.TIMER, FnRetract, STATE.Idle);
  238. Transition(STATE.Retracting, MSG.Abort, FnAbortRetract, STATE.Idle);
  239. Running = true;
  240. }
  241. private bool RobotGoto(object[] param)
  242. {
  243. return _robot.Goto((ModuleName)param[0], (int)param[1], (Hand)param[2]);
  244. }
  245. private bool FnStartExtend(object[] param)
  246. {
  247. return _pmExtendRoutine.Start(param) == RState.Running;
  248. }
  249. private bool FnExtend(object[] param)
  250. {
  251. RState ret = _pmExtendRoutine.Monitor();
  252. if (ret == RState.Failed || ret == RState.Timeout)
  253. {
  254. PostMsg(MSG.Error);
  255. return false;
  256. }
  257. return ret == RState.End;
  258. }
  259. private bool FnAbortExtend(object[] param)
  260. {
  261. _pmExtendRoutine.Abort();
  262. return true;
  263. }
  264. private bool FnStartRetract(object[] param)
  265. {
  266. return _pmRetractRoutine.Start(param) == RState.Running;
  267. }
  268. private bool FnRetract(object[] param)
  269. {
  270. RState ret = _pmRetractRoutine.Monitor();
  271. if (ret == RState.Failed || ret == RState.Timeout)
  272. {
  273. PostMsg(MSG.Error);
  274. return false;
  275. }
  276. return ret == RState.End;
  277. }
  278. private bool FnAbortRetract(object[] param)
  279. {
  280. _pmRetractRoutine.Abort();
  281. return true;
  282. }
  283. private bool fnAbortPMSwap(object[] param)
  284. {
  285. _pmswaproutine.Abort();
  286. return true;
  287. }
  288. private bool fnPMSwapTimeout(object[] param)
  289. {
  290. RState ret = _pmswaproutine.Monitor();
  291. if (ret == RState.Failed || ret == RState.Timeout)
  292. {
  293. PostMsg(MSG.Error);
  294. return false;
  295. }
  296. return ret == RState.End;
  297. }
  298. private bool fnStartPMSwap(object[] param)
  299. {
  300. return _pmswaproutine.Start(param) == RState.Running;
  301. }
  302. private bool fnAbortSwap(object[] param)
  303. {
  304. _swaproutine.Abort();
  305. return true;
  306. }
  307. private bool fnSwapTimeout(object[] param)
  308. {
  309. RState ret = _swaproutine.Monitor();
  310. if (ret == RState.Failed || ret == RState.Timeout)
  311. {
  312. PostMsg(MSG.Error);
  313. return false;
  314. }
  315. return ret == RState.End;
  316. }
  317. private bool fnStartSwap(object[] param)
  318. {
  319. return _swaproutine.Start(param) == RState.Running;
  320. }
  321. private bool fnStartAlign(object[] param)
  322. {
  323. if (float.TryParse(param[0].ToString(), out float angle))
  324. {
  325. return _vpa.AlignWithAngle(angle);
  326. }
  327. else
  328. {
  329. LOG.Write(eEvent.ERR_TM,ModuleName.VPA,$"wrong angle, value is {param[0]}.");
  330. return false;
  331. }
  332. }
  333. private bool fnAlignTimeout(object[] param)
  334. {
  335. if (_vpa.Status == RState.End)
  336. {
  337. return true;
  338. }
  339. else if (_vpa.Status != RState.Running)
  340. {
  341. LOG.Write(eEvent.ERR_TM, ModuleName.VPA, $"PreAligner align failed: {_vpa.Status}");
  342. return true;
  343. }
  344. return false;
  345. }
  346. private bool fnAbortAlign(object[] param)
  347. {
  348. return true;
  349. }
  350. private bool fnAbortPMPlace(object[] param)
  351. {
  352. _placepmRoutine.Abort();
  353. return true;
  354. }
  355. private bool fnPMPlaceTimeout(object[] param)
  356. {
  357. RState ret = _placepmRoutine.Monitor();
  358. if (ret == RState.Failed || ret == RState.Timeout)
  359. {
  360. PostMsg(MSG.Error);
  361. return false;
  362. }
  363. return ret == RState.End;
  364. }
  365. private bool fnStartPMPlace(object[] param)
  366. {
  367. return _placepmRoutine.Start(param) == RState.Running;
  368. }
  369. private bool fnAbortPMPick(object[] param)
  370. {
  371. _pickpmRoutine.Abort();
  372. return true;
  373. }
  374. private bool fnPMPickTimeout(object[] param)
  375. {
  376. RState ret = _pickpmRoutine.Monitor();
  377. if (ret == RState.Failed || ret == RState.Timeout)
  378. {
  379. PostMsg(MSG.Error);
  380. return false;
  381. }
  382. return ret == RState.End;
  383. }
  384. private bool fnStartPMPick(object[] param)
  385. {
  386. return _pickpmRoutine.Start(param) == RState.Running;
  387. }
  388. private bool fnAbortVent(object[] param)
  389. {
  390. _ventRoutine.Abort();
  391. return true;
  392. }
  393. private bool fnVentTimeout(object[] param)
  394. {
  395. RState ret = _ventRoutine.Monitor();
  396. if (ret == RState.Failed || ret == RState.Timeout)
  397. {
  398. PostMsg(MSG.Error);
  399. return false;
  400. }
  401. return ret == RState.End;
  402. }
  403. private bool fnStartVent(object[] param)
  404. {
  405. return _ventRoutine.Start(param) == RState.Running;
  406. }
  407. private bool fnAbortPump(object[] param)
  408. {
  409. _pumpRoutine.Abort();
  410. return true;
  411. }
  412. private bool fnPumpTimeout(object[] param)
  413. {
  414. RState ret = _pumpRoutine.Monitor();
  415. if (ret == RState.Failed || ret == RState.Timeout)
  416. {
  417. PostMsg(MSG.Error);
  418. return false;
  419. }
  420. return ret == RState.End;
  421. }
  422. private bool fnStartPump(object[] param)
  423. {
  424. return _pumpRoutine.Start(param) == RState.Running;
  425. }
  426. private bool fnAbortPlace(object[] param)
  427. {
  428. return true;
  429. }
  430. private bool fnPlaceTimeout(object[] param)
  431. {
  432. RState ret = _placeRoutine.Monitor();
  433. if (ret == RState.Failed || ret == RState.Timeout)
  434. {
  435. PostMsg(MSG.Error);
  436. return false;
  437. }
  438. return ret == RState.End;
  439. }
  440. private bool fnStartPlace(object[] param)
  441. {
  442. return _placeRoutine.Start(param) == RState.Running;
  443. }
  444. private bool fnAbortPick(object[] param)
  445. {
  446. _pickRoutine.Abort();
  447. return true;
  448. }
  449. private bool fnStartPick(object[] param)
  450. {
  451. return _pickRoutine.Start(param) == RState.Running;
  452. }
  453. private bool fnPickTimeout(object[] param)
  454. {
  455. RState ret = _pickRoutine.Monitor();
  456. if (ret == RState.Failed || ret == RState.Timeout)
  457. {
  458. PostMsg(MSG.Error);
  459. return false;
  460. }
  461. return ret == RState.End;
  462. }
  463. private bool fnAbortHome(object[] param)
  464. {
  465. _homeRoutine.Abort();
  466. return true;
  467. }
  468. private bool fnHome(object[] param)
  469. {
  470. if (fsm.State == (int)STATE.Init && param.Length > 0)//带参home
  471. {
  472. return false;
  473. }
  474. else
  475. return _homeRoutine.Start(param) == RState.Running;
  476. }
  477. private bool fnHomeTimeout(object[] param)
  478. {
  479. RState ret = _homeRoutine.Monitor();
  480. if (ret == RState.Failed || ret == RState.Timeout)
  481. {
  482. PostMsg(MSG.Error);
  483. return false;
  484. }
  485. return ret == RState.End;
  486. }
  487. private bool fnOffline(object[] param)
  488. {
  489. throw new NotImplementedException();
  490. }
  491. private bool fnOnline(object[] param)
  492. {
  493. throw new NotImplementedException();
  494. }
  495. private bool fnError(object[] param)
  496. {
  497. return true;
  498. }
  499. public bool Check(int msg, out string reason, params object[] args)
  500. {
  501. reason = "";
  502. return true;
  503. }
  504. public bool CheckAcked(int msg)
  505. {
  506. return fsm.CheckExecuted(msg);
  507. }
  508. public bool CheckToPostMessage(int msg, params object[] args)
  509. {
  510. if (!fsm.FindTransition(fsm.State, msg))
  511. {
  512. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  513. return false;
  514. }
  515. Running = true;
  516. fsm.PostMsg(msg, args);
  517. return true;
  518. }
  519. public int Invoke(string function, params object[] args)
  520. {
  521. switch (function)
  522. {
  523. case "Home":
  524. CheckToPostMessage((int)MSG.Home);
  525. return (int)MSG.Home;
  526. }
  527. return (int)FSM_MSG.NONE;
  528. }
  529. }
  530. }