RouteManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. public RouteManager()
  100. {
  101. Name = "System";
  102. if (ModuleHelper.IsInstalled(ModuleName.PMA))
  103. PMA = new PMEntity(ModuleName.PMA);
  104. if (ModuleHelper.IsInstalled(ModuleName.PMB))
  105. PMB = new PMEntity(ModuleName.PMB);
  106. if (ModuleHelper.IsInstalled(ModuleName.PMC))
  107. PMC = new PMEntity(ModuleName.PMC);
  108. if (ModuleHelper.IsInstalled(ModuleName.PMD))
  109. PMD = new PMEntity(ModuleName.PMD);
  110. if (ModuleHelper.IsInstalled(ModuleName.TM))
  111. TM = new TMEntity();
  112. if (ModuleHelper.IsInstalled(ModuleName.LLA))
  113. LLA = new LLEntity(ModuleName.LLA);
  114. if (ModuleHelper.IsInstalled(ModuleName.LLB))
  115. LLB = new LLEntity(ModuleName.LLB);
  116. if (ModuleHelper.IsInstalled(ModuleName.EFEM))
  117. EFEM = new EfemEntity();
  118. fsm = new StateMachine<RouteManager>(Name, (int)RtState.Init, 200);
  119. SubscribeOperation();
  120. }
  121. public bool Check(int msg, out string reason, params object[] args)
  122. {
  123. if (!fsm.FindTransition(fsm.State, msg))
  124. {
  125. reason = String.Format("{0} is in {1} state,can not do {2}", Name, 0, (MSG)msg);
  126. return false;
  127. }
  128. if (msg == (int)MSG.StartCycle)
  129. {
  130. //if (!IsAutoMode)
  131. {
  132. reason = String.Format("can not do {0}, isn't auto mode.", msg.ToString());
  133. return false;
  134. }
  135. }
  136. reason = "";
  137. return true;
  138. }
  139. void SubscribeDataVariable()
  140. {
  141. DATA.Subscribe("Rt.Status", () => ((RtState)fsm.State).ToString());
  142. DATA.Subscribe(ModuleName.System.ToString(), "AlarmEvent", EV.GetAlarmEvent);
  143. DATA.Subscribe("System.IsAutoMode", () => IsAutoMode);
  144. DATA.Subscribe("System.IsIdle", () => IsIdle || IsInit);
  145. DATA.Subscribe("System.IsAlarm", () => IsAlarm || IsEntityError);
  146. DATA.Subscribe("System.IsBusy", () => IsRunning);
  147. DATA.Subscribe("System.IsWaitUnload", () => /*_isWaitUnload && */IsAutoMode);
  148. //DATA.Subscribe("System.IsConnectedWithHost", () => Singleton<FaManager>.Instance.IsConnected);
  149. DATA.Subscribe("EquipmentMode", () => IsAutoMode ? 0 : 1, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  150. DATA.Subscribe("EquipmentStatus", () =>
  151. {
  152. //"0 = Uninit
  153. //1 = Idle
  154. //2 = Running
  155. //3 = Error
  156. //4 = Pause
  157. //"
  158. if (IsInit) return 0;
  159. if (IsIdle) return 1;
  160. if (IsAlarm) return 3;
  161. return 2;
  162. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  163. }
  164. void SubscribeOperation()
  165. {
  166. OP.Subscribe("CreateWafer", InvokeCreateWafer);
  167. OP.Subscribe("DeleteWafer", InvokeDeleteWafer);
  168. OP.Subscribe("System.Home", (cmd, args) => CheckToPostMessage((int)MSG.HOME, args));
  169. OP.Subscribe("TMCycle.Start", (cmd, args) => CheckToPostMessage((int)MSG.TMCycle, args));
  170. OP.Subscribe("TMCycle.Abort", (cmd, args) => CheckToPostMessage((int)MSG.StopCycle, args));
  171. DATA.Subscribe("SYSTEM.FsmState", () => (((RtState)fsm.State).ToString()));
  172. DATA.Subscribe("TMCycle.CycleIndex", () => (_TMCycle.CycleIndex));
  173. }
  174. public bool CheckToPostMessage(int msg, params object[] args)
  175. {
  176. if (!fsm.FindTransition(fsm.State, msg))
  177. {
  178. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(RtState)fsm.State} state,can not do {(MSG)msg}");
  179. return false;
  180. }
  181. Running = true;
  182. fsm.PostMsg(msg, args);
  183. return true;
  184. }
  185. private bool InvokeCreateWafer(string arg1, object[] args)
  186. {
  187. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  188. int slot = (int)args[1];
  189. WaferStatus state = WaferStatus.Normal;
  190. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  191. {
  192. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  193. {
  194. EV.PostInfoLog("System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot));
  195. }
  196. else if (WaferManager.Instance.CreateWafer(chamber, slot, state) != null)
  197. {
  198. //EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferCreate, chamber.ToString(), slot + 1, state.ToString());
  199. LOG.Write(eEvent.EV_WAFER_CREATE, ModuleName.System, chamber.ToString(), (slot + 1).ToString(), state.ToString());
  200. }
  201. }
  202. else
  203. {
  204. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  205. return false;
  206. }
  207. return true;
  208. }
  209. private bool InvokeDeleteWafer(string arg1, object[] args)
  210. {
  211. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  212. int slot = (int)args[1];
  213. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  214. {
  215. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  216. {
  217. WaferManager.Instance.DeleteWafer(chamber, slot);
  218. EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1);
  219. }
  220. else
  221. {
  222. EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1));
  223. }
  224. }
  225. else
  226. {
  227. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  228. return false;
  229. }
  230. return true;
  231. }
  232. public PMEntity GetPM(ModuleName mod)
  233. {
  234. if (ModuleHelper.IsInstalled(mod))
  235. {
  236. switch (mod)
  237. {
  238. case ModuleName.PMA:
  239. return PMA;
  240. case ModuleName.PMB:
  241. return PMB;
  242. case ModuleName.PMC:
  243. return PMC;
  244. case ModuleName.PMD:
  245. return PMD;
  246. }
  247. }
  248. return null;
  249. }
  250. public LLEntity GetLL(ModuleName mod)
  251. {
  252. if (ModuleHelper.IsInstalled(mod))
  253. {
  254. switch (mod)
  255. {
  256. case ModuleName.LLA:
  257. return LLA;
  258. case ModuleName.LLB:
  259. return LLB;
  260. }
  261. }
  262. return null;
  263. }
  264. public TMEntity GetTM()
  265. {
  266. return TM;
  267. }
  268. protected override bool Init()
  269. {
  270. PMA?.Initialize();
  271. PMB?.Initialize();
  272. TM?.Initialize();
  273. LLA?.Initialize();
  274. LLB?.Initialize();
  275. EFEM?.Initialize();
  276. _TMCycle = new TMCycle();
  277. BuildTransitionTable();
  278. return true;
  279. }
  280. private void BuildTransitionTable()
  281. {
  282. fsm = new StateMachine<RouteManager>(ModuleName.System.ToString(), (int)RtState.Init, 50);
  283. //Init sequence
  284. Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing);
  285. Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing);
  286. Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing);
  287. //// EnterExitTransition<RtState, FSM_MSG>(RtState.Initializing, fStartInit, FSM_MSG.NONE, null);
  288. ///
  289. Transition(RtState.Idle, FSM_MSG.TIMER, FsmMonitor, RtState.Idle);
  290. Transition(RtState.Init, FSM_MSG.TIMER, FsmMonitor, RtState.Init);
  291. Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle);
  292. Transition(RtState.Initializing, MSG.ERROR, FsmError, RtState.Error);
  293. Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init);
  294. // TM Cycle
  295. Transition(RtState.Idle, MSG.TMCycle, FsmStartTMCycle, RtState.TMCycle);
  296. Transition(RtState.TMCycle, FSM_MSG.TIMER, FsmMonitorTMCycle, RtState.Idle);
  297. Transition(RtState.TMCycle, MSG.StopCycle, FsmStopTMCycle, RtState.Idle);
  298. }
  299. private bool FsmMonitor(object[] objs)
  300. {
  301. _debugRoutine();
  302. return true;
  303. }
  304. private bool FsmStartHome(object[] objs)
  305. {
  306. PMA?.Invoke("Home");
  307. PMB?.Invoke("Home");
  308. PMC?.Invoke("Home");
  309. PMD?.Invoke("Home");
  310. TM?.Invoke("Home");
  311. LLA?.Invoke("Home");
  312. LLB?.Invoke("Home");
  313. return true;
  314. }
  315. private bool FsmMonitorHome(object[] objs)
  316. {
  317. bool CheckHomed(string name, bool bValid, bool bDone)
  318. {
  319. if (bValid && !bDone)
  320. {
  321. if (fsm.ElapsedTime > 20 * 1000)
  322. {
  323. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"{name} home timeout");
  324. PostMsg(MSG.ERROR);
  325. return true;
  326. }
  327. else
  328. return false;
  329. }
  330. return true;
  331. }
  332. return CheckHomed("PMA", PMA != null, PMA != null&&PMA.IsIdle) &&
  333. CheckHomed("PMB", PMB != null, PMB != null&&PMA.IsIdle) &&
  334. CheckHomed("PMC", PMC != null, PMC != null&&PMC.IsIdle) &&
  335. CheckHomed("PMD", PMD != null, PMD != null&&PMD.IsIdle) &&
  336. CheckHomed("LLA", LLA != null, LLA != null&&LLA.IsIdle) &&
  337. CheckHomed("LLB", LLB != null, LLB != null&&LLB.IsIdle) &&
  338. CheckHomed("TM", TM != null, TM != null&&TM.IsIdle);
  339. }
  340. private bool FsmError(object[] objs)
  341. {
  342. return true;
  343. }
  344. private bool FsmAbort(object[] objs)
  345. {
  346. return true;
  347. }
  348. private bool FsmStartTMCycle(object[] objs)
  349. {
  350. return _TMCycle.Start(objs) == RState.Running;
  351. }
  352. private bool FsmMonitorTMCycle(object[] objs)
  353. {
  354. RState ret = _TMCycle.Monitor();
  355. if (ret == RState.Failed || ret == RState.Timeout)
  356. {
  357. PostMsg(MSG.ERROR);
  358. return false;
  359. }
  360. return ret == RState.End;
  361. }
  362. private bool FsmStopTMCycle(object[] objs)
  363. {
  364. _TMCycle.Abort();
  365. return true;
  366. }
  367. private void _debugRoutine()
  368. {
  369. int flag = 0;
  370. // Test Home routine
  371. if (flag == 1)
  372. {
  373. PostMsg(MSG.HOME);
  374. }
  375. else if (flag == 2)
  376. {
  377. PostMsg(MSG.TMCycle);
  378. }
  379. }
  380. }
  381. }