EfemEntity.cs 33 KB

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