EfemEntity.cs 33 KB

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