SystemDispatcher.cs 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using Aitex.Core.Common;
  6. using Aitex.Core.RT.Routine;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Sorter.Common;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.Util;
  11. using Aitex.Core.RT.DataCenter;
  12. using Aitex.Core.RT.RecipeCenter;
  13. using Aitex.Core.RT.Fsm;
  14. using MECF.Framework.Common.Jobs;
  15. using MECF.Framework.Common.Routine;
  16. using MECF.Framework.Common.Equipment;
  17. using MECF.Framework.Common.SubstrateTrackings;
  18. using MECF.Framework.Common.Schedulers;
  19. using MECF.Framework.Common.DBCore;
  20. using Venus_Core;
  21. using Venus_RT.Modules.Schedulers;
  22. using Venus_RT.Scheduler;
  23. using Venus_Unity;
  24. namespace Venus_RT.Modules
  25. {
  26. public class WaferTask
  27. {
  28. public ModuleName sourceMod { get; }
  29. public int sourceSlot { get; }
  30. public ModuleName destMod { get; private set; }
  31. public int destSlot { get; private set; }
  32. public ModuleName currentMod { get; private set; }
  33. public int currentSlot { get; private set; }
  34. public ModuleName nextMod { get; private set; }
  35. public int nextSlot { get; private set; }
  36. public ModuleName routedMod { get; private set; }
  37. public int routedSlot { get; private set; }
  38. public Hand hand { get; private set; }
  39. public SequenceLLInOutPath llInOutPath { get; }
  40. public string processRecipe { get; }
  41. public string wtwCleanRecipe { get; }
  42. public float temperature { get; }
  43. public Guid waferId { get; }
  44. public int elapseTime { get { return (int)(DateTime.Now - _scheduledTime).TotalSeconds; } }
  45. public int llDelayTime { get; }
  46. public RState pressureStatus { get; private set; }
  47. public RState temperatureStatus { get; private set; }
  48. public RState movingStatus { get; private set; }
  49. public bool IsCompleted { get { return movingStatus == RState.End && currentMod == destMod && currentSlot == destSlot; } }
  50. public bool IsTimeout { get { return elapseTime > 600; } }
  51. public bool IsAligned { get; set; }
  52. public bool IsWaitingAtmMoving
  53. {
  54. get
  55. {
  56. if (movingStatus != RState.Init)
  57. return false;
  58. return !IsWaitingVacMoving;
  59. }
  60. }
  61. public bool IsWaitingVacMoving
  62. {
  63. get
  64. {
  65. if ( movingStatus == RState.Init &&
  66. (ModuleHelper.IsPm(nextMod) ||
  67. ModuleHelper.IsTMRobot(nextMod) ||
  68. ModuleHelper.IsPm(currentMod) ||
  69. ModuleHelper.IsTMRobot(currentMod)))
  70. return true;
  71. return false;
  72. }
  73. }
  74. public delegate void WaferMoveHandler(WaferTask wafer, MoveItem item);
  75. public event WaferMoveHandler OnWaferArrived;
  76. public event WaferMoveHandler OnWaferLeaved;
  77. private DateTime _scheduledTime;
  78. public WaferTask(ModuleName source, int srcSlot, ModuleName dest, int dstSlot, float temp, Guid waferID, string recipeName, string wtwClean, SequenceLLInOutPath inOutPath, int LLDelay)
  79. {
  80. sourceMod = currentMod = nextMod = routedMod = source;
  81. sourceSlot = currentSlot = nextSlot = routedSlot = srcSlot;
  82. destMod = dest;
  83. destSlot = dstSlot;
  84. hand = Hand.None;
  85. temperature = temp;
  86. waferId = waferID;
  87. processRecipe = recipeName;
  88. wtwCleanRecipe = wtwClean;
  89. llInOutPath = inOutPath;
  90. llDelayTime = LLDelay;
  91. IsAligned = false;
  92. pressureStatus = RState.Init;
  93. temperatureStatus = RState.Init;
  94. movingStatus = RState.Init;
  95. _scheduledTime = DateTime.Now;
  96. }
  97. public RState Run()
  98. {
  99. if (movingStatus == RState.Running)
  100. {
  101. var wafer = WaferManager.Instance.GetWafer(nextMod, nextSlot);
  102. if (!wafer.IsEmpty && wafer.InnerId == waferId)
  103. {
  104. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Wafer {wafer.WaferOrigin} from {currentMod}.{currentSlot + 1} move to {nextMod}.{nextSlot + 1}");
  105. if (!ModuleHelper.IsTMRobot(currentMod) && !ModuleHelper.IsEFEMRobot(currentMod))
  106. {
  107. OnWaferLeaved(this, new MoveItem(currentMod, currentSlot, nextMod, nextSlot, hand));
  108. }
  109. if (!ModuleHelper.IsTMRobot(nextMod) && !ModuleHelper.IsEFEMRobot(nextMod))
  110. {
  111. OnWaferArrived(this, new MoveItem(currentMod, currentSlot, nextMod, nextSlot, hand));
  112. }
  113. currentMod = nextMod;
  114. currentSlot = nextSlot;
  115. movingStatus = RState.End;
  116. }
  117. }
  118. if (ModuleHelper.IsPm(destMod) && temperatureStatus == RState.Init)
  119. {
  120. var pmScheduler = Singleton<TransferModule>.Instance.GetScheduler(destMod) as SchedulerPM;
  121. if (pmScheduler != null && pmScheduler.IsAvailable)
  122. {
  123. pmScheduler.Preheating(temperature);
  124. temperatureStatus = RState.Running;
  125. }
  126. }
  127. return RState.Running;
  128. }
  129. public void RouteTo(ModuleName mod, int slot)
  130. {
  131. routedMod = mod;
  132. routedSlot = slot;
  133. movingStatus = RState.Init;
  134. }
  135. public void MoveTo(ModuleName mod, int slot)
  136. {
  137. nextMod = mod;
  138. nextSlot = slot;
  139. movingStatus = RState.Running;
  140. }
  141. public void Return()
  142. {
  143. destMod = sourceMod;
  144. destSlot = sourceSlot;
  145. movingStatus = RState.End;
  146. pressureStatus = RState.End;
  147. temperatureStatus = RState.End;
  148. }
  149. }
  150. public enum ModuleStatus
  151. {
  152. Idle,
  153. WaitMove,
  154. Moving,
  155. NotReady,
  156. // PM Status
  157. WaitProcess,
  158. StartProcess,
  159. Processing,
  160. StartIdleClean,
  161. IdleClean,
  162. WaitPreJobClean,
  163. StartPreJobClean,
  164. PreJobClean,
  165. WaitPostJobClean,
  166. StartPostJobClean,
  167. PostJobClean,
  168. WaitWTWClean,
  169. StartWTWClean,
  170. WTWClean,
  171. // Align Status
  172. WaitAlign,
  173. StartAlign,
  174. Aligning,
  175. // Loadlock Status
  176. ExchangeWaferWithEFEM,
  177. ExchangeWaferWithTM,
  178. }
  179. public class ModuleTask
  180. {
  181. public ModuleName Module { get; }
  182. public ModuleStatus Status { get; set; }
  183. public SchedulerModule Scheduler { get; }
  184. public virtual int SlotNum { get; protected set; }
  185. public virtual bool IsIdle { get { return Status == ModuleStatus.Idle; } }
  186. private int _currentActionTime = 0;
  187. public virtual int IdleTime
  188. {
  189. get
  190. {
  191. if (Status != ModuleStatus.Idle)
  192. return 0;
  193. return (int)(DateTime.Now - _moduleTimer).TotalSeconds;
  194. }
  195. }
  196. public virtual int BusyTime
  197. {
  198. get
  199. {
  200. if (Status == ModuleStatus.Idle)
  201. return 0;
  202. return (int)(DateTime.Now - _moduleTimer).TotalSeconds;
  203. }
  204. }
  205. public virtual int BusyLastTime
  206. {
  207. get
  208. {
  209. if (Status == ModuleStatus.Idle)
  210. return 0;
  211. return _currentActionTime - BusyTime;
  212. }
  213. }
  214. public virtual int TimeToReady
  215. {
  216. get
  217. {
  218. if(Scheduler.IsAvailable)
  219. return 0;
  220. return int.MaxValue;
  221. }
  222. }
  223. protected DateTime _moduleTimer;
  224. public ModuleTask(ModuleName mod)
  225. {
  226. Module = mod;
  227. Scheduler = Singleton<TransferModule>.Instance.GetScheduler(mod);
  228. SlotNum = 1;
  229. _moduleTimer = DateTime.Now;
  230. }
  231. public virtual RState Run()
  232. {
  233. Status = Scheduler.IsAvailable ? ModuleStatus.Idle : ModuleStatus.NotReady;
  234. return RState.Running;
  235. }
  236. public virtual void WaferArrived(WaferTask wafer)
  237. {
  238. }
  239. public virtual void WaferLeaved(WaferTask wafer)
  240. {
  241. }
  242. }
  243. public class PMTask : ModuleTask
  244. {
  245. private WaferTask _wafer;
  246. private SchedulerPM _pmScheduler => Scheduler as SchedulerPM;
  247. public ModuleStatus JobCleanStatus { get; set; }
  248. public PMTask(ModuleName pm) : base(pm)
  249. {
  250. JobCleanStatus = ModuleStatus.Idle;
  251. }
  252. public override RState Run()
  253. {
  254. if(Scheduler.IsAvailable)
  255. {
  256. switch (Status)
  257. {
  258. case ModuleStatus.Idle:
  259. {
  260. if(_wafer == null)
  261. {
  262. if(_pmScheduler.RunIdleCleanTask()) // Check Idle Clean
  263. {
  264. Status = ModuleStatus.StartIdleClean;
  265. }
  266. }
  267. }
  268. break;
  269. case ModuleStatus.WaitProcess:
  270. {
  271. Scheduler.EventWaferArrived?.Invoke(this, new WaferMoveArgs(ModuleName.TMRobot, 0, Module, 0));
  272. Status = ModuleStatus.StartProcess;
  273. }
  274. break;
  275. case ModuleStatus.WaitPreJobClean:
  276. {
  277. }
  278. break;
  279. case ModuleStatus.WaitPostJobClean:
  280. {
  281. }
  282. break;
  283. case ModuleStatus.WaitWTWClean:
  284. {
  285. }
  286. break;
  287. case ModuleStatus.Processing:
  288. {
  289. var wafer = WaferManager.Instance.GetWafer(Module, 0);
  290. if(!wafer.IsEmpty && wafer.ProcessState == EnumWaferProcessStatus.Completed)
  291. {
  292. _wafer.Return();
  293. Status = ModuleStatus.Idle;
  294. }
  295. }
  296. break;
  297. case ModuleStatus.IdleClean:
  298. case ModuleStatus.PreJobClean:
  299. case ModuleStatus.PostJobClean:
  300. case ModuleStatus.WTWClean:
  301. {
  302. Status = ModuleStatus.Idle;
  303. }
  304. break;
  305. }
  306. }
  307. else
  308. {
  309. switch(Status)
  310. {
  311. case ModuleStatus.StartProcess:
  312. Status = ModuleStatus.Processing;
  313. break;
  314. case ModuleStatus.StartIdleClean:
  315. Status = ModuleStatus.IdleClean;
  316. break;
  317. case ModuleStatus.StartPreJobClean:
  318. Status = ModuleStatus.PreJobClean;
  319. break;
  320. case ModuleStatus.StartPostJobClean:
  321. Status = ModuleStatus.PostJobClean;
  322. break;
  323. case ModuleStatus.StartWTWClean:
  324. Status = ModuleStatus.WTWClean;
  325. break;
  326. }
  327. }
  328. return RState.Running;
  329. }
  330. public override void WaferArrived(WaferTask wafer)
  331. {
  332. _wafer = wafer;
  333. Status = ModuleStatus.WaitProcess;
  334. }
  335. public override void WaferLeaved(WaferTask wafer)
  336. {
  337. if(!string.IsNullOrEmpty(_wafer.wtwCleanRecipe.Trim()))
  338. {
  339. Status = ModuleStatus.WaitWTWClean;
  340. }
  341. _wafer = null;
  342. }
  343. }
  344. public class AlignerTask : ModuleTask
  345. {
  346. private WaferTask _wafer;
  347. public AlignerTask(ModuleName aligner) : base(aligner)
  348. {
  349. }
  350. public override RState Run()
  351. {
  352. var efemRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot) as SchedulerEfemRobot;
  353. if (efemRobot == null || !efemRobot.IsAvailable)
  354. return RState.Running;
  355. switch (Status)
  356. {
  357. case ModuleStatus.WaitAlign:
  358. {
  359. efemRobot.Align(0);
  360. Status = ModuleStatus.Aligning;
  361. }
  362. break;
  363. case ModuleStatus.Aligning:
  364. {
  365. _wafer.IsAligned = true;
  366. Status = ModuleStatus.Idle;
  367. }
  368. break;
  369. }
  370. return RState.Running;
  371. }
  372. public override void WaferArrived(WaferTask wafer)
  373. {
  374. _wafer = wafer;
  375. Status = ModuleStatus.WaitAlign;
  376. }
  377. public override void WaferLeaved(WaferTask wafer)
  378. {
  379. _wafer = null;
  380. }
  381. }
  382. public class LoadPortTask : ModuleTask
  383. {
  384. public LoadPortTask(ModuleName lp) : base(lp)
  385. {
  386. SlotNum = SC.GetValue<int>($"EFEM.LoadPort.SlotNumber");
  387. }
  388. public override RState Run()
  389. {
  390. return base.Run();
  391. }
  392. }
  393. public class TMRobotTask : ModuleTask
  394. {
  395. public TMRobotTask(ModuleName tmRobot) : base(tmRobot)
  396. {
  397. SlotNum = 2;
  398. }
  399. public override RState Run()
  400. {
  401. return base.Run();
  402. }
  403. }
  404. public class EFEMRobotTask : ModuleTask
  405. {
  406. public EFEMRobotTask(ModuleName efemRobot) : base(efemRobot)
  407. {
  408. SlotNum = 2;
  409. }
  410. public override RState Run()
  411. {
  412. return base.Run();
  413. }
  414. public bool PostMoveItems(MoveItem[] items)
  415. {
  416. Status = ModuleStatus.Moving;
  417. return (Scheduler as SchedulerEfemRobot).PostMoveItems(items);
  418. }
  419. }
  420. public class LoadlockTask : ModuleTask
  421. {
  422. public LoadlockTask(ModuleName ll) : base(ll)
  423. {
  424. SlotNum = SC.GetValue<int>($"{ll}.SlotNumber");
  425. }
  426. public override int TimeToReady
  427. {
  428. get
  429. {
  430. if (Scheduler.IsAvailable)
  431. return 0;
  432. return int.MaxValue;
  433. }
  434. }
  435. }
  436. class SystemDispatcher : ICycle
  437. {
  438. private List<ControlJobInfo> _lstControlJobs = new List<ControlJobInfo>();
  439. private List<ProcessJobInfo> _lstProcessJobs = new List<ProcessJobInfo>();
  440. private List<WaferTask> _lstWaferTasks = new List<WaferTask>();
  441. private Dictionary<ModuleName, ModuleTask> _dictModuleTask = new Dictionary<ModuleName, ModuleTask>();
  442. private Queue<WaferInfo> _qeReturnWafers = new Queue<WaferInfo>();
  443. private Queue<List<MoveItem>> _efemSchdActions = new Queue<List<MoveItem>>();
  444. private Queue<List<MoveItem>> _tmSchdActions = new Queue<List<MoveItem>>();
  445. private List<MoveItem> _curEfemAction = new List<MoveItem>();
  446. private List<MoveItem> _curTmAction = new List<MoveItem>();
  447. private RState _efemRobotStatus { get { return (_dictModuleTask[ModuleName.EfemRobot].Scheduler as SchedulerEfemRobot).RobotStatus; } }
  448. private RState _tmRobotStatus { get { return (_dictModuleTask[ModuleName.TMRobot].Scheduler as SchedulerTMRobot).RobotStatus; } }
  449. private SchedulerFACallback _faCallback;
  450. private SchedulerDBCallback _dbCallback;
  451. private bool _isCycleMode;
  452. private int _cycleSetPoint = 0;
  453. private int _cycledCount = 0;
  454. private int _cycledWafer = 0;
  455. private float _throughput = 0.0f;
  456. private readonly int _LLASlotNumber = 4;
  457. private readonly int _LLBSlotNumber = 4;
  458. private int _efemRobotSingleArmOption = 0;
  459. private int _tmRobotSingleArmOption = 0;
  460. private Dictionary<ModuleName, int> _lpCycleWafer = new Dictionary<ModuleName, int>();
  461. private Dictionary<ModuleName, int> _lpCycleCount = new Dictionary<ModuleName, int>();
  462. private Stopwatch _cycleWatch = new Stopwatch();
  463. private SequenceLLInOutPath _LLInOutPath = SequenceLLInOutPath.DInDOut;
  464. public SequenceLLInOutPath LLInOutPath => _LLInOutPath;
  465. public bool HasJobRunning => _lstControlJobs.Count > 0;
  466. private RState _cycleState = RState.Init;
  467. public RState CycleState => _cycleState;
  468. #region public interface
  469. public SystemDispatcher()
  470. {
  471. _faCallback = new SchedulerFACallback();
  472. _dbCallback = new SchedulerDBCallback();
  473. _efemRobotSingleArmOption = SC.GetValue<int>("EFEM.SingleArmOption");
  474. _tmRobotSingleArmOption = SC.GetValue<int>("TM.SingleArmOption");
  475. _LLASlotNumber = SC.GetValue<int>("LLA.SlotNumber");
  476. _LLBSlotNumber = SC.GetValue<int>("LLB.SlotNumber");
  477. InitModules();
  478. DATA.Subscribe("Scheduler.CycledCount", () => _cycledCount, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  479. DATA.Subscribe("Scheduler.CycledWafer", () => _cycledWafer, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  480. DATA.Subscribe("Scheduler.CycleSetPoint", () => _cycleSetPoint, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  481. DATA.Subscribe("Scheduler.Throughput", () => _throughput, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  482. DATA.Subscribe("Scheduler.PjIdList", () => Array.ConvertAll(_lstProcessJobs.ToArray(), x => x.InnerId.ToString()).ToList());
  483. DATA.Subscribe("Scheduler.PjNameList", () => Array.ConvertAll(_lstProcessJobs.ToArray(), x => x.LotName.ToString()).ToList());
  484. }
  485. public RState Start(params object[] objs)
  486. {
  487. return RState.Running;
  488. }
  489. public RState Monitor()
  490. {
  491. prelude();
  492. RunWaferTask();
  493. RunModuleTasks();
  494. //UpdateProcessJobStatus();
  495. //UpdateControlJobStatus();
  496. //UpdateLLInOutPathProperty();
  497. RoutingATMWafers();
  498. RoutingVacWafers();
  499. epilogue();
  500. return _cycleState;
  501. }
  502. public void Abort()
  503. {
  504. }
  505. public bool CreateJob(Dictionary<string, object> param,out string reason)
  506. {
  507. reason = "";
  508. _isCycleMode = SC.GetValue<bool>("System.IsCycleMode");
  509. _cycleSetPoint = _isCycleMode ? SC.GetValue<int>("System.CycleCount") : 0;
  510. string[] slotSequence = (string[])param["SlotSequence"];
  511. string jobId = (string)param["JobId"];
  512. string module = (string)param["Module"];
  513. //bool autoStart = (bool)param["AutoStart"];
  514. string lotId = jobId;
  515. if (param.ContainsKey("LotId"))
  516. lotId = (string)param["LotId"];
  517. string preCleanRecipe = param.ContainsKey("PreCleanRecipeName") ? (string)param["PreCleanRecipeName"] : string.Empty;
  518. string postCleanRecipe = param.ContainsKey("PostCleanRecipeName") ? (string)param["PostCleanRecipeName"] : string.Empty;
  519. if (slotSequence.Length != SC.GetValue<int>("EFEM.LoadPort.SlotNumber"))
  520. {
  521. reason = $"slot sequence parameter not valid, length is {slotSequence.Length}, should be {SC.GetValue<int>("EFEM.LoadPort.SlotNumber")}";
  522. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  523. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  524. return false;
  525. }
  526. if (!ModuleHelper.IsLoadPort(ModuleHelper.Converter(module)))
  527. {
  528. reason = $"{module} should be LoadPort";
  529. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  530. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  531. return false;
  532. }
  533. if (string.IsNullOrEmpty(jobId))
  534. {
  535. jobId = "CJ_Local_" + module;
  536. }
  537. if (_lstControlJobs.Exists(x => x.Name == jobId))
  538. {
  539. reason = $"{jobId} already created";
  540. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  541. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  542. return false;
  543. }
  544. ControlJobInfo cj = new ControlJobInfo();
  545. cj.Name = jobId;
  546. cj.Module = module;
  547. cj.LotName = lotId;
  548. cj.LotInnerId = Guid.NewGuid();
  549. cj.LotWafers = new List<WaferInfo>();
  550. cj.SetState(EnumControlJobState.WaitingForStart);
  551. cj.JetState = EnumJetCtrlJobState.Created;
  552. cj.PreJobClean = preCleanRecipe;
  553. cj.PostJobClean = postCleanRecipe;
  554. Dictionary<string, bool[]> seqSlot = new Dictionary<string, bool[]>();
  555. Dictionary<string, List<Tuple<ModuleName, int>>> seqSlotWafers = new Dictionary<string, List<Tuple<ModuleName, int>>>();
  556. Dictionary<string, string> indexSequence = new Dictionary<string, string>();
  557. bool enableGroupBySequence = SC.GetValue<bool>("Scheduler.GroupWaferBySequence");
  558. for (int i = 0; i < SC.GetValue<int>("EFEM.LoadPort.SlotNumber"); i++)
  559. {
  560. if (string.IsNullOrEmpty(slotSequence[i]) || string.IsNullOrEmpty(slotSequence[i].Trim()))
  561. continue;
  562. string groupName = enableGroupBySequence ? slotSequence[i].Trim() : i.ToString();
  563. indexSequence[groupName] = slotSequence[i];
  564. if (!seqSlot.ContainsKey(groupName))
  565. {
  566. seqSlot[groupName] = new bool[SC.GetValue<int>("EFEM.LoadPort.SlotNumber")];
  567. }
  568. if (!seqSlotWafers.ContainsKey(groupName))
  569. {
  570. seqSlotWafers[groupName] = new List<Tuple<ModuleName, int>>();
  571. }
  572. seqSlot[groupName][i] = true;
  573. if (!WaferManager.Instance.CheckHasWafer(module, i))
  574. {
  575. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"job wafer: {module} slot {i + 1} not in the carrier");
  576. return false;
  577. }
  578. if (!WaferManager.Instance.CheckWafer(ModuleHelper.Converter(module), i, WaferStatus.Normal))
  579. {
  580. reason = $"job wafer: {module} slot {i + 1} status is {WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).Status}";
  581. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  582. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  583. return false;
  584. }
  585. if (WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).ProcessState != EnumWaferProcessStatus.Idle)
  586. {
  587. reason = $"job wafer: {module} slot {i + 1} process status is {WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).ProcessState}";
  588. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  589. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  590. return false;
  591. }
  592. cj.LotWafers.Add(WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i));
  593. WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).SequenceName = slotSequence[i];
  594. seqSlotWafers[groupName].Add(Tuple.Create(ModuleHelper.Converter(module), i));
  595. cj.JobWaferSize = WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).Size;
  596. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Assigned wafer job, wafer {module}.{i + 1}, sequence: {slotSequence[i]}");
  597. }
  598. if (seqSlotWafers.Count == 0)
  599. {
  600. reason = $"job has not assign wafer";
  601. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System,reason );
  602. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  603. return false;
  604. }
  605. List<ProcessJobInfo> pjs = new List<ProcessJobInfo>();
  606. string[] seqs = seqSlot.Keys.ToArray();
  607. for (int i = 0; i < seqs.Length; i++)
  608. {
  609. ProcessJobInfo pj = new ProcessJobInfo();
  610. pj.Name = jobId + "_" + (i + 1);
  611. pj.Sequence = SequenceInfoHelper.GetInfo(indexSequence[seqs[i]]);
  612. pj.ControlJobName = cj.Name;
  613. pj.LotName = lotId;
  614. pj.SlotWafers = seqSlotWafers[seqs[i]];
  615. pj.SetState(EnumProcessJobState.Queued);
  616. if (!CheckSequencePmReady(pj.Sequence, out reason))
  617. {
  618. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"no valid chamber for the {reason}");
  619. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  620. return false;
  621. }
  622. if (!RouteManager.IsATMMode && !CheckSequenceRecipeFileValid(pj.Sequence, out reason))
  623. {
  624. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"recipe file not valid in the sequence, {reason}");
  625. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  626. return false;
  627. }
  628. pjs.Add(pj);
  629. }
  630. _dbCallback.LotUpdate(cj);
  631. foreach (var pj in pjs)
  632. {
  633. cj.ProcessJobNameList.Add(pj.Name);
  634. _lstProcessJobs.Add(pj);
  635. }
  636. _lstControlJobs.Add(cj);
  637. //AssociatedPMWithLP(cj);
  638. _faCallback.JobCreated(cj, GetFirstProcessJob(cj));
  639. return true;
  640. }
  641. public bool CheckAllJobDone()
  642. {
  643. foreach (var cj in _lstControlJobs)
  644. {
  645. if (cj.State == EnumControlJobState.Executing || cj.State == EnumControlJobState.Paused)
  646. return false;
  647. }
  648. return true;
  649. }
  650. public bool CheckJobJustDone(out string sJobName)
  651. {
  652. foreach (var cj in _lstControlJobs)
  653. {
  654. if (cj.State == EnumControlJobState.Completed && !cj.BeenPosted)
  655. {
  656. //LP;WaferSize;Lot;Number;Start;End;
  657. sJobName = $"{cj.Module};{cj.JobWaferSize};{cj.LotName};{cj.LotWafers.Count};{cj.StartTime:T};{cj.EndTime:T}";
  658. cj.BeenPosted = true;
  659. return true;
  660. }
  661. }
  662. sJobName = "NULL";
  663. return false;
  664. }
  665. public ProcessJobInfo GetFirstProcessJob(ControlJobInfo cj)
  666. {
  667. foreach (var pj in _lstProcessJobs)
  668. {
  669. if (pj.ControlJobName == cj.Name)
  670. return pj;
  671. }
  672. return null;
  673. }
  674. public void AbortJob(string jobName)
  675. {
  676. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  677. if (cj == null)
  678. {
  679. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"abort job rejected, not found job with id {jobName}");
  680. return;
  681. }
  682. List<ProcessJobInfo> pjAbortList = new List<ProcessJobInfo>();
  683. foreach (var pj in _lstProcessJobs)
  684. {
  685. if (pj.ControlJobName == cj.Name)
  686. {
  687. pj.SetState(EnumProcessJobState.Aborting);
  688. pjAbortList.Add(pj);
  689. int unprocessed = 0;
  690. int aborted = 0;
  691. WaferInfo[] wafers = WaferManager.Instance.GetWaferByProcessJob(pj.Name);
  692. foreach (var waferInfo in wafers)
  693. {
  694. waferInfo.ProcessJob = null;
  695. waferInfo.NextSequenceStep = 0;
  696. if (waferInfo.ProcessState != EnumWaferProcessStatus.Completed)
  697. unprocessed++;
  698. }
  699. JobDataRecorder.EndPJ(pj.InnerId.ToString(), aborted, unprocessed);
  700. }
  701. }
  702. _faCallback.JobAborted(cj, GetFirstProcessJob(cj));
  703. _dbCallback.LotFinished(cj);
  704. foreach (var pj in pjAbortList)
  705. {
  706. _lstProcessJobs.Remove(pj);
  707. }
  708. _lstControlJobs.Remove(cj);
  709. }
  710. public void StopJob(string jobName)
  711. {
  712. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  713. if (cj == null)
  714. {
  715. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"stop job rejected, not found job with id {jobName}");
  716. return;
  717. }
  718. foreach (var pj in _lstProcessJobs)
  719. {
  720. if (pj.ControlJobName == cj.Name)
  721. {
  722. pj.SetState(EnumProcessJobState.Stopping);
  723. }
  724. }
  725. _faCallback.JobStopped(cj, GetFirstProcessJob(cj));
  726. _dbCallback.LotFinished(cj);
  727. }
  728. public void ResumeJob(string jobName)
  729. {
  730. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  731. if (cj == null)
  732. {
  733. //LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"resume job rejected, not found job with id {jobName}");
  734. return;
  735. }
  736. if (cj.State == EnumControlJobState.Paused)
  737. {
  738. cj.SetState(EnumControlJobState.Executing);
  739. }
  740. _faCallback.JobResumed(cj, GetFirstProcessJob(cj));
  741. _cycleState = RState.Running;
  742. }
  743. public void PauseJob(string jobName)
  744. {
  745. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  746. if (cj == null)
  747. {
  748. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"pause job rejected, not found job with id {jobName}");
  749. return;
  750. }
  751. if (cj.State == EnumControlJobState.Executing)
  752. {
  753. cj.SetState(EnumControlJobState.Paused);
  754. }
  755. _faCallback.JobPaused(cj, GetFirstProcessJob(cj));
  756. if (!_lstControlJobs.Exists(job => job.State == EnumControlJobState.Executing))
  757. _cycleState = RState.Paused;
  758. }
  759. public bool StartJob(string jobName,out string reason)
  760. {
  761. reason = "";
  762. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  763. if (cj == null)
  764. {
  765. reason = $"start job rejected, not found job with id {jobName}";
  766. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  767. return false;
  768. }
  769. if (cj.State == EnumControlJobState.WaitingForStart)
  770. {
  771. cj.SetState(EnumControlJobState.Executing);
  772. //PreJobClean(cj);
  773. cj.JetState = EnumJetCtrlJobState.Quequed;
  774. cj.StartTime = DateTime.Now;
  775. _dbCallback.LotCreated(cj);
  776. _faCallback.JobStarted(cj, GetFirstProcessJob(cj));
  777. //if (!_blockingWatcher.IsRunning)
  778. // _blockingWatcher.Restart();
  779. //ResetTraceFlag();
  780. }
  781. if (!_cycleWatch.IsRunning)
  782. {
  783. _cycleWatch.Restart();
  784. }
  785. if (!_lpCycleWafer.Keys.Contains(ModuleHelper.Converter(cj.Module)))
  786. {
  787. _lpCycleCount.Add(ModuleHelper.Converter(cj.Module), 0);
  788. _lpCycleWafer.Add(ModuleHelper.Converter(cj.Module), 0);
  789. }
  790. _cycleState = RState.Running;
  791. return true;
  792. }
  793. public void Clear()
  794. {
  795. foreach (var module in _dictModuleTask)
  796. {
  797. module.Value.Status = ModuleStatus.Idle;
  798. module.Value.Scheduler.ResetTask();
  799. }
  800. _lstWaferTasks.Clear();
  801. _tmSchdActions.Clear();
  802. _curTmAction.Clear();
  803. _efemSchdActions.Clear();
  804. _curEfemAction.Clear();
  805. _lstControlJobs.Clear();
  806. _lstProcessJobs.Clear();
  807. _cycleState = RState.End;
  808. }
  809. // manual return one wafer while system is auto running
  810. public bool ManualReturnWafer(object[] objs)
  811. {
  812. ModuleName SourceModule = (ModuleName)objs[0];
  813. int SourceSlot = (int)objs[1];
  814. if (!_dictModuleTask.Keys.Contains(SourceModule))
  815. {
  816. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"Invalid source module {SourceModule} for manual return wafer");
  817. return false;
  818. }
  819. if (WaferManager.Instance.CheckNoWafer(SourceModule, SourceSlot))
  820. {
  821. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"Can not return wafer as {SourceModule} {SourceSlot} has no wafer");
  822. return false;
  823. }
  824. if (!_dictModuleTask[SourceModule].Scheduler.IsIdle)
  825. {
  826. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"The module: {SourceModule} is not ready for return wafer");
  827. return false;
  828. }
  829. var wafer = WaferManager.Instance.GetWafer(SourceModule, SourceSlot);
  830. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Manual return wafer: {wafer.WaferOrigin} at {SourceModule} {SourceSlot} while system is auto running");
  831. _qeReturnWafers.Enqueue(wafer);
  832. return true;
  833. }
  834. public RState CheckManualReturnWafer()
  835. {
  836. var pmWaferCount = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && WaferManager.Instance.CheckHasWafer(mod.Key, 0)).Count();
  837. var tmRobotWaferCount = (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 0) ? 1 : 0) + (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 1) ? 1 : 0);
  838. if (!_dictModuleTask[ModuleName.TMRobot].Scheduler.IsIdle && (pmWaferCount > 0 || tmRobotWaferCount > 0))
  839. {
  840. LOG.Write(eEvent.ERR_ROUTER, ModuleName.TMRobot, $"The TM Robot is not ready for return wafer.");
  841. return RState.Failed;
  842. }
  843. if (!_dictModuleTask[ModuleName.EfemRobot].Scheduler.IsIdle)
  844. {
  845. LOG.Write(eEvent.ERR_ROUTER, ModuleName.EfemRobot, $"The EFEM Robot is not ready for return wafer.");
  846. return RState.Failed;
  847. }
  848. foreach (var mod in _dictModuleTask)
  849. {
  850. if (ModuleHelper.IsLoadPort(mod.Key))
  851. continue;
  852. if (!mod.Value.IsIdle)
  853. {
  854. LOG.Write(eEvent.ERR_ROUTER, mod.Key, $"{mod.Key} is not ready for return wafer.");
  855. return RState.Failed;
  856. }
  857. int nSlotNumber = ModuleHelper.IsLoadLock(mod.Key) ? (mod.Key == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber) : (ModuleHelper.IsTMRobot(mod.Key) || ModuleHelper.IsEFEMRobot(mod.Key) ? 2 : 1);
  858. for (int slot = 0; slot < nSlotNumber; slot++)
  859. {
  860. var wafer = WaferManager.Instance.GetWafer(mod.Key, slot);
  861. if (wafer.IsEmpty)
  862. continue;
  863. var destLP = (ModuleName)wafer.OriginStation;
  864. if (!_dictModuleTask[destLP].Scheduler.IsAvailable)
  865. {
  866. LOG.Write(eEvent.ERR_ROUTER, destLP, $"The destination Loadport {destLP} is not ready for return wafer.");
  867. return RState.Failed;
  868. }
  869. }
  870. }
  871. Clear();
  872. return RState.Running;
  873. }
  874. public RState ReturnAllWafers()
  875. {
  876. int systemWaferCount = 0;
  877. foreach (var mod in _dictModuleTask)
  878. {
  879. if (ModuleHelper.IsLoadPort(mod.Key))
  880. continue;
  881. int nSlotNumber = ModuleHelper.IsLoadLock(mod.Key) ? (mod.Key == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber) : (ModuleHelper.IsTMRobot(mod.Key) || ModuleHelper.IsEFEMRobot(mod.Key) ? 2 : 1);
  882. for (int slot = 0; slot < nSlotNumber; slot++)
  883. {
  884. var wafer = WaferManager.Instance.GetWafer(mod.Key, slot);
  885. if (!wafer.IsEmpty)
  886. systemWaferCount++;
  887. }
  888. }
  889. if (systemWaferCount == 0)
  890. return RState.End;
  891. ReturnVacWafers();
  892. ReturnAtmWafers();
  893. return RState.Running;
  894. }
  895. #endregion
  896. #region internal implementation
  897. private void InitModules()
  898. {
  899. foreach (var module in ModuleHelper.InstalledModules)
  900. {
  901. if (ModuleHelper.IsInstalled(module))
  902. {
  903. if(ModuleHelper.IsPm(module))
  904. {
  905. _dictModuleTask[module] = new PMTask(module);
  906. }
  907. else if (ModuleHelper.IsLoadLock(module))
  908. {
  909. _dictModuleTask[module] = new LoadlockTask(module);
  910. }
  911. else if(ModuleHelper.IsLoadPort(module))
  912. {
  913. _dictModuleTask[module] = new LoadPortTask(module);
  914. }
  915. else if(ModuleHelper.IsTMRobot(module))
  916. {
  917. _dictModuleTask[module] = new TMRobotTask(module);
  918. }
  919. else if(ModuleHelper.IsEFEMRobot(module))
  920. {
  921. _dictModuleTask[module] = new EFEMRobotTask(module);
  922. }
  923. else if(ModuleHelper.IsAligner(module))
  924. {
  925. _dictModuleTask[module] = new AlignerTask(module);
  926. }
  927. }
  928. }
  929. }
  930. private void prelude()
  931. {
  932. }
  933. private void epilogue()
  934. {
  935. UpdateProcessJobStatus();
  936. UpdateControlJobStatus();
  937. UpdateLLInOutPathProperty();
  938. CreateNewWaferTask();
  939. _lstWaferTasks.RemoveAll(item => ModuleHelper.IsLoadPort(item.destMod) && ModuleHelper.IsLoadPort(item.currentMod) && item.pressureStatus == RState.End);
  940. }
  941. private void RunWaferTask()
  942. {
  943. foreach (var task in _lstWaferTasks)
  944. {
  945. task.Run();
  946. }
  947. }
  948. private void RunModuleTasks()
  949. {
  950. foreach (var task in _dictModuleTask)
  951. {
  952. task.Value.Run();
  953. }
  954. }
  955. private bool CheckAllWaferReturned(ProcessJobInfo pj, bool checkAllProcessed)
  956. {
  957. bool allWaferReturn = true;
  958. foreach (var slot in pj.SlotWafers)
  959. {
  960. var wafer = WaferManager.Instance.GetWafer(slot.Item1, slot.Item2);
  961. if(wafer.IsEmpty || (wafer.ProcessState != EnumWaferProcessStatus.Completed && checkAllProcessed))
  962. {
  963. allWaferReturn = false;
  964. break;
  965. }
  966. }
  967. return allWaferReturn;
  968. }
  969. private void UpdateProcessJobStatus()
  970. {
  971. if (_efemRobotStatus == RState.Running || _tmRobotStatus == RState.Running)
  972. return;
  973. foreach (var pj in _lstProcessJobs)
  974. {
  975. if (CheckAllWaferReturned(pj, pj.State != EnumProcessJobState.Stopping))
  976. {
  977. pj.SetState(EnumProcessJobState.ProcessingComplete);
  978. JobDataRecorder.EndPJ(pj.InnerId.ToString(), 0, 0);
  979. }
  980. }
  981. }
  982. private void UpdateControlJobStatus()
  983. {
  984. if (_lstControlJobs.Count == 0 || _efemRobotStatus == RState.Running || _tmRobotStatus == RState.Running)
  985. return;
  986. bool allControlJobComplete = true;
  987. List<ControlJobInfo> cjRemoveList = new List<ControlJobInfo>();
  988. foreach (var cj in _lstControlJobs)
  989. {
  990. if (cj.JetState == EnumJetCtrlJobState.Quequed) // active quequed control job: need prejob clean or don need prejob clean
  991. {
  992. var runingJobs = _lstControlJobs.FindAll(job => IsCtrlJobRuning(job));
  993. if (runingJobs.Count == 0 || (runingJobs.Count == 1 && IsCtrlJobEndingState(runingJobs.First())))
  994. {
  995. var quequedJobs = _lstControlJobs.FindAll(job => job.JetState == EnumJetCtrlJobState.Quequed);
  996. var firstQuequeJob = quequedJobs.OrderBy(job => job.StartTime).First();
  997. if (firstQuequeJob.InnerId == cj.InnerId)
  998. {
  999. if (IsCtrlJobNeedPreClean(cj))
  1000. {
  1001. cj.JetState = EnumJetCtrlJobState.PreJobClean;
  1002. var PMs = GetWaitPreCleanPMsByCtrlJob(cj);
  1003. foreach (var pm in PMs)
  1004. {
  1005. var pmTask = _dictModuleTask[pm] as PMTask;
  1006. if(pmTask != null)
  1007. {
  1008. pmTask.JobCleanStatus = ModuleStatus.WaitPreJobClean;
  1009. }
  1010. }
  1011. }
  1012. else
  1013. {
  1014. ActiveControlJob(cj);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. else if (IsCtrlJobRuning(cj))
  1020. {
  1021. if (cj.JetState == EnumJetCtrlJobState.PreJobClean)
  1022. {
  1023. if (IsJobCleanDone(cj))
  1024. {
  1025. ActiveControlJob(cj);
  1026. }
  1027. }
  1028. else
  1029. {
  1030. if (cj.JetState == EnumJetCtrlJobState.Processing && IsCtrlJobNeedPostClean(cj)) // active post job clean
  1031. {
  1032. if (IsAllJobWaferProcessedOrProcessing(cj))
  1033. {
  1034. cj.JetState = EnumJetCtrlJobState.PostJobClean;
  1035. var PMs = GetWaitPreCleanPMsByCtrlJob(cj);
  1036. foreach (var pm in PMs)
  1037. {
  1038. var pmTask = _dictModuleTask[pm] as PMTask;
  1039. if (pmTask != null)
  1040. {
  1041. pmTask.JobCleanStatus = ModuleStatus.WaitPostJobClean;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. // control job complete
  1047. if (IsAllProcessJobComplete(cj) && (!IsCtrlJobNeedPostClean(cj) || cj.JetState == EnumJetCtrlJobState.PostJobClean && IsJobCleanDone(cj)))
  1048. {
  1049. cj.JetState = EnumJetCtrlJobState.Completed;
  1050. cj.SetState(EnumControlJobState.Completed);
  1051. cj.EndTime = DateTime.Now;
  1052. _faCallback.JobFinished(cj, GetFirstProcessJob(cj));
  1053. _dbCallback.LotFinished(cj);
  1054. if (!(_isCycleMode && _cycledCount < _cycleSetPoint))
  1055. (Singleton<TransferModule>.Instance.GetScheduler(ModuleHelper.Converter(cj.Module)) as SchedulerLoadPort).NoteJobComplete();
  1056. _lpCycleCount[ModuleHelper.Converter(cj.Module)]++;
  1057. }
  1058. }
  1059. int countProcessed = 0;
  1060. foreach (var pjName in cj.ProcessJobNameList)
  1061. {
  1062. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1063. if (pj == null)
  1064. {
  1065. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Not find pj named {pjName} in {cj.Name}");
  1066. continue;
  1067. }
  1068. // caculate process wafer by process
  1069. if (_isCycleMode && _cycledCount < (_isCycleMode ? _cycleSetPoint : 0))
  1070. {
  1071. foreach (var pjSlotWafer in pj.SlotWafers)
  1072. {
  1073. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1074. if (!wafer.IsEmpty && wafer.ProcessState == EnumWaferProcessStatus.Completed)
  1075. countProcessed++;
  1076. }
  1077. }
  1078. }
  1079. int lpCycleWafer = _lpCycleCount[ModuleHelper.Converter(cj.Module)] * cj.LotWafers.Count + (cj.JetState == EnumJetCtrlJobState.Completed && cj.LotWafers.Count == countProcessed ? 0 : countProcessed);
  1080. if (_lpCycleCount[ModuleHelper.Converter(cj.Module)] != _cycledCount || lpCycleWafer != _lpCycleWafer[ModuleHelper.Converter(cj.Module)])
  1081. {
  1082. _lpCycleWafer[ModuleHelper.Converter(cj.Module)] = lpCycleWafer;
  1083. }
  1084. }
  1085. LoadportCassetteState state = (LoadportCassetteState)DATA.Poll($"{cj.Module}.CassetteState");
  1086. if (cj.State == EnumControlJobState.Completed && state != LoadportCassetteState.Normal && cj.JetState == EnumJetCtrlJobState.Completed)
  1087. {
  1088. cjRemoveList.Add(cj);
  1089. }
  1090. allControlJobComplete = allControlJobComplete && cj.State == EnumControlJobState.Completed;
  1091. }
  1092. if (_isCycleMode && _cycledCount < (_isCycleMode ? _cycleSetPoint : 0))
  1093. {
  1094. int totolCycleWafer = _lpCycleWafer.Sum(item => item.Value);
  1095. if (totolCycleWafer != _cycledWafer || _lpCycleCount.Sum(item => item.Value) > 0 && _throughput < 0.01) // refresh _throughput in time
  1096. {
  1097. _cycledWafer = totolCycleWafer;
  1098. if (_cycledCount > 0)
  1099. {
  1100. _throughput = (float)(_cycledWafer / _cycleWatch.Elapsed.TotalHours);
  1101. }
  1102. else
  1103. {
  1104. _throughput = 0;
  1105. }
  1106. }
  1107. if (allControlJobComplete)
  1108. {
  1109. _cycledCount++;
  1110. if (_cycledCount < _cycleSetPoint)
  1111. {
  1112. foreach (var cj in _lstControlJobs)
  1113. {
  1114. cj.SetState(EnumControlJobState.Executing);
  1115. cj.JetState = EnumJetCtrlJobState.Quequed;
  1116. cj.LotInnerId = Guid.NewGuid();
  1117. _dbCallback.LotCreated(cj);
  1118. }
  1119. foreach (var pj in _lstProcessJobs)
  1120. {
  1121. pj.SetState(EnumProcessJobState.Queued);
  1122. pj.InnerId = Guid.NewGuid();
  1123. foreach (var pjSlotWafer in pj.SlotWafers)
  1124. {
  1125. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1126. wafer.ProcessJob = null;
  1127. wafer.NextSequenceStep = 0;
  1128. wafer.ProcessState = EnumWaferProcessStatus.Idle;
  1129. }
  1130. }
  1131. _lstWaferTasks.Clear();
  1132. }
  1133. else
  1134. _cycleState = RState.End;
  1135. }
  1136. }
  1137. foreach (var cj in cjRemoveList)
  1138. {
  1139. List<ProcessJobInfo> pjRemoveList = new List<ProcessJobInfo>();
  1140. foreach (var pj in _lstProcessJobs)
  1141. {
  1142. if (pj.ControlJobName == cj.Name)
  1143. pjRemoveList.Add(pj);
  1144. }
  1145. foreach (var pj in pjRemoveList)
  1146. {
  1147. _lstProcessJobs.Remove(pj);
  1148. }
  1149. _lstControlJobs.Remove(cj);
  1150. }
  1151. }
  1152. private bool ActiveProcessJob(ProcessJobInfo pj)
  1153. {
  1154. foreach (var pjSlotWafer in pj.SlotWafers)
  1155. {
  1156. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1157. wafer.ProcessJob = pj;
  1158. wafer.NextSequenceStep = 0;
  1159. WaferDataRecorder.SetPjInfo(wafer.InnerId.ToString(), pj.InnerId.ToString());
  1160. }
  1161. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == pj.ControlJobName);
  1162. CarrierInfo carrier = CarrierManager.Instance.GetCarrier(cj.Module);
  1163. JobDataRecorder.StartPJ(pj.InnerId.ToString(), carrier.InnerId.ToString(), cj.InnerId.ToString(), pj.Name, cj.Module, cj.Module, pj.SlotWafers.Count);
  1164. pj.SetState(EnumProcessJobState.Processing);
  1165. return true;
  1166. }
  1167. private bool ActiveControlJob(ControlJobInfo cj)
  1168. {
  1169. cj.JetState = EnumJetCtrlJobState.Processing;
  1170. foreach (var pjName in cj.ProcessJobNameList)
  1171. {
  1172. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1173. if (pj == null)
  1174. {
  1175. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Not find pj named {pjName} in {cj.Name}");
  1176. continue;
  1177. }
  1178. if (pj.State == EnumProcessJobState.Queued)
  1179. {
  1180. ActiveProcessJob(pj);
  1181. }
  1182. }
  1183. return true;
  1184. }
  1185. private bool IsAllJobWaferProcessedOrProcessing(ControlJobInfo cj)
  1186. {
  1187. List<ModuleName> allModules = _dictModuleTask.Keys.ToList();
  1188. int original = (int)ModuleHelper.Converter(cj.Module);
  1189. foreach (var mod in allModules)
  1190. {
  1191. if (ModuleHelper.IsLoadPort(mod) && (int)mod != original)
  1192. continue;
  1193. var wafers = WaferManager.Instance.GetWafers(mod);
  1194. foreach (var wafer in wafers)
  1195. {
  1196. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null || wafer.ProcessJob.ControlJobName != cj.Name)
  1197. continue;
  1198. if (wafer.ProcessState != EnumWaferProcessStatus.Completed && wafer.ProcessState != EnumWaferProcessStatus.InProcess)
  1199. return false; ;
  1200. }
  1201. }
  1202. return true;
  1203. }
  1204. private bool IsAllProcessJobComplete(ControlJobInfo cj)
  1205. {
  1206. foreach (var pj in _lstProcessJobs)
  1207. {
  1208. if (pj.ControlJobName == cj.Name && pj.State != EnumProcessJobState.ProcessingComplete)
  1209. {
  1210. return false;
  1211. }
  1212. }
  1213. return true;
  1214. }
  1215. private bool IsCtrlJobRuning(ControlJobInfo cj)
  1216. {
  1217. return cj.JetState == EnumJetCtrlJobState.PreJobClean ||
  1218. cj.JetState == EnumJetCtrlJobState.Processing ||
  1219. cj.JetState == EnumJetCtrlJobState.PostJobClean;
  1220. }
  1221. private bool IsCtrlJobEndingState(ControlJobInfo cj)
  1222. {
  1223. return cj.JetState == EnumJetCtrlJobState.PostJobClean ||
  1224. (cj.JetState == EnumJetCtrlJobState.Processing
  1225. && IsAllJobWaferProcessedOrProcessing(cj)
  1226. && !IsCtrlJobNeedPostClean(cj));
  1227. }
  1228. private bool IsCtrlJobNeedPreClean(ControlJobInfo cj)
  1229. {
  1230. return cj.PreJobClean.Trim().Length > 0;
  1231. }
  1232. private bool IsCtrlJobNeedPostClean(ControlJobInfo cj)
  1233. {
  1234. return cj.PostJobClean.Trim().Length > 0;
  1235. }
  1236. private List<ModuleName> GetWaitPreCleanPMsByCtrlJob(ControlJobInfo cj)
  1237. {
  1238. var pmlist = new List<ModuleName>();
  1239. foreach (var pj in _lstProcessJobs)
  1240. {
  1241. if (pj.ControlJobName == cj.Name)
  1242. {
  1243. foreach (var pm in pj.Sequence.PMs)
  1244. {
  1245. if (!pmlist.Contains(pm))
  1246. pmlist.Add(pm);
  1247. }
  1248. }
  1249. }
  1250. return pmlist;
  1251. }
  1252. private bool IsJobCleanDone(ControlJobInfo cj)
  1253. {
  1254. var pms = GetWaitPreCleanPMsByCtrlJob(cj);
  1255. foreach(var pm in pms)
  1256. {
  1257. var pmTask = _dictModuleTask[pm] as PMTask;
  1258. if (pmTask.JobCleanStatus != ModuleStatus.Idle)
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. private void WaferArrived(WaferTask wafer, MoveItem item)
  1264. {
  1265. _dictModuleTask[item.DestinationModule].WaferArrived(wafer);
  1266. }
  1267. private void WaferLeaved(WaferTask wafer, MoveItem item)
  1268. {
  1269. _dictModuleTask[item.SourceModule].WaferLeaved(wafer);
  1270. }
  1271. private ModuleName GetComingAvailablePM()
  1272. {
  1273. Dictionary<ModuleName, int> pmAssociatedWaferCount = new Dictionary<ModuleName, int>();
  1274. foreach (var mod in _dictModuleTask)
  1275. {
  1276. if (ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsOnline)
  1277. {
  1278. pmAssociatedWaferCount[mod.Key] = 0;
  1279. }
  1280. }
  1281. foreach (var wafer in _lstWaferTasks)
  1282. {
  1283. if (ModuleHelper.IsPm(wafer.destMod) && _dictModuleTask[wafer.destMod].Scheduler.IsOnline )
  1284. {
  1285. pmAssociatedWaferCount[wafer.destMod]++;
  1286. }
  1287. }
  1288. var oderedPMCount = pmAssociatedWaferCount.OrderBy(p => p.Value).ToDictionary(p => p.Key, o => o.Value);
  1289. if (oderedPMCount.First().Value < 2)
  1290. return oderedPMCount.First().Key;
  1291. return ModuleName.System;
  1292. }
  1293. private void CreateNewWaferTask()
  1294. {
  1295. var cj = _lstControlJobs.Find(c => c.JetState == EnumJetCtrlJobState.PreJobClean || c.JetState == EnumJetCtrlJobState.Processing);
  1296. if (cj != null)
  1297. {
  1298. var pm = GetComingAvailablePM();
  1299. if (pm != ModuleName.System)
  1300. {
  1301. foreach (var wafer in cj.LotWafers)
  1302. {
  1303. if (wafer.IsEmpty)
  1304. continue;
  1305. if (wafer.ProcessJob.Sequence.PMs.Contains(pm) && wafer.ProcessState == EnumWaferProcessStatus.Idle && !_lstWaferTasks.Exists(task => task.sourceMod == (ModuleName)wafer.OriginStation && task.sourceSlot == wafer.OriginSlot))
  1306. {
  1307. CreateWaferTasks(wafer, pm);
  1308. return;
  1309. }
  1310. }
  1311. }
  1312. }
  1313. }
  1314. private void CreateWaferTasks(WaferInfo wafer, ModuleName pm)
  1315. {
  1316. var recipeName = wafer.ProcessJob.Sequence.GetRecipe(pm);
  1317. var recipeContent = RecipeFileManager.Instance.LoadRecipe(pm.ToString(), recipeName, false, RecipeType.Process.ToString());
  1318. Recipe recipe = Recipe.Load(recipeContent);
  1319. var waferTask = new WaferTask((ModuleName)wafer.OriginStation,
  1320. wafer.OriginSlot,
  1321. pm,
  1322. 0,
  1323. (float)Convert.ToDouble(recipe.Header.Temperature),
  1324. wafer.InnerId,
  1325. recipeName,
  1326. wafer.ProcessJob.Sequence.WTWCleanRecipe,
  1327. wafer.ProcessJob.Sequence.LLInOutPath, 0);
  1328. waferTask.OnWaferArrived += WaferArrived;
  1329. waferTask.OnWaferLeaved += WaferLeaved;
  1330. _lstWaferTasks.Add(waferTask);
  1331. }
  1332. private bool ForwardATMWafers(ModuleName ll)
  1333. {
  1334. var waferStaus = GetLLReadyInOutSlots(ll);
  1335. var atmWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End ||wafer.movingStatus == RState.Init) && (ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod))).ToList();
  1336. var notAlignedWafer = atmWafers.Where(wafer => !wafer.IsAligned).ToList();
  1337. var readyInWafers = _lstWaferTasks.Where(wafer => ModuleHelper.IsLoadPort(wafer.currentMod) && wafer.movingStatus == RState.Init && wafer.pressureStatus == RState.Init).OrderBy(wafer => wafer.currentSlot).ToArray();
  1338. var freeHands = GetEFEMFreeHand();
  1339. if ((atmWafers.Count >= 2 && atmWafers.Count(wafer => !wafer.IsAligned) == 0) || freeHands.Count == 0 || readyInWafers.Length == 0)
  1340. return false;
  1341. // force all wafer aligned
  1342. if (notAlignedWafer.Count > 0 && _lstWaferTasks.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 0)
  1343. {
  1344. notAlignedWafer.First().RouteTo(ModuleName.Aligner1, 0);
  1345. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(notAlignedWafer.First().currentMod, notAlignedWafer.First().currentSlot, ModuleName.Aligner1, 0, (Hand)notAlignedWafer.First().currentSlot) });
  1346. }
  1347. // forward one wafer
  1348. else if ((readyInWafers.Length > 0) && (atmWafers.Count == 0 || (atmWafers.Count == 1 && _lstWaferTasks.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 0 && waferStaus.emptySlot.Count >= 2)))
  1349. {
  1350. readyInWafers[0].RouteTo(ModuleName.Aligner1, 0);
  1351. // pick from LP
  1352. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyInWafers[0].currentMod, readyInWafers[0].currentSlot, ModuleName.EfemRobot, (int)freeHands[0], freeHands[0]) });
  1353. // place to aligner
  1354. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.EfemRobot, (int)freeHands[0], ModuleName.Aligner1, 0, freeHands[0]) });
  1355. }
  1356. // forward aligner wafer
  1357. else if (atmWafers.Count == 1 && waferStaus.emptySlot.Count >= 2 && ModuleHelper.IsAligner(atmWafers.First().currentMod) && freeHands.Count > 1)
  1358. {
  1359. atmWafers.First().RouteTo(ModuleName.EfemRobot, (int)freeHands[1]);
  1360. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(atmWafers.First().currentMod, atmWafers.First().currentSlot, ModuleName.EfemRobot, (int)freeHands[1], freeHands[1]) });
  1361. }
  1362. return _efemSchdActions.Count > 0;
  1363. }
  1364. private bool ExchangeWaferWithLL(ModuleName ll)
  1365. {
  1366. if (IsLLReservedByTM(ll))
  1367. return false;
  1368. var waferStaus = GetLLReadyInOutSlots(ll);
  1369. var atmWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.IsAligned && (ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod))).ToList();
  1370. var freeHands = GetEFEMFreeHand();
  1371. var swapActions = new List<MoveItem>();
  1372. var validHands = new List<Hand>();
  1373. int placeCount = 0;
  1374. if ( waferStaus.emptySlot.Count > 0 &&
  1375. freeHands.Count >= 1 &&
  1376. atmWafers.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 1 &&
  1377. (atmWafers.Count(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)) == 0 || waferStaus.emptySlot.Count >= 2))
  1378. {
  1379. var alignerWafer = atmWafers.Where(wafer => ModuleHelper.IsAligner(wafer.currentMod)).First();
  1380. if(CanWaferGotoLL(alignerWafer, ll))
  1381. {
  1382. alignerWafer.RouteTo(ll, waferStaus.emptySlot[placeCount]);
  1383. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(alignerWafer.currentMod, alignerWafer.currentSlot, ModuleName.EfemRobot, (int)freeHands[0], freeHands[0]) });
  1384. swapActions.Add(new MoveItem(ModuleName.EfemRobot, (int)freeHands[0], ll, waferStaus.emptySlot[placeCount], freeHands[0]));
  1385. validHands.Add(freeHands[0]);
  1386. placeCount++;
  1387. }
  1388. }
  1389. var armWafers = atmWafers.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToArray();
  1390. foreach(var wafer in armWafers)
  1391. {
  1392. if(CanWaferGotoLL(wafer, ll) && placeCount < waferStaus.emptySlot.Count)
  1393. {
  1394. wafer.RouteTo(ll, waferStaus.emptySlot[placeCount]);
  1395. swapActions.Add(new MoveItem(wafer.currentMod, wafer.currentSlot, ll, waferStaus.emptySlot[placeCount], (Hand)wafer.currentSlot));
  1396. if(!validHands.Contains((Hand)wafer.currentSlot))
  1397. validHands.Add((Hand)wafer.currentSlot);
  1398. placeCount++;
  1399. }
  1400. }
  1401. if (!validHands.Contains(Hand.Blade1) && WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, (int)Hand.Blade1) && _efemRobotSingleArmOption != 2)
  1402. validHands.Add(Hand.Blade1);
  1403. if (!validHands.Contains(Hand.Blade2) && WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, (int)Hand.Blade2) && _efemRobotSingleArmOption != 1)
  1404. validHands.Add(Hand.Blade2);
  1405. int pickCount = 0;
  1406. var returnActions = new List<MoveItem>();
  1407. foreach(var slot in waferStaus.outSlot)
  1408. {
  1409. if(pickCount < validHands.Count)
  1410. {
  1411. var outWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == ll && wafer.currentSlot == slot);
  1412. if(outWafer != null)
  1413. {
  1414. outWafer.RouteTo(outWafer.destMod, outWafer.destSlot);
  1415. swapActions.Add(new MoveItem(ll, waferStaus.outSlot[pickCount], ModuleName.EfemRobot, (int)validHands[pickCount], validHands[pickCount]));
  1416. returnActions.Add(new MoveItem(ModuleName.EfemRobot, (int)validHands[pickCount], outWafer.destMod, outWafer.destSlot, validHands[pickCount]));
  1417. pickCount++;
  1418. }
  1419. }
  1420. }
  1421. if(swapActions.Count > 0)
  1422. {
  1423. _efemSchdActions.Enqueue(swapActions);
  1424. }
  1425. foreach(var action in returnActions)
  1426. {
  1427. _efemSchdActions.Enqueue(new List<MoveItem> { action });
  1428. }
  1429. return _efemSchdActions.Count > 0;
  1430. }
  1431. private void RoutingATMWafers()
  1432. {
  1433. if (_efemSchdActions.Count > 0 || _curEfemAction.Count > 0)
  1434. {
  1435. RunSchdEFEMActions();
  1436. return;
  1437. }
  1438. if (_efemRobotStatus != RState.End)
  1439. return;
  1440. if(_LLASlotNumber == 2)
  1441. {
  1442. Routing2SlotATMSystem();
  1443. }
  1444. else if(_LLASlotNumber == 4)
  1445. {
  1446. Routing4SlotATMSystem();
  1447. }
  1448. }
  1449. private void Routing4SlotATMSystem()
  1450. {
  1451. var atmWaferCount = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod)).Count();
  1452. var efemRobotWafers = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToList();
  1453. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut) // don need check system wafer existence as no blocking risk
  1454. {
  1455. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  1456. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  1457. var inLLWaferStatus = GetLLReadyInOutSlots(inLL);
  1458. var outLLWaferStatus = GetLLReadyInOutSlots(outLL);
  1459. if (efemRobotWafers.Count() >= 1)
  1460. {
  1461. if (inLLWaferStatus.emptySlot.Count >= 2 && !IsLLReservedByTM(inLL)) // wait for in loadlock ready
  1462. {
  1463. if (_dictModuleTask[inLL].TimeToReady < 2)
  1464. {
  1465. ExchangeWaferWithLL(inLL);
  1466. }
  1467. }
  1468. if ((inLLWaferStatus.emptySlot.Count == 0 || (outLLWaferStatus.outSlot.Count >= 1 && _dictModuleTask[outLL].TimeToReady < 2)) && !IsLLReservedByTM(outLL)) // ready return wafer
  1469. {
  1470. ExchangeWaferWithLL(outLL);
  1471. }
  1472. }
  1473. else
  1474. {
  1475. if (((outLLWaferStatus.outSlot.Count >= 2 && _dictModuleTask[outLL].TimeToReady < 10) ||
  1476. (outLLWaferStatus.outSlot.Count == 1 && _dictModuleTask[outLL].TimeToReady < 2)) && !IsLLReservedByTM(outLL))
  1477. {
  1478. ExchangeWaferWithLL(outLL);
  1479. }
  1480. else if (inLLWaferStatus.emptySlot.Count >= 2)
  1481. {
  1482. if (((_dictModuleTask[inLL].TimeToReady > 20 || _dictModuleTask[inLL].Scheduler.IsVac) && atmWaferCount < 2) || atmWaferCount == 0)
  1483. {
  1484. ForwardATMWafers(inLL);
  1485. }
  1486. else if (atmWaferCount >= 2 && !IsLLReservedByTM(inLL))
  1487. {
  1488. ExchangeWaferWithLL(inLL);
  1489. }
  1490. }
  1491. }
  1492. }
  1493. else // must check whole system wafer existance to avoid path blocked
  1494. {
  1495. var vacWaferCount = _lstWaferTasks.Where(task => ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod) || ModuleHelper.IsLoadLock(task.currentMod)).Count();
  1496. var preferWaferVacCount = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsOnline).Count() +
  1497. (LLInOutPath == SequenceLLInOutPath.AInAOut ? _LLASlotNumber / 2 : (LLInOutPath == SequenceLLInOutPath.BInBOut ? _LLBSlotNumber / 2 : _LLASlotNumber));
  1498. var inOutIndicator = vacWaferCount - preferWaferVacCount;
  1499. if (inOutIndicator < 0)
  1500. {
  1501. // ready to push in
  1502. var readyLLs = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 10 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).emptySlot.Count > 1).OrderBy(mod => mod.Value.TimeToReady).ToList();
  1503. if (readyLLs.Count > 0 && atmWaferCount >= 1)
  1504. {
  1505. ExchangeWaferWithLL(readyLLs.First().Key);
  1506. }
  1507. // forward wafer to system
  1508. else if (atmWaferCount < 2)
  1509. {
  1510. ForwardATMWafers(ModuleName.LLA);
  1511. }
  1512. if (_efemSchdActions.Count == 0)
  1513. {
  1514. var readyReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 2 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).outSlot.Count >= 1).OrderByDescending(mod => GetLLReadyInOutSlots(mod.Key).outSlot.Count).ToList();
  1515. if (readyReturnLL.Count > 0)
  1516. {
  1517. ExchangeWaferWithLL(readyReturnLL.First().Key);
  1518. }
  1519. }
  1520. }
  1521. else
  1522. {
  1523. // ready double return LL
  1524. var readyDoubleReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 10 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).outSlot.Count > 1).OrderBy(mod => mod.Value.TimeToReady).ToList();
  1525. if (readyDoubleReturnLL.Count > 0)
  1526. {
  1527. ExchangeWaferWithLL(readyDoubleReturnLL.First().Key);
  1528. }
  1529. // ready single return LL
  1530. var readySingleReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 2 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).outSlot.Count == 1).OrderBy(mod => mod.Value.TimeToReady).ToList();
  1531. if (readySingleReturnLL.Count > 0)
  1532. {
  1533. ExchangeWaferWithLL(readySingleReturnLL.First().Key);
  1534. }
  1535. if (atmWaferCount == 0 || _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 15).Count() == 0)
  1536. {
  1537. ForwardATMWafers(ModuleName.LLA);
  1538. }
  1539. }
  1540. }
  1541. }
  1542. private void Routing2SlotATMSystem()
  1543. {
  1544. var atmWaferCount = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod)).Count();
  1545. var efemRobotWafers = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToList();
  1546. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut) // don need check system wafer existence as no blocking risk
  1547. {
  1548. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  1549. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  1550. var inLLWaferStatus = GetLLReadyInOutSlots(inLL);
  1551. var outLLWaferStatus = GetLLReadyInOutSlots(outLL);
  1552. // whether can do double return in 5 second, single return with single arm option or LL under atm pressure
  1553. if ((outLLWaferStatus.outSlot.Count == 2 && _dictModuleTask[outLL].TimeToReady < 5 && (atmWaferCount < 2 || !_dictModuleTask[outLL].Scheduler.IsAtm)) ||
  1554. (outLLWaferStatus.outSlot.Count == 1 && ((_dictModuleTask[outLL].TimeToReady < 2 && _dictModuleTask[outLL].Scheduler.IsAtm)
  1555. || _efemRobotSingleArmOption != 0
  1556. || _lstWaferTasks.Where(wt => ModuleHelper.IsLoadPort(wt.destMod)).Count() == 1)))
  1557. {
  1558. if (ExchangeWaferWithLL(outLL))
  1559. return;
  1560. }
  1561. if(atmWaferCount == 0 || (atmWaferCount == 1 && inLLWaferStatus.emptySlot.Count == 2 && _efemRobotSingleArmOption == 0 && (outLLWaferStatus.outSlot.Count < 2 || _dictModuleTask[outLL].TimeToReady > 20)))
  1562. {
  1563. if (ForwardATMWafers(inLL))
  1564. return;
  1565. }
  1566. if((inLLWaferStatus.emptySlot.Count == 2 || (inLLWaferStatus.emptySlot.Count == 1 && _efemRobotSingleArmOption != 0) && _dictModuleTask[inLL].TimeToReady < 2))
  1567. {
  1568. ExchangeWaferWithLL(inLL);
  1569. }
  1570. }
  1571. else // must check whole system wafer existance to avoid path blocked
  1572. {
  1573. var vacWaferCount = _lstWaferTasks.Where(task => ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod) || ModuleHelper.IsLoadLock(task.currentMod)).Count();
  1574. var preferWaferVacCount = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsOnline).Count() +
  1575. (LLInOutPath == SequenceLLInOutPath.AInAOut ? _LLASlotNumber / 2 : (LLInOutPath == SequenceLLInOutPath.BInBOut ? _LLBSlotNumber / 2 : _LLASlotNumber));
  1576. var inOutIndicator = vacWaferCount - preferWaferVacCount;
  1577. if (inOutIndicator < 0)
  1578. {
  1579. // ready to push in
  1580. var readyLLs = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 10 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).emptySlot.Count > 1).OrderBy(mod => mod.Value.TimeToReady).ToList();
  1581. if (readyLLs.Count > 0 && atmWaferCount >= 1)
  1582. {
  1583. ExchangeWaferWithLL(readyLLs.First().Key);
  1584. }
  1585. // forward wafer to system
  1586. else if (atmWaferCount < 2)
  1587. {
  1588. ForwardATMWafers(ModuleName.LLA);
  1589. }
  1590. if (_efemSchdActions.Count == 0)
  1591. {
  1592. var readyReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 2 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).outSlot.Count >= 1).OrderByDescending(mod => GetLLReadyInOutSlots(mod.Key).outSlot.Count).ToList();
  1593. if (readyReturnLL.Count > 0)
  1594. {
  1595. ExchangeWaferWithLL(readyReturnLL.First().Key);
  1596. }
  1597. }
  1598. }
  1599. else
  1600. {
  1601. // ready double return LL
  1602. var readyDoubleReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 10 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).outSlot.Count > 1).OrderBy(mod => mod.Value.TimeToReady).ToList();
  1603. if (readyDoubleReturnLL.Count > 0)
  1604. {
  1605. ExchangeWaferWithLL(readyDoubleReturnLL.First().Key);
  1606. }
  1607. // ready single return LL
  1608. var readySingleReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 2 && !IsLLReservedByTM(mod.Key) && GetLLReadyInOutSlots(mod.Key).outSlot.Count == 1).OrderBy(mod => mod.Value.TimeToReady).ToList();
  1609. if (readySingleReturnLL.Count > 0)
  1610. {
  1611. ExchangeWaferWithLL(readySingleReturnLL.First().Key);
  1612. }
  1613. if (atmWaferCount == 0 || _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.TimeToReady < 15).Count() == 0)
  1614. {
  1615. ForwardATMWafers(ModuleName.LLA);
  1616. }
  1617. }
  1618. }
  1619. }
  1620. private bool isReturnActionsDone(List<MoveItem> items)
  1621. {
  1622. foreach(var item in items)
  1623. {
  1624. if (WaferManager.Instance.CheckHasWafer(item.SourceModule, item.SourceSlot) || WaferManager.Instance.CheckNoWafer(item.DestinationModule, item.DestinationSlot))
  1625. return false;
  1626. }
  1627. return true;
  1628. }
  1629. private bool IsMovingActionsDone(List<MoveItem> actions)
  1630. {
  1631. foreach(var ac in actions)
  1632. {
  1633. var task = _lstWaferTasks.Find(wafer => (wafer.currentMod == ac.SourceModule && wafer.currentSlot == ac.SourceSlot) || (wafer.currentMod == ac.DestinationModule && wafer.currentSlot == ac.DestinationSlot));
  1634. if (task != null && task.movingStatus == RState.Running)
  1635. return false; ;
  1636. }
  1637. return true;
  1638. }
  1639. private bool IsLLReservedByTM(ModuleName ll)
  1640. {
  1641. foreach(var item in _curTmAction)
  1642. {
  1643. if (item.DestinationModule == ll || item.SourceModule == ll)
  1644. return true;
  1645. }
  1646. foreach(var action in _tmSchdActions)
  1647. {
  1648. foreach(var item in action)
  1649. {
  1650. if (item.DestinationModule == ll || item.SourceModule == ll)
  1651. return true;
  1652. }
  1653. }
  1654. return false;
  1655. }
  1656. private bool IsLLReservedByEFEM(ModuleName ll)
  1657. {
  1658. foreach (var item in _curEfemAction)
  1659. {
  1660. if (item.DestinationModule == ll || item.SourceModule == ll)
  1661. return true;
  1662. }
  1663. foreach (var action in _efemSchdActions)
  1664. {
  1665. foreach (var item in action)
  1666. {
  1667. if (item.DestinationModule == ll || item.SourceModule == ll)
  1668. return true;
  1669. }
  1670. }
  1671. return false;
  1672. }
  1673. private void RunSchdEFEMActions()
  1674. {
  1675. var efemRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot) as SchedulerEfemRobot;
  1676. if (efemRobot == null || !efemRobot.IsAvailable)
  1677. return;
  1678. if (_dictModuleTask[ModuleName.EfemRobot].IsIdle)
  1679. {
  1680. if(_efemSchdActions.Count > 0 )
  1681. {
  1682. if(_curEfemAction.Count == 0 || IsMovingActionsDone(_curEfemAction))
  1683. {
  1684. var nextActions = _efemSchdActions.First();
  1685. if (nextActions.Exists(action => !_lstWaferTasks.Exists(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot)))
  1686. return;
  1687. var nextModule = nextActions.First().Module;
  1688. if (ModuleHelper.IsLoadLock(nextModule) && !_dictModuleTask[nextModule].IsIdle)
  1689. return;
  1690. _curEfemAction = _efemSchdActions.Dequeue();
  1691. foreach (var action in _curEfemAction)
  1692. {
  1693. var waferTask = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot);
  1694. waferTask.MoveTo(action.DestinationModule, action.DestinationSlot);
  1695. }
  1696. (_dictModuleTask[ModuleName.EfemRobot].Scheduler as SchedulerEfemRobot).PostMoveItems(_curEfemAction.ToArray());
  1697. }
  1698. }
  1699. else if(_curEfemAction.Count >= 0 && IsMovingActionsDone(_curEfemAction)) // all scheduled actions done
  1700. {
  1701. _curEfemAction.Clear();
  1702. }
  1703. }
  1704. }
  1705. private void RunSchdTMActions()
  1706. {
  1707. if (_dictModuleTask[ModuleName.TMRobot].IsIdle)
  1708. {
  1709. if (_tmSchdActions.Count > 0)
  1710. {
  1711. if (_curTmAction.Count == 0 || IsMovingActionsDone(_curTmAction))
  1712. {
  1713. var nextActions = _tmSchdActions.First();
  1714. if (nextActions.Exists(action => !_lstWaferTasks.Exists(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot)))
  1715. return;
  1716. _curTmAction = _tmSchdActions.Dequeue();
  1717. foreach(var action in _curTmAction)
  1718. {
  1719. var waferTask = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot);
  1720. waferTask.MoveTo(action.DestinationModule, action.DestinationSlot);
  1721. }
  1722. (_dictModuleTask[ModuleName.TMRobot].Scheduler as SchedulerTMRobot).SendMoveItems(_curTmAction.ToArray());
  1723. }
  1724. }
  1725. else if (_curTmAction.Count >= 0 && IsMovingActionsDone(_curTmAction)) // all scheduled actions done
  1726. {
  1727. _curTmAction.Clear();
  1728. }
  1729. }
  1730. }
  1731. private int TimeForNextModuleReady(WaferTask task)
  1732. {
  1733. if (ModuleHelper.IsPm(task.destMod) && (ModuleHelper.IsLoadLock(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod)))
  1734. return _dictModuleTask[task.destMod].TimeToReady;
  1735. var llbWaferStatus = GetLLReadyInOutSlots(ModuleName.LLB);
  1736. var llaWaferStatus = GetLLReadyInOutSlots(ModuleName.LLA);
  1737. if (ModuleHelper.IsLoadPort(task.destMod) && (ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod)))
  1738. {
  1739. if (task.llInOutPath == SequenceLLInOutPath.AInBOut || task.llInOutPath == SequenceLLInOutPath.BInBOut)
  1740. {
  1741. if(llbWaferStatus.emptySlot.Count > 0)
  1742. {
  1743. return _dictModuleTask[ModuleName.LLB].TimeToReady;
  1744. }
  1745. }
  1746. else if(task.llInOutPath == SequenceLLInOutPath.BInAOut || task.llInOutPath == SequenceLLInOutPath.AInAOut)
  1747. {
  1748. if(llaWaferStatus.emptySlot.Count > 0)
  1749. {
  1750. return _dictModuleTask[ModuleName.LLA].TimeToReady;
  1751. }
  1752. }
  1753. else if(task.llInOutPath == SequenceLLInOutPath.DInDOut)
  1754. {
  1755. if(llaWaferStatus.emptySlot.Count > 0 && llbWaferStatus.emptySlot.Count > 0)
  1756. {
  1757. if (_dictModuleTask[ModuleName.LLA].TimeToReady == _dictModuleTask[ModuleName.LLB].TimeToReady)
  1758. {
  1759. return _dictModuleTask[ModuleName.LLA].TimeToReady;
  1760. }
  1761. else
  1762. {
  1763. return Math.Min(_dictModuleTask[ModuleName.LLA].TimeToReady, _dictModuleTask[ModuleName.LLB].TimeToReady);
  1764. }
  1765. }
  1766. else if(llaWaferStatus.emptySlot.Count > 0)
  1767. {
  1768. return _dictModuleTask[ModuleName.LLA].TimeToReady;
  1769. }
  1770. else if(llbWaferStatus.emptySlot.Count > 0)
  1771. {
  1772. return _dictModuleTask[ModuleName.LLB].TimeToReady;
  1773. }
  1774. }
  1775. }
  1776. else if(ModuleHelper.IsPm(task.destMod) && (ModuleHelper.IsLoadLock(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod)))
  1777. {
  1778. if(_lstWaferTasks.Find(wafer => wafer.currentMod == task.destMod || wafer.routedMod == task.destMod) == null)
  1779. {
  1780. return _dictModuleTask[task.destMod].TimeToReady;
  1781. }
  1782. }
  1783. return int.MaxValue;
  1784. }
  1785. List<Hand> GetTMValidFreeHands(List<WaferTask> robotWafers, List<MoveItem> swapActions)
  1786. {
  1787. var hands = new List<Hand>();
  1788. for(int i = 0; i < 2; i++)
  1789. {
  1790. if(_tmRobotSingleArmOption == 0 || _tmRobotSingleArmOption == i + 1)
  1791. {
  1792. if ((!robotWafers.Exists(wafer => wafer.currentSlot == i) ||
  1793. ( robotWafers.Exists(wafer => wafer.currentSlot == i) &&
  1794. swapActions.Exists(item => item.SourceModule == ModuleName.TMRobot && item.SourceSlot == i))) &&
  1795. !swapActions.Exists(item => item.DestinationModule == ModuleName.TMRobot && item.DestinationSlot == i))
  1796. hands.Add((Hand)i);
  1797. }
  1798. }
  1799. return hands;
  1800. }
  1801. (ModuleName llPath, List<MoveItem> swapActions) FindBestLLSwapPlan(ModuleName ll, List<WaferTask> robotWafers)
  1802. {
  1803. ModuleName path = ll == ModuleName.System ? ModuleName.System : ll;
  1804. List<ModuleName> lls = ll == ModuleName.System ? new List<ModuleName> { ModuleName.LLA, ModuleName.LLB } : new List<ModuleName> { ll };
  1805. List<MoveItem> actions = new List<MoveItem>();
  1806. Dictionary<ModuleName, List<MoveItem>> llSwapActions = new Dictionary<ModuleName, List<MoveItem>>();
  1807. foreach (var loadlock in lls)
  1808. {
  1809. llSwapActions[loadlock] = new List<MoveItem>();
  1810. if (IsLLReservedByEFEM(loadlock))
  1811. continue;
  1812. var llWaferStatus = GetLLReadyInOutSlots(loadlock);
  1813. foreach(var wafer in robotWafers)
  1814. {
  1815. if(_dictModuleTask[loadlock].TimeToReady == 0 && !IsLLReservedByEFEM(loadlock) && CanWaferGotoLL(wafer,loadlock) && llWaferStatus.emptySlot.Count > llSwapActions[loadlock].Count)
  1816. {
  1817. llSwapActions[loadlock].Add(new MoveItem(ModuleName.TMRobot, wafer.currentSlot, loadlock, llWaferStatus.emptySlot[llSwapActions[loadlock].Count], (Hand)wafer.currentSlot));
  1818. }
  1819. }
  1820. int prePickTime = 20;
  1821. int prePickCount = 0;
  1822. var hands = GetTMValidFreeHands(robotWafers, llSwapActions[loadlock]);
  1823. foreach (var slot in llWaferStatus.inSlot)
  1824. {
  1825. var wafer = _lstWaferTasks.Find(task => task.currentMod == loadlock && task.currentSlot == slot);
  1826. if (wafer != null && _dictModuleTask[wafer.destMod].TimeToReady <= prePickTime && _dictModuleTask[wafer.destMod].Scheduler.IsOnline && hands.Count > prePickCount)
  1827. {
  1828. if(_lstWaferTasks.Exists(task => task.currentMod == wafer.destMod))
  1829. {
  1830. if( hands.Count == 2 && prePickCount == 0 && (_dictModuleTask.Count(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.TimeToReady < prePickTime && WaferManager.Instance.CheckNoWafer(mod.Key, 0)) == 0 || llWaferStatus.inSlot.Count == 1))
  1831. {
  1832. llSwapActions[loadlock].Add(new MoveItem(loadlock, slot, ModuleName.TMRobot, (int)hands[prePickCount], hands[prePickCount]));
  1833. prePickCount++;
  1834. break;
  1835. }
  1836. }
  1837. else
  1838. {
  1839. llSwapActions[loadlock].Add(new MoveItem(loadlock, slot, ModuleName.TMRobot, (int)hands[prePickCount], hands[prePickCount]));
  1840. prePickCount++;
  1841. }
  1842. }
  1843. }
  1844. }
  1845. var validateActions = llSwapActions.Where(item => item.Value.Count > 0).ToDictionary(k => k.Key, v => v.Value);
  1846. if(validateActions.Count == 1)
  1847. {
  1848. (path, actions) = (validateActions.First().Key, validateActions.First().Value);
  1849. }
  1850. else if(validateActions.Count == 2)
  1851. {
  1852. var sortedActions = validateActions.OrderByDescending(item => item.Value.Count + (_dictModuleTask[item.Key].Scheduler.IsVac ? 2 : 0)).ToDictionary(k => k.Key, v => v.Value);
  1853. (path, actions) = (sortedActions.First().Key, sortedActions.First().Value);
  1854. }
  1855. return (path, actions);
  1856. }
  1857. (List<WaferTask> pmWafers, ModuleName destLL, List<MoveItem> swapAction) FindBestReturnWafersPlan(Hand[] freeHands)
  1858. {
  1859. Dictionary<ModuleName, List<WaferTask>> returnWafers = new Dictionary<ModuleName, List<WaferTask>>();
  1860. Dictionary<ModuleName, List<MoveItem>> llSwapActions = new Dictionary<ModuleName, List<MoveItem>>();
  1861. Dictionary<ModuleName, int> llValues = new Dictionary<ModuleName, int>();
  1862. foreach(var ll in new List<ModuleName> { ModuleName.LLA, ModuleName.LLB})
  1863. {
  1864. var llWaferStatus = GetLLReadyInOutSlots(ll);
  1865. returnWafers[ll] = new List<WaferTask>();
  1866. llSwapActions[ll] = new List<MoveItem>();
  1867. llValues[ll] = 0;
  1868. if (IsLLReservedByEFEM(ll))
  1869. continue;
  1870. var readyReturnWafers = _lstWaferTasks.Where( wafer => ModuleHelper.IsPm(wafer.currentMod) &&
  1871. ModuleHelper.IsLoadPort(wafer.destMod) &&
  1872. wafer.movingStatus == RState.End &&
  1873. _dictModuleTask[wafer.currentMod].TimeToReady <= 2 &&
  1874. CanWaferGotoLL(wafer, ll)).Take(Math.Min(freeHands.Length, llWaferStatus.emptySlot.Count)).ToList();
  1875. int prePickTime = 10 * llSwapActions[ll].Count;
  1876. int handIndex = 0;
  1877. if(_dictModuleTask[ll].TimeToReady <= prePickTime)
  1878. {
  1879. foreach (var wafer in readyReturnWafers)
  1880. {
  1881. if(handIndex < freeHands.Length)
  1882. {
  1883. returnWafers[ll].Add(wafer);
  1884. llSwapActions[ll].Add(new MoveItem(ModuleName.TMRobot, (int)freeHands[handIndex], ll, llWaferStatus.emptySlot[handIndex], freeHands[handIndex]));
  1885. handIndex++;
  1886. }
  1887. }
  1888. int prePickCount = 0;
  1889. handIndex = 0;
  1890. foreach (var slot in llWaferStatus.inSlot)
  1891. {
  1892. var wafer = _lstWaferTasks.Find(task => task.currentMod == ll && task.currentSlot == slot);
  1893. if (wafer != null && _dictModuleTask[wafer.destMod].TimeToReady <= prePickTime && _dictModuleTask[wafer.destMod].Scheduler.IsOnline && freeHands.Length > prePickCount)
  1894. {
  1895. if (_lstWaferTasks.Exists(task => task.currentMod == wafer.destMod))
  1896. {
  1897. if (freeHands.Length == 2 && prePickCount == 0 && (_dictModuleTask.Count(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.TimeToReady <= prePickTime && WaferManager.Instance.CheckNoWafer(mod.Key, 0)) == 0 || llWaferStatus.inSlot.Count == 1))
  1898. {
  1899. llSwapActions[ll].Add(new MoveItem(ll, slot, ModuleName.TMRobot, (int)freeHands[prePickCount], freeHands[prePickCount]));
  1900. prePickCount++;
  1901. break;
  1902. }
  1903. }
  1904. else
  1905. {
  1906. llSwapActions[ll].Add(new MoveItem(ll, slot, ModuleName.TMRobot, (int)freeHands[prePickCount], freeHands[prePickCount]));
  1907. prePickCount++;
  1908. }
  1909. }
  1910. }
  1911. llValues[ll] = returnWafers[ll].Count * 2 + llSwapActions[ll].Count + (_dictModuleTask[ll].Scheduler.IsVac ? 3 : 0);
  1912. }
  1913. }
  1914. var preferLL = llValues.OrderByDescending(v => v.Value).First().Key;
  1915. return (returnWafers[preferLL], preferLL, llSwapActions[preferLL]);
  1916. }
  1917. void RoutingTMSwapActions(List<MoveItem> actions)
  1918. {
  1919. foreach (var action in actions)
  1920. {
  1921. if (ModuleHelper.IsTMRobot(action.SourceModule))
  1922. {
  1923. var waferTask = _lstWaferTasks.Find(wafer => wafer.currentMod == ModuleName.TMRobot && wafer.currentSlot == action.SourceSlot);
  1924. waferTask.RouteTo(action.DestinationModule, action.DestinationSlot);
  1925. }
  1926. else
  1927. {
  1928. var waferTask = _lstWaferTasks.Find(wafer => wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot);
  1929. waferTask.RouteTo(action.DestinationModule, action.DestinationSlot);
  1930. }
  1931. }
  1932. }
  1933. private bool ManaulReturnPMWafer()
  1934. {
  1935. var mReturnWafer = _qeReturnWafers.Peek();
  1936. if (ModuleHelper.IsPm((ModuleName)mReturnWafer.Station))
  1937. {
  1938. var hands = GetTMValidFreeHands(_lstWaferTasks.Where(wt => ModuleHelper.IsTMRobot(wt.currentMod)).ToList(), new List<MoveItem>());
  1939. var waferTask = _lstWaferTasks.Find(wt => wt.currentMod == (ModuleName)mReturnWafer.Station && wt.waferId == mReturnWafer.InnerId);
  1940. if (waferTask != null && hands.Count > 0)
  1941. {
  1942. waferTask.Return();
  1943. var outLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && CanWaferGotoLL(waferTask, mod.Key) && GetLLReadyInOutSlots(mod.Key).emptySlot.Count > 0 && !IsLLReservedByEFEM(mod.Key));
  1944. if (outLL.Count() > 0)
  1945. {
  1946. var slot = GetLLReadyInOutSlots(outLL.First().Key).emptySlot.First();
  1947. waferTask.RouteTo(outLL.First().Key, slot);
  1948. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(waferTask.currentMod, 0, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  1949. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), outLL.First().Key, slot, hands.First()) });
  1950. _qeReturnWafers.Dequeue();
  1951. return true;
  1952. }
  1953. }
  1954. }
  1955. return false;
  1956. }
  1957. private void RoutingVacWafers()
  1958. {
  1959. if(_tmSchdActions.Count > 0 || _curTmAction.Count > 0)
  1960. {
  1961. RunSchdTMActions();
  1962. return;
  1963. }
  1964. if(_tmRobotStatus == RState.End)
  1965. {
  1966. // handle manual return wafer if exist
  1967. if(_qeReturnWafers.Count > 0)
  1968. {
  1969. if (ManaulReturnPMWafer())
  1970. return;
  1971. }
  1972. if(_LLASlotNumber == 2)
  1973. {
  1974. Routing2SlotVacSystem();
  1975. }
  1976. else if(_LLASlotNumber == 4)
  1977. {
  1978. Routing4SlotVacSystem();
  1979. }
  1980. }
  1981. }
  1982. private void Routing2SlotVacSystem()
  1983. {
  1984. var robotWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && ModuleHelper.IsTMRobot(wafer.currentMod)).OrderBy(wafer => TimeForNextModuleReady(wafer)).ToList();
  1985. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut)
  1986. {
  1987. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  1988. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  1989. var inLLWaferStatus = GetLLReadyInOutSlots(inLL);
  1990. var outLLWaferStatus = GetLLReadyInOutSlots(outLL);
  1991. if(robotWafers.Count == 2)
  1992. {
  1993. int returnCount = 0;
  1994. foreach (var wafer in robotWafers)
  1995. {
  1996. if (ModuleHelper.IsPm(wafer.destMod))
  1997. {
  1998. if (_dictModuleTask[wafer.destMod].TimeToReady == 0 &&
  1999. !_lstWaferTasks.Exists(waferT => waferT.currentMod == wafer.destMod) &&
  2000. !_lstWaferTasks.Exists(waferT => waferT.routedMod == wafer.destMod))
  2001. {
  2002. wafer.RouteTo(wafer.destMod, 0);
  2003. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  2004. }
  2005. }
  2006. else
  2007. {
  2008. if (_dictModuleTask[outLL].TimeToReady <= 5 && !IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > returnCount)
  2009. {
  2010. wafer.RouteTo(outLL, outLLWaferStatus.emptySlot[returnCount]);
  2011. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, outLL, outLLWaferStatus.emptySlot[returnCount], (Hand)wafer.currentSlot) });
  2012. returnCount++;
  2013. }
  2014. }
  2015. }
  2016. }
  2017. else if(robotWafers.Count == 1)
  2018. {
  2019. int freeHand = 1 - robotWafers.First().currentSlot;
  2020. if (ModuleHelper.IsPm(robotWafers.First().destMod))
  2021. {
  2022. if(_dictModuleTask[robotWafers.First().destMod].TimeToReady <= 5)
  2023. {
  2024. if(_lstWaferTasks.Exists(waferT => waferT.currentMod == robotWafers.First().destMod))
  2025. {
  2026. var pmActions = new List<MoveItem>();
  2027. var pmWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == robotWafers.First().destMod);
  2028. pmWafer.RouteTo(ModuleName.TMRobot, freeHand);
  2029. pmActions.Add(new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, freeHand, (Hand)freeHand));
  2030. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, pmWafer.currentMod, 0, (Hand)robotWafers.First().currentSlot));
  2031. _tmSchdActions.Enqueue(pmActions);
  2032. }
  2033. else
  2034. {
  2035. robotWafers.First().RouteTo(robotWafers.First().destMod, 0);
  2036. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, robotWafers.First().destMod, 0, (Hand)robotWafers.First().currentSlot) });
  2037. }
  2038. }
  2039. }
  2040. else
  2041. {
  2042. if (_dictModuleTask[outLL].TimeToReady <= 5 && !IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > 0)
  2043. {
  2044. robotWafers.First().RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2045. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, outLL, outLLWaferStatus.emptySlot[0], (Hand)robotWafers.First().currentSlot) });
  2046. }
  2047. }
  2048. if (_tmSchdActions.Count == 0 && _tmRobotSingleArmOption == 0)
  2049. {
  2050. // try push in one wafer
  2051. if (!IsLLReservedByEFEM(inLL) && _dictModuleTask[inLL].Scheduler.IsVac && inLLWaferStatus.inSlot.Count > 0)
  2052. {
  2053. foreach (var slot in inLLWaferStatus.inSlot)
  2054. {
  2055. var llWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == inLL && wafer.currentSlot == slot);
  2056. if (llWafer != null && !_lstWaferTasks.Exists(wt => wt.currentMod == llWafer.destMod) && _dictModuleTask[llWafer.destMod].TimeToReady < 15)
  2057. {
  2058. llWafer.RouteTo(llWafer.destMod, 0);
  2059. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(inLL, slot, ModuleName.TMRobot, freeHand, (Hand)freeHand) });
  2060. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, freeHand, llWafer.destMod, 0, (Hand)freeHand) });
  2061. return;
  2062. }
  2063. }
  2064. }
  2065. // try to return one wafer
  2066. if (!IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > 0)
  2067. {
  2068. var reayReturnWafer = _lstWaferTasks.Find(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod) && _dictModuleTask[wt.currentMod].TimeToReady < 2);
  2069. if (reayReturnWafer != null)
  2070. {
  2071. reayReturnWafer.RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2072. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(reayReturnWafer.currentMod, 0, ModuleName.TMRobot, freeHand, (Hand)freeHand) });
  2073. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, freeHand, outLL, outLLWaferStatus.emptySlot[0], (Hand)freeHand) });
  2074. return;
  2075. }
  2076. }
  2077. }
  2078. }
  2079. else // no wafer on robot arm
  2080. {
  2081. var readyInPms = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && !_lstWaferTasks.Exists(wt => wt.currentMod == mod.Key) && mod.Value.Scheduler.IsOnline && mod.Value.TimeToReady < 15);
  2082. var readyOutWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod) && _dictModuleTask[wt.currentMod].TimeToReady < 2).ToList() ;
  2083. if(_tmRobotSingleArmOption != 0)
  2084. {
  2085. var hands = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2086. if (hands.Count == 0)
  2087. return;
  2088. if(!IsLLReservedByEFEM(inLL) && _dictModuleTask[inLL].TimeToReady <= 5 && inLLWaferStatus.inSlot.Count > 0 && readyInPms.Count() > 0)
  2089. {
  2090. foreach(var pm in readyInPms)
  2091. {
  2092. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && inLLWaferStatus.inSlot.Contains(wt.currentSlot) && wt.destMod == pm.Key);
  2093. if(llWafer != null)
  2094. {
  2095. llWafer.RouteTo(llWafer.destMod, 0);
  2096. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(inLL, llWafer.currentSlot, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  2097. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), llWafer.destMod, 0, hands.First()) });
  2098. return;
  2099. }
  2100. }
  2101. }
  2102. if(readyOutWafers.Count > 0 && !IsLLReservedByEFEM(outLL) && _dictModuleTask[outLL].TimeToReady < 50 && outLLWaferStatus.emptySlot.Count > 0)
  2103. {
  2104. var returnWafer = readyOutWafers.First();
  2105. returnWafer.RouteTo(outLL, outLLWaferStatus.emptySlot.First());
  2106. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(returnWafer.currentMod, 0, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  2107. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), outLL, outLLWaferStatus.emptySlot.First(), hands.First()) });
  2108. return;
  2109. }
  2110. return;
  2111. }
  2112. if(!IsLLReservedByEFEM(inLL) && _dictModuleTask[inLL].TimeToReady <= 5 && inLLWaferStatus.inSlot.Count > 0)
  2113. {
  2114. if(inLLWaferStatus.inSlot.Count == 2)
  2115. {
  2116. if(readyInPms.Count() > 0) // double move from LL to PM
  2117. {
  2118. int pickCount = 0;
  2119. var swapActions = new List<MoveItem>();
  2120. var singleActions = new List<MoveItem>();
  2121. foreach(var pm in readyInPms)
  2122. {
  2123. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && inLLWaferStatus.inSlot.Contains(wt.currentSlot) && wt.destMod == pm.Key);
  2124. if(llWafer != null)
  2125. {
  2126. llWafer.RouteTo(pm.Key, 0);
  2127. swapActions.Add(new MoveItem(inLL, llWafer.currentSlot, ModuleName.TMRobot, pickCount, (Hand)pickCount));
  2128. singleActions.Add(new MoveItem(ModuleName.TMRobot, pickCount, pm.Key, 0, (Hand)pickCount));
  2129. pickCount++;
  2130. }
  2131. }
  2132. if(pickCount == 1)
  2133. {
  2134. int remainSlot = swapActions.First().SourceSlot == inLLWaferStatus.inSlot[0] ? inLLWaferStatus.inSlot[1] : inLLWaferStatus.inSlot[0];
  2135. swapActions.Add(new MoveItem(inLL, remainSlot, ModuleName.TMRobot, 1, Hand.Blade2));
  2136. }
  2137. if(swapActions.Count > 0)
  2138. {
  2139. _tmSchdActions.Enqueue(swapActions);
  2140. foreach(var ac in singleActions)
  2141. {
  2142. _tmSchdActions.Enqueue(new List<MoveItem> { ac });
  2143. }
  2144. }
  2145. }
  2146. else
  2147. {
  2148. foreach(var slot in inLLWaferStatus.inSlot)
  2149. {
  2150. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && wt.currentSlot == slot);
  2151. if (llWafer != null)
  2152. {
  2153. if(!_lstWaferTasks.Exists(wt => wt.currentMod == llWafer.destMod) && _dictModuleTask[llWafer.destMod].Scheduler.IsOnline)
  2154. {
  2155. var swapActions = new List<MoveItem>();
  2156. swapActions.Add(new MoveItem(inLL, slot, ModuleName.TMRobot, 0, Hand.Blade1));
  2157. int remainSlot = slot == inLLWaferStatus.inSlot[0] ? inLLWaferStatus.inSlot[1] : inLLWaferStatus.inSlot[0];
  2158. swapActions.Add(new MoveItem(inLL, remainSlot, ModuleName.TMRobot, 1, Hand.Blade2));
  2159. _tmSchdActions.Enqueue(swapActions);
  2160. if (_dictModuleTask[llWafer.destMod].TimeToReady <= 30)
  2161. {
  2162. llWafer.RouteTo(llWafer.destMod, 0);
  2163. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, 0, llWafer.destMod, 0, Hand.Blade1) });
  2164. }
  2165. return;
  2166. }
  2167. }
  2168. }
  2169. }
  2170. }
  2171. else
  2172. {
  2173. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && wt.currentSlot == inLLWaferStatus.inSlot.First());
  2174. if (llWafer != null)
  2175. {
  2176. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(inLL, inLLWaferStatus.inSlot.First(), ModuleName.TMRobot, 0, Hand.Blade1) });
  2177. if (!_lstWaferTasks.Exists(wt => wt.currentMod == llWafer.destMod) && _dictModuleTask[llWafer.destMod].TimeToReady < 15)
  2178. {
  2179. llWafer.RouteTo(llWafer.destMod, 0);
  2180. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, 0, llWafer.destMod, 0, Hand.Blade1) });
  2181. }
  2182. }
  2183. }
  2184. }
  2185. if (_tmSchdActions.Count > 0)
  2186. return;
  2187. if(!IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > 0 && readyOutWafers.Count > 0)
  2188. {
  2189. if(readyOutWafers.Count >= 2 && outLLWaferStatus.emptySlot.Count == 2)
  2190. {
  2191. readyOutWafers[0].RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2192. readyOutWafers[1].RouteTo(outLL, outLLWaferStatus.emptySlot[1]);
  2193. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyOutWafers[0].currentMod, readyOutWafers[0].currentSlot, ModuleName.TMRobot, 0, Hand.Blade1) });
  2194. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyOutWafers[1].currentMod, readyOutWafers[1].currentSlot, ModuleName.TMRobot, 1, Hand.Blade2) });
  2195. var swapActions = new List<MoveItem>();
  2196. swapActions.Add(new MoveItem(ModuleName.TMRobot, 0, outLL, outLLWaferStatus.emptySlot[0], Hand.Blade1));
  2197. swapActions.Add(new MoveItem(ModuleName.TMRobot, 1, outLL, outLLWaferStatus.emptySlot[1], Hand.Blade2));
  2198. _tmSchdActions.Enqueue(swapActions);
  2199. }
  2200. else
  2201. {
  2202. var freeHands = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2203. readyOutWafers[0].RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2204. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyOutWafers[0].currentMod, readyOutWafers[0].currentSlot, ModuleName.TMRobot, (int)freeHands[0], freeHands[0]) });
  2205. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)freeHands[0], outLL, outLLWaferStatus.emptySlot[0], freeHands[0]) });
  2206. }
  2207. }
  2208. }
  2209. }
  2210. else
  2211. {
  2212. if (robotWafers.Count == 2)
  2213. {
  2214. foreach (var wafer in robotWafers)
  2215. {
  2216. if (wafer.movingStatus == RState.End &&
  2217. ModuleHelper.IsPm(wafer.destMod) &&
  2218. _dictModuleTask[wafer.destMod].TimeToReady == 0 &&
  2219. !_lstWaferTasks.Exists(waferT => waferT.currentMod == wafer.destMod) &&
  2220. !_lstWaferTasks.Exists(waferT => waferT.routedMod == wafer.destMod))
  2221. {
  2222. wafer.RouteTo(wafer.destMod, 0);
  2223. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  2224. }
  2225. }
  2226. if (_tmSchdActions.Count == 0)
  2227. {
  2228. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2229. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2230. {
  2231. RoutingTMSwapActions(swapActions.swapActions);
  2232. _tmSchdActions.Enqueue(swapActions.swapActions);
  2233. }
  2234. }
  2235. }
  2236. else if (robotWafers.Count == 1)
  2237. {
  2238. if (ModuleHelper.IsPm(robotWafers[0].destMod) && _dictModuleTask[robotWafers[0].destMod].TimeToReady == 0)
  2239. {
  2240. var pmActions = new List<MoveItem>();
  2241. var pmWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == robotWafers[0].destMod && ModuleHelper.IsLoadPort(wafer.destMod));
  2242. if (pmWafer != null && string.IsNullOrEmpty(pmWafer.wtwCleanRecipe.Trim()))
  2243. {
  2244. int pickSlot = 1 - robotWafers[0].currentSlot;
  2245. pmWafer.RouteTo(ModuleName.TMRobot, pickSlot);
  2246. pmActions.Add(new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, pickSlot, (Hand)pickSlot));
  2247. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, pmWafer.currentMod, 0, (Hand)robotWafers[0].currentSlot));
  2248. _tmSchdActions.Enqueue(pmActions);
  2249. return;
  2250. }
  2251. if (!_lstWaferTasks.Exists(wafer => wafer.currentMod == robotWafers[0].destMod))
  2252. {
  2253. robotWafers[0].RouteTo(robotWafers[0].destMod, 0);
  2254. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, robotWafers[0].destMod, 0, (Hand)robotWafers[0].currentSlot));
  2255. _tmSchdActions.Enqueue(pmActions);
  2256. return;
  2257. }
  2258. }
  2259. // try to pick a wafer from LL
  2260. if (_tmSchdActions.Count == 0)
  2261. {
  2262. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2263. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2264. {
  2265. RoutingTMSwapActions(swapActions.swapActions);
  2266. _tmSchdActions.Enqueue(swapActions.swapActions);
  2267. return;
  2268. }
  2269. }
  2270. // try to return a wafer from PM
  2271. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2272. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2273. if (moveActions.pmWafers.Count > 0)
  2274. {
  2275. MoveItem placeAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2276. moveActions.pmWafers.First().RouteTo(placeAction.DestinationModule, placeAction.DestinationSlot);
  2277. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers.First().currentMod, 0, ModuleName.TMRobot, (int)freeHand[0], freeHand[0]) });
  2278. MoveItem pickAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.SourceModule));
  2279. if (pickAction != null)
  2280. {
  2281. var pickWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == pickAction.SourceModule && wafer.currentSlot == pickAction.SourceSlot);
  2282. pickWafer.RouteTo(pickAction.DestinationModule, pickAction.DestinationSlot);
  2283. }
  2284. _tmSchdActions.Enqueue(moveActions.swapAction);
  2285. }
  2286. }
  2287. else // robot arm is empty
  2288. {
  2289. // try return wafers from PM
  2290. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2291. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2292. if (moveActions.pmWafers.Count > 0)
  2293. {
  2294. int handIndex = 0;
  2295. var placeActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2296. foreach (var action in placeActions)
  2297. {
  2298. moveActions.pmWafers[handIndex].RouteTo(action.DestinationModule, action.DestinationSlot);
  2299. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers[handIndex].currentMod, 0, ModuleName.TMRobot, (int)freeHand[handIndex], freeHand[handIndex]) });
  2300. handIndex++;
  2301. }
  2302. var pickActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.SourceModule)).ToList();
  2303. foreach (var ac in pickActions)
  2304. {
  2305. var llWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == ac.SourceModule && wafer.currentSlot == ac.SourceSlot);
  2306. llWafer.RouteTo(ac.DestinationModule, ac.DestinationSlot);
  2307. }
  2308. _tmSchdActions.Enqueue(moveActions.swapAction);
  2309. }
  2310. if (_tmSchdActions.Count == 0)
  2311. {
  2312. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2313. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2314. {
  2315. RoutingTMSwapActions(swapActions.swapActions);
  2316. _tmSchdActions.Enqueue(swapActions.swapActions);
  2317. }
  2318. }
  2319. }
  2320. }
  2321. }
  2322. private void Routing4SlotVacSystem()
  2323. {
  2324. var robotWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && ModuleHelper.IsTMRobot(wafer.currentMod)).OrderBy(wafer => TimeForNextModuleReady(wafer)).ToList();
  2325. if (robotWafers.Count == 2)
  2326. {
  2327. foreach (var wafer in robotWafers)
  2328. {
  2329. if (wafer.movingStatus == RState.End &&
  2330. ModuleHelper.IsPm(wafer.destMod) &&
  2331. _dictModuleTask[wafer.destMod].TimeToReady == 0 &&
  2332. !_lstWaferTasks.Exists(waferT => waferT.currentMod == wafer.destMod) &&
  2333. !_lstWaferTasks.Exists(waferT => waferT.routedMod == wafer.destMod))
  2334. {
  2335. wafer.RouteTo(wafer.destMod, 0);
  2336. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  2337. }
  2338. }
  2339. if (_tmSchdActions.Count == 0)
  2340. {
  2341. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2342. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2343. {
  2344. RoutingTMSwapActions(swapActions.swapActions);
  2345. _tmSchdActions.Enqueue(swapActions.swapActions);
  2346. }
  2347. }
  2348. }
  2349. else if (robotWafers.Count == 1)
  2350. {
  2351. if (ModuleHelper.IsPm(robotWafers[0].destMod) && _dictModuleTask[robotWafers[0].destMod].TimeToReady == 0)
  2352. {
  2353. var pmActions = new List<MoveItem>();
  2354. var pmWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == robotWafers[0].destMod && ModuleHelper.IsLoadPort(wafer.destMod));
  2355. if (pmWafer != null && string.IsNullOrEmpty(pmWafer.wtwCleanRecipe.Trim()))
  2356. {
  2357. int pickSlot = 1 - robotWafers[0].currentSlot;
  2358. pmWafer.RouteTo(ModuleName.TMRobot, pickSlot);
  2359. pmActions.Add(new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, pickSlot, (Hand)pickSlot));
  2360. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, pmWafer.currentMod, 0, (Hand)robotWafers[0].currentSlot));
  2361. _tmSchdActions.Enqueue(pmActions);
  2362. return;
  2363. }
  2364. if (!_lstWaferTasks.Exists(wafer => wafer.currentMod == robotWafers[0].destMod))
  2365. {
  2366. robotWafers[0].RouteTo(robotWafers[0].destMod, 0);
  2367. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, robotWafers[0].destMod, 0, (Hand)robotWafers[0].currentSlot));
  2368. _tmSchdActions.Enqueue(pmActions);
  2369. return;
  2370. }
  2371. }
  2372. // try to pick a wafer from LL
  2373. if (_tmSchdActions.Count == 0)
  2374. {
  2375. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2376. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2377. {
  2378. RoutingTMSwapActions(swapActions.swapActions);
  2379. _tmSchdActions.Enqueue(swapActions.swapActions);
  2380. return;
  2381. }
  2382. }
  2383. // try to return a wafer from PM
  2384. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2385. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2386. if (moveActions.pmWafers.Count > 0)
  2387. {
  2388. MoveItem placeAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2389. moveActions.pmWafers.First().RouteTo(placeAction.DestinationModule, placeAction.DestinationSlot);
  2390. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers.First().currentMod, 0, ModuleName.TMRobot, (int)freeHand[0], freeHand[0]) });
  2391. MoveItem pickAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.SourceModule));
  2392. if (pickAction != null)
  2393. {
  2394. var pickWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == pickAction.SourceModule && wafer.currentSlot == pickAction.SourceSlot);
  2395. pickWafer.RouteTo(pickAction.DestinationModule, pickAction.DestinationSlot);
  2396. }
  2397. _tmSchdActions.Enqueue(moveActions.swapAction);
  2398. }
  2399. }
  2400. else // robot arm is empty
  2401. {
  2402. // try return wafers from PM
  2403. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2404. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2405. if (moveActions.pmWafers.Count > 0)
  2406. {
  2407. int handIndex = 0;
  2408. var placeActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2409. foreach (var action in placeActions)
  2410. {
  2411. moveActions.pmWafers[handIndex].RouteTo(action.DestinationModule, action.DestinationSlot);
  2412. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers[handIndex].currentMod, 0, ModuleName.TMRobot, (int)freeHand[handIndex], freeHand[handIndex]) });
  2413. handIndex++;
  2414. }
  2415. var pickActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.SourceModule)).ToList();
  2416. foreach (var ac in pickActions)
  2417. {
  2418. var llWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == ac.SourceModule && wafer.currentSlot == ac.SourceSlot);
  2419. llWafer.RouteTo(ac.DestinationModule, ac.DestinationSlot);
  2420. }
  2421. _tmSchdActions.Enqueue(moveActions.swapAction);
  2422. }
  2423. if (_tmSchdActions.Count == 0)
  2424. {
  2425. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2426. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2427. {
  2428. RoutingTMSwapActions(swapActions.swapActions);
  2429. _tmSchdActions.Enqueue(swapActions.swapActions);
  2430. }
  2431. }
  2432. }
  2433. }
  2434. private List<Hand> GetTMFreeHand()
  2435. {
  2436. var lstHands = new List<Hand>();
  2437. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0) && _tmRobotSingleArmOption != 2)
  2438. lstHands.Add(Hand.Blade1);
  2439. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1) && _tmRobotSingleArmOption != 1)
  2440. lstHands.Add(Hand.Blade2);
  2441. return lstHands;
  2442. }
  2443. private List<Hand> GetEFEMFreeHand()
  2444. {
  2445. var lstHands = new List<Hand>();
  2446. if (WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, 0) && _efemRobotSingleArmOption != 2)
  2447. lstHands.Add(Hand.Blade1);
  2448. if (WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, 1) && _efemRobotSingleArmOption != 1)
  2449. lstHands.Add(Hand.Blade2);
  2450. return lstHands;
  2451. }
  2452. private bool CanWaferGotoLL(WaferTask task, ModuleName ll)
  2453. {
  2454. bool bVacWafer = ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod);
  2455. if (bVacWafer)
  2456. {
  2457. if (ModuleHelper.IsPm(task.destMod))
  2458. return false;
  2459. switch (task.llInOutPath)
  2460. {
  2461. case SequenceLLInOutPath.AInAOut:
  2462. case SequenceLLInOutPath.BInAOut:
  2463. return ll == ModuleName.LLA;
  2464. case SequenceLLInOutPath.AInBOut:
  2465. case SequenceLLInOutPath.BInBOut:
  2466. return ll == ModuleName.LLB;
  2467. case SequenceLLInOutPath.DInDOut:
  2468. return true;
  2469. }
  2470. }
  2471. else
  2472. {
  2473. if (ModuleHelper.IsLoadPort(task.destMod))
  2474. return false;
  2475. switch (task.llInOutPath)
  2476. {
  2477. case SequenceLLInOutPath.AInAOut:
  2478. case SequenceLLInOutPath.AInBOut:
  2479. return ll == ModuleName.LLA;
  2480. case SequenceLLInOutPath.BInAOut:
  2481. case SequenceLLInOutPath.BInBOut:
  2482. return ll == ModuleName.LLB;
  2483. case SequenceLLInOutPath.DInDOut:
  2484. return true;
  2485. }
  2486. }
  2487. return false;
  2488. }
  2489. private (List<int> inSlot, List<int> outSlot, List<int> emptySlot) GetLLReadyInOutSlots(ModuleName ll)
  2490. {
  2491. var readInSlots = new List<int>();
  2492. var readyOutSlots = new List<int>();
  2493. var emptySlots = new List<int>();
  2494. for(int slot = 0; slot < (ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber); slot++)
  2495. {
  2496. if(WaferManager.Instance.CheckNoWafer(ll, slot) && !_lstWaferTasks.Exists(wafer => (wafer.routedMod == ll && wafer.routedSlot == slot)|| (wafer.currentMod == ll && wafer.currentSlot == slot) || (wafer.nextMod == ll && wafer.nextSlot == slot)))
  2497. {
  2498. emptySlots.Add(slot);
  2499. }
  2500. if(WaferManager.Instance.CheckHasWafer(ll, slot))
  2501. {
  2502. Guid waferID = WaferManager.Instance.GetWafer(ll, slot).InnerId;
  2503. var waferTask = _lstWaferTasks.Find(wafer => wafer.waferId == waferID);
  2504. if(waferTask != null && waferTask.movingStatus == RState.End)
  2505. {
  2506. if (ModuleHelper.IsPm(waferTask.destMod))
  2507. readInSlots.Add(slot);
  2508. else if (_dictModuleTask[ll].Scheduler.WaferArrivedTicks(slot) > waferTask.llDelayTime * 1000)
  2509. readyOutSlots.Add(slot);
  2510. }
  2511. }
  2512. }
  2513. return (readInSlots, readyOutSlots, emptySlots);
  2514. }
  2515. private (List<WaferInfo> wafers, List<int> emptySlots) GetLLWaferExistance(ModuleName ll)
  2516. {
  2517. var lstWafers = new List<WaferInfo>();
  2518. var lstEmptySlots = new List<int>();
  2519. for (int slot = 0; slot < (ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber); slot++)
  2520. {
  2521. var wafer = WaferManager.Instance.GetWafer(ll, slot);
  2522. if(wafer != null && !wafer.IsEmpty)
  2523. {
  2524. lstWafers.Add(wafer);
  2525. }
  2526. else
  2527. {
  2528. lstEmptySlots.Add(slot);
  2529. }
  2530. }
  2531. return (lstWafers, lstEmptySlots);
  2532. }
  2533. private void ReturnVacWafers()
  2534. {
  2535. if (_tmSchdActions.Count > 0 || _curTmAction.Count > 0)
  2536. {
  2537. RunSchdTMReturnActions();
  2538. return;
  2539. }
  2540. if (_tmRobotStatus != RState.End)
  2541. return;
  2542. var hands = GetTMFreeHand();
  2543. var lls = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && !IsLLReservedByEFEM(mod.Key) && mod.Value.Scheduler.IsIdle && GetLLWaferExistance(mod.Key).emptySlots.Count > 0).OrderByDescending(mod => GetLLReadyInOutSlots(mod.Key).emptySlot.Count);
  2544. if(lls.Count() > 0)
  2545. {
  2546. // return robot wafers
  2547. int moveCount = 0;
  2548. var llActions = new List<MoveItem>();
  2549. var emptySlots = GetLLWaferExistance(lls.First().Key).emptySlots;
  2550. for(int arm = 0; arm < 2; arm++)
  2551. {
  2552. var wafer = WaferManager.Instance.GetWafer(ModuleName.TMRobot, arm);
  2553. if(wafer != null && !wafer.IsEmpty && moveCount < emptySlots.Count)
  2554. {
  2555. llActions.Add(new MoveItem(ModuleName.TMRobot, arm, lls.First().Key, emptySlots[moveCount], (Hand)arm));
  2556. moveCount++;
  2557. }
  2558. }
  2559. if(moveCount > 0)
  2560. {
  2561. _tmSchdActions.Enqueue(llActions);
  2562. return;
  2563. }
  2564. // return PM wafers
  2565. var PickActions = new List<MoveItem>();
  2566. var hasWaferPMs = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsIdle && WaferManager.Instance.CheckHasWafer(mod.Key, 0));
  2567. foreach(var pm in hasWaferPMs)
  2568. {
  2569. if (moveCount < hands.Count && moveCount < emptySlots.Count)
  2570. {
  2571. PickActions.Add(new MoveItem(pm.Key, 0, ModuleName.TMRobot, (int)hands[moveCount], hands[moveCount]));
  2572. llActions.Add(new MoveItem(ModuleName.TMRobot, (int)hands[moveCount], lls.First().Key, emptySlots[moveCount], hands[moveCount]));
  2573. moveCount++;
  2574. }
  2575. }
  2576. if(moveCount > 0)
  2577. {
  2578. foreach(var ac in PickActions)
  2579. {
  2580. _tmSchdActions.Enqueue(new List<MoveItem> { ac });
  2581. }
  2582. _tmSchdActions.Enqueue(llActions);
  2583. }
  2584. }
  2585. }
  2586. private void ReturnAtmWafers()
  2587. {
  2588. if (_efemSchdActions.Count > 0 || _curEfemAction.Count > 0)
  2589. {
  2590. RunSchdEFEMReturnActions();
  2591. return;
  2592. }
  2593. if (_efemRobotStatus != RState.End)
  2594. return;
  2595. // return Robot Wafer
  2596. for(int i = 0; i < 2; i++)
  2597. {
  2598. var wafer = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, i);
  2599. if(wafer != null && !wafer.IsEmpty)
  2600. {
  2601. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.EfemRobot, i, (ModuleName)wafer.OriginStation, wafer.OriginSlot, (Hand)i) });
  2602. }
  2603. }
  2604. if (_efemSchdActions.Count > 0)
  2605. return;
  2606. var hands = GetEFEMFreeHand();
  2607. // return Aligner wafer
  2608. if(WaferManager.Instance.CheckHasWafer(ModuleName.Aligner, 0) && hands.Count > 0)
  2609. {
  2610. var wafer = WaferManager.Instance.GetWafer(ModuleName.Aligner, 0);
  2611. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.Aligner, 0, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  2612. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), (ModuleName)wafer.OriginStation, wafer.OriginSlot, hands.First()) });
  2613. return;
  2614. }
  2615. // return Loadlock wafer
  2616. var lls = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && !IsLLReservedByTM(mod.Key) && mod.Value.Scheduler.IsIdle && GetLLWaferExistance(mod.Key).wafers.Count > 0).OrderByDescending(mod => GetLLWaferExistance(mod.Key).wafers.Count);
  2617. if(lls.Count() > 0)
  2618. {
  2619. int returnCount = 0;
  2620. var llActions = new List<MoveItem>();
  2621. var placActions = new List<MoveItem>();
  2622. foreach(var wafer in GetLLWaferExistance(lls.First().Key).wafers)
  2623. {
  2624. if(returnCount < hands.Count)
  2625. {
  2626. llActions.Add(new MoveItem(lls.First().Key, wafer.Slot, ModuleName.EfemRobot, (int)hands[returnCount], hands[returnCount]));
  2627. placActions.Add(new MoveItem(ModuleName.EfemRobot, (int)hands[returnCount], (ModuleName)wafer.OriginStation, wafer.OriginSlot, hands[returnCount]));
  2628. returnCount++;
  2629. }
  2630. }
  2631. if (returnCount > 0)
  2632. {
  2633. _efemSchdActions.Enqueue(llActions);
  2634. foreach (var ac in placActions)
  2635. {
  2636. _efemSchdActions.Enqueue(new List<MoveItem> { ac });
  2637. }
  2638. }
  2639. }
  2640. }
  2641. private void RunSchdEFEMReturnActions()
  2642. {
  2643. var efemRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot) as SchedulerEfemRobot;
  2644. if (efemRobot == null || !efemRobot.IsAvailable)
  2645. return;
  2646. if (_efemSchdActions.Count > 0)
  2647. {
  2648. if (_curEfemAction.Count == 0 || isReturnActionsDone(_curEfemAction))
  2649. {
  2650. var nextActions = _efemSchdActions.First();
  2651. var nextModule = nextActions.First().Module;
  2652. if (ModuleHelper.IsLoadLock(nextModule) && !_dictModuleTask[nextModule].IsIdle)
  2653. return;
  2654. _curEfemAction = _efemSchdActions.Dequeue();
  2655. efemRobot.PostMoveItems(_curEfemAction.ToArray());
  2656. }
  2657. }
  2658. else if (_curEfemAction.Count >= 0 && isReturnActionsDone(_curEfemAction)) // all scheduled actions done
  2659. {
  2660. _curEfemAction.Clear();
  2661. }
  2662. }
  2663. private void RunSchdTMReturnActions()
  2664. {
  2665. var tmRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.TMRobot) as SchedulerTMRobot;
  2666. if (tmRobot == null || !tmRobot.IsAvailable)
  2667. return;
  2668. if (_tmSchdActions.Count > 0)
  2669. {
  2670. if (_curTmAction.Count == 0 || isReturnActionsDone(_curTmAction))
  2671. {
  2672. var nextActions = _tmSchdActions.First();
  2673. var nextModule = nextActions.First().Module;
  2674. if (ModuleHelper.IsLoadLock(nextModule) && !_dictModuleTask[nextModule].IsIdle)
  2675. return;
  2676. _curTmAction = _tmSchdActions.Dequeue();
  2677. tmRobot.SendMoveItems(_curTmAction.ToArray());
  2678. }
  2679. }
  2680. else if (_curTmAction.Count >= 0 && isReturnActionsDone(_curTmAction)) // all scheduled actions done
  2681. {
  2682. _curTmAction.Clear();
  2683. }
  2684. }
  2685. #endregion
  2686. #region sequence/recipe operation
  2687. private bool CheckSequencePmReady(SequenceInfo seq, out string reason)
  2688. {
  2689. reason = "";
  2690. foreach(var pm in seq.PMs)
  2691. {
  2692. if (ModuleHelper.IsInstalled(pm) && (_dictModuleTask[pm].Scheduler.IsOnline || !Singleton<RouteManager>.Instance.IsAutoMode))
  2693. return true;
  2694. }
  2695. reason = $"Sequence {seq.Name} no valid PM, " + string.Join("|", seq.PMs);
  2696. return false;
  2697. }
  2698. private bool CheckSequenceRecipeFileValid(SequenceInfo seq, out string reason)
  2699. {
  2700. for (int i = 0; i < seq.Steps.Count; i++)
  2701. {
  2702. SequenceStepInfo stepInfo = seq.Steps[i];
  2703. foreach (var module in stepInfo.StepModules)
  2704. {
  2705. if (ModuleHelper.IsPm(module))
  2706. {
  2707. string attr = $"{module}Recipe";
  2708. if (stepInfo.StepParameter.ContainsKey(attr)
  2709. && !string.IsNullOrEmpty((string)stepInfo.StepParameter[attr]))
  2710. {
  2711. var recipeName = (string)stepInfo.StepParameter[attr];
  2712. if (!string.IsNullOrEmpty(recipeName))
  2713. {
  2714. var recipeContent =
  2715. RecipeFileManager.Instance.LoadRecipe($"{module}", recipeName, false, "Process");
  2716. if (string.IsNullOrEmpty(recipeContent))
  2717. {
  2718. reason = $"Can not find recipe file{recipeName}";
  2719. return false;
  2720. }
  2721. }
  2722. }
  2723. }
  2724. }
  2725. }
  2726. reason = "";
  2727. return true;
  2728. }
  2729. private void UpdateLLInOutPathProperty()
  2730. {
  2731. if (_lstControlJobs.Count == 0)
  2732. return;
  2733. List<SequenceLLInOutPath> inOutPaths = new List<SequenceLLInOutPath>();
  2734. foreach (var cj in _lstControlJobs)
  2735. {
  2736. if (cj.State == EnumControlJobState.Executing)
  2737. {
  2738. foreach (var pjName in cj.ProcessJobNameList)
  2739. {
  2740. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  2741. if (pj == null || pj.State != EnumProcessJobState.Processing)
  2742. continue;
  2743. if (!inOutPaths.Exists(item => item == pj.Sequence.LLInOutPath))
  2744. inOutPaths.Add(pj.Sequence.LLInOutPath);
  2745. }
  2746. }
  2747. }
  2748. if (inOutPaths.Count == 1)
  2749. {
  2750. _LLInOutPath = inOutPaths[0];
  2751. }
  2752. }
  2753. #endregion
  2754. }
  2755. }