LoadPortModule.cs 18 KB

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