EfemEntity.cs 39 KB

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