EfemEntity.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. return false;
  398. }
  399. return ret == RState.End;
  400. }
  401. private bool FnAbortPick(object[] param)
  402. {
  403. _pickRoutine.Abort();
  404. return true;
  405. }
  406. private bool FnStartPlace(object[] param)
  407. {
  408. return _placeRoutine.Start(param) == RState.Running;
  409. }
  410. private bool FnPlaceTimeout(object[] param)
  411. {
  412. RState ret = _placeRoutine.Monitor();
  413. if (ret == RState.Failed || ret == RState.Timeout)
  414. {
  415. PostMsg(MSG.Error);
  416. return false;
  417. }
  418. return ret == RState.End;
  419. }
  420. private bool FnAbortPlace(object[] param)
  421. {
  422. _placeRoutine.Abort();
  423. return true;
  424. }
  425. private bool FnStartSwap(object[] param)
  426. {
  427. return _swapRoutine.Start(param) == RState.Running;
  428. }
  429. private bool FnSwapTimeout(object[] param)
  430. {
  431. RState ret = _swapRoutine.Monitor();
  432. if (ret == RState.Failed || ret == RState.Timeout)
  433. {
  434. PostMsg(MSG.Error);
  435. return false;
  436. }
  437. return ret == RState.End;
  438. }
  439. private bool FnAbortSwap(object[] param)
  440. {
  441. _swapRoutine.Abort();
  442. return true;
  443. }
  444. private bool fnGoto(object[] param)
  445. {
  446. // module
  447. ModuleName unit = ModuleName.EFEM;
  448. if (param[0] is string s1)
  449. unit = ModuleNameString.ToEnum(s1);
  450. else if (param[0] is ModuleName mod)
  451. unit = mod;
  452. else
  453. throw new ArgumentException("Argument error");
  454. _efem.Goto(unit, Hand.Blade1);
  455. return true;
  456. }
  457. private bool fnLift(object[] param)
  458. {
  459. // module
  460. ModuleName unit = ModuleName.EFEM;
  461. if (param[0] is string s1)
  462. unit = ModuleNameString.ToEnum(s1);
  463. else if (param[0] is ModuleName mod)
  464. unit = mod;
  465. else
  466. throw new ArgumentException("Argument error");
  467. bool isUp = true;
  468. if (param.Length > 1)
  469. {
  470. isUp = (bool) param[1];
  471. }
  472. if (isUp)
  473. {
  474. if (!_efem.SetPinUp(unit))
  475. return false;
  476. }
  477. else
  478. {
  479. if (!_efem.SetPinDown(unit))
  480. return false;
  481. }
  482. LiftMessage = isUp ? "Up" : "Down";
  483. return true;
  484. }
  485. private bool fnLiftTimeout(object[] param)
  486. {
  487. if (LiftMessage == "Up")
  488. {
  489. return _efem.LiftIsDown == false && _efem.LiftIsUp == true;
  490. }
  491. else if (LiftMessage == "Down")
  492. {
  493. return _efem.LiftIsDown == true && _efem.LiftIsUp == false;
  494. }
  495. return false;
  496. }
  497. private bool fnAlign(object[] param)
  498. {
  499. return _alignRoutine.Start(param) == RState.Running;
  500. }
  501. private bool fnAlignTimeout(object[] param)
  502. {
  503. RState ret = _alignRoutine.Monitor();
  504. if (ret == RState.Failed || ret == RState.Timeout)
  505. {
  506. PostMsg(MSG.Error);
  507. return false;
  508. }
  509. return ret == RState.End;
  510. }
  511. private bool fnMap(object[] param)
  512. {
  513. // module
  514. ModuleName unit = ModuleName.EFEM;
  515. if (param[0] is string s1)
  516. unit = ModuleNameString.ToEnum(s1);
  517. else if (param[0] is ModuleName mod)
  518. unit = mod;
  519. else
  520. throw new ArgumentException("Argument error");
  521. if (!_efem.Map(unit))
  522. return false;
  523. return true;
  524. }
  525. private bool fnGrip(object[] param)
  526. {
  527. Hand arm = (Hand)Enum.Parse(typeof(Hand), (string)param[0]);
  528. if (!_efem.Grip(arm, true))
  529. return false;
  530. return true;
  531. }
  532. private bool fnUngrip(object[] param)
  533. {
  534. Hand arm = (Hand)Enum.Parse(typeof(Hand), (string)param[0]);
  535. if (!_efem.Grip(arm, false))
  536. return false;
  537. return true;
  538. }
  539. public int Invoke(string function, params object[] args)
  540. {
  541. switch (function)
  542. {
  543. case "Home":
  544. CheckToPostMessage((int)MSG.HomeAll);
  545. return (int)MSG.HomeAll;
  546. }
  547. return (int)FSM_MSG.NONE;
  548. }
  549. public bool CheckAcked(int msg)
  550. {
  551. return fsm.CheckExecuted(msg);
  552. }
  553. internal void InvokeReset()
  554. {
  555. if (fsm.State == (int) STATE.Error)
  556. {
  557. PostMsg((int)MSG.Recover);
  558. }
  559. }
  560. public int InvokeAlign(string module, int reserv, float time)
  561. {
  562. if (CheckToPostMessage((int)MSG.Align, module, reserv, time))
  563. return (int)MSG.Align;
  564. return (int)FSM_MSG.NONE;
  565. }
  566. public int InvokeLiftDown(string module)
  567. {
  568. if (CheckToPostMessage((int)MSG.Lift, module, false))
  569. return (int)MSG.Lift;
  570. return (int)FSM_MSG.NONE;
  571. }
  572. public int InvokePick(ModuleName source, int slot, Hand hand, WaferSize size)
  573. {
  574. if (CheckToPostMessage((int)MSG.Pick, source, slot, hand, size))
  575. return (int)MSG.Pick;
  576. return (int)FSM_MSG.NONE;
  577. }
  578. public int InvokeGoto(ModuleName source, int slot)
  579. {
  580. if (CheckToPostMessage((int)MSG.Goto, source, slot))
  581. return (int)MSG.Goto;
  582. return (int)FSM_MSG.NONE;
  583. }
  584. public int InvokePlace(ModuleName target, int slot, Hand hand, WaferSize size)
  585. {
  586. if (CheckToPostMessage((int)MSG.Place, target, slot, hand, size))
  587. return (int)MSG.Place;
  588. return (int)FSM_MSG.NONE;
  589. }
  590. public int InvokePickAndPlace(ModuleName targetModule, Hand pickHand, int pickSlot, Hand placeHand, int placeSlot, WaferSize size)
  591. {
  592. if (CheckToPostMessage((int)MSG.Swap, targetModule, pickSlot, pickHand, placeHand, placeSlot, size))
  593. return (int)MSG.Swap;
  594. return (int)FSM_MSG.NONE;
  595. }
  596. public int InvokeMap(string target )
  597. {
  598. if (CheckToPostMessage((int)MSG.Map, target ))
  599. return (int)MSG.Map;
  600. return (int)FSM_MSG.NONE;
  601. }
  602. public int InvokeFlip(Hand hand)
  603. {
  604. if (CheckToPostMessage((int)MSG.Flip, hand))
  605. return (int)MSG.Flip;
  606. return (int)FSM_MSG.NONE;
  607. }
  608. public bool IsPrepareTransferReady(ModuleName module, EnumTransferType type, int slot)
  609. {
  610. //if (type == EnumTransferType.Pick)
  611. //{
  612. // //需要补充:判断LP 放好了,而且已经map过。
  613. // return _efem[module].HasCassette && _efem[module].IsMapped;
  614. //}
  615. //else if (type == EnumTransferType.Place)
  616. //{
  617. // //需要补充:判断LP 放好了,而且已经map过。
  618. // return _efem[module].HasCassette && _efem[module].IsMapped;
  619. //}
  620. return false;
  621. }
  622. internal bool CheckReadyRunNewJob(ModuleName module)
  623. {
  624. //???
  625. return true;
  626. }
  627. internal bool CheckReadyTransfer(ModuleName module)
  628. {
  629. //return _efem[module].HasCassette && _efem[module].IsMapped;
  630. return true;
  631. }
  632. internal bool CheckPlaced(ModuleName module)
  633. {
  634. //return _efem[module].HasCassette;
  635. return true;
  636. }
  637. internal void NoteJobStart(ModuleName module)
  638. {
  639. //_efem[module].NoteJobStart();
  640. }
  641. internal void NoteJobComplete(ModuleName module)
  642. {
  643. //_efem[module].NoteJobComplete();
  644. }
  645. private void _debugRoutine()
  646. {
  647. int flag = 0;
  648. // Test Home routine
  649. if (flag == 1)
  650. {
  651. PostMsg(MSG.HomeAll);
  652. }
  653. else if (flag == 2)
  654. {
  655. WaferManager.Instance.CreateWafer(ModuleName.Aligner1, 0, WaferStatus.Normal);
  656. WaferManager.Instance.DeleteWafer(ModuleName.EfemRobot, 0);
  657. var item = new MoveItem(ModuleName.Aligner1, 0, ModuleName.EfemRobot, 0, Hand.Blade1);
  658. var items = new Queue<MoveItem>();
  659. items.Enqueue(item);
  660. PostMsg(MSG.Pick, items);
  661. }
  662. else if (flag == 3)
  663. {
  664. var item = new MoveItem( ModuleName.EfemRobot, 0, ModuleName.Aligner1, 0, Hand.Blade1);
  665. var items = new Queue<MoveItem>();
  666. items.Enqueue(item);
  667. PostMsg(MSG.Place, items);
  668. }
  669. else if (flag == 4)
  670. {
  671. WaferManager.Instance.CreateWafer(ModuleName.LLA, 0, WaferStatus.Normal);
  672. WaferManager.Instance.CreateWafer(ModuleName.LLA, 1, WaferStatus.Normal);
  673. WaferManager.Instance.DeleteWafer(ModuleName.LLA, 2);
  674. WaferManager.Instance.DeleteWafer(ModuleName.LLA, 3);
  675. WaferManager.Instance.CreateWafer(ModuleName.EfemRobot, 0, WaferStatus.Normal);
  676. WaferManager.Instance.CreateWafer(ModuleName.EfemRobot, 1, WaferStatus.Normal);
  677. var item = new MoveItem(ModuleName.EfemRobot, 0, ModuleName.LLA, 2, Hand.Blade1);
  678. var items = new Queue<MoveItem>();
  679. items.Enqueue(item);
  680. item = new MoveItem(ModuleName.EfemRobot, 1, ModuleName.LLA, 3, Hand.Blade2);
  681. items.Enqueue(item);
  682. item = new MoveItem(ModuleName.LLA, 0, ModuleName.EfemRobot, 0, Hand.Blade1);
  683. items.Enqueue(item);
  684. item = new MoveItem(ModuleName.LLA, 1, ModuleName.EfemRobot, 1, Hand.Blade2);
  685. items.Enqueue(item);
  686. PostMsg(MSG.Swap, items);
  687. }
  688. else if (flag == 5)
  689. {
  690. PostMsg(MSG.HomeRB);
  691. }
  692. else if (flag == 6)
  693. {
  694. PostMsg(MSG.HomeAL, ModuleName.Aligner1);
  695. }
  696. else if (flag == 8)
  697. {
  698. OP.DoOperation("LP1.Load");
  699. }
  700. else if(flag == 9)
  701. {
  702. OP.DoOperation("LP1.Unload");
  703. }
  704. }
  705. }
  706. /// <summary>
  707. /// LP entity
  708. /// </summary>
  709. class LoadportEntity : Entity, IEntity
  710. {
  711. private enum STATE
  712. {
  713. Unknown,
  714. Idle, // 1
  715. Initializing, // 2
  716. Initialized, // 3
  717. Mapping, // 4
  718. Mapped, // 5
  719. }
  720. public enum MSG
  721. {
  722. Home, // 0
  723. Map, // 1
  724. ActionDone, // 2
  725. RecHwMsg, // 3
  726. Recover, // 4
  727. Abort, // 5
  728. Online, // 6
  729. Error // 7
  730. }
  731. private readonly EfemBase _efem;
  732. private ModuleName Module { get; }
  733. public LoadportEntity(ModuleName mod, EfemBase efem)
  734. {
  735. this.Module = mod;
  736. _efem = efem;
  737. InitFsmMap();
  738. OP.Subscribe($"{Module}.Home", (cmd, args) => { PostMsg(MSG.Home); return true; });
  739. OP.Subscribe($"{Module}.Abort", (cmd, args) => { PostMsg(MSG.Abort); return true; });
  740. //OP.Subscribe($"{Module}.Map", (cmd, args) => { PostMsg(MSG.Map); return true; });
  741. OP.Subscribe($"{Module}.Online", (cmd, args) => { PostMsg(MSG.Online); return true; });
  742. DATA.Subscribe($"{Module}.Status", () => ((STATE)fsm.State).ToString());
  743. DATA.Subscribe($"{Module}.FsmState", () => ((STATE)fsm.State).ToString());
  744. DATA.Subscribe($"{Module}.FsmPrevState", () => ((STATE)fsm.PrevState).ToString());
  745. DATA.Subscribe($"{Module}.FsmLastMessage", GetFsmLastMessage);
  746. }
  747. private string GetFsmLastMessage()
  748. {
  749. int msg = fsm.LastMsg;
  750. if (msg >= (int)MSG.Home && msg <= (int)MSG.Error)
  751. return ((MSG)msg).ToString();
  752. if (msg == (int)FSM_MSG.TIMER)
  753. return "Timer";
  754. return msg.ToString();
  755. }
  756. private void InitFsmMap()
  757. {
  758. fsm = new StateMachine<LoadportEntity>($"LPM_{Module}", (int)STATE.Idle, 500);
  759. AnyStateTransition(FSM_MSG.TIMER, fnMonitor, FSM_STATE.SAME);
  760. //AnyStateTransition(MSG.RecHwMsg, fnRecMsg, FSM_STATE.SAME);
  761. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  762. AnyStateTransition(MSG.Recover, fnRecover, STATE.Idle);
  763. AnyStateTransition(MSG.Abort, fnRecover, STATE.Idle);
  764. EnterExitTransition<STATE, FSM_MSG>(STATE.Initializing, fnEnterExecute, FSM_MSG.NONE, fnExitExecute);
  765. // Home
  766. AnyStateTransition(MSG.Home, fnHome, STATE.Initializing);
  767. Transition(STATE.Initializing, MSG.ActionDone, fnActionDone, STATE.Idle);
  768. }
  769. private bool fnOnline(object[] param)
  770. {
  771. bool online = (bool)param[0];
  772. //_efem.SetOnline(Module, online);
  773. return true;
  774. }
  775. private bool fnRecover(object[] param)
  776. {
  777. return true;
  778. }
  779. private bool fnMonitor(object[] param)
  780. {
  781. STATE curSt = (STATE)fsm.State;
  782. if (curSt == STATE.Initializing || curSt == STATE.Mapping)
  783. {
  784. if (fsm.ElapsedTime > 20 * 1000)
  785. {
  786. PostMsg(MSG.Recover);
  787. }
  788. }
  789. return true;
  790. }
  791. private bool fnEnterExecute(object[] param)
  792. {
  793. return true;
  794. }
  795. private bool fnExitExecute(object[] param)
  796. {
  797. return true;
  798. }
  799. private bool fnActionDone(object[] param)
  800. {
  801. return true;
  802. }
  803. private bool fnHome(object[] param)
  804. {
  805. _efem.Home(Module);
  806. return true;
  807. }
  808. public bool Check(int msg, out string reason, params object[] args)
  809. {
  810. throw new NotImplementedException();
  811. }
  812. }
  813. }