EfemEntity.cs 39 KB

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