SchedulerTMRobot.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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. private 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. void PackSwapCmds(MoveItem[] swapItems, TMEntity.MSG swapType, int swapIndex)
  82. {
  83. SchedulerItem swap = new SchedulerItem();
  84. swap.MoveType = swapType;
  85. swap.target = swapItems[0].DestinationModule;
  86. swap.moveList = new Queue<MoveItem>();
  87. for (int i = 0; i < swapIndex; i++)
  88. {
  89. if(!ModuleHelper.IsTM(swapItems[i].SourceModule))
  90. {
  91. SchedulerItem item = new SchedulerItem();
  92. item.MoveType = ModuleHelper.IsLoadLock(swapItems[i].SourceModule) ? TMEntity.MSG.Pick : TMEntity.MSG.PMPick;
  93. item.target = swapItems[i].SourceModule;
  94. item.moveList = new Queue<MoveItem>();
  95. item.moveList.Enqueue(new MoveItem(swapItems[i].SourceModule, swapItems[i].SourceSlot, ModuleName.TM, i, (Hand)i));
  96. _schedulerList.Enqueue(item);
  97. swap.moveList.Enqueue(new MoveItem(ModuleName.TM, i, swapItems[i].DestinationModule, swapItems[i].DestinationSlot, (Hand)i));
  98. }
  99. }
  100. for (int j = swapIndex; j < swapItems.Length; j++)
  101. {
  102. swap.moveList.Enqueue(new MoveItem(swapItems[j].SourceModule, swapItems[j].SourceSlot, ModuleName.TM, j - swapIndex, (Hand)(j - swapIndex)));
  103. }
  104. _schedulerList.Enqueue(swap);
  105. for (int j = swapIndex; j < swapItems.Length; j++)
  106. {
  107. if (!ModuleHelper.IsTM(swapItems[j].DestinationModule))
  108. {
  109. SchedulerItem item = new SchedulerItem();
  110. item.MoveType = ModuleHelper.IsLoadLock(swapItems[j].DestinationModule) ? TMEntity.MSG.Place : TMEntity.MSG.PMPlace;
  111. item.target = swapItems[j].DestinationModule;
  112. item.moveList = new Queue<MoveItem>();
  113. item.moveList.Enqueue(new MoveItem(ModuleName.TM, j - swapIndex, swapItems[j].DestinationModule, swapItems[j].DestinationSlot, (Hand)(j - swapIndex)));
  114. _schedulerList.Enqueue(item);
  115. }
  116. }
  117. }
  118. if(WaferManager.Instance.CheckNoWafer(ModuleName.TM, 0) && WaferManager.Instance.CheckNoWafer(ModuleName.TM, 1))
  119. {
  120. if (items.Length == 4)
  121. {
  122. if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  123. items[0].DestinationModule == items[1].DestinationModule &&
  124. items[0].DestinationModule == items[2].SourceModule &&
  125. items[0].DestinationModule == items[3].SourceModule)
  126. {
  127. PackSwapCmds(items, TMEntity.MSG.Swap, 2);
  128. }
  129. }
  130. else if (items.Length == 3)
  131. {
  132. if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  133. items[0].DestinationModule == items[1].DestinationModule &&
  134. items[0].DestinationModule == items[2].SourceModule)
  135. {
  136. PackSwapCmds(items, TMEntity.MSG.Swap, 2);
  137. }
  138. else if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  139. items[0].DestinationModule == items[1].SourceModule &&
  140. items[0].DestinationModule == items[2].SourceModule)
  141. {
  142. PackSwapCmds(items, TMEntity.MSG.Swap, 1);
  143. }
  144. }
  145. else if (items.Length == 2)
  146. {
  147. if (ModuleHelper.IsLoadLock(items[0].DestinationModule) &&
  148. items[0].DestinationModule == items[1].DestinationModule)
  149. {
  150. PackSwapCmds(items, TMEntity.MSG.Swap, 2);
  151. }
  152. else if (ModuleHelper.IsLoadLock(items[0].SourceModule) &&
  153. items[0].SourceModule == items[1].SourceModule)
  154. {
  155. PackSwapCmds(items, TMEntity.MSG.Swap, 0);
  156. }
  157. else if (ModuleHelper.IsPm(items[0].SourceModule) &&
  158. items[0].SourceModule == items[1].DestinationModule)
  159. {
  160. PackSwapCmds(items, TMEntity.MSG.PMSwap, 1);
  161. }
  162. }
  163. }
  164. Hand freeHand = SelectFreeHand();
  165. if(freeHand == Hand.None)
  166. {
  167. LOG.Write(eEvent.WARN_ROUTER, ModuleName.TM, "No Free Arm to transfer wafer");
  168. return false;
  169. }
  170. if(_schedulerList.Count == 0)
  171. {
  172. foreach(var moveItem in items)
  173. {
  174. if(!ModuleHelper.IsTM(moveItem.SourceModule))
  175. {
  176. SchedulerItem item = new SchedulerItem();
  177. item.MoveType = ModuleHelper.IsLoadLock(moveItem.SourceModule) ? TMEntity.MSG.Pick : TMEntity.MSG.PMPick;
  178. item.target = moveItem.SourceModule;
  179. item.moveList = new Queue<MoveItem>();
  180. item.moveList.Enqueue(new MoveItem(moveItem.SourceModule, moveItem.SourceSlot, ModuleName.TM, (int)freeHand, freeHand));
  181. _schedulerList.Enqueue(item);
  182. }
  183. if (!ModuleHelper.IsTM(moveItem.DestinationModule))
  184. {
  185. SchedulerItem item = new SchedulerItem();
  186. item.MoveType = ModuleHelper.IsLoadLock(moveItem.DestinationModule) ? TMEntity.MSG.Place : TMEntity.MSG.PMPlace;
  187. item.target = moveItem.DestinationModule;
  188. item.moveList = new Queue<MoveItem>();
  189. item.moveList.Enqueue(new MoveItem(ModuleName.TM, (int)freeHand, moveItem.DestinationModule, moveItem.DestinationSlot, freeHand));
  190. _schedulerList.Enqueue(item);
  191. }
  192. }
  193. }
  194. return true;
  195. }
  196. Hand SelectFreeHand()
  197. {
  198. if (WaferManager.Instance.CheckNoWafer(ModuleName.TM, 0))
  199. return Hand.Blade1;
  200. if (WaferManager.Instance.CheckNoWafer(ModuleName.TM, 1))
  201. return Hand.Blade2;
  202. return Hand.None;
  203. }
  204. bool RunSchedulers()
  205. {
  206. if(_entity.IsIdle && CheckTaskDone())
  207. {
  208. if (_schedulerList.Count == 0)
  209. return true;
  210. _currentScheduler = _schedulerList.Dequeue();
  211. Queue<MoveItem> moveItems = new Queue<MoveItem>();
  212. foreach(var item in _currentScheduler.moveList)
  213. {
  214. moveItems.Enqueue(item);
  215. }
  216. if(_entity.CheckToPostMessage((int)_currentScheduler.MoveType, moveItems))
  217. {
  218. _entityTaskToken = (int)_currentScheduler.MoveType;
  219. }
  220. else
  221. _entityTaskToken = (int)FSM_MSG.NONE;
  222. }
  223. return false;
  224. }
  225. }
  226. }