LoadPortModule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Device.Unit;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.Fsm;
  6. using Aitex.Core.RT.OperationCenter;
  7. using Aitex.Core.RT.Routine;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Utilities;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Alarms;
  12. using MECF.Framework.Common.Equipment;
  13. using MECF.Framework.Common.Event;
  14. using MECF.Framework.Common.Schedulers;
  15. using MECF.Framework.Common.SubstrateTrackings;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using FurnaceRT.Equipments.Systems;
  22. using Aitex.Core.Util;
  23. using MECF.Framework.Common.Device.Bases;
  24. using Aitex.Core.RT.Log;
  25. namespace FurnaceRT.Equipments.LPs
  26. {
  27. public partial class LoadPortModule : LoadPortModuleBase
  28. {
  29. public enum STATE
  30. {
  31. NotInstall,
  32. NotConnected,
  33. Init,
  34. Idle,
  35. Homing,
  36. Unloading,
  37. Loading,
  38. Error,
  39. InTransfer,
  40. }
  41. public enum MSG
  42. {
  43. Home,
  44. Disconnected,
  45. Connected,
  46. Reset,
  47. Abort,
  48. Error,
  49. Load,
  50. AutoLoad,
  51. Unload,
  52. AutoUnload,
  53. AutoLoadRetry,
  54. LoadRetry,
  55. UnloadRetry,
  56. AutoUnloadRetry,
  57. InTransfer,
  58. TransferComplete,
  59. ToInit,
  60. }
  61. public override bool IsReady
  62. {
  63. get { return FsmState == (int)STATE.Idle && CheckAllMessageProcessed(); }
  64. }
  65. public override bool IsError
  66. {
  67. get
  68. {
  69. return FsmState == (int)STATE.Error;
  70. }
  71. }
  72. public override bool IsInit
  73. {
  74. get { return FsmState == (int)STATE.Init; }
  75. }
  76. public event Action<string> OnEnterError;
  77. public string CurrentLotId { get; set; }
  78. public List<string> WaitLotId { get; set; }
  79. //public bool IsLoaded => SMIFDevice.IsLoaded;
  80. //public bool IsUnloaded => SMIFDevice.IsUnloaded;
  81. //public bool IsExtend => SMIFDevice.IsExtend;
  82. //public bool IsRetract => SMIFDevice.IsRetract;
  83. #region fields
  84. private LoadPortLoad _loadRoutine;
  85. private LoadPortAutoLoad _autoLoadRoutine;
  86. private LoadPortUnload _unloadRoutine;
  87. private LoadPortAutoUnload _autoUnloadRoutine;
  88. private LoadPortHome _homeRoutine;
  89. #endregion
  90. public LoadPortModule(ModuleName module) : base(SC.GetValue<int>("System.CassetteSlotCount"))
  91. {
  92. Name = module.ToString();
  93. Module = module.ToString();
  94. IsOnline = true;
  95. }
  96. public override bool Initialize()
  97. {
  98. InitRoutine();
  99. InitDevice();
  100. InitFsm();
  101. InitOp();
  102. InitData();
  103. InitAlarmEvent();
  104. Singleton<EventManager>.Instance.OnAlarmEvent += Instance_OnAlarmEvent;
  105. return base.Initialize();
  106. }
  107. private void Instance_OnAlarmEvent(EventItem item)
  108. {
  109. if (item != null && item.Level == EventLevel.Alarm && (item.Source == Name || item.Source == Module))
  110. {
  111. DEVICE.GetDevice<SignalTowerBase>("System.SignalTower")?.Reset();
  112. LOG.Write($"{item.Source} {item.EventEnum} {item.Description}\n");
  113. PostMsg(MSG.Error);
  114. }
  115. }
  116. private void InitRoutine()
  117. {
  118. _loadRoutine = new LoadPortLoad(this);
  119. _autoLoadRoutine = new LoadPortAutoLoad(this);
  120. _unloadRoutine = new LoadPortUnload(this);
  121. _autoUnloadRoutine = new LoadPortAutoUnload(this);
  122. _homeRoutine = new LoadPortHome(this);
  123. }
  124. private void InitData()
  125. {
  126. DATA.Subscribe($"{Module}.Status", () => StringFsmStatus);
  127. DATA.Subscribe($"{Module}.IsOnline", () => IsOnline);
  128. DATA.Subscribe($"{Module}.IsError", () => IsError);
  129. DATA.Subscribe($"{Name}.CurrentLotId", () => CurrentLotId);
  130. DATA.Subscribe($"{Name}.WaitLotId", () => WaitLotId);
  131. DATA.Subscribe($"{Name}.CarrierId", () => CarrierId);
  132. }
  133. private void InitOp()
  134. {
  135. OP.Subscribe($"{Module}.Home", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Home));
  136. OP.Subscribe($"{Module}.Abort", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Abort));
  137. OP.Subscribe($"{Module}.Load", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Load));
  138. OP.Subscribe($"{Module}.Unload", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Unload));
  139. OP.Subscribe($"{Module}.Reset", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Reset));
  140. OP.Subscribe($"{Module}.ReadCarrierId", (string cmd, object[] args) => ReadCarrierId(args));
  141. OP.Subscribe($"{Module}.AlarmAction", (string cmd, object[] args) =>
  142. {
  143. Enum.TryParse(args[0].ToString(), out AlarmAction alarmAction);
  144. string eventName = null;
  145. if (args.Length > 1)
  146. eventName = args[1].ToString();
  147. if (eventName != null)
  148. {
  149. EV.ClearAlarmEvent(eventName);
  150. var item = _triggeredAlarmList.FirstOrDefault(x => x.EventEnum == eventName);
  151. if (item != null)
  152. {
  153. item.Reset();
  154. _triggeredAlarmList.Remove(item);
  155. }
  156. if (item != null)
  157. {
  158. switch (alarmAction)
  159. {
  160. case AlarmAction.Retry:
  161. {
  162. CheckToPostMessage((int)item.RetryMessage, item.RetryMessageParas);
  163. }
  164. break;
  165. case AlarmAction.Abort:
  166. {
  167. CheckToPostMessage((int)MSG.Abort);
  168. }
  169. break;
  170. case AlarmAction.Clear:
  171. {
  172. int alarmCount = 0;
  173. var alarms = EV.GetAlarmEvent();
  174. foreach (var alarm in alarms)
  175. {
  176. if (alarm.Level == EventLevel.Alarm && alarm.Source == Name)
  177. alarmCount++;
  178. }
  179. if (alarmCount == 0)
  180. CheckToPostMessage((int)MSG.Reset);
  181. }
  182. break;
  183. case AlarmAction.Continue:
  184. {
  185. int alarmCount = 0;
  186. var alarms = EV.GetAlarmEvent();
  187. foreach (var alarm in alarms)
  188. {
  189. if (alarm.Level == EventLevel.Alarm && alarm.Source == Name)
  190. alarmCount++;
  191. }
  192. if (alarmCount == 0)
  193. CheckToPostMessage((int)MSG.Reset);
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. return true;
  200. });
  201. }
  202. private void InitFsm()
  203. {
  204. EnumLoop<STATE>.ForEach((item) =>
  205. {
  206. MapState((int)item, item.ToString());
  207. });
  208. EnumLoop<MSG>.ForEach((item) =>
  209. {
  210. MapMessage((int)item, item.ToString());
  211. });
  212. EnableFsm(100, IsInstalled ? STATE.Idle : STATE.NotInstall);
  213. //init
  214. Transition(STATE.Init, MSG.Home, FsmStartHome, STATE.Homing);
  215. Transition(STATE.Idle, MSG.Abort, null, STATE.Idle);
  216. Transition(STATE.Idle, MSG.Reset, null, STATE.Idle);
  217. Transition(STATE.Init, MSG.Reset, null, STATE.Init);
  218. Transition(STATE.Error, MSG.Abort, null, STATE.Error);
  219. AnyStateTransition(MSG.ToInit, FsmToInit, STATE.Init);
  220. Transition(STATE.Error, MSG.Home, FsmStartHome, STATE.Homing);
  221. Transition(STATE.Idle, MSG.Home, FsmStartHome, STATE.Homing);
  222. Transition(STATE.Homing, FSM_MSG.TIMER, FsmMonitorHomeTask, STATE.Idle);
  223. Transition(STATE.Homing, MSG.Error, null, STATE.Init);
  224. Transition(STATE.Homing, MSG.Abort, FsmAbortTask, STATE.Init);
  225. Transition(STATE.Idle, MSG.InTransfer, null, STATE.InTransfer);
  226. Transition(STATE.InTransfer, MSG.TransferComplete, null, STATE.Idle);
  227. Transition(STATE.InTransfer, MSG.Abort, null, STATE.Idle);
  228. Transition(STATE.Idle, MSG.Abort, null, STATE.Idle);
  229. Transition(STATE.Idle, MSG.Reset, null, STATE.Idle);
  230. Transition(STATE.Error, MSG.Abort, null, STATE.Error);
  231. //Error
  232. AnyStateTransition(MSG.Error, FsmOnError, STATE.Error);
  233. Transition(STATE.Error, MSG.Reset, FsmReset, STATE.Idle);
  234. EnterExitTransition<STATE, FSM_MSG>(STATE.Error, FsmEnterError, FSM_MSG.NONE, FsmExitError);
  235. //load
  236. Transition(STATE.Error, MSG.LoadRetry, FsmStartLoad, STATE.Loading);
  237. Transition(STATE.Error, MSG.AutoLoadRetry, FsmStartAutoLoad, STATE.Loading);
  238. Transition(STATE.Idle, MSG.Load, FsmStartLoad, STATE.Loading);
  239. Transition(STATE.Idle, MSG.AutoLoad, FsmStartAutoLoad, STATE.Loading);
  240. Transition(STATE.Loading, FSM_MSG.TIMER, FsmMonitorLoadTask, STATE.Idle);
  241. Transition(STATE.Loading, MSG.Abort, FsmAbortTask, STATE.Idle);
  242. //unload
  243. Transition(STATE.Error, MSG.UnloadRetry, FsmStartUnload, STATE.Unloading);
  244. Transition(STATE.Error, MSG.AutoUnloadRetry, FsmStartAutoUnload, STATE.Unloading);
  245. Transition(STATE.Idle, MSG.Unload, FsmStartUnload, STATE.Unloading);
  246. Transition(STATE.Idle, MSG.AutoUnload, FsmStartAutoUnload, STATE.Unloading);
  247. Transition(STATE.Unloading, FSM_MSG.TIMER, FsmMonitorUnloadTask, STATE.Idle);
  248. Transition(STATE.Unloading, MSG.Abort, FsmAbortTask, STATE.Idle);
  249. }
  250. #region Service Functions
  251. public override bool Home(out string reason)
  252. {
  253. if (!CheckToPostMessage((int)MSG.Home))
  254. {
  255. reason = $"Can not home in {StringFsmStatus} status";
  256. return false;
  257. }
  258. reason = string.Empty;
  259. return true;
  260. }
  261. public override void Reset()
  262. {
  263. _triggeredAlarmList.ForEach(x => x.Reset());
  264. _triggeredAlarmList.Clear();
  265. if (IsError)
  266. {
  267. CheckToPostMessage((int)MSG.Reset);
  268. }
  269. }
  270. private bool FsmReset(object[] param)
  271. {
  272. return true;
  273. }
  274. public override bool PrepareTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType, out string reason)
  275. {
  276. reason = string.Empty;
  277. return true;
  278. }
  279. public override bool TransferHandoff(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType, out string reason)
  280. {
  281. reason = string.Empty;
  282. return true;
  283. }
  284. public override bool PostTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType, out string reason)
  285. {
  286. reason = string.Empty;
  287. return true;
  288. }
  289. public override bool CheckReadyForTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType,
  290. out string reason)
  291. {
  292. reason = string.Empty;
  293. if (!IsReady)
  294. {
  295. reason = $"{Module} status={StringFsmStatus} is busy";
  296. return false;
  297. }
  298. if (transferType == EnumTransferType.Place)
  299. {
  300. if (!LPDevice.IsFoupPresent || (SC.GetValue<bool>("System.IsSimulatorMode") && CarrierManager.Instance.CheckNoCarrier(Module, 0)))
  301. return true;
  302. reason = $"foup present";
  303. return false;
  304. }
  305. if (transferType == EnumTransferType.Pick)
  306. {
  307. if (LPDevice.IsFoupPresent)
  308. return true;
  309. reason = $"foup not present";
  310. return false;
  311. }
  312. if (!IsReleased)
  313. {
  314. reason = $"{Module} is not release";
  315. return false;
  316. }
  317. return false;
  318. }
  319. public override void NoteTransferStart(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
  320. {
  321. CheckToPostMessage(MSG.InTransfer);
  322. }
  323. public override void NoteTransferStop(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
  324. {
  325. if (FsmState == (int)STATE.InTransfer)
  326. CheckToPostMessage(MSG.TransferComplete);
  327. }
  328. private bool FsmMonitorLoadTask(object[] param)
  329. {
  330. Result ret = MonitorRoutine();
  331. if (ret == Result.FAIL)
  332. {
  333. PostMsg(MSG.Error);
  334. return false;
  335. }
  336. if (ret == Result.DONE)
  337. IsOnline = true;
  338. return ret == Result.DONE;
  339. }
  340. private bool FsmMonitorUnloadTask(object[] param)
  341. {
  342. Result ret = MonitorRoutine();
  343. if (ret == Result.FAIL)
  344. {
  345. PostMsg(MSG.Error);
  346. return false;
  347. }
  348. return ret == Result.DONE;
  349. }
  350. private bool FsmStartLoad(object[] param)
  351. {
  352. QueueRoutine.Clear();
  353. _loadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  354. QueueRoutine.Enqueue(_loadRoutine);
  355. Result ret = QueueRoutine.Peek().Start();
  356. if (ret == Result.FAIL || ret == Result.DONE)
  357. return false;
  358. return ret == Result.RUN;
  359. }
  360. private bool FsmStartAutoLoad(object[] param)
  361. {
  362. QueueRoutine.Clear();
  363. _autoLoadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  364. QueueRoutine.Enqueue(_autoLoadRoutine);
  365. Result ret = QueueRoutine.Peek().Start();
  366. if (ret == Result.FAIL || ret == Result.DONE)
  367. return false;
  368. return ret == Result.RUN;
  369. }
  370. private bool FsmStartUnload(object[] param)
  371. {
  372. QueueRoutine.Clear();
  373. _loadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  374. Result ret = StartRoutine(_unloadRoutine);
  375. if (ret == Result.FAIL || ret == Result.DONE)
  376. return false;
  377. return ret == Result.RUN;
  378. }
  379. private bool FsmStartAutoUnload(object[] param)
  380. {
  381. QueueRoutine.Clear();
  382. _autoUnloadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  383. Result ret = StartRoutine(_autoUnloadRoutine);
  384. if (ret == Result.FAIL || ret == Result.DONE)
  385. return false;
  386. return ret == Result.RUN;
  387. }
  388. private bool FsmStartHome(object[] param)
  389. {
  390. Result ret = StartRoutine(_homeRoutine);
  391. if (ret == Result.FAIL || ret == Result.DONE)
  392. return false;
  393. return ret == Result.RUN;
  394. }
  395. private bool FsmMonitorHomeTask(object[] param)
  396. {
  397. Result ret = MonitorRoutine();
  398. if (ret == Result.FAIL)
  399. {
  400. PostMsg(MSG.Error);
  401. return false;
  402. }
  403. if (ret == Result.DONE)
  404. {
  405. return true;
  406. }
  407. return false;
  408. }
  409. private bool FsmExitError(object[] param)
  410. {
  411. return true;
  412. }
  413. private bool FsmEnterError(object[] param)
  414. {
  415. if (OnEnterError != null)
  416. OnEnterError(Module);
  417. return true;
  418. }
  419. private bool FsmAbortTask(object[] param)
  420. {
  421. AbortRoutine();
  422. return true;
  423. }
  424. private bool FsmToInit(object[] param)
  425. {
  426. return true;
  427. }
  428. private bool FsmOnError(object[] param)
  429. {
  430. if (FsmState == (int)STATE.Error)
  431. {
  432. return false;
  433. }
  434. if (FsmState != (int)STATE.Init && FsmState != (int)STATE.Idle)
  435. AbortRoutine();
  436. //AbortRoutine();
  437. //if (FsmState == (int)STATE.Init)
  438. // return false;
  439. return true;
  440. }
  441. public void InvokeLoad()
  442. {
  443. CheckToPostMessage((int)MSG.Load);
  444. }
  445. public void InvokeUnload()
  446. {
  447. CheckToPostMessage((int)MSG.Unload);
  448. }
  449. #endregion
  450. }
  451. }