SystemDispatcher.cs 155 KB

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