SchedulerTMRobot.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System.Diagnostics;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aitex.Core.RT.Fsm;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Equipment;
  12. using MECF.Framework.Common.Schedulers;
  13. using MECF.Framework.Common.SubstrateTrackings;
  14. using Venus_RT.Scheduler;
  15. namespace Venus_RT.Modules.Schedulers
  16. {
  17. class SchedulerItem
  18. {
  19. public TMEntity.MSG MoveType { get; set; }
  20. public ModuleName target { get; set; }
  21. public Queue<MoveItem> moveList { get; set; }
  22. }
  23. public class SchedulerTMRobot : SchedulerModule
  24. {
  25. public override bool IsAvailable
  26. {
  27. get { return _entity.IsIdle && /*_entity.IsOnline && */RunSchedulers(); }
  28. }
  29. public override bool IsOnline
  30. {
  31. get { return _entity.IsOnline; }
  32. }
  33. public override bool IsError
  34. {
  35. get { return _entity.IsError; }
  36. }
  37. private TMEntity _entity = null;
  38. public int _entityTaskToken = (int)FSM_MSG.NONE;
  39. private Queue<SchedulerItem> _schedulerList = new Queue<SchedulerItem>();
  40. private SchedulerItem _currentScheduler = null;
  41. public SchedulerTMRobot() : base(ModuleName.TM.ToString())
  42. {
  43. _entity = Singleton<RouteManager>.Instance.TM;
  44. }
  45. private bool CheckTaskDone()
  46. {
  47. bool ret = false;
  48. switch (_entityTaskToken)
  49. {
  50. case (int)TMEntity.MSG.Pick:
  51. case (int)TMEntity.MSG.PMPick:
  52. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  53. WaferManager.Instance.CheckNoWafer(_currentScheduler.moveList.Peek().SourceModule, _currentScheduler.moveList.Peek().SourceSlot);
  54. break;
  55. case (int)TMEntity.MSG.Place:
  56. case (int)TMEntity.MSG.PMPlace:
  57. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  58. WaferManager.Instance.CheckNoWafer(_currentScheduler.moveList.Peek().SourceModule, _currentScheduler.moveList.Peek().SourceSlot);
  59. break;
  60. case (int)TMEntity.MSG.Swap:
  61. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  62. WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Last().DestinationModule, _currentScheduler.moveList.Last().DestinationSlot);
  63. break;
  64. case (int)TMEntity.MSG.PMSwap:
  65. ret = WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().DestinationModule, _currentScheduler.moveList.Peek().DestinationSlot) &&
  66. WaferManager.Instance.CheckHasWafer(_currentScheduler.moveList.Peek().SourceModule, _currentScheduler.moveList.Peek().SourceSlot);
  67. break;
  68. case (int)FSM_MSG.NONE:
  69. ret = true;
  70. break;
  71. }
  72. if(ret && _entityTaskToken != (int)FSM_MSG.NONE)
  73. {
  74. _entityTaskToken = (int)FSM_MSG.NONE;
  75. LOG.Write(eEvent.EV_ROUTER, ModuleName.TM, $"Scheduler, { _currentScheduler.target} Task done: { _currentScheduler.MoveType}");
  76. }
  77. return ret;
  78. }
  79. public bool PostMoveItems(MoveItem[] items)
  80. {
  81. foreach(var item in items)
  82. {
  83. LOG.Write(eEvent.EV_ROUTER, ModuleName.TMRobot, $"Post Moving Item: {item.SourceModule} Slot {item.SourceSlot + 1} => {item.DestinationModule} Slot {item.DestinationSlot + 1}");
  84. }
  85. void PackPMSwapCmds(MoveItem[] swapItems)
  86. {
  87. // must swap the item order for PM swap
  88. if (!ModuleHelper.IsTMRobot(swapItems[1].SourceModule))
  89. {
  90. SchedulerItem item = new SchedulerItem();
  91. item.MoveType = TMEntity.MSG.Pick;
  92. item.target = swapItems[1].SourceModule;
  93. item.moveList = new Queue<MoveItem>();
  94. item.moveList.Enqueue(new MoveItem(swapItems[1].SourceModule, swapItems[1].SourceSlot, ModuleName.TMRobot, 0, (Hand)0));
  95. _schedulerList.Enqueue(item);
  96. }
  97. SchedulerItem swap = new SchedulerItem();
  98. swap.MoveType = TMEntity.MSG.PMSwap;
  99. swap.target = swapItems[0].SourceModule;
  100. swap.moveList = new Queue<MoveItem>();
  101. swap.moveList.Enqueue(new MoveItem(swapItems[0].SourceModule, swapItems[0].SourceSlot, ModuleName.TMRobot, 1, (Hand)1));
  102. swap.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 0, swapItems[1].DestinationModule, swapItems[1].DestinationSlot, (Hand)0));
  103. _schedulerList.Enqueue(swap);
  104. if (!ModuleHelper.IsTMRobot(swapItems[0].DestinationModule))
  105. {
  106. SchedulerItem last = new SchedulerItem();
  107. last.MoveType = TMEntity.MSG.Place;
  108. last.target = swapItems[0].DestinationModule;
  109. last.moveList = new Queue<MoveItem>();
  110. last.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, 1, swapItems[0].DestinationModule, swapItems[0].DestinationSlot, (Hand)1));
  111. _schedulerList.Enqueue(last);
  112. }
  113. }
  114. void PackSwapCmds(MoveItem[] swapItems, TMEntity.MSG swapType, int swapIndex)
  115. {
  116. SchedulerItem swap = new SchedulerItem();
  117. swap.MoveType = swapType;
  118. swap.target = swapItems[0].DestinationModule;
  119. swap.moveList = new Queue<MoveItem>();
  120. for (int i = 0; i < swapIndex; i++)
  121. {
  122. if(!ModuleHelper.IsTMRobot(swapItems[i].SourceModule))
  123. {
  124. SchedulerItem item = new SchedulerItem();
  125. item.MoveType = ModuleHelper.IsLoadLock(swapItems[i].SourceModule) ? TMEntity.MSG.Pick : TMEntity.MSG.PMPick;
  126. item.target = swapItems[i].SourceModule;
  127. item.moveList = new Queue<MoveItem>();
  128. item.moveList.Enqueue(new MoveItem(swapItems[i].SourceModule, swapItems[i].SourceSlot, ModuleName.TMRobot, i, (Hand)i));
  129. _schedulerList.Enqueue(item);
  130. swap.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, i, swapItems[i].DestinationModule, swapItems[i].DestinationSlot, (Hand)i));
  131. }
  132. }
  133. for (int j = swapIndex; j < swapItems.Length; j++)
  134. {
  135. swap.moveList.Enqueue(new MoveItem(swapItems[j].SourceModule, swapItems[j].SourceSlot, ModuleName.TMRobot, j - swapIndex, (Hand)(j - swapIndex)));
  136. }
  137. _schedulerList.Enqueue(swap);
  138. for (int j = swapIndex; j < swapItems.Length; j++)
  139. {
  140. if (!ModuleHelper.IsTMRobot(swapItems[j].DestinationModule))
  141. {
  142. SchedulerItem item = new SchedulerItem();
  143. item.MoveType = ModuleHelper.IsLoadLock(swapItems[j].DestinationModule) ? TMEntity.MSG.Place : TMEntity.MSG.PMPlace;
  144. item.target = swapItems[j].DestinationModule;
  145. item.moveList = new Queue<MoveItem>();
  146. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, j - swapIndex, swapItems[j].DestinationModule, swapItems[j].DestinationSlot, (Hand)(j - swapIndex)));
  147. _schedulerList.Enqueue(item);
  148. }
  149. }
  150. }
  151. if(WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0) && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1))
  152. {
  153. if (items.Length == 4)
  154. {
  155. if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  156. items[0].DestinationModule == items[1].DestinationModule &&
  157. items[0].DestinationModule == items[2].SourceModule &&
  158. items[0].DestinationModule == items[3].SourceModule)
  159. {
  160. PackSwapCmds(items, TMEntity.MSG.Swap, 2);
  161. }
  162. }
  163. else if (items.Length == 3)
  164. {
  165. if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  166. items[0].DestinationModule == items[1].DestinationModule &&
  167. items[0].DestinationModule == items[2].SourceModule)
  168. {
  169. PackSwapCmds(items, TMEntity.MSG.Swap, 2);
  170. }
  171. else if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  172. items[0].DestinationModule == items[1].SourceModule &&
  173. items[0].DestinationModule == items[2].SourceModule)
  174. {
  175. PackSwapCmds(items, TMEntity.MSG.Swap, 1);
  176. }
  177. }
  178. else if (items.Length == 2)
  179. {
  180. if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  181. items[0].DestinationModule == items[1].DestinationModule)
  182. {
  183. PackSwapCmds(items, TMEntity.MSG.Swap, 2);
  184. }
  185. else if (ModuleHelper.IsLoadLock(items[0].SourceModule) &&
  186. items[0].SourceModule == items[1].SourceModule)
  187. {
  188. PackSwapCmds(items, TMEntity.MSG.Swap, 0);
  189. }
  190. else if (ModuleHelper.IsPm(items[0].SourceModule) &&
  191. items[0].SourceModule == items[1].DestinationModule)
  192. {
  193. PackPMSwapCmds(items);
  194. }
  195. }
  196. }
  197. Hand freeHand = SelectFreeHand();
  198. if(freeHand == Hand.None && !ModuleHelper.IsTMRobot(items[0].SourceModule))
  199. {
  200. LOG.Write(eEvent.WARN_ROUTER, ModuleName.TMRobot, "No Free Arm to transfer wafer");
  201. return false;
  202. }
  203. if(_schedulerList.Count == 0)
  204. {
  205. foreach(var moveItem in items)
  206. {
  207. if(!ModuleHelper.IsTMRobot(moveItem.SourceModule) && !ModuleHelper.IsTMRobot(moveItem.DestinationModule))
  208. {
  209. SchedulerItem item = new SchedulerItem();
  210. item.MoveType = ModuleHelper.IsLoadLock(moveItem.SourceModule) ? TMEntity.MSG.Pick : TMEntity.MSG.PMPick;
  211. item.target = moveItem.SourceModule;
  212. item.moveList = new Queue<MoveItem>();
  213. item.moveList.Enqueue(new MoveItem(moveItem.SourceModule, moveItem.SourceSlot, ModuleName.TMRobot, (int)freeHand, freeHand));
  214. _schedulerList.Enqueue(item);
  215. item = new SchedulerItem();
  216. item.MoveType = ModuleHelper.IsLoadLock(moveItem.DestinationModule) ? TMEntity.MSG.Place : TMEntity.MSG.PMPlace;
  217. item.target = moveItem.DestinationModule;
  218. item.moveList = new Queue<MoveItem>();
  219. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, (int)freeHand, moveItem.DestinationModule, moveItem.DestinationSlot, freeHand));
  220. _schedulerList.Enqueue(item);
  221. }
  222. else if(ModuleHelper.IsTMRobot(moveItem.SourceModule))
  223. {
  224. SchedulerItem item = new SchedulerItem();
  225. item.MoveType = ModuleHelper.IsLoadLock(moveItem.DestinationModule) ? TMEntity.MSG.Place : TMEntity.MSG.PMPlace;
  226. item.target = moveItem.DestinationModule;
  227. item.moveList = new Queue<MoveItem>();
  228. item.moveList.Enqueue(new MoveItem(ModuleName.TMRobot, moveItem.SourceSlot, moveItem.DestinationModule, moveItem.DestinationSlot, (Hand)moveItem.SourceSlot));
  229. _schedulerList.Enqueue(item);
  230. }
  231. else if(ModuleHelper.IsTMRobot(moveItem.DestinationModule))
  232. {
  233. SchedulerItem item = new SchedulerItem();
  234. item.MoveType = ModuleHelper.IsLoadLock(moveItem.SourceModule) ? TMEntity.MSG.Pick : TMEntity.MSG.PMPick;
  235. item.target = moveItem.SourceModule;
  236. item.moveList = new Queue<MoveItem>();
  237. item.moveList.Enqueue(new MoveItem(moveItem.SourceModule, moveItem.SourceSlot, ModuleName.TMRobot, moveItem.DestinationSlot, (Hand)moveItem.DestinationSlot));
  238. _schedulerList.Enqueue(item);
  239. }
  240. }
  241. }
  242. RunSchedulers();
  243. return true;
  244. }
  245. Hand SelectFreeHand()
  246. {
  247. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0))
  248. return Hand.Blade1;
  249. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1))
  250. return Hand.Blade2;
  251. return Hand.None;
  252. }
  253. bool RunSchedulers()
  254. {
  255. if(_entity.IsIdle && CheckTaskDone())
  256. {
  257. if (_schedulerList.Count == 0)
  258. return true;
  259. _currentScheduler = _schedulerList.Dequeue();
  260. Queue<MoveItem> moveItems = new Queue<MoveItem>();
  261. foreach(var item in _currentScheduler.moveList)
  262. {
  263. moveItems.Enqueue(item);
  264. LOG.Write(eEvent.INFO_TM, ModuleName.TMRobot, $"TM Moving Items: {item.SourceModule} Slot {item.SourceSlot + 1} => {item.DestinationModule} Slot {item.DestinationSlot + 1}");
  265. }
  266. if(_entity.CheckToPostMessage((int)_currentScheduler.MoveType, moveItems))
  267. {
  268. _entityTaskToken = (int)_currentScheduler.MoveType;
  269. }
  270. else
  271. _entityTaskToken = (int)FSM_MSG.NONE;
  272. }
  273. return false;
  274. }
  275. }
  276. }