EfemEntity.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 _signalTower;
  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. _signalTower = 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. _signalTower.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. _signalTower.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. public EfemBackroundMsgProcessor(EfemBase efem)
  857. {
  858. _efem = efem;
  859. }
  860. public void Monitor()
  861. {
  862. if(Singleton<RouteManager>.Instance.EFEM.RobotStatus != RState.Running && _pendingCmds.Count > 0)
  863. {
  864. if(_pendingCmds.TryDequeue(out object[] args))
  865. {
  866. EfemEntity.MSG cmd = (EfemEntity.MSG)args[0];
  867. if ((int)cmd > (int)EfemEntity.MSG.BackroundCmd && (int)cmd < (int)EfemEntity.MSG.MaxMsgValue)
  868. {
  869. processBackroundCommand(cmd, args);
  870. }
  871. else
  872. {
  873. LOG.Write(eEvent.ERR_EFEM_ROBOT, ModuleName.EFEM, $"Invalid EFEM backround command {cmd}");
  874. }
  875. }
  876. }
  877. }
  878. public void SendBackroundCommand(object[] args)
  879. {
  880. _pendingCmds.Enqueue(args);
  881. }
  882. private void processBackroundCommand(EfemEntity.MSG cmd, object[] args)
  883. {
  884. switch(cmd)
  885. {
  886. case EfemEntity.MSG.Light:
  887. _efem.SetLamp((LightType)args[1], (LightStatus)args[2]);
  888. break;
  889. case EfemEntity.MSG.SetThickness:
  890. _efem.SetThickness((ModuleName)args[1], (string)args[2]);
  891. break;
  892. case EfemEntity.MSG.HomeLP:
  893. _efem.Home((ModuleName)args[1]);
  894. break;
  895. case EfemEntity.MSG.Load:
  896. _efem.Load((ModuleName)args[1]);
  897. break;
  898. case EfemEntity.MSG.Unload:
  899. _efem.Unload((ModuleName)args[1]);
  900. break;
  901. case EfemEntity.MSG.Dock:
  902. _efem.Dock((ModuleName)args[1]);
  903. break;
  904. case EfemEntity.MSG.Undock:
  905. _efem.Undock((ModuleName)args[1]);
  906. break;
  907. case EfemEntity.MSG.Clamp:
  908. _efem.Clamp((ModuleName)args[1], (bool)args[2]);
  909. break;
  910. case EfemEntity.MSG.Unclamp:
  911. _efem.Unclamp((ModuleName)args[1]);
  912. break;
  913. case EfemEntity.MSG.Map:
  914. _efem.Map((ModuleName)args[1]);
  915. break;
  916. case EfemEntity.MSG.ReadCarrierId:
  917. _efem.ReadCarrierId((ModuleName)args[1]);
  918. break;
  919. case EfemEntity.MSG.WriteCarrierID:
  920. _efem.WriteCarrierId((ModuleName)args[1], (string)args[2]);
  921. break;
  922. case EfemEntity.MSG.ReadTagData:
  923. _efem.ReadCarrierId((ModuleName)args[1]);
  924. break;
  925. case EfemEntity.MSG.WriteTagData:
  926. _efem.WriteCarrierId((ModuleName)args[1], (string)args[2]);
  927. break;
  928. default:
  929. LOG.Write(eEvent.ERR_EFEM_ROBOT, ModuleName.EFEM, $"unprocessed Efem command {cmd}");
  930. break;
  931. }
  932. }
  933. }
  934. }