SETMEntity.cs 17 KB

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