LoadPortEntity.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Fsm;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.RT.OperationCenter;
  7. using Aitex.Core.RT.Routine;
  8. using Aitex.Core.Util;
  9. using EFEM.RT.Modules;
  10. using EFEM.RT.Routines.LP;
  11. using MECF.Framework.Common.Equipment;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  13. namespace EFEM.RT.Devices
  14. {
  15. class LoadPortEntity : Entity, IEntity
  16. {
  17. public enum STATE
  18. {
  19. INIT,
  20. IDLE,
  21. HOME,
  22. LOAD,
  23. UNLOAD,
  24. READ,
  25. WRITE,
  26. ReadAndLoad,
  27. ERROR,
  28. Reset,
  29. };
  30. public enum MSG
  31. {
  32. HOME,
  33. ReadRFID,
  34. WriteRFID,
  35. Load,
  36. Unload,
  37. ReadAndLoad,
  38. RESET,
  39. ABORT,
  40. ERROR,
  41. };
  42. //routine
  43. private HomeRoutine homeRoutine = null;
  44. private LoadFoupRoutine loadRoutine = null;
  45. private UnloadFoupRoutine unloadRoutine = null;
  46. private ReadAndLoadRoutine readAndLoadRoutine = null;
  47. public string Name { get; set; }
  48. public bool IsIdle
  49. {
  50. get
  51. {
  52. return fsm.State == (int)LoadPortEntity.STATE.IDLE;
  53. }
  54. }
  55. public bool IsRunning
  56. {
  57. get
  58. {
  59. return fsm.State != (int)LoadPortEntity.STATE.INIT
  60. && fsm.State != (int)LoadPortEntity.STATE.IDLE
  61. && fsm.State != (int)LoadPortEntity.STATE.ERROR;
  62. }
  63. }
  64. public int FsmState
  65. {
  66. get
  67. {
  68. return fsm.State;
  69. }
  70. }
  71. public bool IsAlarm { get { return false; } }
  72. public bool IsWarning { get { return false; } }
  73. private ModuleName _chamber;
  74. public LoadPortEntity(string name)
  75. {
  76. Name = name;
  77. _chamber = (ModuleName)Enum.Parse(typeof(ModuleName), Name);
  78. fsm = new StateMachine<LoadPortEntity>(Name, (int)LoadPortEntity.STATE.INIT, 50);
  79. AnyStateTransition(LoadPortEntity.MSG.ERROR, fError, LoadPortEntity.STATE.ERROR);
  80. AnyStateTransition((int)FSM_MSG.ALARM, fError, (int)LoadPortEntity.STATE.ERROR);
  81. AnyStateTransition(LoadPortEntity.MSG.RESET, fInit, LoadPortEntity.STATE.IDLE);
  82. Transition(LoadPortEntity.STATE.INIT, FSM_MSG.TIMER, fInit, LoadPortEntity.STATE.IDLE);
  83. Transition(LoadPortEntity.STATE.INIT, LoadPortEntity.MSG.ERROR, fError, LoadPortEntity.STATE.ERROR);
  84. Transition(LoadPortEntity.STATE.ERROR, LoadPortEntity.MSG.HOME, fStartHome, LoadPortEntity.STATE.HOME);
  85. Transition(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.HOME, fStartHome, LoadPortEntity.STATE.HOME);
  86. Transition(LoadPortEntity.STATE.HOME, FSM_MSG.TIMER, fHome, LoadPortEntity.STATE.IDLE);
  87. Transition(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.Load, fStartLoad, LoadPortEntity.STATE.LOAD);
  88. Transition(LoadPortEntity.STATE.LOAD, FSM_MSG.TIMER, fLoad, LoadPortEntity.STATE.IDLE);
  89. Transition(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.Unload, fStartUnload, LoadPortEntity.STATE.UNLOAD);
  90. Transition(LoadPortEntity.STATE.UNLOAD, FSM_MSG.TIMER, fUnload, LoadPortEntity.STATE.IDLE);
  91. Transition(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.ReadAndLoad, fStartReadAndLoad, LoadPortEntity.STATE.ReadAndLoad);
  92. Transition(LoadPortEntity.STATE.ReadAndLoad, FSM_MSG.TIMER, fReadAndLoad, LoadPortEntity.STATE.IDLE);
  93. Running = true;
  94. }
  95. public bool Check(int msg, out string reason, object[] objs)
  96. {
  97. bool bRet = true;
  98. reason = "";
  99. switch (msg)
  100. {
  101. case (int)LoadPortEntity.MSG.Load:
  102. {
  103. if (fsm.State != (int)LoadPortEntity.STATE.IDLE)
  104. {
  105. reason = String.Format("{0} is busy {1},can't excute {2}", Name, State2String(fsm.State), Msg2String(msg));
  106. return false;
  107. }
  108. }
  109. break;
  110. default:
  111. break;
  112. }
  113. return bRet;
  114. }
  115. private string Msg2String(int msg)
  116. {
  117. string result = "";
  118. switch (msg)
  119. {
  120. case (int)LoadPortEntity.MSG.RESET:
  121. result = "复位";
  122. break;
  123. case (int)LoadPortEntity.MSG.ABORT:
  124. result = "终止";
  125. break;
  126. }
  127. return result;
  128. }
  129. private string State2String(int state)
  130. {
  131. string result = "";
  132. switch (state)
  133. {
  134. case (int)LoadPortEntity.STATE.INIT:
  135. case (int)LoadPortEntity.STATE.Reset:
  136. result = "初始化";
  137. break;
  138. case (int)LoadPortEntity.STATE.IDLE:
  139. result = "就绪";
  140. break;
  141. case (int)LoadPortEntity.STATE.ERROR:
  142. result = "错误";
  143. break;
  144. }
  145. return result;
  146. }
  147. protected override bool Init()
  148. {
  149. loadRoutine = new LoadFoupRoutine(Name, "Load");
  150. loadRoutine.Chamber = _chamber;
  151. loadRoutine.Initalize();
  152. unloadRoutine = new UnloadFoupRoutine();
  153. unloadRoutine.Initalize();
  154. unloadRoutine.Chamber = _chamber;
  155. homeRoutine = new HomeRoutine(Name, "Home");
  156. homeRoutine.Initalize();
  157. homeRoutine.Chamber = _chamber;
  158. readAndLoadRoutine = new ReadAndLoadRoutine(Name, "Read And Load");
  159. readAndLoadRoutine.Initalize();
  160. readAndLoadRoutine.Chamber = _chamber;
  161. OP.Subscribe($"{_chamber}.LPReset", (string cmd, object[] args) =>
  162. {
  163. if (FsmState != 8) return true;
  164. return CheckToPostMsg(MSG.RESET);
  165. });
  166. return true;
  167. }
  168. protected override void Term()
  169. {
  170. }
  171. #region
  172. private bool fStartHome(object[] objs)
  173. {
  174. Running = false;
  175. Result ret = homeRoutine.Start(objs);
  176. if (ret == Result.DONE)
  177. {
  178. return false;
  179. }
  180. else if (ret == Result.FAIL)
  181. {
  182. return false; //do noting
  183. }
  184. return true;
  185. }
  186. private bool fHome(object[] objs)
  187. {
  188. Result ret = homeRoutine.Monitor();
  189. if (ret == Result.DONE)
  190. {
  191. Running = true;
  192. return true;
  193. }
  194. else if (ret == Result.FAIL)
  195. {
  196. //do nothing
  197. //state = (int)STATE.ERROR;
  198. return true;
  199. }
  200. return false;
  201. }
  202. private bool fStartLoad(object[] objs)
  203. {
  204. Running = false;
  205. Result ret = loadRoutine.Start(objs);
  206. if (ret == Result.DONE)
  207. {
  208. return false;
  209. }
  210. else if (ret == Result.FAIL)
  211. {
  212. return false; //do noting
  213. }
  214. return true;
  215. }
  216. private bool fLoad(object[] objs)
  217. {
  218. Result ret = loadRoutine.Monitor();
  219. if (ret == Result.DONE)
  220. {
  221. Running = true;
  222. return true;
  223. }
  224. else if (ret == Result.FAIL)
  225. {
  226. PostMsg(MSG.RESET);
  227. LoadPortBaseDevice lp = DEVICE.GetDevice<LoadPortBaseDevice>(Name);
  228. if (lp != null)
  229. {
  230. lp.CheckToPostMessage(LoadPortMsg.Error);
  231. }
  232. return true;
  233. }
  234. return false;
  235. }
  236. private bool fStartUnload(object[] objs)
  237. {
  238. Running = false;
  239. Result ret = unloadRoutine.Start(objs);
  240. if (ret == Result.DONE)
  241. {
  242. return false;
  243. }
  244. else if (ret == Result.FAIL)
  245. {
  246. return false; //do noting
  247. }
  248. return true;
  249. }
  250. private bool fUnload(object[] objs)
  251. {
  252. Result ret = unloadRoutine.Monitor();
  253. if (ret == Result.DONE)
  254. {
  255. Running = true;
  256. return true;
  257. }
  258. else if (ret == Result.FAIL)
  259. {
  260. //do nothing
  261. //state = (int)STATE.ERROR;
  262. PostMsg(MSG.RESET);
  263. LoadPortBaseDevice lp = DEVICE.GetDevice<LoadPortBaseDevice>(Name);
  264. if (lp != null)
  265. {
  266. lp.CheckToPostMessage(LoadPortMsg.Error);
  267. }
  268. return true;
  269. }
  270. return false;
  271. }
  272. private bool fStartReadAndLoad(object[] objs)
  273. {
  274. Running = false;
  275. Result ret = readAndLoadRoutine.Start(objs);
  276. if (ret == Result.DONE)
  277. {
  278. return false;
  279. }
  280. else if (ret == Result.FAIL)
  281. {
  282. return false; //do noting
  283. }
  284. return true;
  285. }
  286. private bool fReadAndLoad(object[] objs)
  287. {
  288. Result ret = readAndLoadRoutine.Monitor();
  289. if (ret == Result.DONE)
  290. {
  291. Running = true;
  292. return true;
  293. }
  294. else if (ret == Result.FAIL)
  295. {
  296. //do nothing
  297. //state = (int)STATE.ERROR;
  298. PostMsg(MSG.ERROR);
  299. return false;
  300. }
  301. return false;
  302. }
  303. private bool fInit(object[] objs)
  304. {
  305. return true;
  306. }
  307. private bool fError(object[] objs)
  308. {
  309. Singleton<RouteManager>.Instance.CheckToPostMsg(RouteManager.MSG.ERROR);
  310. return true;
  311. }
  312. public bool CheckToPostMsg(MSG msg)
  313. {
  314. if (!fsm.FindTransition(fsm.State, (int)msg))
  315. {
  316. EV.PostMessage("System", EventEnum.DefaultWarning,
  317. string.Format("{0} is in {1} state,can not do {2}", Name, (STATE)fsm.State, (MSG)msg));
  318. return false;
  319. }
  320. PostMsg(msg);
  321. return true;
  322. }
  323. #endregion
  324. }
  325. }