LoadPortModule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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.InTransfer, null, STATE.InTransfer);
  227. Transition(STATE.InTransfer, MSG.TransferComplete, null, STATE.Idle);
  228. Transition(STATE.InTransfer, MSG.Abort, null, STATE.Idle);
  229. Transition(STATE.Idle, MSG.Abort, null, STATE.Idle);
  230. Transition(STATE.Idle, MSG.Reset, null, STATE.Idle);
  231. Transition(STATE.Error, MSG.Abort, null, STATE.Error);
  232. //Error
  233. AnyStateTransition(MSG.Error, FsmOnError, STATE.Error);
  234. Transition(STATE.Error, MSG.Reset, FsmReset, STATE.Idle);
  235. EnterExitTransition<STATE, FSM_MSG>(STATE.Error, FsmEnterError, FSM_MSG.NONE, FsmExitError);
  236. //load
  237. Transition(STATE.Error, MSG.LoadRetry, FsmStartLoad, STATE.Loading);
  238. Transition(STATE.Error, MSG.AutoLoadRetry, FsmStartAutoLoad, STATE.Loading);
  239. Transition(STATE.Idle, MSG.Load, FsmStartLoad, STATE.Loading);
  240. Transition(STATE.Idle, MSG.AutoLoad, FsmStartAutoLoad, STATE.Loading);
  241. Transition(STATE.Loading, FSM_MSG.TIMER, FsmMonitorLoadTask, STATE.Idle);
  242. Transition(STATE.Loading, MSG.Abort, FsmAbortTask, STATE.Idle);
  243. //unload
  244. Transition(STATE.Error, MSG.UnloadRetry, FsmStartUnload, STATE.Unloading);
  245. Transition(STATE.Error, MSG.AutoUnloadRetry, FsmStartAutoUnload, STATE.Unloading);
  246. Transition(STATE.Idle, MSG.Unload, FsmStartUnload, STATE.Unloading);
  247. Transition(STATE.Idle, MSG.AutoUnload, FsmStartAutoUnload, STATE.Unloading);
  248. Transition(STATE.Unloading, FSM_MSG.TIMER, FsmMonitorUnloadTask, STATE.Idle);
  249. Transition(STATE.Unloading, MSG.Abort, FsmAbortTask, STATE.Idle);
  250. }
  251. #region Service Functions
  252. public override bool Home(out string reason)
  253. {
  254. if (!CheckToPostMessage((int)MSG.Home))
  255. {
  256. reason = $"Can not home in {StringFsmStatus} status";
  257. return false;
  258. }
  259. reason = string.Empty;
  260. return true;
  261. }
  262. public override void Reset()
  263. {
  264. _triggeredAlarmList.ForEach(x => x.Reset());
  265. _triggeredAlarmList.Clear();
  266. if (IsError)
  267. {
  268. CheckToPostMessage((int)MSG.Reset);
  269. }
  270. }
  271. private bool FsmReset(object[] param)
  272. {
  273. return true;
  274. }
  275. public override bool PrepareTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType, out string reason)
  276. {
  277. reason = string.Empty;
  278. return true;
  279. }
  280. public override bool TransferHandoff(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType, out string reason)
  281. {
  282. reason = string.Empty;
  283. return true;
  284. }
  285. public override bool PostTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType, out string reason)
  286. {
  287. reason = string.Empty;
  288. return true;
  289. }
  290. public override bool CheckReadyForTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType,
  291. out string reason)
  292. {
  293. reason = string.Empty;
  294. if (!IsReady)
  295. {
  296. reason = $"{Module} status={StringFsmStatus} is busy";
  297. return false;
  298. }
  299. if (transferType == EnumTransferType.Place)
  300. {
  301. if (!LPDevice.IsFoupPresent || (SC.GetValue<bool>("System.IsSimulatorMode") && CarrierManager.Instance.CheckNoCarrier(Module, 0)))
  302. return true;
  303. reason = $"foup present";
  304. return false;
  305. }
  306. if (transferType == EnumTransferType.Pick)
  307. {
  308. if (LPDevice.IsFoupPresent)
  309. return true;
  310. reason = $"foup not present";
  311. return false;
  312. }
  313. if (!IsReleased)
  314. {
  315. reason = $"{Module} is not release";
  316. return false;
  317. }
  318. return false;
  319. }
  320. public override void NoteTransferStart(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
  321. {
  322. CheckToPostMessage(MSG.InTransfer);
  323. }
  324. public override void NoteTransferStop(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
  325. {
  326. if (FsmState == (int)STATE.InTransfer)
  327. CheckToPostMessage(MSG.TransferComplete);
  328. }
  329. private bool FsmMonitorLoadTask(object[] param)
  330. {
  331. Result ret = MonitorRoutine();
  332. if (ret == Result.FAIL)
  333. {
  334. PostMsg(MSG.Error);
  335. return false;
  336. }
  337. if (ret == Result.DONE)
  338. IsOnline = true;
  339. return ret == Result.DONE;
  340. }
  341. private bool FsmMonitorUnloadTask(object[] param)
  342. {
  343. Result ret = MonitorRoutine();
  344. if (ret == Result.FAIL)
  345. {
  346. PostMsg(MSG.Error);
  347. return false;
  348. }
  349. return ret == Result.DONE;
  350. }
  351. private bool FsmStartLoad(object[] param)
  352. {
  353. QueueRoutine.Clear();
  354. _loadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  355. QueueRoutine.Enqueue(_loadRoutine);
  356. Result ret = QueueRoutine.Peek().Start();
  357. if (ret == Result.FAIL || ret == Result.DONE)
  358. return false;
  359. return ret == Result.RUN;
  360. }
  361. private bool FsmStartAutoLoad(object[] param)
  362. {
  363. QueueRoutine.Clear();
  364. _autoLoadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  365. QueueRoutine.Enqueue(_autoLoadRoutine);
  366. Result ret = QueueRoutine.Peek().Start();
  367. if (ret == Result.FAIL || ret == Result.DONE)
  368. return false;
  369. return ret == Result.RUN;
  370. }
  371. private bool FsmStartUnload(object[] param)
  372. {
  373. QueueRoutine.Clear();
  374. _loadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  375. Result ret = StartRoutine(_unloadRoutine);
  376. if (ret == Result.FAIL || ret == Result.DONE)
  377. return false;
  378. return ret == Result.RUN;
  379. }
  380. private bool FsmStartAutoUnload(object[] param)
  381. {
  382. QueueRoutine.Clear();
  383. _autoUnloadRoutine.Init(param.Length > 0 ? (bool)param[0] : false);
  384. Result ret = StartRoutine(_autoUnloadRoutine);
  385. if (ret == Result.FAIL || ret == Result.DONE)
  386. return false;
  387. return ret == Result.RUN;
  388. }
  389. private bool FsmStartHome(object[] param)
  390. {
  391. Result ret = StartRoutine(_homeRoutine);
  392. if (ret == Result.FAIL || ret == Result.DONE)
  393. return false;
  394. return ret == Result.RUN;
  395. }
  396. private bool FsmMonitorHomeTask(object[] param)
  397. {
  398. Result ret = MonitorRoutine();
  399. if (ret == Result.FAIL)
  400. {
  401. PostMsg(MSG.Error);
  402. return false;
  403. }
  404. if (ret == Result.DONE)
  405. {
  406. return true;
  407. }
  408. return false;
  409. }
  410. private bool FsmExitError(object[] param)
  411. {
  412. return true;
  413. }
  414. private bool FsmEnterError(object[] param)
  415. {
  416. if (OnEnterError != null)
  417. OnEnterError(Module);
  418. return true;
  419. }
  420. private bool FsmAbortTask(object[] param)
  421. {
  422. AbortRoutine();
  423. return true;
  424. }
  425. private bool FsmToInit(object[] param)
  426. {
  427. return true;
  428. }
  429. private bool FsmOnError(object[] param)
  430. {
  431. if (FsmState == (int)STATE.Error)
  432. {
  433. return false;
  434. }
  435. if (FsmState != (int)STATE.Init && FsmState != (int)STATE.Idle)
  436. AbortRoutine();
  437. //AbortRoutine();
  438. //if (FsmState == (int)STATE.Init)
  439. // return false;
  440. return true;
  441. }
  442. public void InvokeLoad()
  443. {
  444. CheckToPostMessage((int)MSG.Load);
  445. }
  446. public void InvokeUnload()
  447. {
  448. CheckToPostMessage((int)MSG.Unload);
  449. }
  450. #endregion
  451. }
  452. }