SETMEntity.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 double VCEPressure => _tm.VCEPressure;
  110. public RState RobotStatus
  111. {
  112. get
  113. {
  114. if (_robot.Status != RState.Running)
  115. {
  116. if (_robotWatch.ElapsedMilliseconds < 100)
  117. return RState.Running;
  118. else
  119. return _robot.Status;
  120. }
  121. else
  122. return RState.Running;
  123. }
  124. }
  125. public bool IsOnline { get; internal set; }
  126. //public bool IsTMVac => _tm.IsTMVac;
  127. //public bool IsTMATM => _tm.IsTMATM;
  128. #endregion
  129. #region 私有变量
  130. private readonly HongHuTM _tm;
  131. private readonly ITransferRobot _robot;
  132. private readonly IPreAlign _vpa;
  133. private readonly SEMFHomeRoutine _homeRoutine;
  134. private readonly SEMFPickRoutine _pickRoutine;
  135. private readonly SEMFPlaceRoutine _placeRoutine;
  136. private readonly SEMFVentRoutine _ventRoutine;
  137. private readonly SEMFPumpRoutine _pumpRoutine;
  138. private readonly SEMFPMPickRoutine _pickpmRoutine;
  139. private readonly SEMFPMPlaceRoutine _placepmRoutine;
  140. private readonly SEMFSwapRoutine _swaproutine;
  141. private readonly SEMFPMSwapRoutine _pmswaproutine;
  142. private readonly SEMFPMRetractRoutine _pmRetractRoutine;
  143. private readonly SEMFPMExtendRoutine _pmExtendRoutine;
  144. //private readonly
  145. private readonly Stopwatch _robotWatch = new Stopwatch();
  146. #endregion
  147. public SETMEntity()
  148. {
  149. _tm = DEVICE.GetDevice<HongHuTM>("SETM");
  150. if(ModuleHelper.IsInstalled(ModuleName.TMRobot))
  151. _robot = new HongHuVR();
  152. _vpa = new HongHuVPA(ModuleName.VPA);
  153. _robotWatch = new Stopwatch();
  154. _homeRoutine = new SEMFHomeRoutine(_tm,_robot, _vpa);
  155. _pickRoutine = new SEMFPickRoutine(_tm,_robot, _vpa);
  156. _placeRoutine = new SEMFPlaceRoutine(_tm, _robot, _vpa);
  157. _pumpRoutine = new SEMFPumpRoutine(_tm, ModuleName.SETM);
  158. _ventRoutine = new SEMFVentRoutine(_tm, ModuleName.SETM);
  159. _pickpmRoutine = new SEMFPMPickRoutine(_tm, _robot);
  160. _placepmRoutine = new SEMFPMPlaceRoutine(_tm, _robot);
  161. _swaproutine = new SEMFSwapRoutine(_tm, _robot);
  162. _pmswaproutine = new SEMFPMSwapRoutine(_tm, _robot);
  163. _pmExtendRoutine = new SEMFPMExtendRoutine(_tm, _robot, _vpa);
  164. _pmRetractRoutine = new SEMFPMRetractRoutine(_tm, _robot, _vpa);
  165. InitFsmMap();
  166. }
  167. protected override bool Init()
  168. {
  169. DATA.Subscribe($"SETM.FsmState", () => ((STATE)fsm.State).ToString());
  170. DATA.Subscribe("SETM.RobotMoveAction", () => _robot.TMRobotMoveInfo, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  171. DATA.Subscribe("SETM.RobotMoveAction.ArmTarget", () => _robot.TMRobotMoveInfo.ArmTarget.ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  172. DATA.Subscribe("SETM.RobotMoveAction.BladeTarget", () => _robot.TMRobotMoveInfo.BladeTarget, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  173. DATA.Subscribe("SETM.RobotMoveAction.RobotAction", () => _robot.TMRobotMoveInfo.Action.ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  174. OP.Subscribe("SETM.Goto", (cmd, args) => RobotGoto(args));
  175. OP.Subscribe("SETM.Home", (cmd, args) => { PostMsg(MSG.Home); return true; });
  176. OP.Subscribe("SETM.Pick", (cmd, args) => { PostMsg(MSG.Pick, args); return true; });
  177. OP.Subscribe("SETM.Place", (cmd, args) => { PostMsg(MSG.Place, args); return true; });
  178. OP.Subscribe("SETM.Extend", (cmd, args) => { PostMsg(MSG.Extend, args); return true; });
  179. OP.Subscribe("SETM.Retract", (cmd, args) => { PostMsg(MSG.Retract, args); return true; });
  180. OP.Subscribe("SETM.PMPick", (cmd, args) => { PostMsg(MSG.PMPick, args); return true; });
  181. OP.Subscribe("SETM.PMPlace", (cmd, args) => { PostMsg(MSG.PMPlace, args); return true; });
  182. OP.Subscribe("SETM.PumpDown", (cmd, args) => { PostMsg(MSG.Pump); return true; });
  183. OP.Subscribe("SETM.Vent", (cmd, args) => { PostMsg(MSG.Vent); return true; });
  184. return true;
  185. }
  186. private void InitFsmMap()
  187. {
  188. fsm = new StateMachine<SETMEntity>("SETM", (int)STATE.Init, 50);
  189. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  190. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  191. AnyStateTransition(MSG.Offline, fnOffline, FSM_STATE.SAME);
  192. AnyStateTransition(MSG.Home, fnHome, STATE.Initializing);
  193. //Home
  194. Transition(STATE.Initializing, FSM_MSG.TIMER, fnHomeTimeout, STATE.Idle);
  195. Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  196. //Pick
  197. Transition(STATE.Idle, MSG.Pick, fnStartPick, STATE.Picking);
  198. Transition(STATE.Picking, FSM_MSG.TIMER, fnPickTimeout, STATE.Idle);
  199. Transition(STATE.Picking, MSG.Abort, fnAbortPick, STATE.Idle);
  200. //Place
  201. Transition(STATE.Idle, MSG.Place, fnStartPlace, STATE.Placing);
  202. Transition(STATE.Placing, FSM_MSG.TIMER, fnPlaceTimeout, STATE.Idle);
  203. Transition(STATE.Placing, MSG.Abort, fnAbortPlace, STATE.Idle);
  204. //Pump
  205. Transition(STATE.Idle, MSG.Pump, fnStartPump, STATE.Pumping);
  206. Transition(STATE.Pumping, FSM_MSG.TIMER, fnPumpTimeout, STATE.Idle);
  207. Transition(STATE.Pumping, MSG.Abort, fnAbortPump, STATE.Idle);
  208. //Vent
  209. Transition(STATE.Idle, MSG.Vent, fnStartVent, STATE.Venting);
  210. Transition(STATE.Venting, FSM_MSG.TIMER, fnVentTimeout, STATE.Idle);
  211. Transition(STATE.Venting, MSG.Abort, fnAbortVent, STATE.Idle);
  212. //PMPick
  213. Transition(STATE.Idle, MSG.PMPick, fnStartPMPick, STATE.PMPicking);
  214. Transition(STATE.PMPicking, FSM_MSG.TIMER, fnPMPickTimeout, STATE.Idle);
  215. Transition(STATE.PMPicking, MSG.Abort, fnAbortPMPick, STATE.Idle);
  216. //PMPlace
  217. Transition(STATE.Idle, MSG.PMPlace, fnStartPMPlace, STATE.PMPlacing);
  218. Transition(STATE.PMPlacing, FSM_MSG.TIMER, fnPMPlaceTimeout, STATE.Idle);
  219. Transition(STATE.PMPlacing, MSG.Abort, fnAbortPMPlace, STATE.Idle);
  220. //PA align
  221. Transition(STATE.Idle, MSG.Align, fnStartAlign, STATE.Aligning);
  222. Transition(STATE.Aligning, FSM_MSG.TIMER, fnAlignTimeout, STATE.Idle);
  223. Transition(STATE.Aligning, MSG.Abort, fnAbortAlign, STATE.Idle);
  224. //Swap
  225. Transition(STATE.Idle, MSG.Swap, fnStartSwap, STATE.Swapping);
  226. Transition(STATE.Swapping, FSM_MSG.TIMER, fnSwapTimeout, STATE.Idle);
  227. Transition(STATE.Swapping, MSG.Abort, fnAbortSwap, STATE.Idle);
  228. //PM Swap
  229. Transition(STATE.Idle, MSG.PMSwap, fnStartPMSwap, STATE.PMSwaping);
  230. Transition(STATE.PMSwaping, FSM_MSG.TIMER, fnPMSwapTimeout, STATE.Idle);
  231. Transition(STATE.PMSwaping, MSG.Abort, fnAbortPMSwap, STATE.Idle);
  232. //Extend
  233. Transition(STATE.Idle, MSG.Extend, FnStartExtend, STATE.Extending);
  234. Transition(STATE.Extending, FSM_MSG.TIMER, FnExtend, STATE.Idle);
  235. Transition(STATE.Extending, MSG.Abort, FnAbortExtend, STATE.Idle);
  236. //Retract
  237. Transition(STATE.Idle, MSG.Retract, FnStartRetract, STATE.Retracting);
  238. Transition(STATE.Retracting, FSM_MSG.TIMER, FnRetract, STATE.Idle);
  239. Transition(STATE.Retracting, MSG.Abort, FnAbortRetract, STATE.Idle);
  240. Running = true;
  241. }
  242. private bool RobotGoto(object[] param)
  243. {
  244. return _robot.Goto((ModuleName)param[0], (int)param[1], (Hand)param[2]);
  245. }
  246. private bool FnStartExtend(object[] param)
  247. {
  248. return _pmExtendRoutine.Start(param) == RState.Running;
  249. }
  250. private bool FnExtend(object[] param)
  251. {
  252. RState ret = _pmExtendRoutine.Monitor();
  253. if (ret == RState.Failed || ret == RState.Timeout)
  254. {
  255. PostMsg(MSG.Error);
  256. return false;
  257. }
  258. return ret == RState.End;
  259. }
  260. private bool FnAbortExtend(object[] param)
  261. {
  262. _pmExtendRoutine.Abort();
  263. return true;
  264. }
  265. private bool FnStartRetract(object[] param)
  266. {
  267. return _pmRetractRoutine.Start(param) == RState.Running;
  268. }
  269. private bool FnRetract(object[] param)
  270. {
  271. RState ret = _pmRetractRoutine.Monitor();
  272. if (ret == RState.Failed || ret == RState.Timeout)
  273. {
  274. PostMsg(MSG.Error);
  275. return false;
  276. }
  277. return ret == RState.End;
  278. }
  279. private bool FnAbortRetract(object[] param)
  280. {
  281. _pmRetractRoutine.Abort();
  282. return true;
  283. }
  284. private bool fnAbortPMSwap(object[] param)
  285. {
  286. _pmswaproutine.Abort();
  287. return true;
  288. }
  289. private bool fnPMSwapTimeout(object[] param)
  290. {
  291. RState ret = _pmswaproutine.Monitor();
  292. if (ret == RState.Failed || ret == RState.Timeout)
  293. {
  294. PostMsg(MSG.Error);
  295. return false;
  296. }
  297. return ret == RState.End;
  298. }
  299. private bool fnStartPMSwap(object[] param)
  300. {
  301. return _pmswaproutine.Start(param) == RState.Running;
  302. }
  303. private bool fnAbortSwap(object[] param)
  304. {
  305. _swaproutine.Abort();
  306. return true;
  307. }
  308. private bool fnSwapTimeout(object[] param)
  309. {
  310. RState ret = _swaproutine.Monitor();
  311. if (ret == RState.Failed || ret == RState.Timeout)
  312. {
  313. PostMsg(MSG.Error);
  314. return false;
  315. }
  316. return ret == RState.End;
  317. }
  318. private bool fnStartSwap(object[] param)
  319. {
  320. return _swaproutine.Start(param) == RState.Running;
  321. }
  322. private bool fnStartAlign(object[] param)
  323. {
  324. if (float.TryParse(param[0].ToString(), out float angle))
  325. {
  326. return _vpa.AlignWithAngle(angle);
  327. }
  328. else
  329. {
  330. LOG.Write(eEvent.ERR_TM,ModuleName.VPA,$"wrong angle, value is {param[0]}.");
  331. return false;
  332. }
  333. }
  334. private bool fnAlignTimeout(object[] param)
  335. {
  336. if (_vpa.Status == RState.End)
  337. {
  338. return true;
  339. }
  340. else if (_vpa.Status != RState.Running)
  341. {
  342. LOG.Write(eEvent.ERR_TM, ModuleName.VPA, $"PreAligner align failed: {_vpa.Status}");
  343. return true;
  344. }
  345. return false;
  346. }
  347. private bool fnAbortAlign(object[] param)
  348. {
  349. return true;
  350. }
  351. private bool fnAbortPMPlace(object[] param)
  352. {
  353. _placepmRoutine.Abort();
  354. return true;
  355. }
  356. private bool fnPMPlaceTimeout(object[] param)
  357. {
  358. RState ret = _placepmRoutine.Monitor();
  359. if (ret == RState.Failed || ret == RState.Timeout)
  360. {
  361. PostMsg(MSG.Error);
  362. return false;
  363. }
  364. return ret == RState.End;
  365. }
  366. private bool fnStartPMPlace(object[] param)
  367. {
  368. return _placepmRoutine.Start(param) == RState.Running;
  369. }
  370. private bool fnAbortPMPick(object[] param)
  371. {
  372. _pickpmRoutine.Abort();
  373. return true;
  374. }
  375. private bool fnPMPickTimeout(object[] param)
  376. {
  377. RState ret = _pickpmRoutine.Monitor();
  378. if (ret == RState.Failed || ret == RState.Timeout)
  379. {
  380. PostMsg(MSG.Error);
  381. return false;
  382. }
  383. return ret == RState.End;
  384. }
  385. private bool fnStartPMPick(object[] param)
  386. {
  387. return _pickpmRoutine.Start(param) == RState.Running;
  388. }
  389. private bool fnAbortVent(object[] param)
  390. {
  391. _ventRoutine.Abort();
  392. return true;
  393. }
  394. private bool fnVentTimeout(object[] param)
  395. {
  396. RState ret = _ventRoutine.Monitor();
  397. if (ret == RState.Failed || ret == RState.Timeout)
  398. {
  399. PostMsg(MSG.Error);
  400. return false;
  401. }
  402. return ret == RState.End;
  403. }
  404. private bool fnStartVent(object[] param)
  405. {
  406. return _ventRoutine.Start(param) == RState.Running;
  407. }
  408. private bool fnAbortPump(object[] param)
  409. {
  410. _pumpRoutine.Abort();
  411. return true;
  412. }
  413. private bool fnPumpTimeout(object[] param)
  414. {
  415. RState ret = _pumpRoutine.Monitor();
  416. if (ret == RState.Failed || ret == RState.Timeout)
  417. {
  418. PostMsg(MSG.Error);
  419. return false;
  420. }
  421. return ret == RState.End;
  422. }
  423. private bool fnStartPump(object[] param)
  424. {
  425. return _pumpRoutine.Start(param) == RState.Running;
  426. }
  427. private bool fnAbortPlace(object[] param)
  428. {
  429. return true;
  430. }
  431. private bool fnPlaceTimeout(object[] param)
  432. {
  433. RState ret = _placeRoutine.Monitor();
  434. if (ret == RState.Failed || ret == RState.Timeout)
  435. {
  436. PostMsg(MSG.Error);
  437. return false;
  438. }
  439. return ret == RState.End;
  440. }
  441. private bool fnStartPlace(object[] param)
  442. {
  443. return _placeRoutine.Start(param) == RState.Running;
  444. }
  445. private bool fnAbortPick(object[] param)
  446. {
  447. _pickRoutine.Abort();
  448. return true;
  449. }
  450. private bool fnStartPick(object[] param)
  451. {
  452. return _pickRoutine.Start(param) == RState.Running;
  453. }
  454. private bool fnPickTimeout(object[] param)
  455. {
  456. RState ret = _pickRoutine.Monitor();
  457. if (ret == RState.Failed || ret == RState.Timeout)
  458. {
  459. PostMsg(MSG.Error);
  460. return false;
  461. }
  462. return ret == RState.End;
  463. }
  464. private bool fnAbortHome(object[] param)
  465. {
  466. _homeRoutine.Abort();
  467. return true;
  468. }
  469. private bool fnHome(object[] param)
  470. {
  471. if (fsm.State == (int)STATE.Init && param.Length > 0)//带参home
  472. {
  473. return false;
  474. }
  475. else
  476. return _homeRoutine.Start(param) == RState.Running;
  477. }
  478. private bool fnHomeTimeout(object[] param)
  479. {
  480. RState ret = _homeRoutine.Monitor();
  481. if (ret == RState.Failed || ret == RState.Timeout)
  482. {
  483. PostMsg(MSG.Error);
  484. return false;
  485. }
  486. return ret == RState.End;
  487. }
  488. private bool fnOffline(object[] param)
  489. {
  490. throw new NotImplementedException();
  491. }
  492. private bool fnOnline(object[] param)
  493. {
  494. throw new NotImplementedException();
  495. }
  496. private bool fnError(object[] param)
  497. {
  498. return true;
  499. }
  500. public bool Check(int msg, out string reason, params object[] args)
  501. {
  502. reason = "";
  503. return true;
  504. }
  505. public bool CheckAcked(int msg)
  506. {
  507. return fsm.CheckExecuted(msg);
  508. }
  509. public bool CheckToPostMessage(int msg, params object[] args)
  510. {
  511. if (!fsm.FindTransition(fsm.State, msg))
  512. {
  513. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  514. return false;
  515. }
  516. Running = true;
  517. fsm.PostMsg(msg, args);
  518. return true;
  519. }
  520. public int Invoke(string function, params object[] args)
  521. {
  522. switch (function)
  523. {
  524. case "Home":
  525. CheckToPostMessage((int)MSG.Home);
  526. return (int)MSG.Home;
  527. }
  528. return (int)FSM_MSG.NONE;
  529. }
  530. }
  531. }