SchedulerSETMRobot.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using Aitex.Core.RT.Fsm;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using Aitex.Sorter.Common;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.Schedulers;
  8. using MECF.Framework.Common.SubstrateTrackings;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Interop;
  15. using Venus_Core;
  16. using Venus_RT.Modules.TM.VenusEntity;
  17. using Venus_RT.Scheduler;
  18. namespace Venus_RT.Modules.Schedulers
  19. {
  20. public class SchedulerSETMRobot : SchedulerModule
  21. {
  22. class SchedulerItem
  23. {
  24. public SETMEntity.MSG MoveType { get; set; }
  25. public ModuleName target { get; set; }
  26. public Queue<MoveItem> moveList { get; set; }
  27. }
  28. public override bool IsAvailable
  29. {
  30. get { return _entity.IsIdle && (_entity.IsOnline || !Singleton<RouteManager>.Instance.IsAutoMode) && RunSchedulers(); }
  31. }
  32. public override bool IsOnline
  33. {
  34. get { return _entity.IsOnline; }
  35. }
  36. public override bool IsError
  37. {
  38. get { return _entity.IsError; }
  39. }
  40. public RState RobotStatus
  41. {
  42. get { return _entity.RobotStatus; }
  43. }
  44. //public bool IsVCESlitDoorClosed => _entity.IsVCESlitDoorClosed(Module);
  45. private SETMEntity _entity;
  46. public int _entityTaskToken = (int)FSM_MSG.NONE;
  47. private Queue<SchedulerItem> _schedulerList = new Queue<SchedulerItem>();
  48. private SchedulerItem _currentScheduler = null;
  49. private int _singleArmOption = 0;
  50. public SchedulerSETMRobot(ModuleName module) : base(module.ToString())
  51. {
  52. _entity = Singleton<RouteManager>.Instance.seTM;
  53. _singleArmOption = SC.GetValue<int>($"{module}.SingleArmOption");
  54. }
  55. public bool PostMoveItems(MoveItem[] items)
  56. {
  57. foreach (var item in items)
  58. {
  59. LOG.Write(eEvent.EV_ROUTER, ModuleName.TMRobot, $"Post Moving Item: {item.SourceModule} Slot {item.SourceSlot + 1} => {item.DestinationModule} Slot {item.DestinationSlot + 1}");
  60. }
  61. //pm swap
  62. void PackPMSwapCmds(MoveItem[] swapItems)
  63. {
  64. // must swap the item order for PM swap
  65. if (!ModuleHelper.IsTMRobot(swapItems[1].SourceModule))
  66. {
  67. SchedulerItem item = new SchedulerItem();
  68. item.MoveType = SETMEntity.MSG.Pick;
  69. item.target = swapItems[1].SourceModule;
  70. item.moveList = new Queue<MoveItem>();
  71. item.moveList.Enqueue(new MoveItem(swapItems[1].SourceModule, swapItems[1].SourceSlot, ModuleName.TMRobot, 0, (Hand)0));
  72. _schedulerList.Enqueue(item);
  73. }
  74. SchedulerItem swap = new SchedulerItem();
  75. swap.MoveType = SETMEntity.MSG.PMSwap;
  76. swap.target = swapItems[0].SourceModule;
  77. swap.moveList = new Queue<MoveItem>();
  78. swap.moveList.Enqueue(new MoveItem(swapItems[0].SourceModule, swapItems[0].SourceSlot, ModuleName.TMRobot, 1, (Hand)1));
  79. swap.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 0, swapItems[1].DestinationModule, swapItems[1].DestinationSlot, (Hand)0));
  80. _schedulerList.Enqueue(swap);
  81. if (!ModuleHelper.IsTMRobot(swapItems[0].DestinationModule))
  82. {
  83. SchedulerItem last = new SchedulerItem();
  84. last.MoveType = SETMEntity.MSG.Place;
  85. last.target = swapItems[0].DestinationModule;
  86. last.moveList = new Queue<MoveItem>();
  87. last.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 1, swapItems[0].DestinationModule, swapItems[0].DestinationSlot, (Hand)1));
  88. _schedulerList.Enqueue(last);
  89. }
  90. }
  91. void PackSwapCmds(MoveItem[] swapItems, SETMEntity.MSG swapType, int swapIndex)
  92. {
  93. SchedulerItem swap = new SchedulerItem();
  94. swap.MoveType = swapType;
  95. swap.target = swapItems[0].DestinationModule;
  96. swap.moveList = new Queue<MoveItem>();
  97. for (int i = 0; i < swapIndex; i++)
  98. {
  99. if (!ModuleHelper.IsTMRobot(swapItems[i].SourceModule))
  100. {
  101. SchedulerItem item = new SchedulerItem();
  102. item.MoveType = !ModuleHelper.IsPm(swapItems[i].SourceModule) ? SETMEntity.MSG.Pick : SETMEntity.MSG.PMPick;
  103. item.target = swapItems[i].SourceModule;
  104. item.moveList = new Queue<MoveItem>();
  105. item.moveList.Enqueue(new MoveItem(swapItems[i].SourceModule, swapItems[i].SourceSlot, ModuleName.TMRobot, i, (Hand)i));
  106. _schedulerList.Enqueue(item);
  107. swap.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, i, swapItems[i].DestinationModule, swapItems[i].DestinationSlot, (Hand)i));
  108. }
  109. }
  110. for (int j = swapIndex; j < swapItems.Length; j++)
  111. {
  112. swap.moveList.Enqueue(new MoveItem(swapItems[j].SourceModule, swapItems[j].SourceSlot, ModuleName.TMRobot, j - swapIndex, (Hand)(j - swapIndex)));
  113. }
  114. _schedulerList.Enqueue(swap);
  115. for (int j = swapIndex; j < swapItems.Length; j++)
  116. {
  117. if (!ModuleHelper.IsTMRobot(swapItems[j].DestinationModule))
  118. {
  119. SchedulerItem item = new SchedulerItem();
  120. item.MoveType = !ModuleHelper.IsPm(swapItems[j].DestinationModule) ? SETMEntity.MSG.Place : SETMEntity.MSG.PMPlace;
  121. item.target = swapItems[j].DestinationModule;
  122. item.moveList = new Queue<MoveItem>();
  123. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, j - swapIndex, swapItems[j].DestinationModule, swapItems[j].DestinationSlot, (Hand)(j - swapIndex)));
  124. _schedulerList.Enqueue(item);
  125. }
  126. }
  127. }
  128. //双臂模式 检查是否双臂有wafer
  129. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0) && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1) && _singleArmOption == 0)
  130. {
  131. if (items.Length == 4)
  132. {
  133. //是回到vce 且下一个也是 再下两个要出来 可以做swap
  134. if (ModuleHelper.IsVCE(items[0].DestinationModule) &&
  135. items[0].DestinationModule == items[1].DestinationModule &&
  136. items[0].DestinationModule == items[2].SourceModule &&
  137. items[0].DestinationModule == items[3].SourceModule)
  138. {
  139. PackSwapCmds(items, SETMEntity.MSG.Swap, 2);
  140. }
  141. else if (items.Length == 3)
  142. {
  143. if (ModuleHelper.IsVCE(items[0].DestinationModule) &&
  144. items[0].DestinationModule == items[1].DestinationModule &&
  145. items[0].DestinationModule == items[2].SourceModule)
  146. {
  147. PackSwapCmds(items, SETMEntity.MSG.Swap, 2);
  148. }
  149. else if (ModuleHelper.IsVCE(items[0].DestinationModule) &&
  150. items[0].DestinationModule == items[1].SourceModule &&
  151. items[0].DestinationModule == items[2].SourceModule)
  152. {
  153. PackSwapCmds(items, SETMEntity.MSG.Swap, 1);
  154. }
  155. else if (ModuleHelper.IsVCE(items[0].SourceModule) &&
  156. items[0].SourceModule == items[1].SourceModule &&
  157. ModuleHelper.IsPm(items[1].DestinationModule) &&
  158. items[1].DestinationModule == items[2].SourceModule)
  159. {
  160. SchedulerItem llDoublePick = new SchedulerItem();
  161. llDoublePick.MoveType = SETMEntity.MSG.Swap;
  162. llDoublePick.target = items[0].SourceModule;
  163. llDoublePick.moveList = new Queue<MoveItem>();
  164. llDoublePick.moveList.Enqueue(new MoveItem(items[0].SourceModule, items[0].SourceSlot, ModuleName.TMRobot, 0, 0));
  165. llDoublePick.moveList.Enqueue(new MoveItem(items[1].SourceModule, items[1].SourceSlot, ModuleName.TMRobot, 1, (Hand)1));
  166. _schedulerList.Enqueue(llDoublePick);
  167. SchedulerItem pm1_place = new SchedulerItem();
  168. pm1_place.MoveType = SETMEntity.MSG.PMPlace;
  169. pm1_place.target = items[0].DestinationModule;
  170. pm1_place.moveList = new Queue<MoveItem>();
  171. pm1_place.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 0, items[0].DestinationModule, items[0].DestinationSlot, 0));
  172. _schedulerList.Enqueue(pm1_place);
  173. SchedulerItem pm2_swap = new SchedulerItem();
  174. pm2_swap.MoveType = SETMEntity.MSG.PMSwap;
  175. pm2_swap.target = items[2].SourceModule;
  176. pm2_swap.moveList = new Queue<MoveItem>();
  177. pm2_swap.moveList.Enqueue(new MoveItem(items[2].SourceModule, items[2].SourceSlot, ModuleName.TMRobot, 0, 0));
  178. pm2_swap.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 1, items[1].DestinationModule, items[1].DestinationSlot, (Hand)1));
  179. _schedulerList.Enqueue(pm2_swap);
  180. if (ModuleHelper.IsVCE(items[2].DestinationModule))
  181. {
  182. SchedulerItem llPlace = new SchedulerItem();
  183. llPlace.MoveType = SETMEntity.MSG.Place;
  184. llPlace.target = items[2].DestinationModule;
  185. llPlace.moveList = new Queue<MoveItem>();
  186. llPlace.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 0, items[2].DestinationModule, items[2].DestinationSlot, 0));
  187. _schedulerList.Enqueue(llPlace);
  188. }
  189. }
  190. }
  191. }
  192. else if (items.Length == 2)
  193. {
  194. if (ModuleHelper.IsTMRobot(items[0].DestinationModule)
  195. && ModuleHelper.IsTMRobot(items[1].DestinationModule)
  196. &&ModuleHelper.IsVCE(items[0].SourceModule)
  197. &&ModuleHelper.IsVCE(items[1].SourceModule)
  198. &&WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot,0) && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1))
  199. {
  200. SchedulerItem llDoublePick = new SchedulerItem();
  201. llDoublePick.MoveType = SETMEntity.MSG.Swap;
  202. llDoublePick.target = items[0].SourceModule;
  203. llDoublePick.moveList = new Queue<MoveItem>();
  204. llDoublePick.moveList.Enqueue(new MoveItem(items[0].SourceModule, items[0].SourceSlot, ModuleName.TMRobot, 0, 0));
  205. llDoublePick.moveList.Enqueue(new MoveItem(items[1].SourceModule, items[1].SourceSlot, ModuleName.TMRobot, 1, (Hand)1));
  206. _schedulerList.Enqueue(llDoublePick);
  207. }
  208. else if (ModuleHelper.IsVCE(items[0].DestinationModule) &&
  209. items[0].DestinationModule == items[1].DestinationModule)
  210. {
  211. PackSwapCmds(items, SETMEntity.MSG.Swap, 2);
  212. }
  213. else if (ModuleHelper.IsVCE(items[0].SourceModule) &&
  214. items[0].SourceModule == items[1].SourceModule)
  215. {
  216. PackSwapCmds(items, SETMEntity.MSG.Swap, 0);
  217. }
  218. else if (ModuleHelper.IsPm(items[0].SourceModule) &&
  219. items[0].SourceModule == items[1].DestinationModule)
  220. {
  221. PackPMSwapCmds(items);
  222. }
  223. }
  224. }
  225. Hand freeHand = SelectFreeHand();
  226. //如果没有空的手臂 直接报错
  227. if (freeHand == Hand.None && !ModuleHelper.IsTMRobot(items[0].SourceModule))
  228. {
  229. LOG.Write(eEvent.WARN_ROUTER, ModuleName.TMRobot, "No Free Arm to transfer wafer");
  230. return false;
  231. }
  232. if (_schedulerList.Count == 0)
  233. {
  234. if (items.Length >= 2 && items[0].SourceModule == items[1].DestinationModule && ModuleHelper.IsPm(items[0].SourceModule))
  235. {
  236. SchedulerItem item = new SchedulerItem();
  237. item.MoveType = SETMEntity.MSG.PMSwap;
  238. item.target = items[0].SourceModule;
  239. item.moveList = new Queue<MoveItem>();
  240. item.moveList.Enqueue(new MoveItem(items[0].SourceModule, items[0].SourceSlot, items[1].DestinationModule, items[1].DestinationSlot, (Hand)(items[1].SourceSlot == 1 ? 0 : 1)));
  241. _schedulerList.Enqueue(item);
  242. item = new SchedulerItem();
  243. item.MoveType = SETMEntity.MSG.Place;
  244. item.target = items[0].SourceModule;
  245. item.moveList = new Queue<MoveItem>();
  246. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, items[1].SourceSlot, items[0].DestinationModule, items[0].DestinationSlot, (Hand)(items[1].SourceSlot == 1 ? 0 : 1)));
  247. _schedulerList.Enqueue(item);
  248. items = items.Skip(2).ToArray();
  249. }
  250. if (items.Length == 3
  251. && ModuleHelper.IsTMRobot(items[1].DestinationModule)
  252. && ModuleHelper.IsTMRobot(items[2].DestinationModule)
  253. && items[1].DestinationSlot == items[2].DestinationSlot
  254. )
  255. {
  256. items[1].DestinationSlot = 0;
  257. items[2].DestinationSlot = 1;
  258. }
  259. foreach (var moveitem in items)
  260. {
  261. //如果不是从tm上来去 就要补上tmrobot中间过程
  262. if (!ModuleHelper.IsTMRobot(moveitem.SourceModule) && !ModuleHelper.IsTMRobot(moveitem.DestinationModule))
  263. {
  264. //如何取
  265. SchedulerItem item = new SchedulerItem();
  266. item.MoveType = !ModuleHelper.IsPm(moveitem.SourceModule) ? SETMEntity.MSG.Pick : SETMEntity.MSG.PMPick;
  267. item.target = moveitem.SourceModule;
  268. item.moveList = new Queue<MoveItem>();
  269. item.moveList.Enqueue(new MoveItem(moveitem.SourceModule, moveitem.SourceSlot, ModuleName.TMRobot, (int)freeHand, freeHand));
  270. _schedulerList.Enqueue(item);
  271. //如何放
  272. item = new SchedulerItem();
  273. item.MoveType = !ModuleHelper.IsPm(moveitem.DestinationModule) ? SETMEntity.MSG.Place : SETMEntity.MSG.PMPlace;
  274. item.target = moveitem.DestinationModule;
  275. item.moveList = new Queue<MoveItem>();
  276. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, (int)freeHand, moveitem.DestinationModule, moveitem.DestinationSlot, freeHand));
  277. _schedulerList.Enqueue(item);
  278. }
  279. //如果是place
  280. else if (ModuleHelper.IsTMRobot(moveitem.SourceModule))
  281. {
  282. SchedulerItem item = new SchedulerItem();
  283. item.MoveType = !ModuleHelper.IsPm(moveitem.DestinationModule) ? SETMEntity.MSG.Place : SETMEntity.MSG.PMPlace;
  284. item.target = moveitem.DestinationModule;
  285. item.moveList = new Queue<MoveItem>();
  286. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, moveitem.SourceSlot, moveitem.DestinationModule, moveitem.DestinationSlot, (Hand)moveitem.SourceSlot));
  287. _schedulerList.Enqueue(item);
  288. }
  289. //如果是pick
  290. else if (ModuleHelper.IsTMRobot(moveitem.DestinationModule))
  291. {
  292. SchedulerItem item = new SchedulerItem();
  293. item.MoveType = !ModuleHelper.IsPm(moveitem.SourceModule) ? SETMEntity.MSG.Pick : SETMEntity.MSG.PMPick;
  294. item.target = moveitem.SourceModule;
  295. item.moveList = new Queue<MoveItem>();
  296. item.moveList.Enqueue(new MoveItem(moveitem.SourceModule, moveitem.SourceSlot, ModuleName.TMRobot, moveitem.DestinationSlot, (Hand)moveitem.DestinationSlot));
  297. _schedulerList.Enqueue(item);
  298. }
  299. }
  300. }
  301. RunSchedulers();
  302. return true;
  303. }
  304. bool RunSchedulers()
  305. {
  306. //非运行状态
  307. if (_entity.IsIdle && CheckTaskDone())
  308. {
  309. if (_schedulerList.Count == 0)
  310. return true;
  311. _currentScheduler = _schedulerList.Dequeue();
  312. Queue<MoveItem> moveItems = new Queue<MoveItem>();
  313. foreach (var item in _currentScheduler.moveList)
  314. {
  315. moveItems.Enqueue(item);
  316. LOG.Write(eEvent.INFO_TM, ModuleName.TMRobot, $"TM Moving Items: {item.SourceModule} Slot {item.SourceSlot + 1} => {item.DestinationModule} Slot {item.DestinationSlot + 1}");
  317. }
  318. if (_entity.CheckToPostMessage((int)_currentScheduler.MoveType, moveItems))
  319. {
  320. _entityTaskToken = (int)_currentScheduler.MoveType;
  321. }
  322. else
  323. _entityTaskToken = (int)FSM_MSG.NONE;
  324. }
  325. return false;
  326. }
  327. //获得没有wafer的手臂
  328. private Hand SelectFreeHand()
  329. {
  330. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0) && _singleArmOption != 2)
  331. return Hand.Blade1;
  332. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1) && _singleArmOption != 1)
  333. return Hand.Blade2;
  334. return Hand.None;
  335. }
  336. private bool CheckTaskDone()
  337. {
  338. bool ret = false;
  339. switch (_entityTaskToken)
  340. {
  341. case (int)SETMEntity.MSG.Pick:
  342. case (int)SETMEntity.MSG.PMPick:
  343. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  344. WaferManager.Instance.CheckNoWafer(_currentScheduler.moveList.Peek().SourceModule, _currentScheduler.moveList.Peek().SourceSlot);
  345. break;
  346. case (int)SETMEntity.MSG.Place:
  347. case (int)SETMEntity.MSG.PMPlace:
  348. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  349. WaferManager.Instance.CheckNoWafer(_currentScheduler.moveList.Peek().SourceModule, _currentScheduler.moveList.Peek().SourceSlot);
  350. break;
  351. case (int)SETMEntity.MSG.Swap:
  352. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  353. WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Last().DestinationModule, _currentScheduler.moveList.Last().DestinationSlot);
  354. break;
  355. case (int)SETMEntity.MSG.PMSwap:
  356. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  357. WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().SourceModule, _currentScheduler.moveList.Peek().SourceSlot);
  358. break;
  359. case (int)SETMEntity.MSG.Align:
  360. ret = _entity.CheckAcked(_entityTaskToken) && _entity.IsIdle ;
  361. break;
  362. case (int)FSM_MSG.NONE:
  363. ret = true;
  364. break;
  365. }
  366. if (ret && _entityTaskToken != (int)FSM_MSG.NONE)
  367. {
  368. _entityTaskToken = (int)FSM_MSG.NONE;
  369. LOG.Write(eEvent.EV_ROUTER, Module, $"Scheduler, {_currentScheduler.target} Task done: {_currentScheduler.MoveType}");
  370. }
  371. return ret;
  372. }
  373. public override bool Align(float angle)
  374. {
  375. _task = TaskType.Align;
  376. if (_entity.CheckToPostMessage((int)SETMEntity.MSG.Align, angle))
  377. {
  378. _entityTaskToken = (int)SETMEntity.MSG.Align;
  379. return true;
  380. }
  381. else
  382. {
  383. _entityTaskToken = (int)FSM_MSG.NONE;
  384. return false;
  385. }
  386. }
  387. public void Abort()
  388. {
  389. _schedulerList.Clear();
  390. }
  391. }
  392. }