TMModule.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aitex.Core.RT.DataCenter;
  7. using Aitex.Core.RT.Device;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.Fsm;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.Routine;
  12. using Aitex.Core.Utilities;
  13. using Aitex.Sorter.Common;
  14. using MECF.Framework.Common.Equipment;
  15. using MECF.Framework.Common.Event;
  16. using MECF.Framework.Common.SubstrateTrackings;
  17. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
  18. using MECF.Framework.RT.ModuleLibrary.TMModules;
  19. namespace JetMainframe.TMs
  20. {
  21. public partial class TMModule : TMModuleBase
  22. {
  23. public enum STATE
  24. {
  25. NotInstall,
  26. Init,
  27. Idle,
  28. Homing,
  29. Picking,
  30. Placing,
  31. RobotGoto,
  32. Swapping,
  33. Mapping,
  34. Load,
  35. Unload,
  36. Error,
  37. NotConnect,
  38. }
  39. public enum MSG
  40. {
  41. Home,
  42. Connected,
  43. Disconnected,
  44. Map,
  45. Pick,
  46. PickAndPlace,
  47. Place,
  48. Goto,
  49. Reset,
  50. Abort,
  51. Error,
  52. ToInit,
  53. }
  54. public override bool IsReady
  55. {
  56. get { return FsmState == (int)STATE.Idle && CheckAllMessageProcessed(); }
  57. }
  58. public override bool IsError
  59. {
  60. get { return FsmState == (int)STATE.Error; }
  61. }
  62. public override bool IsInit
  63. {
  64. get { return FsmState == (int)STATE.Init; }
  65. }
  66. public bool IsBusy
  67. {
  68. get { return !IsInit && !IsError && !IsReady; }
  69. }
  70. public event Action<string> OnEnterError;
  71. public bool IsEfemRobotHomed { get; set; }
  72. public RobotBaseDevice RobotDevice { get; set; }
  73. protected TMHomeRoutine _homeRoutine = null;
  74. protected TMPickRoutine _pickRoutine = null;
  75. protected TMPlaceRoutine _placeRoutine = null;
  76. protected TMGotoRoutine _gotoRoutine = null;
  77. protected TMMapRoutine _mapRoutine = null;
  78. protected TMPickAndPlaceRoutine _pickAndPlaceRoutine = null;
  79. //private bool _isInit;
  80. public TMModule(ModuleName module, int slotCount = 2) : base(slotCount)
  81. {
  82. Module = module.ToString();
  83. Name = module.ToString();
  84. IsOnline = true;
  85. }
  86. public override bool Initialize()
  87. {
  88. InitRoutine();
  89. InitDevice();
  90. InitFsm();
  91. InitOp();
  92. InitData();
  93. return base.Initialize();
  94. }
  95. protected virtual void InitRoutine()
  96. {
  97. _homeRoutine = new TMHomeRoutine(this);
  98. _pickRoutine = new TMPickRoutine(this);
  99. _placeRoutine = new TMPlaceRoutine(this);
  100. _gotoRoutine = new TMGotoRoutine(this);
  101. _mapRoutine = new TMMapRoutine(this);
  102. _pickAndPlaceRoutine = new TMPickAndPlaceRoutine(this);
  103. }
  104. private void InitData()
  105. {
  106. DATA.Subscribe($"{Module}.Status", () => StringFsmStatus);
  107. DATA.Subscribe($"{Module}.IsOnline", () => IsOnline);
  108. DATA.Subscribe($"{Module}.IsHomed", () => IsEfemRobotHomed);
  109. DATA.Subscribe($"{Module}.IsError", () => IsError);
  110. DATA.Subscribe($"{Module}.WaferSize", () => WaferManager.Instance.GetWaferSize(ModuleHelper.Converter(Module), 0).ToString());
  111. }
  112. private void InitOp()
  113. {
  114. OP.Subscribe($"{Name}.Home", (string cmd, object[] args) =>
  115. {
  116. return CheckToPostMessage((int)MSG.Home);
  117. });
  118. OP.Subscribe($"{Name}.Abort", (string cmd, object[] args) =>
  119. {
  120. return CheckToPostMessage((int)MSG.Abort);
  121. });
  122. OP.Subscribe($"{Name}.Reset", (string cmd, object[] args) =>
  123. {
  124. return CheckToPostMessage((int)MSG.Reset);
  125. });
  126. OP.Subscribe($"{Name}.Pick", (string cmd, object[] args) =>
  127. {
  128. return CheckToPostMessage((int)MSG.Pick, args);
  129. });
  130. OP.Subscribe($"{Name}.Place", (string cmd, object[] args) =>
  131. {
  132. return CheckToPostMessage((int)MSG.Place, args);
  133. });
  134. OP.Subscribe($"{Name}.Goto", (string cmd, object[] args) =>
  135. {
  136. return CheckToPostMessage((int)MSG.Goto, args[0]);
  137. });
  138. OP.Subscribe($"{Name}.Map", (string cmd, object[] args) =>
  139. {
  140. return CheckToPostMessage((int)MSG.Map, args[0]);
  141. });
  142. OP.Subscribe($"{Name}.SetOnline", (string cmd, object[] args) =>
  143. {
  144. IsOnline = true;
  145. return true;
  146. });
  147. OP.Subscribe($"{Name}.SetOffline", (string cmd, object[] args) =>
  148. {
  149. IsOnline = false;
  150. return true;
  151. });
  152. //OP.Subscribe("EFEM.Initialize", (method, objects) =>
  153. //{
  154. // Init();
  155. // return true;
  156. //});
  157. //OP.Subscribe("EFEM.ClearError", (method, objects) =>
  158. //{
  159. // ClearError();
  160. // return true;
  161. //});
  162. }
  163. private void InitFsm()
  164. {
  165. EnumLoop<STATE>.ForEach((item) =>
  166. {
  167. MapState((int)item, item.ToString());
  168. });
  169. EnumLoop<MSG>.ForEach((item) =>
  170. {
  171. MapMessage((int)item, item.ToString());
  172. });
  173. EnableFsm(50, IsInstalled ? STATE.Init : STATE.NotInstall);
  174. //Error
  175. AnyStateTransition(MSG.Error, FsmOnError, STATE.Error);
  176. Transition(STATE.Error, MSG.Reset, FsmReset, STATE.Idle);
  177. EnterExitTransition<STATE, FSM_MSG>(STATE.Error, FsmEnterError, FSM_MSG.NONE, FsmExitError);
  178. AnyStateTransition(FSM_MSG.WARNING, fWarning, FSM_STATE.SAME);
  179. AnyStateTransition((int)FSM_MSG.ALARM, fAlarm, (int)STATE.Error);
  180. //connection
  181. AnyStateTransition(MSG.Disconnected, FsmOnDisconnected, STATE.NotConnect);
  182. Transition(STATE.NotConnect, MSG.Connected, FsmOnConnected, STATE.Init);
  183. Transition(STATE.NotConnect, MSG.Reset, FsmResetConnect, STATE.NotConnect);
  184. //Init
  185. Transition(STATE.Init, MSG.Home, FsmStartHome, STATE.Homing);
  186. Transition(STATE.Error, MSG.Home, FsmStartHome, STATE.Homing);
  187. Transition(STATE.Idle, MSG.Home, FsmStartHome, STATE.Homing);
  188. Transition(STATE.Homing, FSM_MSG.TIMER, FsmMonitorHomeTask, STATE.Idle);
  189. Transition(STATE.Homing, MSG.Error, null, STATE.Init);
  190. Transition(STATE.Homing, MSG.Abort, FsmAbortTask, STATE.Init);
  191. EnterExitTransition<STATE, FSM_MSG>(STATE.Idle, FsmEnterIdle, FSM_MSG.NONE, FsmExitIdle);
  192. AnyStateTransition(MSG.ToInit, FsmToInit, STATE.Init);
  193. //robot home
  194. Transition(STATE.Idle, MSG.Map, FsmStartMap, STATE.Mapping);
  195. Transition(STATE.Mapping, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
  196. Transition(STATE.Mapping, MSG.Abort, FsmAbortTask, STATE.Idle);
  197. //robot pick
  198. Transition(STATE.Idle, MSG.Pick, FsmStartRobotPick, STATE.Picking);
  199. Transition(STATE.Picking, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
  200. Transition(STATE.Picking, MSG.Abort, FsmAbortTask, STATE.Idle);
  201. //robot pickAndplace
  202. Transition(STATE.Idle, MSG.PickAndPlace, FsmStartPickAndPlace, STATE.Swapping);
  203. Transition(STATE.Swapping, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
  204. Transition(STATE.Swapping, MSG.Abort, FsmAbortTask, STATE.Idle);
  205. //robot place
  206. Transition(STATE.Idle, MSG.Place, FsmStartRobotPlace, STATE.Placing);
  207. Transition(STATE.Placing, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
  208. Transition(STATE.Placing, MSG.Abort, FsmAbortTask, STATE.Idle);
  209. //robot goto
  210. Transition(STATE.Idle, MSG.Goto, FsmStartRobotGoto, STATE.RobotGoto);
  211. Transition(STATE.RobotGoto, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
  212. Transition(STATE.RobotGoto, MSG.Abort, FsmAbortTask, STATE.Idle);
  213. }
  214. private bool FsmToInit(object[] param)
  215. {
  216. return true;
  217. }
  218. private bool FsmResetConnect(object[] param)
  219. {
  220. //if (!EfemDevice.Connection.IsConnected)
  221. //{
  222. // EfemDevice.Connect();
  223. // return false;
  224. //}
  225. //if (!EfemDevice.EmoAlarm.IsAcknowledged)
  226. //{
  227. // EfemDevice.ClearError();
  228. // return false;
  229. //}
  230. return true;
  231. }
  232. private bool FsmOnConnected(object[] param)
  233. {
  234. //SignalTowerDevice.ResetData();
  235. //LP1Device.ResetData();
  236. //LP2Device.ResetData();
  237. //LP3Device.ResetData();
  238. //LP4Device.ResetData();
  239. //EfemDevice.ResetData();
  240. return true;
  241. }
  242. private bool FsmOnDisconnected(object[] param)
  243. {
  244. return true;
  245. }
  246. private bool FsmExitIdle(object[] param)
  247. {
  248. return true;
  249. }
  250. private bool FsmEnterIdle(object[] param)
  251. {
  252. return true;
  253. }
  254. private bool FsmExitError(object[] param)
  255. {
  256. return true;
  257. }
  258. private bool FsmEnterError(object[] param)
  259. {
  260. if (OnEnterError != null)
  261. OnEnterError(Module);
  262. return true;
  263. }
  264. private bool fWarning(object[] objs)
  265. {
  266. //IsWarning = false;
  267. return true;
  268. }
  269. private bool fAlarm(object[] objs)
  270. {
  271. if (FsmState == (int)STATE.Init)
  272. return false;
  273. return true;
  274. }
  275. private bool FsmStartHome(object[] param)
  276. {
  277. Result ret = StartRoutine(_homeRoutine);
  278. if (ret == Result.FAIL)
  279. {
  280. PostMsg(MSG.Error);
  281. return false;
  282. }
  283. if (ret == Result.DONE)
  284. return false;
  285. //_isInit = false;
  286. IsEfemRobotHomed = false;
  287. return ret == Result.RUN;
  288. }
  289. private bool FsmStartRobotPick(object[] param)
  290. {
  291. if (param.Length == 6)
  292. {
  293. _pickRoutine.Init(ModuleHelper.Converter((string)param[0]), (int)param[1], (Hand)param[2], (double)param[3], (double)param[4], (bool)param[5]);
  294. }
  295. else
  296. {
  297. _pickRoutine.Init(ModuleHelper.Converter((string)param[0]), (int)param[1], (double)param[2], (double)param[3], (bool)param[4]);
  298. }
  299. Result ret = StartRoutine(_pickRoutine);
  300. if (ret == Result.FAIL)
  301. {
  302. PostMsg(MSG.Error);
  303. return false;
  304. }
  305. if (ret == Result.DONE)
  306. return false;
  307. return ret == Result.RUN;
  308. }
  309. private bool FsmStartPickAndPlace(object[] param)
  310. {
  311. _pickAndPlaceRoutine.Init(ModuleHelper.Converter((string)param[0]), (Hand)param[1], (int)param[2], (Hand)param[3], (int)param[4]);
  312. Result ret = StartRoutine(_pickAndPlaceRoutine);
  313. if (ret == Result.FAIL)
  314. {
  315. PostMsg(MSG.Error);
  316. return false;
  317. }
  318. if (ret == Result.DONE)
  319. return false;
  320. return ret == Result.RUN;
  321. }
  322. private bool FsmStartRobotPlace(object[] param)
  323. {
  324. if (param.Length == 6)
  325. {
  326. _placeRoutine.Init(ModuleHelper.Converter((string)param[0]), (int)param[1], (Hand)param[2], (double)param[3], (double)param[4], (bool)param[5]);
  327. }
  328. else
  329. {
  330. _placeRoutine.Init(ModuleHelper.Converter((string)param[0]), (int)param[1], (double)param[2], (double)param[3], (bool)param[4]);
  331. }
  332. Result ret = StartRoutine(_placeRoutine);
  333. if (ret == Result.FAIL)
  334. {
  335. PostMsg(MSG.Error);
  336. return false;
  337. }
  338. if (ret == Result.DONE)
  339. return false;
  340. return ret == Result.RUN;
  341. }
  342. private bool FsmStartRobotGoto(object[] param)
  343. {
  344. _gotoRoutine.Init(ModuleHelper.Converter((string)param[0]), (int)param[1], (Hand)param[2]);
  345. Result ret = StartRoutine(_gotoRoutine);
  346. if (ret == Result.FAIL)
  347. {
  348. PostMsg(MSG.Error);
  349. return false;
  350. }
  351. if (ret == Result.DONE)
  352. return false;
  353. return ret == Result.RUN;
  354. }
  355. private bool FsmStartMap(object[] param)
  356. {
  357. _mapRoutine.Init(ModuleHelper.Converter((string)param[0]));
  358. Result ret = StartRoutine(_mapRoutine);
  359. if (ret == Result.FAIL)
  360. {
  361. PostMsg(MSG.Error);
  362. return false;
  363. }
  364. if (ret == Result.DONE)
  365. return false;
  366. return ret == Result.RUN;
  367. }
  368. private bool FsmAbortTask(object[] param)
  369. {
  370. AbortRoutine();
  371. if (!RobotDevice.IsIdle)
  372. {
  373. RobotDevice.PostMsg(RobotBaseDevice.RobotMsg.Stop);
  374. }
  375. return true;
  376. }
  377. private bool FsmMonitorHomeTask(object[] param)
  378. {
  379. Result ret = MonitorRoutine();
  380. if (ret == Result.FAIL)
  381. {
  382. PostMsg(MSG.Error);
  383. return false;
  384. }
  385. if (ret == Result.DONE)
  386. {
  387. OP.DoOperation($"{Module}.ResetTask");
  388. //_isInit = true;
  389. return true;
  390. }
  391. return false;
  392. }
  393. private bool FsmMonitorTask(object[] param)
  394. {
  395. Result ret = MonitorRoutine();
  396. if (ret == Result.FAIL)
  397. {
  398. PostMsg(MSG.Error);
  399. return false;
  400. }
  401. return ret == Result.DONE;
  402. }
  403. private bool FsmOnError(object[] param)
  404. {
  405. if (FsmState == (int)STATE.Error)
  406. {
  407. return false;
  408. }
  409. if (FsmState == (int)STATE.Picking)
  410. {
  411. _pickRoutine.Abort();
  412. }
  413. if (FsmState == (int)STATE.Placing)
  414. {
  415. _placeRoutine.Abort();
  416. }
  417. if (FsmState == (int)STATE.RobotGoto)
  418. {
  419. _gotoRoutine.Abort();
  420. }
  421. if (FsmState == (int)STATE.Init)
  422. return false;
  423. return true;
  424. }
  425. private bool FsmReset(object[] param)
  426. {
  427. //if (!EfemDevice.Connection.IsConnected)
  428. //{
  429. // EfemDevice.Connect();
  430. // return false;
  431. //}
  432. //if (!EfemDevice.EmoAlarm.IsAcknowledged)
  433. //{
  434. // EfemDevice.ClearError();
  435. // return false;
  436. //}
  437. //if (!_isInit)
  438. //{
  439. // PostMsg(MSG.ToInit);
  440. // return false;
  441. //}
  442. if (RobotDevice.IsError)
  443. {
  444. EV.PostWarningLog(Module, $"Robot in error, home to recover");
  445. RobotDevice.PostMsg(RobotBaseDevice.RobotMsg.Reset);
  446. //return false;
  447. }
  448. return true;
  449. }
  450. #region Service functions
  451. public override bool Home(out string reason)
  452. {
  453. if (!CheckToPostMessage((int)MSG.Home))
  454. {
  455. reason = $"Can not home in {StringFsmStatus} status";
  456. return false;
  457. }
  458. reason = string.Empty;
  459. return true;
  460. }
  461. public override void Reset()
  462. {
  463. if (IsError)
  464. {
  465. //if (RobotDevice.IsError)
  466. //{
  467. // RobotDevice.Reset();
  468. //}
  469. //else if (RobotDevice.RobotState != RobotStateEnum.Idle && RobotDevice.RobotState != RobotStateEnum.Init)
  470. //{
  471. // EV.PostWarningLog(Module, $"Robot in {RobotDevice.RobotState}, need home to reset error");
  472. // return;
  473. //}
  474. CheckToPostMessage((int)MSG.Reset);
  475. }
  476. }
  477. public override bool Pick(ModuleName target, Hand blade, int targetSlot, out string reason)
  478. {
  479. reason = string.Empty;
  480. return true;
  481. }
  482. public override bool Place(ModuleName target, Hand blade, int targetSlot, out string reason)
  483. {
  484. reason = string.Empty;
  485. return true;
  486. }
  487. public override bool Pick(ModuleName target, Hand blade, int targetSlot, double temp1, double temp2, bool EnableCheck, out string reason)
  488. {
  489. CheckToPostMessage((int)MSG.Pick, target.ToString(), targetSlot, blade, temp1, temp2, EnableCheck);
  490. reason = string.Empty;
  491. return true;
  492. }
  493. public override bool Place(ModuleName target, Hand blade, int targetSlot, double temp1, double temp2, bool EnableCheck, out string reason)
  494. {
  495. CheckToPostMessage((int)MSG.Place, target.ToString(), targetSlot, blade, temp1, temp2, EnableCheck);
  496. reason = string.Empty;
  497. return true;
  498. }
  499. public override bool PickAndPlace(ModuleName pickTarget, Hand pickHand, int pickSlot, ModuleName placeTarget, Hand placeHand,
  500. int placeSlot, out string reason)
  501. {
  502. CheckToPostMessage((int)MSG.PickAndPlace, pickTarget.ToString(), pickHand, pickSlot, placeHand, placeSlot);
  503. reason = string.Empty;
  504. return true;
  505. }
  506. public override bool Goto(ModuleName target, Hand blade, int targetSlot, out string reason)
  507. {
  508. CheckToPostMessage((int)MSG.Goto, target.ToString(), targetSlot, blade);
  509. reason = string.Empty;
  510. return true;
  511. }
  512. public override bool Map(ModuleName target, out string reason)
  513. {
  514. if (!CheckToPostMessage((int)MSG.Map, target.ToString()))
  515. {
  516. reason = $"Can not support map in {StringFsmStatus}";
  517. return false;
  518. }
  519. reason = string.Empty;
  520. return true;
  521. }
  522. private void EfemDevice_OnDeviceAlarmStateChanged(string module, AlarmEventItem alarmItem)
  523. {
  524. if (IsInit)
  525. return;
  526. if (!alarmItem.IsAcknowledged)
  527. {
  528. if (alarmItem.Level == EventLevel.Warning)
  529. {
  530. EV.PostWarningLog(Module, alarmItem.Description);
  531. }
  532. else
  533. {
  534. EV.PostAlarmLog(Module, alarmItem.Description);
  535. }
  536. //if (alarmItem.EventEnum == EfemDevice.IsMaintain.EventEnum
  537. //|| alarmItem.EventEnum == EfemDevice.EmoAlarm.EventEnum
  538. //|| alarmItem.EventEnum == EfemDevice.DoorOpen.EventEnum)
  539. //{
  540. // _isInit = false;
  541. // PostMsg(MSG.Error);
  542. //}
  543. //else if (alarmItem.Level == EventLevel.Alarm)
  544. //{
  545. // PostMsg(MSG.Error);
  546. //}
  547. }
  548. else
  549. {
  550. if (IsError)
  551. CheckToPostMessage((int)MSG.Reset);
  552. }
  553. }
  554. private void Connection_OnError(string obj)
  555. {
  556. PostMsg(MSG.Disconnected);
  557. }
  558. private void Connection_OnDisconnected()
  559. {
  560. PostMsg(MSG.Disconnected);
  561. }
  562. private void Connection_OnConnected()
  563. {
  564. PostMsg(MSG.Connected);
  565. }
  566. #endregion
  567. }
  568. }