RouteManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. }
  120. }
  121. else
  122. {
  123. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  124. return false;
  125. }
  126. return true;
  127. }
  128. private bool InvokeDeleteWafer(string arg1, object[] args)
  129. {
  130. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  131. int slot = (int)args[1];
  132. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  133. {
  134. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  135. {
  136. WaferManager.Instance.DeleteWafer(chamber, slot);
  137. EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1);
  138. }
  139. else
  140. {
  141. EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1));
  142. }
  143. }
  144. else
  145. {
  146. EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  147. return false;
  148. }
  149. return true;
  150. }
  151. public PMEntity GetPM(ModuleName mod)
  152. {
  153. if (ModuleHelper.IsInstalled(mod))
  154. {
  155. switch (mod)
  156. {
  157. case ModuleName.PMA:
  158. return PMA;
  159. case ModuleName.PMB:
  160. return PMB;
  161. case ModuleName.PMC:
  162. return PMC;
  163. case ModuleName.PMD:
  164. return PMD;
  165. }
  166. }
  167. return null;
  168. }
  169. public LLEntity GetLL(ModuleName mod)
  170. {
  171. if (ModuleHelper.IsInstalled(mod))
  172. {
  173. switch (mod)
  174. {
  175. case ModuleName.LLA:
  176. return LLA;
  177. case ModuleName.LLB:
  178. return LLB;
  179. }
  180. }
  181. return null;
  182. }
  183. protected override bool Init()
  184. {
  185. PMA?.Initialize();
  186. PMB?.Initialize();
  187. TM?.Initialize();
  188. LLA?.Initialize();
  189. LLB?.Initialize();
  190. _TMCycle = new TMCycle();
  191. BuildTransitionTable();
  192. return true;
  193. }
  194. private void BuildTransitionTable()
  195. {
  196. fsm = new StateMachine<RouteManager>(ModuleName.System.ToString(), (int)RtState.Init, 50);
  197. //Init sequence
  198. Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing);
  199. Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing);
  200. Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing);
  201. //// EnterExitTransition<RtState, FSM_MSG>(RtState.Initializing, fStartInit, FSM_MSG.NONE, null);
  202. ///
  203. Transition(RtState.Idle, FSM_MSG.TIMER, FsmMonitor, RtState.Idle);
  204. Transition(RtState.Init, FSM_MSG.TIMER, FsmMonitor, RtState.Init);
  205. Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle);
  206. Transition(RtState.Initializing, MSG.ERROR, FsmError, RtState.Error);
  207. Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init);
  208. // TM Cycle
  209. Transition(RtState.Idle, MSG.TMCycle, FsmStartTMCycle, RtState.TMCycle);
  210. Transition(RtState.TMCycle, FSM_MSG.TIMER, FsmMonitorTMCycle, RtState.Idle);
  211. Transition(RtState.TMCycle, MSG.ABORT, FsmAbort, RtState.Idle);
  212. }
  213. private bool FsmMonitor(object[] objs)
  214. {
  215. _debugRoutine();
  216. return true;
  217. }
  218. private bool FsmStartHome(object[] objs)
  219. {
  220. PMA?.Invoke("Home");
  221. PMB?.Invoke("Home");
  222. PMC?.Invoke("Home");
  223. PMD?.Invoke("Home");
  224. TM?.Invoke("Home");
  225. LLA?.Invoke("Home");
  226. LLB?.Invoke("Home");
  227. return true;
  228. }
  229. private bool FsmMonitorHome(object[] objs)
  230. {
  231. bool CheckHomed(string name, bool bValid, bool bDone)
  232. {
  233. if (bValid && !bDone)
  234. {
  235. if (fsm.ElapsedTime > 20 * 1000)
  236. {
  237. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"{name} home timeout");
  238. PostMsg(MSG.ERROR);
  239. return true;
  240. }
  241. else
  242. return false;
  243. }
  244. return true;
  245. }
  246. return CheckHomed("PMA", PMA != null, PMA != null&&PMA.IsIdle) &&
  247. CheckHomed("PMB", PMB != null, PMB != null&&PMA.IsIdle) &&
  248. CheckHomed("PMC", PMC != null, PMC != null&&PMC.IsIdle) &&
  249. CheckHomed("PMD", PMD != null, PMD != null&&PMD.IsIdle) &&
  250. CheckHomed("LLA", LLA != null, LLA != null&&LLA.IsIdle) &&
  251. CheckHomed("LLB", LLB != null, LLB != null&&LLB.IsIdle) &&
  252. CheckHomed("TM", TM != null, TM != null&&TM.IsIdle);
  253. }
  254. private bool FsmError(object[] objs)
  255. {
  256. return true;
  257. }
  258. private bool FsmAbort(object[] objs)
  259. {
  260. return true;
  261. }
  262. private bool FsmStartTMCycle(object[] objs)
  263. {
  264. return _TMCycle.Start() == RState.Running;
  265. }
  266. private bool FsmMonitorTMCycle(object[] objs)
  267. {
  268. RState ret = _TMCycle.Monitor();
  269. if (ret == RState.Failed || ret == RState.Timeout)
  270. {
  271. PostMsg(MSG.ERROR);
  272. return false;
  273. }
  274. return ret == RState.End;
  275. }
  276. private void _debugRoutine()
  277. {
  278. int flag = 0;
  279. // Test Home routine
  280. if (flag == 1)
  281. {
  282. PostMsg(MSG.HOME);
  283. }
  284. else if (flag == 2)
  285. {
  286. PostMsg(MSG.TMCycle);
  287. }
  288. }
  289. }
  290. }