EfemEntity.cs 32 KB

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