RouteManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. private TMCycle _TMCycle;
  63. public RouteManager()
  64. {
  65. Name = "System";
  66. if (ModuleHelper.IsInstalled(ModuleName.PMA))
  67. PMA = new PMEntity(ModuleName.PMA);
  68. if (ModuleHelper.IsInstalled(ModuleName.PMB))
  69. PMB = new PMEntity(ModuleName.PMB);
  70. if (ModuleHelper.IsInstalled(ModuleName.PMC))
  71. PMC = new PMEntity(ModuleName.PMC);
  72. if (ModuleHelper.IsInstalled(ModuleName.PMD))
  73. PMD = new PMEntity(ModuleName.PMD);
  74. if (ModuleHelper.IsInstalled(ModuleName.TM))
  75. TM = new TMEntity();
  76. if (ModuleHelper.IsInstalled(ModuleName.LLA))
  77. LLA = new LLEntity(ModuleName.LLA);
  78. if (ModuleHelper.IsInstalled(ModuleName.LLB))
  79. LLB = new LLEntity(ModuleName.LLB);
  80. fsm = new StateMachine<RouteManager>(Name, (int)RtState.Init, 200);
  81. SubscribeOperation();
  82. }
  83. public bool Check(int msg, out string reason, params object[] args)
  84. {
  85. if (!fsm.FindTransition(fsm.State, msg))
  86. {
  87. reason = String.Format("{0} is in {1} state,can not do {2}", Name, 0, (MSG)msg);
  88. return false;
  89. }
  90. if (msg == (int)MSG.StartCycle)
  91. {
  92. //if (!IsAutoMode)
  93. {
  94. reason = String.Format("can not do {0}, isn't auto mode.", msg.ToString());
  95. return false;
  96. }
  97. }
  98. reason = "";
  99. return true;
  100. }
  101. void SubscribeOperation()
  102. {
  103. OP.Subscribe("CreateWafer", InvokeCreateWafer);
  104. OP.Subscribe("DeleteWafer", InvokeDeleteWafer);
  105. OP.Subscribe("System.Home", (cmd, args) => CheckToPostMessage((int)MSG.HOME, args));
  106. OP.Subscribe("TMCycle.Start", (cmd, args) => CheckToPostMessage((int)MSG.TMCycle, args));
  107. OP.Subscribe("TMCycle.Abort", (cmd, args) => CheckToPostMessage((int)MSG.ABORT, args));
  108. DATA.Subscribe("SYSTEM.FsmState", () => (((RtState)fsm.State).ToString()));
  109. DATA.Subscribe("TMCycle.CycleIndex", () => (_TMCycle.CycleIndex));
  110. }
  111. public bool CheckToPostMessage(int msg, params object[] args)
  112. {
  113. if (!fsm.FindTransition(fsm.State, msg))
  114. {
  115. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(RtState)fsm.State} state,can not do {(MSG)msg}");
  116. return false;
  117. }
  118. Running = true;
  119. fsm.PostMsg(msg, args);
  120. return true;
  121. }
  122. private bool InvokeCreateWafer(string arg1, object[] args)
  123. {
  124. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  125. int slot = (int)args[1];
  126. WaferStatus state = WaferStatus.Normal;
  127. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  128. {
  129. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  130. {
  131. EV.PostInfoLog("System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot));
  132. }
  133. else if (WaferManager.Instance.CreateWafer(chamber, slot, state) != null)
  134. {
  135. //EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferCreate, chamber.ToString(), slot + 1, state.ToString());
  136. LOG.Write(eEvent.EV_WAFER_CREATE, ModuleName.System, chamber.ToString(), (slot + 1).ToString(), state.ToString());
  137. }
  138. }
  139. else
  140. {
  141. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  142. return false;
  143. }
  144. return true;
  145. }
  146. private bool InvokeDeleteWafer(string arg1, object[] args)
  147. {
  148. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  149. int slot = (int)args[1];
  150. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  151. {
  152. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  153. {
  154. WaferManager.Instance.DeleteWafer(chamber, slot);
  155. EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1);
  156. }
  157. else
  158. {
  159. EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1));
  160. }
  161. }
  162. else
  163. {
  164. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  165. return false;
  166. }
  167. return true;
  168. }
  169. public PMEntity GetPM(ModuleName mod)
  170. {
  171. if (ModuleHelper.IsInstalled(mod))
  172. {
  173. switch (mod)
  174. {
  175. case ModuleName.PMA:
  176. return PMA;
  177. case ModuleName.PMB:
  178. return PMB;
  179. case ModuleName.PMC:
  180. return PMC;
  181. case ModuleName.PMD:
  182. return PMD;
  183. }
  184. }
  185. return null;
  186. }
  187. public LLEntity GetLL(ModuleName mod)
  188. {
  189. if (ModuleHelper.IsInstalled(mod))
  190. {
  191. switch (mod)
  192. {
  193. case ModuleName.LLA:
  194. return LLA;
  195. case ModuleName.LLB:
  196. return LLB;
  197. }
  198. }
  199. return null;
  200. }
  201. public TMEntity GetTM()
  202. {
  203. return TM;
  204. }
  205. protected override bool Init()
  206. {
  207. PMA?.Initialize();
  208. PMB?.Initialize();
  209. TM?.Initialize();
  210. LLA?.Initialize();
  211. LLB?.Initialize();
  212. _TMCycle = new TMCycle();
  213. BuildTransitionTable();
  214. return true;
  215. }
  216. private void BuildTransitionTable()
  217. {
  218. fsm = new StateMachine<RouteManager>(ModuleName.System.ToString(), (int)RtState.Init, 50);
  219. //Init sequence
  220. Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing);
  221. Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing);
  222. Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing);
  223. //// EnterExitTransition<RtState, FSM_MSG>(RtState.Initializing, fStartInit, FSM_MSG.NONE, null);
  224. ///
  225. Transition(RtState.Idle, FSM_MSG.TIMER, FsmMonitor, RtState.Idle);
  226. Transition(RtState.Init, FSM_MSG.TIMER, FsmMonitor, RtState.Init);
  227. Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle);
  228. Transition(RtState.Initializing, MSG.ERROR, FsmError, RtState.Error);
  229. Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init);
  230. // TM Cycle
  231. Transition(RtState.Idle, MSG.TMCycle, FsmStartTMCycle, RtState.TMCycle);
  232. Transition(RtState.TMCycle, FSM_MSG.TIMER, FsmMonitorTMCycle, RtState.Idle);
  233. Transition(RtState.TMCycle, MSG.ABORT, FsmAbort, RtState.Idle);
  234. }
  235. private bool FsmMonitor(object[] objs)
  236. {
  237. _debugRoutine();
  238. return true;
  239. }
  240. private bool FsmStartHome(object[] objs)
  241. {
  242. PMA?.Invoke("Home");
  243. PMB?.Invoke("Home");
  244. PMC?.Invoke("Home");
  245. PMD?.Invoke("Home");
  246. TM?.Invoke("Home");
  247. LLA?.Invoke("Home");
  248. LLB?.Invoke("Home");
  249. return true;
  250. }
  251. private bool FsmMonitorHome(object[] objs)
  252. {
  253. bool CheckHomed(string name, bool bValid, bool bDone)
  254. {
  255. if (bValid && !bDone)
  256. {
  257. if (fsm.ElapsedTime > 20 * 1000)
  258. {
  259. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"{name} home timeout");
  260. PostMsg(MSG.ERROR);
  261. return true;
  262. }
  263. else
  264. return false;
  265. }
  266. return true;
  267. }
  268. return CheckHomed("PMA", PMA != null, PMA != null&&PMA.IsIdle) &&
  269. CheckHomed("PMB", PMB != null, PMB != null&&PMA.IsIdle) &&
  270. CheckHomed("PMC", PMC != null, PMC != null&&PMC.IsIdle) &&
  271. CheckHomed("PMD", PMD != null, PMD != null&&PMD.IsIdle) &&
  272. CheckHomed("LLA", LLA != null, LLA != null&&LLA.IsIdle) &&
  273. CheckHomed("LLB", LLB != null, LLB != null&&LLB.IsIdle) &&
  274. CheckHomed("TM", TM != null, TM != null&&TM.IsIdle);
  275. }
  276. private bool FsmError(object[] objs)
  277. {
  278. return true;
  279. }
  280. private bool FsmAbort(object[] objs)
  281. {
  282. return true;
  283. }
  284. private bool FsmStartTMCycle(object[] objs)
  285. {
  286. return _TMCycle.Start(objs) == RState.Running;
  287. }
  288. private bool FsmMonitorTMCycle(object[] objs)
  289. {
  290. RState ret = _TMCycle.Monitor();
  291. if (ret == RState.Failed || ret == RState.Timeout)
  292. {
  293. PostMsg(MSG.ERROR);
  294. return false;
  295. }
  296. return ret == RState.End;
  297. }
  298. private void _debugRoutine()
  299. {
  300. int flag = 0;
  301. // Test Home routine
  302. if (flag == 1)
  303. {
  304. PostMsg(MSG.HOME);
  305. }
  306. else if (flag == 2)
  307. {
  308. PostMsg(MSG.TMCycle);
  309. }
  310. }
  311. }
  312. }