EfemEntity.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. using System;
  2. using Aitex.Core.Common;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.Fsm;
  6. using Aitex.Core.RT.OperationCenter;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Utilities;
  9. using Aitex.Sorter.Common;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.Schedulers;
  12. using MECF.Framework.Common.SubstrateTrackings;
  13. using Venus_Core;
  14. using Venus_RT;
  15. using Venus_RT.Devices;
  16. using Venus_RT.Devices.YASKAWA;
  17. using Venus_RT.Devices.EFEM;
  18. using Venus_RT.Modules.LPs;
  19. using Venus_RT.Modules.EFEM;
  20. namespace Venus_RT.Modules
  21. {
  22. class EfemEntity : Entity, IEntity, IModuleEntity
  23. {
  24. private int _bigWafer = 0;
  25. private int _midWafer = 0;
  26. private int _smallWafer = 0;
  27. public enum STATE
  28. {
  29. Unknown, // 0
  30. Initializing, // 1
  31. Idle, // 2
  32. Error, // 3
  33. Picking, // 4
  34. Placing, // 5
  35. Aligning, // 6
  36. Mapping, // 7
  37. Init, // 8
  38. Orgshing, // 9
  39. Lifting, // 10
  40. InitingAL, // 11
  41. InitingRB, // 12
  42. Extending, // 13
  43. Retracting, // 14
  44. //SettingLamp, // 15
  45. Swapping,
  46. Gotoing,
  47. Gripping,
  48. Ungripping,
  49. Fliping,
  50. }
  51. public enum MSG
  52. {
  53. HomeAll, // 0
  54. Pick, // 1
  55. Place, // 2
  56. Align, // 3
  57. ActionDone, // 4
  58. RecHwMsg, // 5
  59. MoveCmd, // 6
  60. //LED, // 7
  61. Recover, // 8
  62. Goto, // 9
  63. Error, // 10
  64. Online, // 11
  65. CommReady, // 12
  66. Lift, // 13
  67. HomeAL, // 14
  68. HomeRB, // 15
  69. Extend, // 16
  70. Retract, // 17
  71. PMLiftPinUp, // 18
  72. PMLiftPinDown, // 19
  73. TurnOffBuzzer,
  74. //SwitchOnBuzzerAndRed,
  75. Abort,
  76. Map,
  77. ToInit,
  78. Cool,
  79. Swap,
  80. Grip,
  81. Ungrip,
  82. Flip,
  83. LiftActionDone,
  84. }
  85. public enum EfemType
  86. {
  87. FutureEfem = 1,
  88. JetEfem = 2,
  89. BrooksEFEM = 3,
  90. }
  91. public bool Check(int msg, out string reason, params object[] args)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. // Fields
  96. //
  97. private readonly string Name;
  98. private readonly EfemBase _efem;
  99. private readonly LoadPortModule[] _lpms = new LoadPortModule[2];
  100. private readonly EfemType _efemType;
  101. public EfemBase EfemDevice => _efem;
  102. public EfemType EFEMType => _efemType;
  103. // routine
  104. private readonly EfemPickRoutine _pickRoutine;
  105. private readonly EfemPlaceRoutine _placeRoutine;
  106. private readonly EfemSwapRoutine _swapRoutine;
  107. // Constructor
  108. //
  109. public EfemEntity()
  110. {
  111. _smallWafer = SC.GetValue<int>($"System.SmallWafer");
  112. _midWafer = SC.GetValue<int>($"System.MidWafer");
  113. _bigWafer = SC.GetValue<int>($"System.BigWafer");
  114. _efemType = (EfemType)SC.GetValue<int>($"EFEM.EfemType");
  115. _efem = new JetEfem();
  116. Name = ModuleName.EFEM.ToString();
  117. _pickRoutine = new EfemPickRoutine(_efem);
  118. _placeRoutine = new EfemPlaceRoutine(_efem);
  119. _swapRoutine = new EfemSwapRoutine(_efem);
  120. InitFsmMap();
  121. }
  122. public void NotifyLP(ModuleName mod, LoadportEntity.MSG msg)
  123. {
  124. _lpms[mod - ModuleName.LP1].PostMsg(msg);
  125. }
  126. public void NotifyLPError(ModuleName mod )
  127. {
  128. _lpms[mod - ModuleName.LP1].PostMsg(LoadportEntity.MSG.ActionDone);
  129. //_lpms[mod - ModuleName.LP1]..OnError();
  130. }
  131. protected override bool Init()
  132. {
  133. _lpms[0] = new LoadPortModule(ModuleName.LP1, _efem);
  134. _lpms[1] = new LoadPortModule(ModuleName.LP2, _efem);
  135. _lpms[0].Initialize();
  136. _lpms[1].Initialize();
  137. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAll); return true; });
  138. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.ClearError}", (cmd, args) => { PostMsg(MSG.Recover); return true; });
  139. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.TurnOffBuzzer}", (cmd, args) => { PostMsg(MSG.TurnOffBuzzer); return true; });
  140. //OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.SwitchOnBuzzerAndRed}", (cmd, args) => { PostMsg(MSG.SwitchOnBuzzerAndRed); return true; });
  141. OP.Subscribe($"{ModuleName.EFEM}.Online", (cmd, args) => { PostMsg(MSG.Online); return true; });
  142. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Pick}", (cmd, args) => { PostMsg(MSG.Pick, args[0], args[1], args[3], args[2]); return true; });
  143. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Place}", (cmd, args) => { PostMsg(MSG.Place, args[0], args[1], args[3], args[2]); return true; });
  144. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Extend}", (cmd, args) => { PostMsg(MSG.Extend, args[0], args[1], args[2]); return true; });
  145. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Retract}", (cmd, args) => { PostMsg(MSG.Retract, args[0], args[1]); return true; });
  146. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Abort}", (cmd, args) => { PostMsg(MSG.Abort); return true; });
  147. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeRB); return true; });
  148. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Grip}", (cmd, args) =>
  149. {
  150. bool isGrip = ((string)args[0]).ToLower() == "on";
  151. PostMsg(isGrip ? MSG.Grip : MSG.Ungrip, args[1]);
  152. return true;
  153. });
  154. //OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Flip}", (cmd, args) => { PostMsg(MSG.Flip, args[0]); return true; });
  155. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Aligner1); return true; });
  156. OP.Subscribe($"{ModuleName.Aligner2}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Aligner2); return true; });
  157. OP.Subscribe($"{ModuleName.Cooling1}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Cooling1); return true; });
  158. OP.Subscribe($"{ModuleName.Cooling2}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Cooling2); return true; });
  159. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Aligner1, args[0]); return true; });
  160. OP.Subscribe($"{ModuleName.Aligner2}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Aligner2, args[0]); return true; });
  161. OP.Subscribe($"{ModuleName.Cooling1}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Cooling1, args[0]); return true; });
  162. OP.Subscribe($"{ModuleName.Cooling2}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Cooling2, args[0]); return true; });
  163. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Aligner1); return true; });
  164. OP.Subscribe($"{ModuleName.Aligner2}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Aligner2); return true; });
  165. OP.Subscribe($"{ModuleName.Cooling1}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Cooling1); return true; });
  166. OP.Subscribe($"{ModuleName.Cooling2}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Cooling2); return true; });
  167. DATA.Subscribe($"{Name}.FsmState", () => ((STATE)fsm.State).ToString());
  168. DATA.Subscribe($"{Name}.FsmPrevState", () => ((STATE)fsm.PrevState).ToString());
  169. DATA.Subscribe($"{Name}.FsmLastMessage", GetFsmLastMessage);
  170. DATA.Subscribe($"{Name}.SmallWafer", () => _smallWafer);
  171. DATA.Subscribe($"{Name}.BigWafer", () => _bigWafer);
  172. DATA.Subscribe($"{Name}.MidWafer", () => _midWafer);
  173. return true;
  174. }
  175. private void InitFsmMap()
  176. {
  177. fsm = new StateMachine<EfemEntity>("EFEM", (int)STATE.Unknown, 50);
  178. fsm.EnableRepeatedMsg(true);
  179. AnyStateTransition(FSM_MSG.TIMER, fnMonitor, FSM_STATE.SAME);
  180. AnyStateTransition(MSG.TurnOffBuzzer, fnTurnOffBuzzer, FSM_STATE.SAME);
  181. AnyStateTransition(MSG.Recover, fnRecover, STATE.Idle);
  182. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  183. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  184. AnyStateTransition(MSG.Abort, fnAbortRobot, STATE.Idle);
  185. AnyStateTransition(MSG.ToInit, fnToInit, STATE.Init);
  186. Transition(STATE.Unknown, MSG.CommReady, null, STATE.Init);
  187. // Home
  188. Transition(STATE.Init, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  189. Transition(STATE.Idle, MSG.HomeAll, fnHomeAll, STATE.Initializing); // 暂时加,出错的时候做 HOME
  190. Transition(STATE.Initializing, MSG.ActionDone, null, STATE. Orgshing);
  191. Transition(STATE.Orgshing, MSG.ActionDone, fnActionDone, STATE.Idle);
  192. Transition(STATE.Idle, MSG.HomeRB, fnHomeRobot, STATE.InitingRB);
  193. Transition(STATE.InitingRB, MSG.ActionDone, null, STATE.Idle);
  194. // Pick wafer
  195. Transition(STATE.Idle, MSG.Pick, FnStartPick, STATE.Picking);
  196. Transition(STATE.Picking, FSM_MSG.TIMER, FnPickTimeout, STATE.Idle);
  197. Transition(STATE.Picking, MSG.Abort, FnAbortPick, STATE.Idle);
  198. // Place wafer
  199. Transition(STATE.Idle, MSG.Place, FnStartPlace, STATE.Placing);
  200. Transition(STATE.Placing, FSM_MSG.TIMER, FnPlaceTimeout, STATE.Idle);
  201. Transition(STATE.Placing, MSG.Abort, FnAbortPlace, STATE.Idle);
  202. // Swap wafer with LL sequence
  203. Transition(STATE.Idle, MSG.Swap, FnStartSwap, STATE.Swapping);
  204. Transition(STATE.Swapping, FSM_MSG.TIMER, FnSwapTimeout, STATE.Idle);
  205. Transition(STATE.Swapping, MSG.Abort, FnAbortSwap, STATE.Idle);
  206. // Goto
  207. Transition(STATE.Idle, MSG.Goto, fnGoto, STATE.Gotoing);
  208. Transition(STATE.Gotoing, MSG.ActionDone, fnActionDone, STATE.Idle);
  209. // Map
  210. Transition(STATE.Idle, MSG.Map, fnMap, STATE.Mapping);
  211. Transition(STATE.Mapping, MSG.ActionDone, fnActionDone, STATE.Idle);
  212. // Grip
  213. Transition(STATE.Idle, MSG.Grip, fnGrip, STATE.Gripping);
  214. Transition(STATE.Gripping, MSG.ActionDone, fnActionDone, STATE.Idle);
  215. // Ungrip
  216. Transition(STATE.Idle, MSG.Ungrip, fnUngrip, STATE.Ungripping);
  217. Transition(STATE.Ungripping, MSG.ActionDone, fnActionDone, STATE.Idle);
  218. // Aligner
  219. Transition(STATE.Idle, MSG.HomeAL, fnHomeAligner, STATE.InitingAL);
  220. Transition(STATE.InitingAL, MSG.ActionDone, fnActionDone, STATE.Idle);
  221. Transition(STATE.Idle, MSG.Lift, fnLift, STATE.Lifting);
  222. Transition(STATE.Lifting, MSG.LiftActionDone, fnActionDone, STATE.Idle);
  223. Transition(STATE.Idle, MSG.Align, fnAlign, STATE.Aligning);
  224. Transition(STATE.Aligning, MSG.ActionDone, fnActionDone, STATE.Idle);
  225. EnumLoop<STATE>.ForEach((item) => { fsm.MapState((int)item, item.ToString()); });
  226. EnumLoop<MSG>.ForEach((item) => { fsm.MapMessage((int)item, item.ToString()); });
  227. }
  228. private bool fnHomeAll(object[] param)
  229. {
  230. _efem.HomeAll();
  231. return true;
  232. }
  233. private bool fnHomeRobot(object[] param)
  234. {
  235. _efem.Home(ModuleName.EfemRobot);
  236. return true;
  237. }
  238. private bool fnHomeAligner(object[] param)
  239. {
  240. // module
  241. ModuleName unit = ModuleName.EFEM;
  242. if (param[0] is string s1)
  243. unit = ModuleNameString.ToEnum(s1);
  244. else if (param[0] is ModuleName mod)
  245. unit = mod;
  246. else
  247. throw new ArgumentException("Argument error");
  248. _efem.Home(unit);
  249. return true;
  250. }
  251. private bool fnEnterExecute(object[] param)
  252. {
  253. return false;
  254. }
  255. private bool fnExitExecute(object[] param)
  256. {
  257. return false;
  258. }
  259. private bool fnActionDone(object[] param)
  260. {
  261. return false;
  262. }
  263. public bool CheckToPostMessage(int msg, params object[] args)
  264. {
  265. if (!fsm.FindTransition(fsm.State, msg))
  266. {
  267. EV.PostWarningLog(Name, $"{Name} is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  268. return false;
  269. }
  270. Running = true;
  271. fsm.PostMsg(msg, args);
  272. return true;
  273. }
  274. private bool fnMonitor(object[] param)
  275. {
  276. STATE curSt = (STATE)fsm.State;
  277. if (curSt == STATE.Initializing || curSt == STATE.Mapping || curSt == STATE.Picking || curSt == STATE.Placing
  278. || curSt == STATE.Orgshing || curSt == STATE.Lifting || curSt == STATE.Extending || curSt == STATE.Retracting
  279. || curSt == STATE.InitingAL || curSt == STATE.InitingRB)
  280. {
  281. int time = SC.GetValue<int>("EFEM.MotionTimeout");
  282. if (fsm.ElapsedTime > time * 1000)
  283. {
  284. EV.PostAlarmLog("EFEM", $"Can not complete motion {curSt} in {time} seconds. ");
  285. PostMsg(MSG.Error);
  286. }
  287. }
  288. if (curSt == STATE.Aligning)
  289. {
  290. if (fsm.ElapsedTime > 10 * 1000)
  291. {
  292. EV.PostAlarmLog(ModuleName.Aligner.ToString(), "Align timeout");
  293. PostMsg(MSG.ActionDone, "alignment timeout");
  294. }
  295. }
  296. return true;
  297. }
  298. private bool fnOnline(object[] param)
  299. {
  300. bool bOnlineFlag = (bool)param[0];
  301. if (_efem is EfemBase efem)
  302. {
  303. efem.SetOnline(bOnlineFlag);
  304. }
  305. return true;
  306. }
  307. private string GetFsmLastMessage()
  308. {
  309. int msg = fsm.LastMsg;
  310. if (msg >= (int)MSG.HomeAll && msg <= (int)MSG.Error)
  311. return ((MSG)msg).ToString();
  312. if (msg == (int)FSM_MSG.TIMER)
  313. return "Timer";
  314. return msg.ToString();
  315. }
  316. private bool fnError(object[] param)
  317. {
  318. return true;
  319. }
  320. private bool fnToInit(object[] param)
  321. {
  322. return true;
  323. }
  324. private bool fnRecover(object[] param)
  325. {
  326. _efem.ClearError();
  327. //_efem.ExecuteAction();
  328. return true;
  329. }
  330. private bool fnAbortRobot(object[] param)
  331. {
  332. //_efem.ExecuteAction();
  333. return true;
  334. }
  335. private bool fnSetLED(object[] param)
  336. {
  337. LightType light = (LightType)param[0];
  338. LightStatus st = (LightStatus)param[1];
  339. _efem.SetLamp(light, st);
  340. return true;
  341. }
  342. private bool fnTurnOffBuzzer(object[] param)
  343. {
  344. return false;
  345. }
  346. //private bool fnSwitchOnBuzzerAndRed(object[] param)
  347. //{
  348. // _efem.SwitchOnBuzzerAndRed();
  349. // return false;
  350. //}
  351. //
  352. private bool fnSetLampDone(object[] param)
  353. {
  354. {
  355. return true;
  356. }
  357. }
  358. private bool FnStartPick(object[] param)
  359. {
  360. return _pickRoutine.Start(param) == RState.Running;
  361. }
  362. private bool FnPickTimeout(object[] param)
  363. {
  364. RState ret = _pickRoutine.Monitor();
  365. if (ret == RState.Failed || ret == RState.Timeout)
  366. {
  367. PostMsg(MSG.Error);
  368. return false;
  369. }
  370. return ret == RState.End;
  371. }
  372. private bool FnAbortPick(object[] param)
  373. {
  374. _pickRoutine.Abort();
  375. return true;
  376. }
  377. private bool FnStartPlace(object[] param)
  378. {
  379. return _placeRoutine.Start(param) == RState.Running;
  380. }
  381. private bool FnPlaceTimeout(object[] param)
  382. {
  383. RState ret = _placeRoutine.Monitor();
  384. if (ret == RState.Failed || ret == RState.Timeout)
  385. {
  386. PostMsg(MSG.Error);
  387. return false;
  388. }
  389. return ret == RState.End;
  390. }
  391. private bool FnAbortPlace(object[] param)
  392. {
  393. _placeRoutine.Abort();
  394. return true;
  395. }
  396. private bool FnStartSwap(object[] param)
  397. {
  398. return _swapRoutine.Start(param) == RState.Running;
  399. }
  400. private bool FnSwapTimeout(object[] param)
  401. {
  402. RState ret = _swapRoutine.Monitor();
  403. if (ret == RState.Failed || ret == RState.Timeout)
  404. {
  405. PostMsg(MSG.Error);
  406. return false;
  407. }
  408. return ret == RState.End;
  409. }
  410. private bool FnAbortSwap(object[] param)
  411. {
  412. _swapRoutine.Abort();
  413. return true;
  414. }
  415. private bool fnGoto(object[] param)
  416. {
  417. // module
  418. ModuleName unit = ModuleName.EFEM;
  419. if (param[0] is string s1)
  420. unit = ModuleNameString.ToEnum(s1);
  421. else if (param[0] is ModuleName mod)
  422. unit = mod;
  423. else
  424. throw new ArgumentException("Argument error");
  425. _efem.Goto(unit, Hand.Blade1);
  426. return true;
  427. }
  428. private bool fnLift(object[] param)
  429. {
  430. // module
  431. ModuleName unit = ModuleName.EFEM;
  432. if (param[0] is string s1)
  433. unit = ModuleNameString.ToEnum(s1);
  434. else if (param[0] is ModuleName mod)
  435. unit = mod;
  436. else
  437. throw new ArgumentException("Argument error");
  438. bool isUp = true;
  439. if (param.Length > 1)
  440. {
  441. isUp = (bool) param[1];
  442. }
  443. if (isUp)
  444. {
  445. if (!_efem.SetPinUp(unit))
  446. return false;
  447. }
  448. else
  449. {
  450. if (!_efem.SetPinDown(unit))
  451. return false;
  452. }
  453. return true;
  454. }
  455. private bool fnAlign(object[] param)
  456. {
  457. // module
  458. ModuleName unit = ModuleName.EFEM;
  459. if (param[0] is string s1)
  460. unit = ModuleNameString.ToEnum(s1);
  461. else if (param[0] is ModuleName mod)
  462. unit = mod;
  463. else
  464. throw new ArgumentException("Argument error");
  465. // wafer size
  466. WaferSize ws1 = WaferSize.WS0;
  467. if (param[1] is string s2)
  468. {
  469. if (Enum.TryParse(s2, out WaferSize p5))
  470. ws1 = p5;
  471. }
  472. else if (param[1] is WaferSize p6)
  473. {
  474. ws1 = p6;
  475. }
  476. if (!_efem.Align(unit, 1000, ws1))
  477. return false;
  478. return true;
  479. }
  480. private bool fnMap(object[] param)
  481. {
  482. // module
  483. ModuleName unit = ModuleName.EFEM;
  484. if (param[0] is string s1)
  485. unit = ModuleNameString.ToEnum(s1);
  486. else if (param[0] is ModuleName mod)
  487. unit = mod;
  488. else
  489. throw new ArgumentException("Argument error");
  490. if (!_efem.Map(unit))
  491. return false;
  492. return true;
  493. }
  494. private bool fnGrip(object[] param)
  495. {
  496. Hand arm = (Hand)Enum.Parse(typeof(Hand), (string)param[0]);
  497. if (!_efem.Grip(arm, true))
  498. return false;
  499. return true;
  500. }
  501. private bool fnUngrip(object[] param)
  502. {
  503. Hand arm = (Hand)Enum.Parse(typeof(Hand), (string)param[0]);
  504. if (!_efem.Grip(arm, false))
  505. return false;
  506. return true;
  507. }
  508. public bool IsIdle
  509. {
  510. get { return fsm.State == (int)STATE.Idle; }
  511. }
  512. public bool IsError
  513. {
  514. get { return fsm.State == (int)STATE.Error; }
  515. }
  516. public bool IsInit
  517. {
  518. get { return fsm.State == (int)STATE.Unknown || fsm.State==(int)STATE.Init; }
  519. }
  520. public bool IsBusy
  521. {
  522. get { return !IsInit && !IsError && !IsIdle; }
  523. }
  524. public bool IsOnline { get; internal set; }
  525. public int Invoke(string function, params object[] args)
  526. {
  527. switch (function)
  528. {
  529. case "Home":
  530. CheckToPostMessage((int)MSG.HomeAll);
  531. return (int)MSG.HomeAll;
  532. }
  533. return (int)FSM_MSG.NONE;
  534. }
  535. public bool CheckAcked(int msg)
  536. {
  537. return fsm.CheckExecuted(msg);
  538. }
  539. internal void InvokeReset()
  540. {
  541. if (fsm.State == (int) STATE.Error)
  542. {
  543. PostMsg((int)MSG.Recover);
  544. }
  545. }
  546. public int InvokeAlign(string module, float time)
  547. {
  548. if (CheckToPostMessage((int)MSG.Align, module, time))
  549. return (int)MSG.Align;
  550. return (int)FSM_MSG.NONE;
  551. }
  552. public int InvokeLiftDown(string module)
  553. {
  554. if (CheckToPostMessage((int)MSG.Lift, module, false))
  555. return (int)MSG.Lift;
  556. return (int)FSM_MSG.NONE;
  557. }
  558. public int InvokePick(ModuleName source, int slot, Hand hand, WaferSize size)
  559. {
  560. if (CheckToPostMessage((int)MSG.Pick, source, slot, hand, size))
  561. return (int)MSG.Pick;
  562. return (int)FSM_MSG.NONE;
  563. }
  564. public int InvokeGoto(ModuleName source, int slot)
  565. {
  566. if (CheckToPostMessage((int)MSG.Goto, source, slot))
  567. return (int)MSG.Goto;
  568. return (int)FSM_MSG.NONE;
  569. }
  570. public int InvokePlace(ModuleName target, int slot, Hand hand, WaferSize size)
  571. {
  572. if (CheckToPostMessage((int)MSG.Place, target, slot, hand, size))
  573. return (int)MSG.Place;
  574. return (int)FSM_MSG.NONE;
  575. }
  576. public int InvokePickAndPlace(ModuleName targetModule, Hand pickHand, int pickSlot, Hand placeHand, int placeSlot, WaferSize size)
  577. {
  578. if (CheckToPostMessage((int)MSG.Swap, targetModule, pickSlot, pickHand, placeHand, placeSlot, size))
  579. return (int)MSG.Swap;
  580. return (int)FSM_MSG.NONE;
  581. }
  582. public int InvokeMap(string target )
  583. {
  584. if (CheckToPostMessage((int)MSG.Map, target ))
  585. return (int)MSG.Map;
  586. return (int)FSM_MSG.NONE;
  587. }
  588. public int InvokeFlip(Hand hand)
  589. {
  590. if (CheckToPostMessage((int)MSG.Flip, hand))
  591. return (int)MSG.Flip;
  592. return (int)FSM_MSG.NONE;
  593. }
  594. public bool IsPrepareTransferReady(ModuleName module, EnumTransferType type, int slot)
  595. {
  596. //if (type == EnumTransferType.Pick)
  597. //{
  598. // //需要补充:判断LP 放好了,而且已经map过。
  599. // return _efem[module].HasCassette && _efem[module].IsMapped;
  600. //}
  601. //else if (type == EnumTransferType.Place)
  602. //{
  603. // //需要补充:判断LP 放好了,而且已经map过。
  604. // return _efem[module].HasCassette && _efem[module].IsMapped;
  605. //}
  606. return false;
  607. }
  608. internal bool CheckReadyRunNewJob(ModuleName module)
  609. {
  610. //???
  611. return true;
  612. }
  613. internal bool CheckReadyTransfer(ModuleName module)
  614. {
  615. //return _efem[module].HasCassette && _efem[module].IsMapped;
  616. return true;
  617. }
  618. internal bool CheckPlaced(ModuleName module)
  619. {
  620. //return _efem[module].HasCassette;
  621. return true;
  622. }
  623. internal void NoteJobStart(ModuleName module)
  624. {
  625. //_efem[module].NoteJobStart();
  626. }
  627. internal void NoteJobComplete(ModuleName module)
  628. {
  629. //_efem[module].NoteJobComplete();
  630. }
  631. }
  632. /// <summary>
  633. /// LP entity
  634. /// </summary>
  635. class LoadportEntity : Entity, IEntity
  636. {
  637. private enum STATE
  638. {
  639. Unknown,
  640. Idle, // 1
  641. Initializing, // 2
  642. Initialized, // 3
  643. Mapping, // 4
  644. Mapped, // 5
  645. }
  646. public enum MSG
  647. {
  648. Home, // 0
  649. Map, // 1
  650. ActionDone, // 2
  651. RecHwMsg, // 3
  652. Recover, // 4
  653. Abort, // 5
  654. Online, // 6
  655. Error // 7
  656. }
  657. private readonly EfemBase _efem;
  658. private ModuleName Module { get; }
  659. public LoadportEntity(ModuleName mod, EfemBase efem)
  660. {
  661. this.Module = mod;
  662. _efem = efem;
  663. InitFsmMap();
  664. OP.Subscribe($"{Module}.Home", (cmd, args) => { PostMsg(MSG.Home); return true; });
  665. OP.Subscribe($"{Module}.Abort", (cmd, args) => { PostMsg(MSG.Abort); return true; });
  666. //OP.Subscribe($"{Module}.Map", (cmd, args) => { PostMsg(MSG.Map); return true; });
  667. OP.Subscribe($"{Module}.Online", (cmd, args) => { PostMsg(MSG.Online); return true; });
  668. DATA.Subscribe($"{Module}.Status", () => ((STATE)fsm.State).ToString());
  669. DATA.Subscribe($"{Module}.FsmState", () => ((STATE)fsm.State).ToString());
  670. DATA.Subscribe($"{Module}.FsmPrevState", () => ((STATE)fsm.PrevState).ToString());
  671. DATA.Subscribe($"{Module}.FsmLastMessage", GetFsmLastMessage);
  672. }
  673. private string GetFsmLastMessage()
  674. {
  675. int msg = fsm.LastMsg;
  676. if (msg >= (int)MSG.Home && msg <= (int)MSG.Error)
  677. return ((MSG)msg).ToString();
  678. if (msg == (int)FSM_MSG.TIMER)
  679. return "Timer";
  680. return msg.ToString();
  681. }
  682. private void InitFsmMap()
  683. {
  684. fsm = new StateMachine<LoadportEntity>($"LPM_{Module}", (int)STATE.Idle, 500);
  685. AnyStateTransition(FSM_MSG.TIMER, fnMonitor, FSM_STATE.SAME);
  686. //AnyStateTransition(MSG.RecHwMsg, fnRecMsg, FSM_STATE.SAME);
  687. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  688. AnyStateTransition(MSG.Recover, fnRecover, STATE.Idle);
  689. AnyStateTransition(MSG.Abort, fnRecover, STATE.Idle);
  690. EnterExitTransition<STATE, FSM_MSG>(STATE.Initializing, fnEnterExecute, FSM_MSG.NONE, fnExitExecute);
  691. // Home
  692. AnyStateTransition(MSG.Home, fnHome, STATE.Initializing);
  693. Transition(STATE.Initializing, MSG.ActionDone, fnActionDone, STATE.Idle);
  694. }
  695. private bool fnOnline(object[] param)
  696. {
  697. bool online = (bool)param[0];
  698. //_efem.SetOnline(Module, online);
  699. return true;
  700. }
  701. private bool fnRecover(object[] param)
  702. {
  703. return true;
  704. }
  705. private bool fnMonitor(object[] param)
  706. {
  707. STATE curSt = (STATE)fsm.State;
  708. if (curSt == STATE.Initializing || curSt == STATE.Mapping)
  709. {
  710. if (fsm.ElapsedTime > 20 * 1000)
  711. {
  712. PostMsg(MSG.Recover);
  713. }
  714. }
  715. return true;
  716. }
  717. private bool fnEnterExecute(object[] param)
  718. {
  719. return true;
  720. }
  721. private bool fnExitExecute(object[] param)
  722. {
  723. return true;
  724. }
  725. private bool fnActionDone(object[] param)
  726. {
  727. return true;
  728. }
  729. private bool fnHome(object[] param)
  730. {
  731. _efem.Home(Module);
  732. return true;
  733. }
  734. public bool Check(int msg, out string reason, params object[] args)
  735. {
  736. throw new NotImplementedException();
  737. }
  738. }
  739. }