RouteManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Fsm;
  8. using Aitex.Core.Common;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.Routine;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.Util;
  15. using MECF.Framework.Common.Equipment;
  16. using MECF.Framework.Common.SubstrateTrackings;
  17. using Venus_Core;
  18. using Venus_RT.Modules.PMs;
  19. using Aitex.Core.RT.Log;
  20. namespace Venus_RT.Modules
  21. {
  22. class RouteManager : Entity, IEntity
  23. {
  24. public enum MSG
  25. {
  26. MoveWafer,
  27. ReturnWafer,
  28. HomeUnit,
  29. PauseAuto,
  30. ResumeAuto,
  31. Stop,
  32. StartCycle,
  33. StopCycle,
  34. HOME,
  35. RESET,
  36. ABORT,
  37. ERROR,
  38. SetAutoMode,
  39. SetManualMode,
  40. ResetIdleCleanTime,
  41. ResetIdlePurgeTime,
  42. CreateJob,
  43. PauseJob,
  44. ResumeJob,
  45. StartJob,
  46. StopJob,
  47. AbortJob,
  48. JobDone,
  49. CassetteLeave, //For unload light control off afer job done
  50. Map,
  51. ReturnAllWafer,
  52. TMCycle,
  53. }
  54. public PMEntity PMA { get; private set; }
  55. public PMEntity PMB { get; private set; }
  56. public PMEntity PMC { get; private set; }
  57. public PMEntity PMD { get; private set; }
  58. public TMEntity TM { get; private set; }
  59. public LLEntity LLA { get; private set; }
  60. public LLEntity LLB { get; private set; }
  61. public EfemEntity EFEM { get; private set; }
  62. public string Name { get; set; }
  63. public bool IsAutoMode
  64. {
  65. get
  66. {
  67. return fsm.State == (int)RtState.AutoRunning || fsm.State == (int)RtState.AutoIdle;
  68. }
  69. }
  70. public bool IsInit
  71. {
  72. get { return fsm.State == (int)RtState.Init; }
  73. }
  74. public bool IsIdle
  75. {
  76. get { return fsm.State == (int)RtState.Idle || fsm.State == (int)RtState.AutoIdle; }
  77. }
  78. public bool IsAlarm
  79. {
  80. get { return fsm.State == (int)RtState.Error; }
  81. }
  82. public bool IsEntityError
  83. {
  84. get
  85. {
  86. return (EFEM?.IsError ?? false)
  87. || (PMA?.IsError ?? false)
  88. || (PMB?.IsError ?? false);
  89. }
  90. }
  91. public bool IsRunning
  92. {
  93. get
  94. {
  95. return !IsInit && !IsAlarm && !IsIdle;
  96. }
  97. }
  98. private TMCycle _TMCycle;
  99. private AutoCycle _AutoCycle;
  100. private bool _isWaitUnload;
  101. public RouteManager()
  102. {
  103. Name = "System";
  104. if (ModuleHelper.IsInstalled(ModuleName.PMA))
  105. PMA = new PMEntity(ModuleName.PMA);
  106. if (ModuleHelper.IsInstalled(ModuleName.PMB))
  107. PMB = new PMEntity(ModuleName.PMB);
  108. if (ModuleHelper.IsInstalled(ModuleName.PMC))
  109. PMC = new PMEntity(ModuleName.PMC);
  110. if (ModuleHelper.IsInstalled(ModuleName.PMD))
  111. PMD = new PMEntity(ModuleName.PMD);
  112. if (ModuleHelper.IsInstalled(ModuleName.TM))
  113. TM = new TMEntity();
  114. if (ModuleHelper.IsInstalled(ModuleName.LLA))
  115. LLA = new LLEntity(ModuleName.LLA);
  116. if (ModuleHelper.IsInstalled(ModuleName.LLB))
  117. LLB = new LLEntity(ModuleName.LLB);
  118. if (ModuleHelper.IsInstalled(ModuleName.EFEM))
  119. EFEM = new EfemEntity();
  120. fsm = new StateMachine<RouteManager>(Name, (int)RtState.Init, 200);
  121. SubscribeOperation();
  122. }
  123. public bool Check(int msg, out string reason, params object[] args)
  124. {
  125. if (!fsm.FindTransition(fsm.State, msg))
  126. {
  127. reason = String.Format("{0} is in {1} state,can not do {2}", Name, 0, (MSG)msg);
  128. return false;
  129. }
  130. if (msg == (int)MSG.StartCycle)
  131. {
  132. //if (!IsAutoMode)
  133. {
  134. reason = String.Format("can not do {0}, isn't auto mode.", msg.ToString());
  135. return false;
  136. }
  137. }
  138. reason = "";
  139. return true;
  140. }
  141. void SubscribeDataVariable()
  142. {
  143. DATA.Subscribe("Rt.Status", () => ((RtState)fsm.State).ToString());
  144. DATA.Subscribe(ModuleName.System.ToString(), "AlarmEvent", EV.GetAlarmEvent);
  145. DATA.Subscribe("System.IsAutoMode", () => IsAutoMode);
  146. DATA.Subscribe("System.IsIdle", () => IsIdle || IsInit);
  147. DATA.Subscribe("System.IsAlarm", () => IsAlarm || IsEntityError);
  148. DATA.Subscribe("System.IsBusy", () => IsRunning);
  149. DATA.Subscribe("System.IsWaitUnload", () => /*_isWaitUnload && */IsAutoMode);
  150. //DATA.Subscribe("System.IsConnectedWithHost", () => Singleton<FaManager>.Instance.IsConnected);
  151. DATA.Subscribe("EquipmentMode", () => IsAutoMode ? 0 : 1, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  152. DATA.Subscribe("EquipmentStatus", () =>
  153. {
  154. //"0 = Uninit
  155. //1 = Idle
  156. //2 = Running
  157. //3 = Error
  158. //4 = Pause
  159. //"
  160. if (IsInit) return 0;
  161. if (IsIdle) return 1;
  162. if (IsAlarm) return 3;
  163. return 2;
  164. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  165. }
  166. void SubscribeOperation()
  167. {
  168. OP.Subscribe("CreateWafer", InvokeCreateWafer);
  169. OP.Subscribe("DeleteWafer", InvokeDeleteWafer);
  170. OP.Subscribe("System.Home", (cmd, args) => CheckToPostMessage((int)MSG.HOME, args));
  171. OP.Subscribe("TMCycle.Start", (cmd, args) => CheckToPostMessage((int)MSG.TMCycle, args));
  172. OP.Subscribe("TMCycle.Abort", (cmd, args) => CheckToPostMessage((int)MSG.StopCycle, args));
  173. DATA.Subscribe("SYSTEM.FsmState", () => (((RtState)fsm.State).ToString()));
  174. DATA.Subscribe("TMCycle.CycleIndex", () => (_TMCycle.CycleIndex));
  175. }
  176. public bool CheckToPostMessage(int msg, params object[] args)
  177. {
  178. if (!fsm.FindTransition(fsm.State, msg))
  179. {
  180. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(RtState)fsm.State} state,can not do {(MSG)msg}");
  181. return false;
  182. }
  183. Running = true;
  184. fsm.PostMsg(msg, args);
  185. return true;
  186. }
  187. private bool InvokeCreateWafer(string arg1, object[] args)
  188. {
  189. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  190. int slot = (int)args[1];
  191. WaferStatus state = WaferStatus.Normal;
  192. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  193. {
  194. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  195. {
  196. EV.PostInfoLog("System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot));
  197. }
  198. else if (WaferManager.Instance.CreateWafer(chamber, slot, state) != null)
  199. {
  200. //EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferCreate, chamber.ToString(), slot + 1, state.ToString());
  201. LOG.Write(eEvent.EV_WAFER_CREATE, ModuleName.System, chamber.ToString(), (slot + 1).ToString(), state.ToString());
  202. }
  203. }
  204. else
  205. {
  206. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  207. return false;
  208. }
  209. return true;
  210. }
  211. private bool InvokeDeleteWafer(string arg1, object[] args)
  212. {
  213. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  214. int slot = (int)args[1];
  215. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  216. {
  217. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  218. {
  219. WaferManager.Instance.DeleteWafer(chamber, slot);
  220. EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1);
  221. }
  222. else
  223. {
  224. EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1));
  225. }
  226. }
  227. else
  228. {
  229. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  230. return false;
  231. }
  232. return true;
  233. }
  234. public PMEntity GetPM(ModuleName mod)
  235. {
  236. if (ModuleHelper.IsInstalled(mod))
  237. {
  238. switch (mod)
  239. {
  240. case ModuleName.PMA:
  241. return PMA;
  242. case ModuleName.PMB:
  243. return PMB;
  244. case ModuleName.PMC:
  245. return PMC;
  246. case ModuleName.PMD:
  247. return PMD;
  248. }
  249. }
  250. return null;
  251. }
  252. public LLEntity GetLL(ModuleName mod)
  253. {
  254. if (ModuleHelper.IsInstalled(mod))
  255. {
  256. switch (mod)
  257. {
  258. case ModuleName.LLA:
  259. return LLA;
  260. case ModuleName.LLB:
  261. return LLB;
  262. }
  263. }
  264. return null;
  265. }
  266. public TMEntity GetTM()
  267. {
  268. return TM;
  269. }
  270. protected override bool Init()
  271. {
  272. PMA?.Initialize();
  273. PMB?.Initialize();
  274. TM?.Initialize();
  275. LLA?.Initialize();
  276. LLB?.Initialize();
  277. EFEM?.Initialize();
  278. _TMCycle = new TMCycle();
  279. _AutoCycle = new AutoCycle();
  280. BuildTransitionTable();
  281. return true;
  282. }
  283. private void BuildTransitionTable()
  284. {
  285. fsm = new StateMachine<RouteManager>(ModuleName.System.ToString(), (int)RtState.Init, 50);
  286. //Init sequence
  287. Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing);
  288. Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing);
  289. Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing);
  290. //// EnterExitTransition<RtState, FSM_MSG>(RtState.Initializing, fStartInit, FSM_MSG.NONE, null);
  291. ///
  292. Transition(RtState.Idle, FSM_MSG.TIMER, FsmMonitor, RtState.Idle);
  293. Transition(RtState.Init, FSM_MSG.TIMER, FsmMonitor, RtState.Init);
  294. Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle);
  295. Transition(RtState.Initializing, MSG.ERROR, FsmError, RtState.Error);
  296. Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init);
  297. // TM Cycle
  298. Transition(RtState.Idle, MSG.TMCycle, FsmStartTMCycle, RtState.TMCycle);
  299. Transition(RtState.TMCycle, FSM_MSG.TIMER, FsmMonitorTMCycle, RtState.Idle);
  300. Transition(RtState.TMCycle, MSG.StopCycle, FsmStopTMCycle, RtState.Idle);
  301. //Auto/manual
  302. Transition(RtState.Idle, MSG.SetAutoMode, FsmStartAutoTransfer, RtState.AutoIdle);
  303. Transition(RtState.AutoRunning, FSM_MSG.TIMER, FsmAutoTransfer, RtState.Idle);
  304. Transition(RtState.AutoRunning, MSG.ABORT, FsmAbortAutoTransfer, RtState.Idle);
  305. //Transition(RtState.AutoRunning, MSG.SetManualMode, FsmStartSetManualMode, RtState.Idle);
  306. Transition(RtState.AutoRunning, MSG.JobDone, FsmJobDone, RtState.AutoIdle);
  307. //Transition(RtState.AutoRunning, MSG.CassetteLeave, fCassetteLeave, RtState.AutoRunning); //For unload light control off afer job done
  308. Transition(RtState.AutoRunning, MSG.CreateJob, FsmCreateJob, RtState.AutoRunning);
  309. Transition(RtState.AutoRunning, MSG.StartJob, FsmStartJob, RtState.AutoRunning);
  310. Transition(RtState.AutoRunning, MSG.PauseJob, FsmPauseJob, RtState.AutoRunning);
  311. Transition(RtState.AutoRunning, MSG.ResumeJob, FsmResumeJob, RtState.AutoRunning);
  312. Transition(RtState.AutoRunning, MSG.StopJob, FsmStopJob, RtState.AutoRunning);
  313. Transition(RtState.AutoRunning, MSG.AbortJob, FsmAbortJob, RtState.AutoRunning);
  314. Transition(RtState.AutoRunning, MSG.Map, FsmMap, RtState.AutoRunning);
  315. Transition(RtState.AutoRunning, MSG.ResetIdleCleanTime, FsmResetIdleCleanTime, RtState.AutoRunning);
  316. Transition(RtState.AutoRunning, MSG.ResetIdlePurgeTime, FsmResetIdlePurgeTime, RtState.AutoRunning);
  317. Transition(RtState.AutoIdle, FSM_MSG.TIMER, FsmMonitorAutoIdle, RtState.AutoIdle);
  318. Transition(RtState.AutoIdle, MSG.SetManualMode, FsmStartSetManualMode, RtState.Idle);
  319. Transition(RtState.AutoIdle, MSG.CreateJob, FsmCreateJob, RtState.AutoIdle);
  320. Transition(RtState.AutoIdle, MSG.StartJob, FsmStartJob, RtState.AutoRunning);
  321. Transition(RtState.AutoIdle, MSG.PauseJob, FsmPauseJob, RtState.AutoIdle);
  322. Transition(RtState.AutoIdle, MSG.ResumeJob, FsmResumeJob, RtState.AutoIdle);
  323. Transition(RtState.AutoIdle, MSG.StopJob, FsmStopJob, RtState.AutoIdle);
  324. Transition(RtState.AutoIdle, MSG.AbortJob, FsmAbortJob, RtState.AutoIdle);
  325. Transition(RtState.AutoIdle, MSG.Map, FsmMap, RtState.AutoIdle);
  326. }
  327. private bool FsmMonitor(object[] objs)
  328. {
  329. _debugRoutine();
  330. return true;
  331. }
  332. private bool FsmStartHome(object[] objs)
  333. {
  334. PMA?.Invoke("Home");
  335. PMB?.Invoke("Home");
  336. PMC?.Invoke("Home");
  337. PMD?.Invoke("Home");
  338. TM?.Invoke("Home");
  339. LLA?.Invoke("Home");
  340. LLB?.Invoke("Home");
  341. return true;
  342. }
  343. private bool FsmMonitorHome(object[] objs)
  344. {
  345. bool CheckHomed(string name, bool bValid, bool bDone)
  346. {
  347. if (bValid && !bDone)
  348. {
  349. if (fsm.ElapsedTime > 20 * 1000)
  350. {
  351. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"{name} home timeout");
  352. PostMsg(MSG.ERROR);
  353. return true;
  354. }
  355. else
  356. return false;
  357. }
  358. return true;
  359. }
  360. return CheckHomed("PMA", PMA != null, PMA != null&&PMA.IsIdle) &&
  361. CheckHomed("PMB", PMB != null, PMB != null&&PMA.IsIdle) &&
  362. CheckHomed("PMC", PMC != null, PMC != null&&PMC.IsIdle) &&
  363. CheckHomed("PMD", PMD != null, PMD != null&&PMD.IsIdle) &&
  364. CheckHomed("LLA", LLA != null, LLA != null&&LLA.IsIdle) &&
  365. CheckHomed("LLB", LLB != null, LLB != null&&LLB.IsIdle) &&
  366. CheckHomed("TM", TM != null, TM != null&&TM.IsIdle);
  367. }
  368. private bool FsmError(object[] objs)
  369. {
  370. return true;
  371. }
  372. private bool FsmAbort(object[] objs)
  373. {
  374. return true;
  375. }
  376. private bool FsmStartTMCycle(object[] objs)
  377. {
  378. return _TMCycle.Start(objs) == RState.Running;
  379. }
  380. private bool FsmMonitorTMCycle(object[] objs)
  381. {
  382. RState ret = _TMCycle.Monitor();
  383. if (ret == RState.Failed || ret == RState.Timeout)
  384. {
  385. PostMsg(MSG.ERROR);
  386. return false;
  387. }
  388. return ret == RState.End;
  389. }
  390. private bool FsmStopTMCycle(object[] objs)
  391. {
  392. _TMCycle.Abort();
  393. return true;
  394. }
  395. private bool FsmStartAutoTransfer(object[] objs)
  396. {
  397. return _AutoCycle.Start(objs) == RState.Running;
  398. }
  399. private bool FsmAutoTransfer(object[] objs)
  400. {
  401. RState ret = _AutoCycle.Monitor();
  402. if (_AutoCycle.CheckJobJustDone(out string jobInfo))
  403. {
  404. EV.PostPopDialogMessage(EventLevel.InformationNoDelay, "Job complete", jobInfo);
  405. }
  406. if (_AutoCycle.CheckAllJobDone())
  407. {
  408. if (!CheckToPostMessage((int)MSG.JobDone))
  409. return false;
  410. }
  411. _isWaitUnload = (bool)DATA.Poll("LP1.NotifyJobDone") || (bool)DATA.Poll("LP2.NotifyJobDone");
  412. return ret == RState.End;
  413. }
  414. private bool FsmAbortAutoTransfer(object[] objs)
  415. {
  416. return true;
  417. }
  418. private bool FsmJobDone(object[] objs)
  419. {
  420. return true;
  421. }
  422. private bool FsmCreateJob(object[] objs)
  423. {
  424. return true;
  425. }
  426. private bool FsmStartJob(object[] objs)
  427. {
  428. return true;
  429. }
  430. private bool FsmPauseJob(object[] objs)
  431. {
  432. return true;
  433. }
  434. private bool FsmResumeJob(object[] objs)
  435. {
  436. return true;
  437. }
  438. private bool FsmStopJob(object[] objs)
  439. {
  440. return true;
  441. }
  442. private bool FsmAbortJob(object[] objs)
  443. {
  444. return true;
  445. }
  446. private bool FsmMap(object[] objs)
  447. {
  448. return true;
  449. }
  450. private bool FsmResetIdleCleanTime(object[] objs)
  451. {
  452. return true;
  453. }
  454. private bool FsmResetIdlePurgeTime(object[] objs)
  455. {
  456. return true;
  457. }
  458. private bool FsmMonitorAutoIdle(object[] objs)
  459. {
  460. return true;
  461. }
  462. private bool FsmStartSetManualMode(object[] objs)
  463. {
  464. return true;
  465. }
  466. private void _debugRoutine()
  467. {
  468. int flag = 0;
  469. // Test Home routine
  470. if (flag == 1)
  471. {
  472. PostMsg(MSG.HOME);
  473. }
  474. else if (flag == 2)
  475. {
  476. PostMsg(MSG.TMCycle);
  477. }
  478. }
  479. }
  480. }