SETMEntity.cs 17 KB

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