RouteManager.cs 14 KB

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