EfemEntity.cs 33 KB

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