SchedulerTMRobot.cs 15 KB

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