RouteManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aitex.Core.RT.Fsm;
  7. using Aitex.Core.Common;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.Routine;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.Util;
  14. using MECF.Framework.Common.Equipment;
  15. using MECF.Framework.Common.SubstrateTrackings;
  16. using Venus_Core;
  17. using Venus_RT.Modules.PMs;
  18. using Aitex.Core.RT.Log;
  19. namespace Venus_RT.Modules
  20. {
  21. class RouteManager : Entity, IEntity
  22. {
  23. public enum MSG
  24. {
  25. MoveWafer,
  26. ReturnWafer,
  27. HomeUnit,
  28. PauseAuto,
  29. ResumeAuto,
  30. Stop,
  31. StartCycle,
  32. HOME,
  33. RESET,
  34. ABORT,
  35. ERROR,
  36. SetAutoMode,
  37. SetManualMode,
  38. ResetIdleCleanTime,
  39. ResetIdlePurgeTime,
  40. CreateJob,
  41. PauseJob,
  42. ResumeJob,
  43. StartJob,
  44. StopJob,
  45. AbortJob,
  46. JobDone,
  47. CassetteLeave, //For unload light control off afer job done
  48. Map,
  49. ReturnAllWafer,
  50. TMCycle,
  51. }
  52. public PMEntity PMA { get; private set; }
  53. public PMEntity PMB { get; private set; }
  54. public PMEntity PMC { get; private set; }
  55. public PMEntity PMD { get; private set; }
  56. public TMEntity TM { get; private set; }
  57. public LLEntity LLA { get; private set; }
  58. public LLEntity LLB { get; private set; }
  59. public EfemEntity EFEM { get; private set; }
  60. public string Name { get; set; }
  61. private TMCycle _TMCycle;
  62. public RouteManager()
  63. {
  64. Name = "System";
  65. if (ModuleHelper.IsInstalled(ModuleName.PMA))
  66. PMA = new PMEntity(ModuleName.PMA);
  67. if (ModuleHelper.IsInstalled(ModuleName.PMB))
  68. PMB = new PMEntity(ModuleName.PMB);
  69. if (ModuleHelper.IsInstalled(ModuleName.PMC))
  70. PMC = new PMEntity(ModuleName.PMC);
  71. if (ModuleHelper.IsInstalled(ModuleName.PMD))
  72. PMD = new PMEntity(ModuleName.PMD);
  73. if (ModuleHelper.IsInstalled(ModuleName.TM))
  74. TM = new TMEntity();
  75. if (ModuleHelper.IsInstalled(ModuleName.LLA))
  76. LLA = new LLEntity(ModuleName.LLA);
  77. if (ModuleHelper.IsInstalled(ModuleName.LLB))
  78. LLB = new LLEntity(ModuleName.LLB);
  79. fsm = new StateMachine<RouteManager>(Name, (int)RtState.Init, 200);
  80. SubscribeOperation();
  81. }
  82. public bool Check(int msg, out string reason, params object[] args)
  83. {
  84. if (!fsm.FindTransition(fsm.State, msg))
  85. {
  86. reason = String.Format("{0} is in {1} state,can not do {2}", Name, 0, (MSG)msg);
  87. return false;
  88. }
  89. if (msg == (int)MSG.StartCycle)
  90. {
  91. //if (!IsAutoMode)
  92. {
  93. reason = String.Format("can not do {0}, isn't auto mode.", msg.ToString());
  94. return false;
  95. }
  96. }
  97. reason = "";
  98. return true;
  99. }
  100. void SubscribeOperation()
  101. {
  102. OP.Subscribe("CreateWafer", InvokeCreateWafer);
  103. OP.Subscribe("DeleteWafer", InvokeDeleteWafer);
  104. }
  105. private bool InvokeCreateWafer(string arg1, object[] args)
  106. {
  107. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  108. int slot = (int)args[1];
  109. WaferStatus state = WaferStatus.Normal;
  110. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  111. {
  112. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  113. {
  114. EV.PostInfoLog("System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot));
  115. }
  116. else if (WaferManager.Instance.CreateWafer(chamber, slot, state) != null)
  117. {
  118. //EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferCreate, chamber.ToString(), slot + 1, state.ToString());
  119. LOG.Write(eEvent.EV_WAFER_CREATE, ModuleName.System, chamber.ToString(), (slot + 1).ToString(), state.ToString());
  120. }
  121. }
  122. else
  123. {
  124. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  125. return false;
  126. }
  127. return true;
  128. }
  129. private bool InvokeDeleteWafer(string arg1, object[] args)
  130. {
  131. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  132. int slot = (int)args[1];
  133. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  134. {
  135. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  136. {
  137. WaferManager.Instance.DeleteWafer(chamber, slot);
  138. EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1);
  139. }
  140. else
  141. {
  142. EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1));
  143. }
  144. }
  145. else
  146. {
  147. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  148. return false;
  149. }
  150. return true;
  151. }
  152. public PMEntity GetPM(ModuleName mod)
  153. {
  154. if (ModuleHelper.IsInstalled(mod))
  155. {
  156. switch (mod)
  157. {
  158. case ModuleName.PMA:
  159. return PMA;
  160. case ModuleName.PMB:
  161. return PMB;
  162. case ModuleName.PMC:
  163. return PMC;
  164. case ModuleName.PMD:
  165. return PMD;
  166. }
  167. }
  168. return null;
  169. }
  170. public LLEntity GetLL(ModuleName mod)
  171. {
  172. if (ModuleHelper.IsInstalled(mod))
  173. {
  174. switch (mod)
  175. {
  176. case ModuleName.LLA:
  177. return LLA;
  178. case ModuleName.LLB:
  179. return LLB;
  180. }
  181. }
  182. return null;
  183. }
  184. protected override bool Init()
  185. {
  186. PMA?.Initialize();
  187. PMB?.Initialize();
  188. TM?.Initialize();
  189. LLA?.Initialize();
  190. LLB?.Initialize();
  191. _TMCycle = new TMCycle();
  192. BuildTransitionTable();
  193. return true;
  194. }
  195. private void BuildTransitionTable()
  196. {
  197. fsm = new StateMachine<RouteManager>(ModuleName.System.ToString(), (int)RtState.Init, 50);
  198. //Init sequence
  199. Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing);
  200. Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing);
  201. Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing);
  202. //// EnterExitTransition<RtState, FSM_MSG>(RtState.Initializing, fStartInit, FSM_MSG.NONE, null);
  203. ///
  204. Transition(RtState.Idle, FSM_MSG.TIMER, FsmMonitor, RtState.Idle);
  205. Transition(RtState.Init, FSM_MSG.TIMER, FsmMonitor, RtState.Init);
  206. Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle);
  207. Transition(RtState.Initializing, MSG.ERROR, FsmError, RtState.Error);
  208. Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init);
  209. // TM Cycle
  210. Transition(RtState.Idle, MSG.TMCycle, FsmStartTMCycle, RtState.TMCycle);
  211. Transition(RtState.TMCycle, FSM_MSG.TIMER, FsmMonitorTMCycle, RtState.Idle);
  212. Transition(RtState.TMCycle, MSG.ABORT, FsmAbort, RtState.Idle);
  213. }
  214. private bool FsmMonitor(object[] objs)
  215. {
  216. _debugRoutine();
  217. return true;
  218. }
  219. private bool FsmStartHome(object[] objs)
  220. {
  221. PMA?.Invoke("Home");
  222. PMB?.Invoke("Home");
  223. PMC?.Invoke("Home");
  224. PMD?.Invoke("Home");
  225. TM?.Invoke("Home");
  226. LLA?.Invoke("Home");
  227. LLB?.Invoke("Home");
  228. return true;
  229. }
  230. private bool FsmMonitorHome(object[] objs)
  231. {
  232. bool CheckHomed(string name, bool bValid, bool bDone)
  233. {
  234. if (bValid && !bDone)
  235. {
  236. if (fsm.ElapsedTime > 20 * 1000)
  237. {
  238. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"{name} home timeout");
  239. PostMsg(MSG.ERROR);
  240. return true;
  241. }
  242. else
  243. return false;
  244. }
  245. return true;
  246. }
  247. return CheckHomed("PMA", PMA != null, PMA != null&&PMA.IsIdle) &&
  248. CheckHomed("PMB", PMB != null, PMB != null&&PMA.IsIdle) &&
  249. CheckHomed("PMC", PMC != null, PMC != null&&PMC.IsIdle) &&
  250. CheckHomed("PMD", PMD != null, PMD != null&&PMD.IsIdle) &&
  251. CheckHomed("LLA", LLA != null, LLA != null&&LLA.IsIdle) &&
  252. CheckHomed("LLB", LLB != null, LLB != null&&LLB.IsIdle) &&
  253. CheckHomed("TM", TM != null, TM != null&&TM.IsIdle);
  254. }
  255. private bool FsmError(object[] objs)
  256. {
  257. return true;
  258. }
  259. private bool FsmAbort(object[] objs)
  260. {
  261. return true;
  262. }
  263. private bool FsmStartTMCycle(object[] objs)
  264. {
  265. return _TMCycle.Start() == RState.Running;
  266. }
  267. private bool FsmMonitorTMCycle(object[] objs)
  268. {
  269. RState ret = _TMCycle.Monitor();
  270. if (ret == RState.Failed || ret == RState.Timeout)
  271. {
  272. PostMsg(MSG.ERROR);
  273. return false;
  274. }
  275. return ret == RState.End;
  276. }
  277. private void _debugRoutine()
  278. {
  279. int flag = 0;
  280. // Test Home routine
  281. if (flag == 1)
  282. {
  283. PostMsg(MSG.HOME);
  284. }
  285. else if (flag == 2)
  286. {
  287. PostMsg(MSG.TMCycle);
  288. }
  289. }
  290. }
  291. }