SchedulerTMRobot.cs 15 KB

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