EfemEntity.cs 32 KB

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