RouteManager.cs 12 KB

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