EfemEntity.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.Common;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Fsm;
  7. using Aitex.Core.RT.OperationCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Utilities;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Equipment;
  12. using MECF.Framework.Common.Schedulers;
  13. using MECF.Framework.Common.SubstrateTrackings;
  14. using Venus_Core;
  15. using Venus_RT;
  16. using Venus_RT.Devices;
  17. using Venus_RT.Devices.YASKAWA;
  18. using Venus_RT.Devices.EFEM;
  19. using Venus_RT.Modules.LPs;
  20. using Venus_RT.Modules.EFEM;
  21. namespace Venus_RT.Modules
  22. {
  23. class EfemEntity : Entity, IEntity, IModuleEntity
  24. {
  25. //private int _bigWafer = 0;
  26. //private int _midWafer = 0;
  27. //private int _smallWafer = 0;
  28. public enum STATE
  29. {
  30. Unknown, // 0
  31. Initializing, // 1
  32. Idle, // 2
  33. Error, // 3
  34. Picking, // 4
  35. Placing, // 5
  36. Aligning, // 6
  37. Mapping, // 7
  38. Init, // 8
  39. Orgshing, // 9
  40. Lifting, // 10
  41. InitingAL, // 11
  42. InitingRB, // 12
  43. Extending, // 13
  44. Retracting, // 14
  45. //SettingLamp, // 15
  46. Swapping,
  47. Gotoing,
  48. Gripping,
  49. Ungripping,
  50. Fliping,
  51. }
  52. public enum MSG
  53. {
  54. HomeAll, // 0
  55. Pick, // 1
  56. Place, // 2
  57. Align, // 3
  58. ActionDone, // 4
  59. MoveCmd, // 6
  60. Recover, // 8
  61. Goto, // 9
  62. Error, // 10
  63. Online, // 11
  64. CommReady, // 12
  65. Lift, // 13
  66. HomeAL, // 14
  67. HomeRB, // 15
  68. Extend, // 16
  69. Retract, // 17
  70. PMLiftPinUp, // 18
  71. PMLiftPinDown, // 19
  72. TurnOffBuzzer,
  73. Abort,
  74. Map,
  75. ToInit,
  76. Cool,
  77. Swap,
  78. Grip,
  79. Ungrip,
  80. Flip,
  81. LiftActionDone,
  82. }
  83. public enum EfemType
  84. {
  85. FutureEfem = 1,
  86. JetEfem = 2,
  87. BrooksEFEM = 3,
  88. }
  89. public bool IsIdle
  90. {
  91. get { return fsm.State == (int)STATE.Idle; }
  92. }
  93. public bool IsError
  94. {
  95. get { return fsm.State == (int)STATE.Error; }
  96. }
  97. public bool IsInit
  98. {
  99. get { return fsm.State == (int)STATE.Unknown || fsm.State == (int)STATE.Init; }
  100. }
  101. public bool IsBusy
  102. {
  103. get { return !IsInit && !IsError && !IsIdle; }
  104. }
  105. public bool IsOnline { get; internal set; }
  106. public bool Check(int msg, out string reason, params object[] args)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. // Fields
  111. //
  112. private readonly string Name;
  113. private readonly EfemBase _efem;
  114. private readonly LoadPortModule[] _lpms = new LoadPortModule[3];
  115. private readonly EfemType _efemType;
  116. public EfemBase EfemDevice => _efem;
  117. public EfemType EFEMType => _efemType;
  118. // routine
  119. private readonly EfemPickRoutine _pickRoutine;
  120. private readonly EfemPlaceRoutine _placeRoutine;
  121. private readonly EfemSwapRoutine _swapRoutine;
  122. private readonly EfemHomeRoutine _homeRoutine;
  123. private readonly EFEMAlignRoutine _alignRoutine;
  124. private string LiftMessage;
  125. // Constructor
  126. //
  127. public EfemEntity()
  128. {
  129. _efemType = (EfemType)SC.GetValue<int>($"EFEM.EfemType");
  130. _efem = new JetEfem();
  131. Name = ModuleName.EFEM.ToString();
  132. _homeRoutine = new EfemHomeRoutine(_efem);
  133. _pickRoutine = new EfemPickRoutine(_efem);
  134. _placeRoutine = new EfemPlaceRoutine(_efem);
  135. _swapRoutine = new EfemSwapRoutine(_efem);
  136. _alignRoutine = new EFEMAlignRoutine(_efem);
  137. InitFsmMap();
  138. }
  139. public void NotifyLP(ModuleName mod, LoadportEntity.MSG msg)
  140. {
  141. _lpms[mod - ModuleName.LP1].PostMsg(msg);
  142. }
  143. public void NotifyLPError(ModuleName mod )
  144. {
  145. _lpms[mod - ModuleName.LP1].PostMsg(LoadportEntity.MSG.ActionDone);
  146. //_lpms[mod - ModuleName.LP1]..OnError();
  147. }
  148. protected override bool Init()
  149. {
  150. _lpms[0] = new LoadPortModule(ModuleName.LP1, _efem);
  151. _lpms[1] = new LoadPortModule(ModuleName.LP2, _efem);
  152. _lpms[2] = new LoadPortModule(ModuleName.LP3, _efem);
  153. _lpms[0].Initialize();
  154. _lpms[1].Initialize();
  155. _lpms[2].Initialize();
  156. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAll); return true; });
  157. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.ClearError}", (cmd, args) => { PostMsg(MSG.Recover); return true; });
  158. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.TurnOffBuzzer}", (cmd, args) => { PostMsg(MSG.TurnOffBuzzer); return true; });
  159. //OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.SwitchOnBuzzerAndRed}", (cmd, args) => { PostMsg(MSG.SwitchOnBuzzerAndRed); return true; });
  160. OP.Subscribe($"{ModuleName.EFEM}.Online", (cmd, args) => { PostMsg(MSG.Online); return true; });
  161. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Pick}", (cmd, args) => { PostMsg(MSG.Pick, args[0]); return true; });
  162. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Place}", (cmd, args) => { PostMsg(MSG.Place, args[0]); return true; });
  163. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Swap}", (cmd, args) => { PostMsg(MSG.Swap, args[0]); return true; });
  164. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Abort}", (cmd, args) => { PostMsg(MSG.Abort); return true; });
  165. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeRB); return true; });
  166. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Grip}", (cmd, args) =>
  167. {
  168. bool isGrip = ((string)args[0]).ToLower() == "on";
  169. PostMsg(isGrip ? MSG.Grip : MSG.Ungrip, args[1]);
  170. return true;
  171. });
  172. //OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Flip}", (cmd, args) => { PostMsg(MSG.Flip, args[0]); return true; });
  173. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Aligner1); return true; });
  174. OP.Subscribe($"{ModuleName.Aligner2}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Aligner2); return true; });
  175. OP.Subscribe($"{ModuleName.Cooling1}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Cooling1); return true; });
  176. OP.Subscribe($"{ModuleName.Cooling2}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Cooling2); return true; });
  177. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Aligner1, args[0], args[1]); return true; });
  178. OP.Subscribe($"{ModuleName.Aligner2}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Aligner2); return true; });
  179. OP.Subscribe($"{ModuleName.Cooling1}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Cooling1); return true; });
  180. OP.Subscribe($"{ModuleName.Cooling2}.{EfemOperation.Align}", (cmd, args) => { PostMsg(MSG.Align, ModuleName.Cooling2); return true; });
  181. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Aligner1); return true; });
  182. OP.Subscribe($"{ModuleName.Aligner2}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Aligner2); return true; });
  183. OP.Subscribe($"{ModuleName.Cooling1}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Cooling1); return true; });
  184. OP.Subscribe($"{ModuleName.Cooling2}.{EfemOperation.Lift}", (cmd, args) => { PostMsg(MSG.Lift, ModuleName.Cooling2); return true; });
  185. DATA.Subscribe($"{Name}.FsmState", () => ((STATE)fsm.State).ToString(),Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  186. DATA.Subscribe($"{Name}.FsmPrevState", () => ((STATE)fsm.PrevState).ToString(), Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  187. DATA.Subscribe($"{Name}.FsmLastMessage", GetFsmLastMessage, Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  188. DATA.Subscribe($"{Name}.RobotMoveAction", () => (_efem.TMRobotMoveInfo), Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  189. return true;
  190. }
  191. private void InitFsmMap()
  192. {
  193. fsm = new StateMachine<EfemEntity>("EFEM", (int)STATE.Unknown, 50);
  194. fsm.EnableRepeatedMsg(true);
  195. //AnyStateTransition(FSM_MSG.TIMER, fnMonitor, FSM_STATE.SAME);
  196. AnyStateTransition(MSG.TurnOffBuzzer, fnTurnOffBuzzer, FSM_STATE.SAME);
  197. AnyStateTransition(MSG.Recover, fnRecover, STATE.Idle);
  198. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  199. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  200. AnyStateTransition(MSG.Abort, fnAbortRobot, STATE.Idle);
  201. AnyStateTransition(MSG.ToInit, fnToInit, STATE.Init);
  202. AnyStateTransition(MSG.CommReady, fnCommReady, STATE.Init);
  203. Transition(STATE.Init, FSM_MSG.TIMER, fnMonitor, STATE.Init);
  204. Transition(STATE.Idle, FSM_MSG.TIMER, fnMonitor, STATE.Idle);
  205. Transition(STATE.Error, FSM_MSG.TIMER, fnMonitor, STATE.Error);
  206. // Home
  207. Transition(STATE.Init, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  208. Transition(STATE.Idle, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  209. Transition(STATE.Error, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  210. Transition(STATE.Initializing, FSM_MSG.TIMER, fnHomingTimeout, STATE.Idle);
  211. // Home Robot
  212. Transition(STATE.Idle, MSG.HomeRB, fnHomeRobot, STATE.InitingRB);
  213. Transition(STATE.InitingRB, FSM_MSG.TIMER, fnHomingTimeout, STATE.Idle);
  214. // Home Aligner
  215. Transition(STATE.Idle, MSG.HomeAL, fnHomeAligner, STATE.InitingAL);
  216. Transition(STATE.InitingAL, FSM_MSG.TIMER, fnHomingTimeout, STATE.Idle);
  217. // Pick wafer
  218. Transition(STATE.Idle, MSG.Pick, FnStartPick, STATE.Picking);
  219. Transition(STATE.Picking, FSM_MSG.TIMER, FnPickTimeout, STATE.Idle);
  220. Transition(STATE.Picking, MSG.Abort, FnAbortPick, STATE.Idle);
  221. // Place wafer
  222. Transition(STATE.Idle, MSG.Place, FnStartPlace, STATE.Placing);
  223. Transition(STATE.Placing, FSM_MSG.TIMER, FnPlaceTimeout, STATE.Idle);
  224. Transition(STATE.Placing, MSG.Abort, FnAbortPlace, STATE.Idle);
  225. // Swap wafer with LL sequence
  226. Transition(STATE.Idle, MSG.Swap, FnStartSwap, STATE.Swapping);
  227. Transition(STATE.Swapping, FSM_MSG.TIMER, FnSwapTimeout, STATE.Idle);
  228. Transition(STATE.Swapping, MSG.Abort, FnAbortSwap, STATE.Idle);
  229. // Goto
  230. Transition(STATE.Idle, MSG.Goto, fnGoto, STATE.Gotoing);
  231. Transition(STATE.Gotoing, MSG.ActionDone, fnActionDone, STATE.Idle);
  232. // Map
  233. Transition(STATE.Idle, MSG.Map, fnMap, STATE.Mapping);
  234. Transition(STATE.Mapping, MSG.ActionDone, fnActionDone, STATE.Idle);
  235. // Grip
  236. Transition(STATE.Idle, MSG.Grip, fnGrip, STATE.Gripping);
  237. Transition(STATE.Gripping, MSG.ActionDone, fnActionDone, STATE.Idle);
  238. // Ungrip
  239. Transition(STATE.Idle, MSG.Ungrip, fnUngrip, STATE.Ungripping);
  240. Transition(STATE.Ungripping, MSG.ActionDone, fnActionDone, STATE.Idle);
  241. // Aligner
  242. Transition(STATE.Idle, MSG.Lift, fnLift, STATE.Lifting);
  243. //Transition(STATE.Lifting, MSG.LiftActionDone, fnActionDone, STATE.Idle);
  244. Transition(STATE.Lifting, FSM_MSG.TIMER, fnLiftTimeout, STATE.Idle);
  245. Transition(STATE.Idle, MSG.Align, fnAlign, STATE.Aligning);
  246. Transition(STATE.Aligning, FSM_MSG.TIMER, fnAlignTimeout, STATE.Idle);
  247. Transition(STATE.Aligning, MSG.ActionDone, fnActionDone, STATE.Idle);
  248. EnumLoop<STATE>.ForEach((item) => { fsm.MapState((int)item, item.ToString()); });
  249. EnumLoop<MSG>.ForEach((item) => { fsm.MapMessage((int)item, item.ToString()); });
  250. Running = true;
  251. }
  252. private bool fnCommReady(object[] param)
  253. {
  254. return true;
  255. }
  256. private bool fnHomeAll(object[] param)
  257. {
  258. return _homeRoutine.Start(ModuleName.EFEM) == RState.Running;
  259. }
  260. private bool fnHomingTimeout(object[] param)
  261. {
  262. RState ret = _homeRoutine.Monitor();
  263. if (ret == RState.Failed || ret == RState.Timeout)
  264. {
  265. PostMsg(MSG.Error);
  266. return false;
  267. }
  268. return ret == RState.End;
  269. }
  270. private bool fnHomeRobot(object[] param)
  271. {
  272. return _homeRoutine.Start(ModuleName.EfemRobot) == RState.Running;
  273. }
  274. private bool fnHomeAligner(object[] param)
  275. {
  276. // module
  277. ModuleName unit = ModuleName.EFEM;
  278. if (param[0] is string s1)
  279. unit = ModuleNameString.ToEnum(s1);
  280. else if (param[0] is ModuleName mod)
  281. unit = mod;
  282. else
  283. throw new ArgumentException("Argument error");
  284. return _homeRoutine.Start(unit) == RState.Running;
  285. }
  286. private bool fnActionDone(object[] param)
  287. {
  288. return false;
  289. }
  290. public bool CheckToPostMessage(int msg, params object[] args)
  291. {
  292. if (!fsm.FindTransition(fsm.State, msg))
  293. {
  294. EV.PostWarningLog(Name, $"{Name} is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  295. return false;
  296. }
  297. Running = true;
  298. fsm.PostMsg(msg, args);
  299. return true;
  300. }
  301. private bool fnMonitor(object[] param)
  302. {
  303. _efem.Monitor();
  304. _debugRoutine();
  305. return true;
  306. }
  307. private bool fnOnline(object[] param)
  308. {
  309. bool bOnlineFlag = (bool)param[0];
  310. if (_efem is EfemBase efem)
  311. {
  312. efem.SetOnline(bOnlineFlag);
  313. }
  314. return true;
  315. }
  316. private string GetFsmLastMessage()
  317. {
  318. int msg = fsm.LastMsg;
  319. if (msg >= (int)MSG.HomeAll && msg <= (int)MSG.Error)
  320. return ((MSG)msg).ToString();
  321. if (msg == (int)FSM_MSG.TIMER)
  322. return "Timer";
  323. return msg.ToString();
  324. }
  325. private bool fnError(object[] param)
  326. {
  327. return true;
  328. }
  329. private bool fnToInit(object[] param)
  330. {
  331. return true;
  332. }
  333. private bool fnRecover(object[] param)
  334. {
  335. _efem.ClearError();
  336. //_efem.ExecuteAction();
  337. return true;
  338. }
  339. private bool fnAbortRobot(object[] param)
  340. {
  341. //_efem.ExecuteAction();
  342. return true;
  343. }
  344. private bool fnSetLED(object[] param)
  345. {
  346. LightType light = (LightType)param[0];
  347. LightStatus st = (LightStatus)param[1];
  348. _efem.SetLamp(light, st);
  349. return true;
  350. }
  351. private bool fnTurnOffBuzzer(object[] param)
  352. {
  353. return false;
  354. }
  355. private bool FnStartPick(object[] param)
  356. {
  357. return _pickRoutine.Start(param) == RState.Running;
  358. }
  359. private bool FnPickTimeout(object[] param)
  360. {
  361. RState ret = _pickRoutine.Monitor();
  362. if (ret == RState.Failed || ret == RState.Timeout)
  363. {
  364. PostMsg(MSG.Error);
  365. return false;
  366. }
  367. return ret == RState.End;
  368. }
  369. private bool FnAbortPick(object[] param)
  370. {
  371. _pickRoutine.Abort();
  372. return true;
  373. }
  374. private bool FnStartPlace(object[] param)
  375. {
  376. return _placeRoutine.Start(param) == RState.Running;
  377. }
  378. private bool FnPlaceTimeout(object[] param)
  379. {
  380. RState ret = _placeRoutine.Monitor();
  381. if (ret == RState.Failed || ret == RState.Timeout)
  382. {
  383. PostMsg(MSG.Error);
  384. return false;
  385. }
  386. return ret == RState.End;
  387. }
  388. private bool FnAbortPlace(object[] param)
  389. {
  390. _placeRoutine.Abort();
  391. return true;
  392. }
  393. private bool FnStartSwap(object[] param)
  394. {
  395. return _swapRoutine.Start(param) == RState.Running;
  396. }
  397. private bool FnSwapTimeout(object[] param)
  398. {
  399. RState ret = _swapRoutine.Monitor();
  400. if (ret == RState.Failed || ret == RState.Timeout)
  401. {
  402. PostMsg(MSG.Error);
  403. return false;
  404. }
  405. return ret == RState.End;
  406. }
  407. private bool FnAbortSwap(object[] param)
  408. {
  409. _swapRoutine.Abort();
  410. return true;
  411. }
  412. private bool fnGoto(object[] param)
  413. {
  414. // module
  415. ModuleName unit = ModuleName.EFEM;
  416. if (param[0] is string s1)
  417. unit = ModuleNameString.ToEnum(s1);
  418. else if (param[0] is ModuleName mod)
  419. unit = mod;
  420. else
  421. throw new ArgumentException("Argument error");
  422. _efem.Goto(unit, Hand.Blade1);
  423. return true;
  424. }
  425. private bool fnLift(object[] param)
  426. {
  427. // module
  428. ModuleName unit = ModuleName.EFEM;
  429. if (param[0] is string s1)
  430. unit = ModuleNameString.ToEnum(s1);
  431. else if (param[0] is ModuleName mod)
  432. unit = mod;
  433. else
  434. throw new ArgumentException("Argument error");
  435. bool isUp = true;
  436. if (param.Length > 1)
  437. {
  438. isUp = (bool) param[1];
  439. }
  440. if (isUp)
  441. {
  442. if (!_efem.SetPinUp(unit))
  443. return false;
  444. }
  445. else
  446. {
  447. if (!_efem.SetPinDown(unit))
  448. return false;
  449. }
  450. LiftMessage = isUp ? "Up" : "Down";
  451. return true;
  452. }
  453. private bool fnLiftTimeout(object[] param)
  454. {
  455. if (LiftMessage == "Up")
  456. {
  457. return _efem.LiftIsDown == false && _efem.LiftIsUp == true;
  458. }
  459. else if (LiftMessage == "Down")
  460. {
  461. return _efem.LiftIsDown == true && _efem.LiftIsUp == false;
  462. }
  463. return false;
  464. }
  465. private bool fnAlign(object[] param)
  466. {
  467. // module
  468. //ModuleName unit = ModuleName.EFEM;
  469. //if (param.Length < 2)
  470. //{
  471. // return _efem.Align(unit, 180, 1000, WaferSize.WS12);
  472. //}
  473. //if (param[0] is string s1)
  474. // unit = ModuleNameString.ToEnum(s1);
  475. //else if (param[0] is ModuleName mod)
  476. // unit = mod;
  477. //else
  478. // throw new ArgumentException("Argument error");
  479. //// wafer size
  480. //WaferSize ws1 = WaferSize.WS0;
  481. //if (param[1] is string s2)
  482. //{
  483. // if (Enum.TryParse(s2, out WaferSize p5))
  484. // ws1 = p5;
  485. //}
  486. //else if (param[1] is WaferSize p6)
  487. //{
  488. // ws1 = p6;
  489. //}
  490. //if (!_efem.Align(unit, 180,1000, ws1))
  491. // return false;
  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, float time)
  554. {
  555. if (CheckToPostMessage((int)MSG.Align, module, 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. }