EfemEntity.cs 30 KB

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