EfemEntity.cs 34 KB

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