SystemDispatcher.cs 198 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343
  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.End;
  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. return RState.Running;
  120. }
  121. public void RouteTo(ModuleName mod, int slot)
  122. {
  123. routedMod = mod;
  124. routedSlot = slot;
  125. movingStatus = RState.Init;
  126. }
  127. public void MoveTo(ModuleName mod, int slot)
  128. {
  129. nextMod = mod;
  130. nextSlot = slot;
  131. movingStatus = RState.Running;
  132. if(ModuleHelper.IsLoadLock(currentMod) && ModuleHelper.IsTMRobot(nextMod) && ModuleHelper.IsLoadLock(routedMod))
  133. {
  134. routedMod = ModuleName.TMRobot; // fix the bug which missing call RouteTo() on Vac side
  135. }
  136. }
  137. public void Return()
  138. {
  139. destMod = sourceMod;
  140. destSlot = sourceSlot;
  141. movingStatus = RState.End;
  142. pressureStatus = RState.End;
  143. temperatureStatus = RState.End;
  144. }
  145. }
  146. public enum ModuleStatus
  147. {
  148. Idle,
  149. WaitMove,
  150. Moving,
  151. NotReady,
  152. // PM Status
  153. WaitProcess,
  154. StartProcess,
  155. Processing,
  156. StartIdleClean,
  157. IdleClean,
  158. WaitPreJobClean,
  159. StartPreJobClean,
  160. PreJobClean,
  161. WaitPostJobClean,
  162. StartPostJobClean,
  163. PostJobClean,
  164. WaitWTWClean,
  165. StartWTWClean,
  166. WTWClean,
  167. // Align Status
  168. WaitAlign,
  169. StartAlign,
  170. Aligning,
  171. // Loadlock Status
  172. ExchangeWaferWithEFEM,
  173. ExchangeWaferWithTM,
  174. }
  175. public class ModuleTask
  176. {
  177. public ModuleName Module { get; }
  178. public ModuleStatus Status { get; set; }
  179. public SchedulerModule Scheduler { get; }
  180. public virtual int SlotNum { get; protected set; }
  181. public virtual bool IsIdle { get { return Status == ModuleStatus.Idle; } }
  182. private int _currentActionTime = 0;
  183. public virtual int IdleTime
  184. {
  185. get
  186. {
  187. if (Status != ModuleStatus.Idle)
  188. return 0;
  189. return (int)(DateTime.Now - _moduleTimer).TotalSeconds;
  190. }
  191. }
  192. public virtual int BusyTime
  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 BusyLastTime
  202. {
  203. get
  204. {
  205. if (Status == ModuleStatus.Idle)
  206. return 0;
  207. return _currentActionTime - BusyTime;
  208. }
  209. }
  210. public virtual int TimeToReady
  211. {
  212. get
  213. {
  214. if(Scheduler.IsAvailable)
  215. return 0;
  216. return int.MaxValue;
  217. }
  218. }
  219. protected DateTime _moduleTimer;
  220. public ModuleTask(ModuleName mod)
  221. {
  222. Module = mod;
  223. Scheduler = Singleton<TransferModule>.Instance.GetScheduler(mod);
  224. SlotNum = 1;
  225. _moduleTimer = DateTime.Now;
  226. }
  227. public virtual RState Run()
  228. {
  229. Status = Scheduler.IsAvailable ? ModuleStatus.Idle : ModuleStatus.NotReady;
  230. return RState.Running;
  231. }
  232. public virtual void WaferArrived(WaferTask wafer, int slot)
  233. {
  234. }
  235. public virtual void WaferLeaved(WaferTask wafer, int slot)
  236. {
  237. }
  238. }
  239. public class PMTask : ModuleTask
  240. {
  241. enum CleanType
  242. {
  243. IdleClean,
  244. PreJobClean,
  245. PostJobClean,
  246. WTWClean,
  247. }
  248. public override int TimeToReady
  249. {
  250. get
  251. {
  252. switch(Status)
  253. {
  254. case ModuleStatus.Idle:
  255. case ModuleStatus.Processing:
  256. case ModuleStatus.IdleClean:
  257. case ModuleStatus.PreJobClean:
  258. {
  259. return Scheduler.IsAvailable ? 0 : (Scheduler.TimeToReady + 500) / 1000;
  260. }
  261. case ModuleStatus.PostJobClean:
  262. case ModuleStatus.WTWClean:
  263. {
  264. if(_pendingCleanTask.Count == 0)
  265. {
  266. return Scheduler.IsAvailable ? 0 : (Scheduler.TimeToReady + 500) / 1000;
  267. }
  268. }
  269. break;
  270. }
  271. return int.MaxValue;
  272. }
  273. }
  274. private WaferTask _wafer;
  275. private SchedulerPM _pmScheduler => Scheduler as SchedulerPM;
  276. private string _preJobCleanRecipe;
  277. private string _postJobCleanRecipe;
  278. private string _wtwCleanRecipe;
  279. private Queue<CleanType> _pendingCleanTask = new Queue<CleanType>();
  280. public PMTask(ModuleName pm) : base(pm)
  281. {
  282. }
  283. public override RState Run()
  284. {
  285. if(Scheduler.IsIdle)
  286. {
  287. switch (Status)
  288. {
  289. case ModuleStatus.Idle:
  290. {
  291. if(WaferManager.Instance.CheckNoWafer(Module, 0) && Scheduler.IsOnline)
  292. {
  293. if (_pendingCleanTask.Count > 0)
  294. {
  295. var cleanTask = _pendingCleanTask.Dequeue();
  296. if (cleanTask == CleanType.PostJobClean && !string.IsNullOrWhiteSpace(_postJobCleanRecipe))
  297. {
  298. Status = ModuleStatus.WaitPostJobClean;
  299. }
  300. else if (cleanTask == CleanType.PreJobClean && !string.IsNullOrWhiteSpace(_preJobCleanRecipe))
  301. {
  302. Status = ModuleStatus.WaitPreJobClean;
  303. }
  304. }
  305. else if (_pmScheduler.RunIdleCleanTask()) // Check Idle Clean
  306. {
  307. Status = ModuleStatus.StartIdleClean;
  308. }
  309. }
  310. }
  311. break;
  312. case ModuleStatus.WaitProcess:
  313. {
  314. Scheduler.EventWaferArrived?.Invoke(this, new WaferMoveArgs(ModuleName.TMRobot, 0, Module, 0));
  315. Status = ModuleStatus.StartProcess;
  316. }
  317. break;
  318. case ModuleStatus.WaitPreJobClean:
  319. {
  320. if(WaferManager.Instance.CheckNoWafer(Module, 0))
  321. {
  322. if (_pmScheduler.RunJobCleanTask(_preJobCleanRecipe))
  323. {
  324. Status = ModuleStatus.StartPreJobClean;
  325. }
  326. else
  327. {
  328. LOG.Write(eEvent.WARN_ROUTER, Module, $"Run Prejob clean recipe{_preJobCleanRecipe} failed");
  329. Status = ModuleStatus.Idle;
  330. _preJobCleanRecipe = string.Empty;
  331. }
  332. }
  333. }
  334. break;
  335. case ModuleStatus.WaitPostJobClean:
  336. {
  337. if (WaferManager.Instance.CheckNoWafer(Module, 0))
  338. {
  339. if (_pmScheduler.RunJobCleanTask(_postJobCleanRecipe))
  340. {
  341. Status = ModuleStatus.StartPostJobClean;
  342. }
  343. else
  344. {
  345. LOG.Write(eEvent.WARN_ROUTER, Module, $"Run Postjob clean recipe{_postJobCleanRecipe} failed");
  346. Status = ModuleStatus.Idle;
  347. _postJobCleanRecipe = string.Empty;
  348. }
  349. }
  350. }
  351. break;
  352. case ModuleStatus.WaitWTWClean:
  353. {
  354. if (WaferManager.Instance.CheckNoWafer(Module, 0))
  355. {if (_pmScheduler.RunJobCleanTask(_wtwCleanRecipe))
  356. {
  357. Status = ModuleStatus.StartWTWClean;
  358. }
  359. else
  360. {
  361. LOG.Write(eEvent.WARN_ROUTER, Module, $"Run WTW Wafer clean recipe{_wtwCleanRecipe} failed");
  362. Status = ModuleStatus.Idle;
  363. _wtwCleanRecipe = string.Empty;
  364. }
  365. }
  366. }
  367. break;
  368. case ModuleStatus.Processing:
  369. {
  370. var wafer = WaferManager.Instance.GetWafer(Module, 0);
  371. if(Scheduler.IsOnline)
  372. {
  373. if (!wafer.IsEmpty && wafer.ProcessState == EnumWaferProcessStatus.Completed)
  374. {
  375. _wafer.Return();
  376. Status = ModuleStatus.Idle;
  377. }
  378. }
  379. else // handle offline exception situation
  380. {
  381. Status = ModuleStatus.Idle;
  382. }
  383. }
  384. break;
  385. case ModuleStatus.PreJobClean:
  386. {
  387. _preJobCleanRecipe = string.Empty;
  388. Status = ModuleStatus.Idle;
  389. }
  390. break;
  391. case ModuleStatus.PostJobClean:
  392. {
  393. _postJobCleanRecipe = string.Empty;
  394. if (_pendingCleanTask.Count > 0)
  395. {
  396. var cleanTask = _pendingCleanTask.Dequeue();
  397. if(cleanTask == CleanType.PreJobClean && !string.IsNullOrWhiteSpace(_preJobCleanRecipe))
  398. {
  399. Status = ModuleStatus.WaitPreJobClean;
  400. break;
  401. }
  402. }
  403. Status = ModuleStatus.Idle;
  404. }
  405. break;
  406. case ModuleStatus.WTWClean:
  407. {
  408. if(_pendingCleanTask.Count > 0)
  409. {
  410. var cleanTask = _pendingCleanTask.Dequeue();
  411. if (cleanTask == CleanType.PostJobClean && !string.IsNullOrWhiteSpace(_postJobCleanRecipe))
  412. {
  413. Status = ModuleStatus.WaitPostJobClean;
  414. }
  415. else if (cleanTask == CleanType.PreJobClean && !string.IsNullOrWhiteSpace(_preJobCleanRecipe))
  416. {
  417. Status = ModuleStatus.WaitPreJobClean;
  418. }
  419. }
  420. else
  421. {
  422. Status = ModuleStatus.Idle;
  423. }
  424. _wtwCleanRecipe = string.Empty;
  425. }
  426. break;
  427. case ModuleStatus.IdleClean:
  428. {
  429. Status = ModuleStatus.Idle;
  430. }
  431. break;
  432. }
  433. }
  434. else
  435. {
  436. switch(Status)
  437. {
  438. case ModuleStatus.StartProcess:
  439. Status = ModuleStatus.Processing;
  440. break;
  441. case ModuleStatus.StartIdleClean:
  442. Status = ModuleStatus.IdleClean;
  443. break;
  444. case ModuleStatus.StartPreJobClean:
  445. Status = ModuleStatus.PreJobClean;
  446. break;
  447. case ModuleStatus.StartPostJobClean:
  448. Status = ModuleStatus.PostJobClean;
  449. break;
  450. case ModuleStatus.StartWTWClean:
  451. Status = ModuleStatus.WTWClean;
  452. break;
  453. }
  454. }
  455. return RState.Running;
  456. }
  457. public override void WaferArrived(WaferTask wafer, int slot)
  458. {
  459. _wafer = wafer;
  460. Status = ModuleStatus.WaitProcess;
  461. }
  462. public override void WaferLeaved(WaferTask wafer, int slot)
  463. {
  464. if(!string.IsNullOrWhiteSpace(_wafer.wtwCleanRecipe.Trim()))
  465. {
  466. Status = ModuleStatus.WaitWTWClean;
  467. _wtwCleanRecipe = _wafer.wtwCleanRecipe;
  468. }
  469. else if(_pendingCleanTask.Count > 0)
  470. {
  471. var cleanTask = _pendingCleanTask.Dequeue();
  472. if (cleanTask == CleanType.PostJobClean && !string.IsNullOrWhiteSpace(_postJobCleanRecipe))
  473. {
  474. Status = ModuleStatus.WaitPostJobClean;
  475. }
  476. else if (cleanTask == CleanType.PreJobClean && !string.IsNullOrWhiteSpace(_preJobCleanRecipe))
  477. {
  478. Status = ModuleStatus.WaitPreJobClean;
  479. }
  480. }
  481. _wafer = null;
  482. }
  483. public void InvokePreJobClean(string preJobClean)
  484. {
  485. _preJobCleanRecipe = preJobClean;
  486. _pendingCleanTask.Enqueue(CleanType.PreJobClean);
  487. }
  488. public bool IsPreJobCleanDone()
  489. {
  490. return !_pendingCleanTask.Contains(CleanType.PreJobClean) && string.IsNullOrWhiteSpace(_preJobCleanRecipe);
  491. }
  492. public void InvokePostJobClean(string postJobClean)
  493. {
  494. _postJobCleanRecipe = postJobClean;
  495. _pendingCleanTask.Enqueue(CleanType.PostJobClean);
  496. }
  497. public bool IsPostJobCleanDone()
  498. {
  499. return !_pendingCleanTask.Contains(CleanType.PostJobClean) && string.IsNullOrWhiteSpace(_postJobCleanRecipe);
  500. }
  501. }
  502. public class AlignerTask : ModuleTask
  503. {
  504. private WaferTask _wafer;
  505. public AlignerTask(ModuleName aligner) : base(aligner)
  506. {
  507. }
  508. public override RState Run()
  509. {
  510. var efemRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot) as SchedulerEfemRobot;
  511. if (efemRobot == null || !efemRobot.IsAvailable)
  512. return RState.Running;
  513. switch (Status)
  514. {
  515. case ModuleStatus.WaitAlign:
  516. {
  517. efemRobot.Align(0);
  518. Status = ModuleStatus.Aligning;
  519. }
  520. break;
  521. case ModuleStatus.Aligning:
  522. {
  523. _wafer.IsAligned = true;
  524. Status = ModuleStatus.Idle;
  525. }
  526. break;
  527. }
  528. return RState.Running;
  529. }
  530. public override void WaferArrived(WaferTask wafer, int slot)
  531. {
  532. _wafer = wafer;
  533. Status = ModuleStatus.WaitAlign;
  534. }
  535. public override void WaferLeaved(WaferTask wafer, int slot)
  536. {
  537. _wafer = null;
  538. }
  539. }
  540. public class LoadPortTask : ModuleTask
  541. {
  542. public LoadPortTask(ModuleName lp) : base(lp)
  543. {
  544. SlotNum = SC.GetValue<int>($"EFEM.LoadPort.SlotNumber");
  545. }
  546. public override RState Run()
  547. {
  548. return base.Run();
  549. }
  550. }
  551. public class TMRobotTask : ModuleTask
  552. {
  553. public TMRobotTask(ModuleName tmRobot) : base(tmRobot)
  554. {
  555. SlotNum = 2;
  556. }
  557. public override RState Run()
  558. {
  559. return base.Run();
  560. }
  561. }
  562. public class EFEMRobotTask : ModuleTask
  563. {
  564. public EFEMRobotTask(ModuleName efemRobot) : base(efemRobot)
  565. {
  566. SlotNum = 2;
  567. }
  568. public override RState Run()
  569. {
  570. return base.Run();
  571. }
  572. public bool PostMoveItems(MoveItem[] items)
  573. {
  574. Status = ModuleStatus.Moving;
  575. return (Scheduler as SchedulerEfemRobot).PostMoveItems(items);
  576. }
  577. }
  578. public class LoadlockTask : ModuleTask
  579. {
  580. public LoadlockTask(ModuleName ll) : base(ll)
  581. {
  582. SlotNum = SC.GetValue<int>($"{ll}.SlotNumber");
  583. }
  584. public override int TimeToReady
  585. {
  586. get { return Scheduler.IsAvailable ? 0 : (Scheduler.TimeToReady + 500) / 1000; }
  587. }
  588. public override void WaferArrived(WaferTask wafer, int slot)
  589. {
  590. Scheduler.WaferArrived(slot);
  591. }
  592. public bool PreVent()
  593. {
  594. if (Scheduler.IsAvailable && Scheduler.IsVac)
  595. {
  596. return (Scheduler as SchedulerLoadLock).PreVent();
  597. }
  598. return false;
  599. }
  600. public bool PrePump()
  601. {
  602. if (Scheduler.IsAvailable && Scheduler.IsAtm)
  603. {
  604. return (Scheduler as SchedulerLoadLock).PrePump();
  605. }
  606. return false;
  607. }
  608. }
  609. enum LLSlotInOutOpt
  610. {
  611. AllInAllOut,
  612. UpperInLowerOut,
  613. LowerInUpperOut,
  614. }
  615. class SystemDispatcher : ICycle
  616. {
  617. private List<ControlJobInfo> _lstControlJobs = new List<ControlJobInfo>();
  618. private List<ProcessJobInfo> _lstProcessJobs = new List<ProcessJobInfo>();
  619. private List<WaferTask> _lstWaferTasks = new List<WaferTask>();
  620. private Dictionary<ModuleName, ModuleTask> _dictModuleTask = new Dictionary<ModuleName, ModuleTask>();
  621. private Queue<WaferInfo> _qeReturnWafers = new Queue<WaferInfo>();
  622. private Queue<List<MoveItem>> _efemSchdActions = new Queue<List<MoveItem>>();
  623. private Queue<List<MoveItem>> _tmSchdActions = new Queue<List<MoveItem>>();
  624. private List<MoveItem> _curEfemAction = new List<MoveItem>();
  625. private List<MoveItem> _curTmAction = new List<MoveItem>();
  626. private RState _efemRobotStatus { get { return (_dictModuleTask[ModuleName.EfemRobot].Scheduler as SchedulerEfemRobot).RobotStatus; } }
  627. private RState _tmRobotStatus { get { return (_dictModuleTask[ModuleName.TMRobot].Scheduler as SchedulerTMRobot).RobotStatus; } }
  628. private SchedulerFACallback _faCallback;
  629. private SchedulerDBCallback _dbCallback;
  630. private bool _isCycleMode;
  631. private int _cycleSetPoint = 0;
  632. private int _cycledCount = 0;
  633. private int _cycledWafer = 0;
  634. private float _throughput = 0.0f;
  635. private readonly int _LLASlotNumber = 4;
  636. private readonly int _LLBSlotNumber = 4;
  637. private int _efemRobotSingleArmOption = 0;
  638. private int _tmRobotSingleArmOption = 0;
  639. private LLSlotInOutOpt _LLSlotInOutOption = 0;
  640. private Dictionary<ModuleName, int> _lpCycleWafer = new Dictionary<ModuleName, int>();
  641. private Dictionary<ModuleName, int> _lpCycleCount = new Dictionary<ModuleName, int>();
  642. private Stopwatch _cycleWatch = new Stopwatch();
  643. private SequenceLLInOutPath _LLInOutPath = SequenceLLInOutPath.DInDOut;
  644. public SequenceLLInOutPath LLInOutPath => _LLInOutPath;
  645. public bool HasJobRunning => _lstControlJobs.Count > 0;
  646. private RState _cycleState = RState.Init;
  647. public RState CycleState => _cycleState;
  648. private Dictionary<string, ControlJobInfo> _loadportControlJobDic = new Dictionary<string, ControlJobInfo>();
  649. public List<string> InUseRecipes = new List<string>();
  650. #region public interface
  651. public SystemDispatcher()
  652. {
  653. _faCallback = new SchedulerFACallback();
  654. _dbCallback = new SchedulerDBCallback();
  655. _efemRobotSingleArmOption = SC.GetValue<int>("EFEM.SingleArmOption");
  656. _tmRobotSingleArmOption = SC.GetValue<int>("TM.SingleArmOption");
  657. _LLASlotNumber = SC.GetValue<int>("LLA.SlotNumber");
  658. _LLBSlotNumber = SC.GetValue<int>("LLB.SlotNumber");
  659. InitModules();
  660. DATA.Subscribe("Scheduler.CycledCount", () => _cycledCount, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  661. DATA.Subscribe("Scheduler.CycledWafer", () => _cycledWafer, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  662. DATA.Subscribe("Scheduler.CycleSetPoint", () => _cycleSetPoint, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  663. DATA.Subscribe("Scheduler.Throughput", () => _throughput, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  664. DATA.Subscribe("Scheduler.PjIdList", () => Array.ConvertAll(_lstProcessJobs.ToArray(), x => x.InnerId.ToString()).ToList());
  665. DATA.Subscribe("Scheduler.PjNameList", () => Array.ConvertAll(_lstProcessJobs.ToArray(), x => x.LotName.ToString()).ToList());
  666. DATA.Subscribe("Scheduler.InUsingRecipe", () => InUseRecipes);
  667. for (int i = 1; i <= 3; i++)
  668. {
  669. _loadportControlJobDic[$"LP{i}"] = null;
  670. string lp = $"LP{i}";
  671. DATA.Subscribe($"{lp}.CurrentControlJob", () => _loadportControlJobDic[lp], SubscriptionAttribute.FLAG.IgnoreSaveDB);
  672. }
  673. }
  674. /// <summary>
  675. /// 获取lp当前的ControlJob
  676. /// </summary>
  677. /// <param name="lp"></param>
  678. /// <returns></returns>
  679. public ControlJobInfo GetLoadPortCurrentControlJob(ModuleName lp)
  680. {
  681. if(ModuleHelper.IsLoadPort(lp))
  682. {
  683. return _loadportControlJobDic.ContainsKey(lp.ToString()) ? _loadportControlJobDic[lp.ToString()] : null;
  684. }
  685. else
  686. {
  687. return null;
  688. }
  689. }
  690. public RState Start(params object[] objs)
  691. {
  692. if(WaferManager.Instance.HasDuplicatedWafer)
  693. {
  694. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, "System has dummy wafers, please verify all the wafer position, and delete the invalid wafer");
  695. return RState.Failed;
  696. }
  697. _efemRobotSingleArmOption = SC.GetValue<int>("EFEM.SingleArmOption");
  698. _tmRobotSingleArmOption = SC.GetValue<int>("TM.SingleArmOption");
  699. _isCycleMode = SC.GetValue<bool>("System.IsCycleMode");
  700. _cycleSetPoint = _isCycleMode ? SC.GetValue<int>("System.CycleCount") : 0;
  701. _LLSlotInOutOption = (LLSlotInOutOpt)SC.GetValue<int>("System.LoadlockSlotInOutOption");
  702. _cycledWafer = 0;
  703. _cycledCount = 0;
  704. _throughput = 0;
  705. // rounding TM robot single arm option
  706. if(_tmRobotSingleArmOption > 2 && _LLSlotInOutOption == LLSlotInOutOpt.AllInAllOut)
  707. {
  708. _tmRobotSingleArmOption = 0;
  709. }
  710. _cycleWatch.Stop();
  711. _lpCycleWafer.Clear();
  712. _lpCycleCount.Clear();
  713. return RState.Running;
  714. }
  715. public RState Monitor()
  716. {
  717. prelude();
  718. RunWaferTask();
  719. RunModuleTasks();
  720. RoutingATMWafers();
  721. RoutingVacWafers();
  722. epilogue();
  723. return _cycleState;
  724. }
  725. public void Abort()
  726. {
  727. }
  728. public bool CreateJob(Dictionary<string, object> param, out string reason)
  729. {
  730. reason = "";
  731. _isCycleMode = SC.GetValue<bool>("System.IsCycleMode");
  732. _cycleSetPoint = _isCycleMode ? SC.GetValue<int>("System.CycleCount") : 0;
  733. string[] slotSequence = (string[])param["SlotSequence"];
  734. string jobId = (string)param["JobId"];
  735. string module = (string)param["Module"];
  736. if (string.IsNullOrEmpty(jobId))
  737. {
  738. jobId = "CJ_Local_" + module;
  739. }
  740. //bool autoStart = (bool)param["AutoStart"];
  741. string lotId = jobId;
  742. if (param.ContainsKey("LotId"))
  743. lotId = (string)param["LotId"];
  744. string preCleanRecipe = param.ContainsKey("PreCleanRecipeName") ? (string)param["PreCleanRecipeName"] : string.Empty;
  745. string postCleanRecipe = param.ContainsKey("PostCleanRecipeName") ? (string)param["PostCleanRecipeName"] : string.Empty;
  746. if (slotSequence.Length != SC.GetValue<int>("EFEM.LoadPort.SlotNumber"))
  747. {
  748. reason = $"slot sequence parameter not valid, length is {slotSequence.Length}, should be {SC.GetValue<int>("EFEM.LoadPort.SlotNumber")}";
  749. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  750. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  751. return false;
  752. }
  753. if (!ModuleHelper.IsLoadPort(ModuleHelper.Converter(module)))
  754. {
  755. reason = $"{module} should be LoadPort";
  756. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  757. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  758. return false;
  759. }
  760. if (_lstControlJobs.Exists(x => x.Name == jobId))
  761. {
  762. reason = $"{jobId} already created";
  763. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  764. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  765. return false;
  766. }
  767. ControlJobInfo cj = new ControlJobInfo();
  768. cj.Name = jobId;
  769. cj.Module = module;
  770. cj.LotName = lotId;
  771. cj.LotInnerId = Guid.NewGuid();
  772. cj.LotWafers = new List<WaferInfo>();
  773. cj.SetState(EnumControlJobState.WaitingForStart);
  774. cj.JetState = EnumJetCtrlJobState.Created;
  775. cj.PreJobClean = preCleanRecipe;
  776. cj.PostJobClean = postCleanRecipe;
  777. cj.SequenceNameList = slotSequence;
  778. Dictionary<string, bool[]> seqSlot = new Dictionary<string, bool[]>();
  779. Dictionary<string, List<Tuple<ModuleName, int>>> seqSlotWafers = new Dictionary<string, List<Tuple<ModuleName, int>>>();
  780. Dictionary<string, string> indexSequence = new Dictionary<string, string>();
  781. bool enableGroupBySequence = SC.GetValue<bool>("Scheduler.GroupWaferBySequence");
  782. for (int i = 0; i < SC.GetValue<int>("EFEM.LoadPort.SlotNumber"); i++)
  783. {
  784. if (string.IsNullOrEmpty(slotSequence[i]) || string.IsNullOrEmpty(slotSequence[i].Trim()))
  785. continue;
  786. string groupName = enableGroupBySequence ? slotSequence[i].Trim() : i.ToString();
  787. indexSequence[groupName] = slotSequence[i];
  788. if (!seqSlot.ContainsKey(groupName))
  789. {
  790. seqSlot[groupName] = new bool[SC.GetValue<int>("EFEM.LoadPort.SlotNumber")];
  791. }
  792. if (!seqSlotWafers.ContainsKey(groupName))
  793. {
  794. seqSlotWafers[groupName] = new List<Tuple<ModuleName, int>>();
  795. }
  796. seqSlot[groupName][i] = true;
  797. if (!WaferManager.Instance.CheckHasWafer(module, i))
  798. {
  799. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"job wafer: {module} slot {i + 1} not in the carrier");
  800. return false;
  801. }
  802. if (!WaferManager.Instance.CheckWafer(ModuleHelper.Converter(module), i, WaferStatus.Normal))
  803. {
  804. reason = $"job wafer: {module} slot {i + 1} status is {WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).Status}";
  805. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  806. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  807. return false;
  808. }
  809. if (WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).ProcessState != EnumWaferProcessStatus.Idle)
  810. {
  811. reason = $"job wafer: {module} slot {i + 1} process status is {WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).ProcessState}";
  812. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  813. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  814. return false;
  815. }
  816. //--- 2024-03-21 增加了waferinfo 相应的lotId信息 start---
  817. WaferInfo waferInfo = WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i);
  818. cj.LotWafers.Add(waferInfo);
  819. waferInfo.SequenceName = slotSequence[i];
  820. waferInfo.LotId = lotId;
  821. //--- 2024-03-21 增加了waferinfo 相应的lotId信息 end---
  822. seqSlotWafers[groupName].Add(Tuple.Create(ModuleHelper.Converter(module), i));
  823. cj.JobWaferSize = WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).Size;
  824. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Assigned wafer job, wafer {module}.{i + 1}, sequence: {slotSequence[i]}");
  825. }
  826. if (seqSlotWafers.Count == 0)
  827. {
  828. reason = $"job has not assign wafer";
  829. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  830. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  831. return false;
  832. }
  833. List<ProcessJobInfo> pjs = new List<ProcessJobInfo>();
  834. string[] seqs = seqSlot.Keys.ToArray();
  835. for (int i = 0; i < seqs.Length; i++)
  836. {
  837. ProcessJobInfo pj = new ProcessJobInfo();
  838. pj.Name = jobId + "_" + (i + 1);
  839. pj.Sequence = SequenceInfoHelper.GetInfo(indexSequence[seqs[i]]);
  840. if (pj.Sequence == null)
  841. {
  842. reason = $"invalid sequence[{indexSequence[seqs[i]]}]";
  843. return false;
  844. }
  845. pj.ControlJobName = cj.Name;
  846. pj.LotName = lotId;
  847. pj.SlotWafers = seqSlotWafers[seqs[i]];
  848. pj.SetState(EnumProcessJobState.Queued);
  849. if (!CheckSequencePmReady(pj.Sequence, out reason))
  850. {
  851. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"no valid chamber for the {reason}");
  852. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  853. return false;
  854. }
  855. if (!RouteManager.IsATMMode && !CheckSequenceRecipeFileValid(pj.Sequence, out reason))
  856. {
  857. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"recipe file not valid in the sequence, {reason}");
  858. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  859. return false;
  860. }
  861. pjs.Add(pj);
  862. }
  863. _dbCallback.LotUpdate(cj);
  864. foreach (var pj in pjs)
  865. {
  866. cj.ProcessJobNameList.Add(pj.Name);
  867. _lstProcessJobs.Add(pj);
  868. }
  869. _lstControlJobs.Add(cj);
  870. //AssociatedPMWithLP(cj);
  871. _loadportControlJobDic[cj.Module] = cj;
  872. _faCallback.JobCreated(cj, GetFirstProcessJob(cj));
  873. return true;
  874. }
  875. public bool CheckAllJobDone()
  876. {
  877. foreach (var cj in _lstControlJobs)
  878. {
  879. if (cj.State == EnumControlJobState.Executing || cj.State == EnumControlJobState.Paused)
  880. return false;
  881. }
  882. InUseRecipes.Clear();
  883. return true;
  884. }
  885. public bool CheckJobJustDone(out string sJobName)
  886. {
  887. foreach (var cj in _lstControlJobs)
  888. {
  889. if (cj.State == EnumControlJobState.Completed && !cj.BeenPosted)
  890. {
  891. //LP;WaferSize;Lot;Number;Start;End;
  892. sJobName = $"{cj.Module};{cj.JobWaferSize};{cj.LotName};{cj.LotWafers.Count};{cj.StartTime:T};{cj.EndTime:T}";
  893. cj.BeenPosted = true;
  894. return true;
  895. }
  896. }
  897. sJobName = "NULL";
  898. return false;
  899. }
  900. public ProcessJobInfo GetFirstProcessJob(ControlJobInfo cj)
  901. {
  902. foreach (var pj in _lstProcessJobs)
  903. {
  904. if (pj.ControlJobName == cj.Name)
  905. return pj;
  906. }
  907. return null;
  908. }
  909. public bool AbortJob(string jobName, out string reason)
  910. {
  911. reason = "";
  912. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  913. if (cj == null)
  914. {
  915. reason = $"abort job rejected, not found job with id {jobName}";
  916. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  917. return false;
  918. }
  919. List<ProcessJobInfo> pjAbortList = new List<ProcessJobInfo>();
  920. foreach (var pj in _lstProcessJobs)
  921. {
  922. if (pj.ControlJobName == cj.Name)
  923. {
  924. pj.SetState(EnumProcessJobState.Aborting);
  925. pjAbortList.Add(pj);
  926. int unprocessed = 0;
  927. int aborted = 0;
  928. WaferInfo[] wafers = WaferManager.Instance.GetWaferByProcessJob(pj.Name);
  929. foreach (var waferInfo in wafers)
  930. {
  931. waferInfo.ProcessJob = null;
  932. waferInfo.NextSequenceStep = 0;
  933. if (waferInfo.ProcessState != EnumWaferProcessStatus.Completed)
  934. unprocessed++;
  935. }
  936. JobDataRecorder.EndPJ(pj.InnerId.ToString(), aborted, unprocessed);
  937. }
  938. }
  939. _faCallback.JobAborted(cj, GetFirstProcessJob(cj));
  940. _dbCallback.LotFinished(cj);
  941. foreach (var pj in pjAbortList)
  942. {
  943. _lstProcessJobs.Remove(pj);
  944. }
  945. _lstControlJobs.Remove(cj);
  946. if (_loadportControlJobDic.ContainsKey(cj.Module))
  947. {
  948. _loadportControlJobDic[cj.Module] = null;
  949. }
  950. return true;
  951. }
  952. public bool StopJob(string jobName, out string reason)
  953. {
  954. reason = "";
  955. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  956. if (cj == null)
  957. {
  958. reason = $"stop job rejected, not found job with id {jobName}";
  959. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  960. return false;
  961. }
  962. foreach (var pj in _lstProcessJobs)
  963. {
  964. if (pj.ControlJobName == cj.Name)
  965. {
  966. pj.SetState(EnumProcessJobState.Stopping);
  967. }
  968. }
  969. _faCallback.JobStopped(cj, GetFirstProcessJob(cj));
  970. _dbCallback.LotFinished(cj);
  971. if (_loadportControlJobDic.ContainsKey(cj.Module))
  972. {
  973. _loadportControlJobDic[cj.Module] = null;
  974. }
  975. return true;
  976. }
  977. public bool ResumeJob(string jobName, out string reason)
  978. {
  979. reason = "";
  980. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  981. if (cj == null)
  982. {
  983. reason = $"resume job rejected, not found job with id {jobName}";
  984. //LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"resume job rejected, not found job with id {jobName}");
  985. return false;
  986. }
  987. if (cj.State == EnumControlJobState.Paused)
  988. {
  989. cj.SetState(EnumControlJobState.Executing);
  990. }
  991. _faCallback.JobResumed(cj, GetFirstProcessJob(cj));
  992. _cycleState = RState.Running;
  993. return true;
  994. }
  995. public bool PauseJob(string jobName, out string reason)
  996. {
  997. reason = "";
  998. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  999. if (cj == null)
  1000. {
  1001. reason = $"pause job rejected, not found job with id {jobName}";
  1002. //LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"pause job rejected, not found job with id {jobName}");
  1003. return false;
  1004. }
  1005. if (cj.State == EnumControlJobState.Executing)
  1006. {
  1007. cj.SetState(EnumControlJobState.Paused);
  1008. }
  1009. _faCallback.JobPaused(cj, GetFirstProcessJob(cj));
  1010. if (!_lstControlJobs.Exists(job => job.State == EnumControlJobState.Executing))
  1011. _cycleState = RState.Paused;
  1012. return true;
  1013. }
  1014. public bool StartJob(string jobName, out string reason)
  1015. {
  1016. reason = "";
  1017. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  1018. if (cj == null)
  1019. {
  1020. reason = $"start job rejected, not found job with id {jobName}";
  1021. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, reason);
  1022. return false;
  1023. }
  1024. if (RtInstance.ConfigType == ConfigType.Kepler2200)
  1025. {
  1026. foreach (var item in _lstProcessJobs)
  1027. {
  1028. if (item.ControlJobName == cj.Name && !CheckSequenceKepler2200TemperatureReady(item.Sequence))
  1029. {
  1030. return false;
  1031. }
  1032. GetAllJobRecipe(cj, item.Sequence);
  1033. }
  1034. }
  1035. if (cj.State == EnumControlJobState.WaitingForStart)
  1036. {
  1037. cj.SetState(EnumControlJobState.Executing);
  1038. //PreJobClean(cj);
  1039. cj.JetState = EnumJetCtrlJobState.Quequed;
  1040. cj.StartTime = DateTime.Now;
  1041. _dbCallback.LotCreated(cj);
  1042. _faCallback.JobStarted(cj, GetFirstProcessJob(cj));
  1043. //if (!_blockingWatcher.IsRunning)
  1044. // _blockingWatcher.Restart();
  1045. //ResetTraceFlag();
  1046. }
  1047. if (!_cycleWatch.IsRunning)
  1048. {
  1049. _cycleWatch.Restart();
  1050. }
  1051. if (!_lpCycleWafer.Keys.Contains(ModuleHelper.Converter(cj.Module)))
  1052. {
  1053. _lpCycleCount.Add(ModuleHelper.Converter(cj.Module), 0);
  1054. _lpCycleWafer.Add(ModuleHelper.Converter(cj.Module), 0);
  1055. }
  1056. _cycleState = RState.Running;
  1057. return true;
  1058. }
  1059. public void Clear()
  1060. {
  1061. foreach (var module in _dictModuleTask)
  1062. {
  1063. module.Value.Status = ModuleStatus.Idle;
  1064. module.Value.Scheduler.ResetTask();
  1065. }
  1066. List<string> keys = _loadportControlJobDic.Keys.ToList();
  1067. foreach (var key in keys)
  1068. {
  1069. _loadportControlJobDic[key] = null;
  1070. }
  1071. _lstWaferTasks.Clear();
  1072. _tmSchdActions.Clear();
  1073. _curTmAction.Clear();
  1074. _efemSchdActions.Clear();
  1075. _curEfemAction.Clear();
  1076. _lstControlJobs.Clear();
  1077. _lstProcessJobs.Clear();
  1078. InUseRecipes.Clear();
  1079. _cycleState = RState.End;
  1080. }
  1081. // manual return one wafer while system is auto running
  1082. public bool ManualReturnWafer(object[] objs)
  1083. {
  1084. ModuleName SourceModule = (ModuleName)objs[0];
  1085. int SourceSlot = (int)objs[1];
  1086. if (!_dictModuleTask.Keys.Contains(SourceModule))
  1087. {
  1088. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"Invalid source module {SourceModule} for manual return wafer");
  1089. return false;
  1090. }
  1091. if (WaferManager.Instance.CheckNoWafer(SourceModule, SourceSlot))
  1092. {
  1093. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"Can not return wafer as {SourceModule} {SourceSlot} has no wafer");
  1094. return false;
  1095. }
  1096. if (!_dictModuleTask[SourceModule].Scheduler.IsIdle)
  1097. {
  1098. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"The module: {SourceModule} is not ready for return wafer");
  1099. return false;
  1100. }
  1101. var wafer = WaferManager.Instance.GetWafer(SourceModule, SourceSlot);
  1102. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Manual return wafer: {wafer.WaferOrigin} at {SourceModule} {SourceSlot} while system is auto running");
  1103. _qeReturnWafers.Enqueue(wafer);
  1104. return true;
  1105. }
  1106. public RState CheckManualReturnWafer()
  1107. {
  1108. var pmWaferCount = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && WaferManager.Instance.CheckHasWafer(mod.Key, 0)).Count();
  1109. var tmRobotWaferCount = (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 0) ? 1 : 0) + (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 1) ? 1 : 0);
  1110. if (!_dictModuleTask[ModuleName.TMRobot].Scheduler.IsIdle && (pmWaferCount > 0 || tmRobotWaferCount > 0))
  1111. {
  1112. LOG.Write(eEvent.ERR_ROUTER, ModuleName.TMRobot, $"The TM Robot is not ready for return wafer.");
  1113. return RState.Failed;
  1114. }
  1115. if (!_dictModuleTask[ModuleName.EfemRobot].Scheduler.IsIdle)
  1116. {
  1117. LOG.Write(eEvent.ERR_ROUTER, ModuleName.EfemRobot, $"The EFEM Robot is not ready for return wafer.");
  1118. return RState.Failed;
  1119. }
  1120. foreach (var mod in _dictModuleTask)
  1121. {
  1122. if (ModuleHelper.IsLoadPort(mod.Key))
  1123. continue;
  1124. if (!mod.Value.Scheduler.IsIdle)
  1125. {
  1126. LOG.Write(eEvent.ERR_ROUTER, mod.Key, $"{mod.Key} is not ready for return wafer.");
  1127. return RState.Failed;
  1128. }
  1129. int nSlotNumber = ModuleHelper.IsLoadLock(mod.Key) ? (mod.Key == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber) : (ModuleHelper.IsTMRobot(mod.Key) || ModuleHelper.IsEFEMRobot(mod.Key) ? 2 : 1);
  1130. for (int slot = 0; slot < nSlotNumber; slot++)
  1131. {
  1132. var wafer = WaferManager.Instance.GetWafer(mod.Key, slot);
  1133. if (wafer.IsEmpty)
  1134. continue;
  1135. var destLP = (ModuleName)wafer.OriginStation;
  1136. if (!_dictModuleTask[destLP].Scheduler.IsAvailable)
  1137. {
  1138. LOG.Write(eEvent.ERR_ROUTER, destLP, $"The destination Loadport {destLP} is not ready for return wafer.");
  1139. return RState.Failed;
  1140. }
  1141. if(!ModuleHelper.IsLoadPort(destLP))
  1142. {
  1143. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"The wafer {wafer.WaferOrigin} cannot be return, please manually drag it.");
  1144. return RState.Failed;
  1145. }
  1146. }
  1147. }
  1148. Clear();
  1149. return RState.Running;
  1150. }
  1151. public RState ReturnAllWafers()
  1152. {
  1153. int systemWaferCount = 0;
  1154. foreach (var mod in _dictModuleTask)
  1155. {
  1156. if (ModuleHelper.IsLoadPort(mod.Key))
  1157. continue;
  1158. int nSlotNumber = ModuleHelper.IsLoadLock(mod.Key) ? (mod.Key == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber) : (ModuleHelper.IsTMRobot(mod.Key) || ModuleHelper.IsEFEMRobot(mod.Key) ? 2 : 1);
  1159. for (int slot = 0; slot < nSlotNumber; slot++)
  1160. {
  1161. var wafer = WaferManager.Instance.GetWafer(mod.Key, slot);
  1162. if (!wafer.IsEmpty)
  1163. systemWaferCount++;
  1164. }
  1165. }
  1166. if (systemWaferCount == 0)
  1167. return RState.End;
  1168. ReturnVacWafers();
  1169. ReturnAtmWafers();
  1170. return RState.Running;
  1171. }
  1172. #endregion
  1173. #region internal implementation
  1174. private void InitModules()
  1175. {
  1176. foreach (var module in ModuleHelper.InstalledModules)
  1177. {
  1178. if (ModuleHelper.IsInstalled(module))
  1179. {
  1180. if(ModuleHelper.IsPm(module))
  1181. {
  1182. _dictModuleTask[module] = new PMTask(module);
  1183. }
  1184. else if (ModuleHelper.IsLoadLock(module))
  1185. {
  1186. _dictModuleTask[module] = new LoadlockTask(module);
  1187. }
  1188. else if(ModuleHelper.IsLoadPort(module))
  1189. {
  1190. _dictModuleTask[module] = new LoadPortTask(module);
  1191. }
  1192. else if(ModuleHelper.IsTMRobot(module))
  1193. {
  1194. _dictModuleTask[module] = new TMRobotTask(module);
  1195. }
  1196. else if(ModuleHelper.IsEFEMRobot(module))
  1197. {
  1198. _dictModuleTask[module] = new EFEMRobotTask(module);
  1199. }
  1200. else if(ModuleHelper.IsAligner(module))
  1201. {
  1202. _dictModuleTask[module] = new AlignerTask(module);
  1203. }
  1204. }
  1205. }
  1206. }
  1207. private void prelude()
  1208. {
  1209. bool available = true;
  1210. foreach(var mod in _dictModuleTask)
  1211. {
  1212. available = mod.Value.Scheduler.IsAvailable; // force scheduler update
  1213. }
  1214. }
  1215. private void epilogue()
  1216. {
  1217. UpdateProcessJobStatus();
  1218. UpdateControlJobStatus();
  1219. UpdateLLInOutPathProperty();
  1220. PrepareLLPressure();
  1221. CreateNewWaferTask();
  1222. _lstWaferTasks.RemoveAll(item => ModuleHelper.IsLoadPort(item.destMod) && ModuleHelper.IsLoadPort(item.currentMod) && item.pressureStatus == RState.End);
  1223. }
  1224. private void RunWaferTask()
  1225. {
  1226. foreach (var task in _lstWaferTasks)
  1227. {
  1228. task.Run();
  1229. }
  1230. }
  1231. private void RunModuleTasks()
  1232. {
  1233. foreach (var task in _dictModuleTask)
  1234. {
  1235. task.Value.Run();
  1236. }
  1237. }
  1238. private bool CheckAllWaferReturned(ProcessJobInfo pj, bool checkAllProcessed)
  1239. {
  1240. bool allWaferReturn = true;
  1241. foreach (var slot in pj.SlotWafers)
  1242. {
  1243. var wafer = WaferManager.Instance.GetWafer(slot.Item1, slot.Item2);
  1244. if(wafer.IsEmpty || (wafer.ProcessState != EnumWaferProcessStatus.Completed && checkAllProcessed))
  1245. {
  1246. allWaferReturn = false;
  1247. break;
  1248. }
  1249. }
  1250. return allWaferReturn;
  1251. }
  1252. private void UpdateProcessJobStatus()
  1253. {
  1254. if (_efemRobotStatus == RState.Running || _tmRobotStatus == RState.Running)
  1255. return;
  1256. foreach (var pj in _lstProcessJobs)
  1257. {
  1258. if (CheckAllWaferReturned(pj, pj.State != EnumProcessJobState.Stopping))
  1259. {
  1260. pj.SetState(EnumProcessJobState.ProcessingComplete);
  1261. JobDataRecorder.EndPJ(pj.InnerId.ToString(), 0, 0);
  1262. }
  1263. }
  1264. }
  1265. private void UpdateControlJobStatus()
  1266. {
  1267. if (_lstControlJobs.Count == 0 || _efemRobotStatus == RState.Running || _tmRobotStatus == RState.Running)
  1268. return;
  1269. bool allControlJobComplete = true;
  1270. List<ControlJobInfo> cjRemoveList = new List<ControlJobInfo>();
  1271. foreach (var cj in _lstControlJobs)
  1272. {
  1273. if (cj.JetState == EnumJetCtrlJobState.Quequed) // active quequed control job: need prejob clean or don need prejob clean
  1274. {
  1275. var runingJobs = _lstControlJobs.FindAll(job => IsCtrlJobRuning(job));
  1276. if (runingJobs.Count == 0 || (runingJobs.Count == 1 && IsCtrlJobEndingState(runingJobs.First())))
  1277. {
  1278. var quequedJobs = _lstControlJobs.FindAll(job => job.JetState == EnumJetCtrlJobState.Quequed);
  1279. var firstQuequeJob = quequedJobs.OrderBy(job => job.StartTime).First();
  1280. if (firstQuequeJob.InnerId == cj.InnerId)
  1281. {
  1282. if (IsCtrlJobNeedPreClean(cj))
  1283. {
  1284. cj.JetState = EnumJetCtrlJobState.PreJobClean;
  1285. var PMs = GetWaitPreCleanPMsByCtrlJob(cj);
  1286. foreach (var pm in PMs)
  1287. {
  1288. var pmTask = _dictModuleTask[pm] as PMTask;
  1289. if(pmTask != null && pmTask.Scheduler.IsOnline)
  1290. {
  1291. pmTask.InvokePreJobClean(cj.PreJobClean);
  1292. }
  1293. }
  1294. }
  1295. else
  1296. {
  1297. ActiveControlJob(cj);
  1298. }
  1299. }
  1300. }
  1301. }
  1302. else if (IsCtrlJobRuning(cj))
  1303. {
  1304. if (cj.JetState == EnumJetCtrlJobState.PreJobClean)
  1305. {
  1306. if (IsPreJobCleanDone(cj))
  1307. {
  1308. ActiveControlJob(cj);
  1309. }
  1310. }
  1311. else
  1312. {
  1313. if (cj.JetState == EnumJetCtrlJobState.Processing && IsCtrlJobNeedPostClean(cj)) // active post job clean
  1314. {
  1315. if (IsAllJobWaferProcessedOrProcessing(cj))
  1316. {
  1317. cj.JetState = EnumJetCtrlJobState.PostJobClean;
  1318. var PMs = GetWaitPreCleanPMsByCtrlJob(cj);
  1319. foreach (var pm in PMs)
  1320. {
  1321. var pmTask = _dictModuleTask[pm] as PMTask;
  1322. if (pmTask != null && pmTask.Scheduler.IsOnline)
  1323. {
  1324. pmTask.InvokePostJobClean(cj.PostJobClean);
  1325. }
  1326. }
  1327. }
  1328. }
  1329. // control job complete
  1330. if (IsAllProcessJobComplete(cj) && (!IsCtrlJobNeedPostClean(cj) || cj.JetState == EnumJetCtrlJobState.PostJobClean && IsPostJobCleanDone(cj)))
  1331. {
  1332. cj.JetState = EnumJetCtrlJobState.Completed;
  1333. cj.SetState(EnumControlJobState.Completed);
  1334. cj.EndTime = DateTime.Now;
  1335. _faCallback.JobFinished(cj, GetFirstProcessJob(cj));
  1336. _dbCallback.LotFinished(cj);
  1337. if (!(_isCycleMode && _cycledCount < _cycleSetPoint))
  1338. (Singleton<TransferModule>.Instance.GetScheduler(ModuleHelper.Converter(cj.Module)) as SchedulerLoadPort).NoteJobComplete();
  1339. _lpCycleCount[ModuleHelper.Converter(cj.Module)]++;
  1340. }
  1341. }
  1342. int countProcessed = 0;
  1343. foreach (var pjName in cj.ProcessJobNameList)
  1344. {
  1345. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1346. if (pj == null)
  1347. {
  1348. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Not find pj named {pjName} in {cj.Name}");
  1349. continue;
  1350. }
  1351. // caculate process wafer by process
  1352. if (_isCycleMode && _cycledCount < (_isCycleMode ? _cycleSetPoint : 0))
  1353. {
  1354. foreach (var pjSlotWafer in pj.SlotWafers)
  1355. {
  1356. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1357. if (!wafer.IsEmpty && wafer.ProcessState == EnumWaferProcessStatus.Completed)
  1358. countProcessed++;
  1359. }
  1360. }
  1361. }
  1362. int lpCycleWafer = _lpCycleCount[ModuleHelper.Converter(cj.Module)] * cj.LotWafers.Count + (cj.JetState == EnumJetCtrlJobState.Completed && cj.LotWafers.Count == countProcessed ? 0 : countProcessed);
  1363. if (_lpCycleCount[ModuleHelper.Converter(cj.Module)] != _cycledCount || lpCycleWafer != _lpCycleWafer[ModuleHelper.Converter(cj.Module)])
  1364. {
  1365. _lpCycleWafer[ModuleHelper.Converter(cj.Module)] = lpCycleWafer;
  1366. }
  1367. }
  1368. LoadportCassetteState state = (LoadportCassetteState)DATA.Poll($"{cj.Module}.CassetteState");
  1369. if (cj.State == EnumControlJobState.Completed && state != LoadportCassetteState.Normal && cj.JetState == EnumJetCtrlJobState.Completed)
  1370. {
  1371. cjRemoveList.Add(cj);
  1372. }
  1373. allControlJobComplete = allControlJobComplete && cj.State == EnumControlJobState.Completed;
  1374. }
  1375. if (_isCycleMode && _cycledCount < (_isCycleMode ? _cycleSetPoint : 0))
  1376. {
  1377. int totolCycleWafer = _lpCycleWafer.Sum(item => item.Value);
  1378. if (totolCycleWafer != _cycledWafer || _lpCycleCount.Sum(item => item.Value) > 0 && _throughput < 0.01) // refresh _throughput in time
  1379. {
  1380. _cycledWafer = totolCycleWafer;
  1381. if (_cycledCount > 0 || allControlJobComplete)
  1382. {
  1383. _throughput = (float)(_cycledWafer / _cycleWatch.Elapsed.TotalHours);
  1384. }
  1385. else
  1386. {
  1387. _throughput = 0;
  1388. }
  1389. }
  1390. if (allControlJobComplete)
  1391. {
  1392. _cycledCount++;
  1393. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Cycle Count: {_cycledCount}, Total Processed Wafer: {_cycledWafer}, Throughput: {_throughput}");
  1394. if (_cycledCount < _cycleSetPoint)
  1395. {
  1396. foreach (var cj in _lstControlJobs)
  1397. {
  1398. cj.SetState(EnumControlJobState.Executing);
  1399. cj.JetState = EnumJetCtrlJobState.Quequed;
  1400. cj.LotInnerId = Guid.NewGuid();
  1401. _dbCallback.LotCreated(cj);
  1402. }
  1403. foreach (var pj in _lstProcessJobs)
  1404. {
  1405. pj.SetState(EnumProcessJobState.Queued);
  1406. pj.InnerId = Guid.NewGuid();
  1407. foreach (var pjSlotWafer in pj.SlotWafers)
  1408. {
  1409. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1410. wafer.ProcessJob = null;
  1411. wafer.NextSequenceStep = 0;
  1412. wafer.ProcessState = EnumWaferProcessStatus.Idle;
  1413. }
  1414. }
  1415. _lstWaferTasks.Clear();
  1416. }
  1417. else
  1418. _cycleState = RState.End;
  1419. }
  1420. }
  1421. foreach (var cj in cjRemoveList)
  1422. {
  1423. List<ProcessJobInfo> pjRemoveList = new List<ProcessJobInfo>();
  1424. foreach (var pj in _lstProcessJobs)
  1425. {
  1426. if (pj.ControlJobName == cj.Name)
  1427. pjRemoveList.Add(pj);
  1428. }
  1429. foreach (var pj in pjRemoveList)
  1430. {
  1431. _lstProcessJobs.Remove(pj);
  1432. }
  1433. _lstControlJobs.Remove(cj);
  1434. }
  1435. }
  1436. private bool ActiveProcessJob(ProcessJobInfo pj)
  1437. {
  1438. foreach (var pjSlotWafer in pj.SlotWafers)
  1439. {
  1440. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1441. wafer.ProcessJob = pj;
  1442. wafer.NextSequenceStep = 0;
  1443. WaferDataRecorder.SetPjInfo(wafer.InnerId.ToString(), pj.InnerId.ToString());
  1444. }
  1445. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == pj.ControlJobName);
  1446. CarrierInfo carrier = CarrierManager.Instance.GetCarrier(cj.Module);
  1447. JobDataRecorder.StartPJ(pj.InnerId.ToString(), carrier.InnerId.ToString(), cj.InnerId.ToString(), pj.Name, cj.Module, cj.Module, pj.SlotWafers.Count);
  1448. pj.SetState(EnumProcessJobState.Processing);
  1449. return true;
  1450. }
  1451. private bool ActiveControlJob(ControlJobInfo cj)
  1452. {
  1453. cj.JetState = EnumJetCtrlJobState.Processing;
  1454. foreach (var pjName in cj.ProcessJobNameList)
  1455. {
  1456. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1457. if (pj == null)
  1458. {
  1459. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Not find pj named {pjName} in {cj.Name}");
  1460. continue;
  1461. }
  1462. if (pj.State == EnumProcessJobState.Queued)
  1463. {
  1464. ActiveProcessJob(pj);
  1465. }
  1466. }
  1467. return true;
  1468. }
  1469. private bool IsAllJobWaferProcessedOrProcessing(ControlJobInfo cj)
  1470. {
  1471. List<ModuleName> allModules = _dictModuleTask.Keys.ToList();
  1472. int original = (int)ModuleHelper.Converter(cj.Module);
  1473. foreach (var mod in allModules)
  1474. {
  1475. if (ModuleHelper.IsLoadPort(mod) && (int)mod != original)
  1476. continue;
  1477. var wafers = WaferManager.Instance.GetWafers(mod);
  1478. foreach (var wafer in wafers)
  1479. {
  1480. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null || wafer.ProcessJob.ControlJobName != cj.Name)
  1481. continue;
  1482. if (wafer.ProcessState != EnumWaferProcessStatus.Completed && wafer.ProcessState != EnumWaferProcessStatus.InProcess)
  1483. return false; ;
  1484. }
  1485. }
  1486. return true;
  1487. }
  1488. private bool IsAllProcessJobComplete(ControlJobInfo cj)
  1489. {
  1490. foreach (var pj in _lstProcessJobs)
  1491. {
  1492. if (pj.ControlJobName == cj.Name && pj.State != EnumProcessJobState.ProcessingComplete)
  1493. {
  1494. return false;
  1495. }
  1496. }
  1497. return true;
  1498. }
  1499. private bool IsCtrlJobRuning(ControlJobInfo cj)
  1500. {
  1501. return cj.JetState == EnumJetCtrlJobState.PreJobClean ||
  1502. cj.JetState == EnumJetCtrlJobState.Processing ||
  1503. cj.JetState == EnumJetCtrlJobState.PostJobClean;
  1504. }
  1505. private bool IsCtrlJobEndingState(ControlJobInfo cj)
  1506. {
  1507. return cj.JetState == EnumJetCtrlJobState.PostJobClean ||
  1508. (cj.JetState == EnumJetCtrlJobState.Processing
  1509. && IsAllJobWaferProcessedOrProcessing(cj)
  1510. && !IsCtrlJobNeedPostClean(cj));
  1511. }
  1512. private bool IsCtrlJobNeedPreClean(ControlJobInfo cj)
  1513. {
  1514. return !string.IsNullOrWhiteSpace(cj.PreJobClean);
  1515. }
  1516. private bool IsCtrlJobNeedPostClean(ControlJobInfo cj)
  1517. {
  1518. return !string.IsNullOrWhiteSpace(cj.PostJobClean.Trim());
  1519. }
  1520. private List<ModuleName> GetWaitPreCleanPMsByCtrlJob(ControlJobInfo cj)
  1521. {
  1522. var pmlist = new List<ModuleName>();
  1523. foreach (var pj in _lstProcessJobs)
  1524. {
  1525. if (pj.ControlJobName == cj.Name)
  1526. {
  1527. foreach (var pm in pj.Sequence.PMs)
  1528. {
  1529. if (!pmlist.Contains(pm))
  1530. pmlist.Add(pm);
  1531. }
  1532. }
  1533. }
  1534. return pmlist;
  1535. }
  1536. private bool IsPreJobCleanDone(ControlJobInfo cj)
  1537. {
  1538. var pms = GetWaitPreCleanPMsByCtrlJob(cj);
  1539. foreach(var pm in pms)
  1540. {
  1541. var pmTask = _dictModuleTask[pm] as PMTask;
  1542. if (pmTask.Scheduler.IsOnline && !pmTask.IsPreJobCleanDone())
  1543. return false;
  1544. }
  1545. return true;
  1546. }
  1547. private bool IsPostJobCleanDone(ControlJobInfo cj)
  1548. {
  1549. var pms = GetWaitPreCleanPMsByCtrlJob(cj);
  1550. foreach (var pm in pms)
  1551. {
  1552. var pmTask = _dictModuleTask[pm] as PMTask;
  1553. if (pmTask.Scheduler.IsOnline && !pmTask.IsPostJobCleanDone())
  1554. return false;
  1555. }
  1556. return true;
  1557. }
  1558. private void WaferArrived(WaferTask wafer, MoveItem item)
  1559. {
  1560. _dictModuleTask[item.DestinationModule].WaferArrived(wafer, item.DestinationSlot);
  1561. //--2024-03-21 增加了PortJobWaferEnd 上报事件 start--
  1562. if (ModuleHelper.IsLoadPort(item.DestinationModule))
  1563. {
  1564. ControlJobInfo currentControlJob = GetLoadPortCurrentControlJob(item.DestinationModule);
  1565. if (currentControlJob != null)
  1566. {
  1567. _faCallback.JobWaferEnd(currentControlJob, item.SourceSlot);
  1568. }
  1569. }
  1570. //--2024-03-21 增加了PortJobWaferEnd 上报事件 end--
  1571. }
  1572. private void WaferLeaved(WaferTask wafer, MoveItem item)
  1573. {
  1574. _dictModuleTask[item.SourceModule].WaferLeaved(wafer, item.DestinationSlot);
  1575. //--2024-03-21 增加了PortJobWaferStart 上报事件 start--
  1576. if (ModuleHelper.IsLoadPort(item.SourceModule))
  1577. {
  1578. ControlJobInfo currentControlJob = GetLoadPortCurrentControlJob(item.SourceModule);
  1579. if (currentControlJob != null)
  1580. {
  1581. _faCallback.JobWaferStart(currentControlJob, item.SourceSlot);
  1582. }
  1583. }
  1584. //--2024-03-21 增加了PortJobWaferStart 上报事件 end--
  1585. }
  1586. private ModuleName GetComingAvailablePM(ControlJobInfo cj)
  1587. {
  1588. var lstInProcessPMs = new List<ModuleName>();
  1589. foreach (var wafer in cj.LotWafers)
  1590. {
  1591. if(!wafer.IsEmpty && wafer.NextSequenceStep == 0 && wafer.ProcessJob != null)
  1592. {
  1593. foreach(var pm in wafer.ProcessJob.Sequence.PMs)
  1594. {
  1595. if (!lstInProcessPMs.Contains(pm) && _dictModuleTask[pm].Scheduler.IsOnline)
  1596. lstInProcessPMs.Add(pm);
  1597. }
  1598. }
  1599. }
  1600. // blocking check while only one loadlock in out
  1601. if (LLInOutPath == SequenceLLInOutPath.AInAOut ||
  1602. LLInOutPath == SequenceLLInOutPath.BInBOut ||
  1603. !_dictModuleTask[ModuleName.LLA].Scheduler.IsOnline ||
  1604. !_dictModuleTask[ModuleName.LLB].Scheduler.IsOnline)
  1605. {
  1606. if (_lstWaferTasks.Count >= lstInProcessPMs.Count + _LLASlotNumber / 2)
  1607. return ModuleName.System;
  1608. }
  1609. Dictionary<ModuleName, int> pmAssociatedWaferCount = new Dictionary<ModuleName, int>();
  1610. foreach (var mod in _dictModuleTask)
  1611. {
  1612. if (ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsOnline && lstInProcessPMs.Contains(mod.Key))
  1613. {
  1614. pmAssociatedWaferCount[mod.Key] = 0;
  1615. }
  1616. }
  1617. var unprocessedWafer = _lstWaferTasks.Where(task => ModuleHelper.IsPm(task.destMod));
  1618. foreach (var wafer in unprocessedWafer)
  1619. {
  1620. if (ModuleHelper.IsPm(wafer.destMod) && _dictModuleTask[wafer.destMod].Scheduler.IsOnline && lstInProcessPMs.Contains(wafer.destMod))
  1621. {
  1622. pmAssociatedWaferCount[wafer.destMod]++;
  1623. }
  1624. }
  1625. var oderedPMCount = pmAssociatedWaferCount.OrderBy(p => p.Value).ToDictionary(p => p.Key, o => o.Value);
  1626. if (oderedPMCount.Count > 0 && oderedPMCount.First().Value < 2)
  1627. return oderedPMCount.First().Key;
  1628. return ModuleName.System;
  1629. }
  1630. private void CreateNewWaferTask()
  1631. {
  1632. var cj = _lstControlJobs.Find(c => c.JetState == EnumJetCtrlJobState.PreJobClean || c.JetState == EnumJetCtrlJobState.Processing);
  1633. if (cj != null)
  1634. {
  1635. var pm = GetComingAvailablePM(cj);
  1636. if (pm != ModuleName.System)
  1637. {
  1638. foreach (var wafer in cj.LotWafers)
  1639. {
  1640. if (wafer.IsEmpty || wafer.ProcessJob == null)
  1641. continue;
  1642. if (wafer.ProcessJob.Sequence.PMs.Contains(pm) && wafer.NextSequenceStep == 0 && !_lstWaferTasks.Exists(task => task.sourceMod == (ModuleName)wafer.OriginStation && task.sourceSlot == wafer.OriginSlot))
  1643. {
  1644. CreateWaferTasks(wafer, pm, wafer.ProcessJob.Sequence.LLDelayTime);
  1645. return;
  1646. }
  1647. }
  1648. }
  1649. }
  1650. }
  1651. private void CreateWaferTasks(WaferInfo wafer, ModuleName pm, int LLDeayTime)
  1652. {
  1653. var recipeName = wafer.ProcessJob.Sequence.GetRecipe(pm);
  1654. var recipeContent = RecipeFileManager.Instance.LoadRecipe(pm.ToString(), recipeName, false, RecipeType.Process.ToString());
  1655. Recipe recipe = Recipe.Load(recipeContent);
  1656. int temperature = 0;
  1657. if(int.TryParse(recipe.Header.Temperature, out int temp))
  1658. {
  1659. temperature = temp;
  1660. }
  1661. var waferTask = new WaferTask((ModuleName)wafer.OriginStation,
  1662. wafer.OriginSlot,
  1663. pm,
  1664. 0,
  1665. temperature,
  1666. wafer.InnerId,
  1667. recipeName,
  1668. wafer.ProcessJob.Sequence.WTWCleanRecipe,
  1669. wafer.ProcessJob.Sequence.LLInOutPath,
  1670. LLDeayTime);
  1671. waferTask.OnWaferArrived += WaferArrived;
  1672. waferTask.OnWaferLeaved += WaferLeaved;
  1673. _lstWaferTasks.Add(waferTask);
  1674. }
  1675. private bool ForwardATMWafers(ModuleName ll)
  1676. {
  1677. var emptySlots = _LLSlotInOutOption == LLSlotInOutOpt.AllInAllOut ? GetLLReadyInOutSlots(ll).emptySlot : GetLLFixedReadyInOutSlots(ll).eInSlot;
  1678. var atmWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End ||wafer.movingStatus == RState.Init) && (ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod))).ToList();
  1679. var notAlignedWafer = atmWafers.Where(wafer => !wafer.IsAligned).ToList();
  1680. var readyInWafers = _lstWaferTasks.Where(wafer => ModuleHelper.IsLoadPort(wafer.currentMod) && wafer.movingStatus == RState.Init && wafer.pressureStatus == RState.Init && CanWaferGotoLL(wafer, ll)).OrderBy(wafer => wafer.currentSlot).ToArray();
  1681. var freeHands = GetEFEMFreeHand();
  1682. if ((atmWafers.Count >= 2 && atmWafers.Count(wafer => !wafer.IsAligned) == 0) || freeHands.Count == 0 || readyInWafers.Length == 0)
  1683. return false;
  1684. // force all wafer aligned
  1685. if (notAlignedWafer.Count > 0 && _lstWaferTasks.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 0)
  1686. {
  1687. notAlignedWafer.First().RouteTo(ModuleName.Aligner1, 0);
  1688. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(notAlignedWafer.First().currentMod, notAlignedWafer.First().currentSlot, ModuleName.Aligner1, 0, (Hand)notAlignedWafer.First().currentSlot) });
  1689. }
  1690. // forward one wafer
  1691. else if ((readyInWafers.Length > 0) && (atmWafers.Count == 0 || (atmWafers.Count == 1 && _lstWaferTasks.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 0 && emptySlots.Count >= 2)))
  1692. {
  1693. readyInWafers[0].RouteTo(ModuleName.Aligner1, 0);
  1694. // pick from LP
  1695. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyInWafers[0].currentMod, readyInWafers[0].currentSlot, ModuleName.EfemRobot, (int)freeHands[0], freeHands[0]) });
  1696. // place to aligner
  1697. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.EfemRobot, (int)freeHands[0], ModuleName.Aligner1, 0, freeHands[0]) });
  1698. }
  1699. // forward aligner wafer
  1700. else if (atmWafers.Count == 1 && emptySlots.Count >= 2 && ModuleHelper.IsAligner(atmWafers.First().currentMod) && freeHands.Count > 1)
  1701. {
  1702. atmWafers.First().RouteTo(ModuleName.EfemRobot, (int)freeHands[1]);
  1703. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(atmWafers.First().currentMod, atmWafers.First().currentSlot, ModuleName.EfemRobot, (int)freeHands[1], freeHands[1]) });
  1704. }
  1705. return _efemSchdActions.Count > 0;
  1706. }
  1707. private bool ExchangeWaferWithLL(ModuleName ll)
  1708. {
  1709. if (IsLLReservedByTM(ll))
  1710. return false;
  1711. var waferStaus = GetLLReadyInOutSlots(ll);
  1712. var atmWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.IsAligned && (ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod))).ToList();
  1713. var freeHands = GetEFEMFreeHand();
  1714. var swapActions = new List<MoveItem>();
  1715. var validHands = new List<Hand>();
  1716. int placeCount = 0;
  1717. if ( waferStaus.emptySlot.Count > 0 &&
  1718. freeHands.Count >= 1 &&
  1719. atmWafers.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 1 &&
  1720. (atmWafers.Count(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)) == 0 || waferStaus.emptySlot.Count >= 2))
  1721. {
  1722. var alignerWafer = atmWafers.Where(wafer => ModuleHelper.IsAligner(wafer.currentMod)).First();
  1723. if (CanWaferGotoLL(alignerWafer, ll))
  1724. {
  1725. alignerWafer.RouteTo(ll, waferStaus.emptySlot[placeCount]);
  1726. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(alignerWafer.currentMod, alignerWafer.currentSlot, ModuleName.EfemRobot, (int)freeHands[0], freeHands[0]) });
  1727. swapActions.Add(new MoveItem(ModuleName.EfemRobot, (int)freeHands[0], ll, waferStaus.emptySlot[placeCount], freeHands[0]));
  1728. validHands.Add(freeHands[0]);
  1729. placeCount++;
  1730. }
  1731. }
  1732. var armWafers = atmWafers.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToArray();
  1733. foreach(var wafer in armWafers)
  1734. {
  1735. if (CanWaferGotoLL(wafer, ll) && placeCount < waferStaus.emptySlot.Count)
  1736. {
  1737. wafer.RouteTo(ll, waferStaus.emptySlot[placeCount]);
  1738. swapActions.Add(new MoveItem(wafer.currentMod, wafer.currentSlot, ll, waferStaus.emptySlot[placeCount], (Hand)wafer.currentSlot));
  1739. if(!validHands.Contains((Hand)wafer.currentSlot))
  1740. validHands.Add((Hand)wafer.currentSlot);
  1741. placeCount++;
  1742. }
  1743. }
  1744. if (!validHands.Contains(Hand.Blade1) && WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, (int)Hand.Blade1) && _efemRobotSingleArmOption != 2)
  1745. validHands.Add(Hand.Blade1);
  1746. if (!validHands.Contains(Hand.Blade2) && WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, (int)Hand.Blade2) && _efemRobotSingleArmOption != 1)
  1747. validHands.Add(Hand.Blade2);
  1748. int pickCount = 0;
  1749. var returnActions = new List<MoveItem>();
  1750. foreach(var slot in waferStaus.outSlot)
  1751. {
  1752. if(pickCount < validHands.Count)
  1753. {
  1754. var outWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == ll && wafer.currentSlot == slot);
  1755. if(outWafer != null)
  1756. {
  1757. outWafer.RouteTo(outWafer.destMod, outWafer.destSlot);
  1758. swapActions.Add(new MoveItem(ll, waferStaus.outSlot[pickCount], ModuleName.EfemRobot, (int)validHands[pickCount], validHands[pickCount]));
  1759. returnActions.Add(new MoveItem(ModuleName.EfemRobot, (int)validHands[pickCount], outWafer.destMod, outWafer.destSlot, validHands[pickCount]));
  1760. pickCount++;
  1761. }
  1762. }
  1763. }
  1764. if(swapActions.Count > 0)
  1765. {
  1766. _efemSchdActions.Enqueue(swapActions);
  1767. }
  1768. foreach(var action in returnActions)
  1769. {
  1770. _efemSchdActions.Enqueue(new List<MoveItem> { action });
  1771. }
  1772. return _efemSchdActions.Count > 0;
  1773. }
  1774. private bool ExchangeWaferWithFixedSlotLL(ModuleName ll)
  1775. {
  1776. if (IsLLReservedByTM(ll))
  1777. return false;
  1778. var llWaferStatus = GetLLFixedReadyInOutSlots(ll);
  1779. var atmWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.IsAligned && (ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod))).ToList();
  1780. var freeHands = GetEFEMFreeHand();
  1781. var swapActions = new List<MoveItem>();
  1782. var validHands = new List<Hand>();
  1783. int placeCount = 0;
  1784. if (llWaferStatus.eInSlot.Count > 0 &&
  1785. freeHands.Count >= 1 &&
  1786. atmWafers.Count(wafer => ModuleHelper.IsAligner(wafer.currentMod)) == 1 &&
  1787. (atmWafers.Count(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)) == 0 || llWaferStatus.eInSlot.Count >= 2))
  1788. {
  1789. var alignerWafer = atmWafers.Where(wafer => ModuleHelper.IsAligner(wafer.currentMod)).First();
  1790. if (CanWaferGotoLL(alignerWafer, ll))
  1791. {
  1792. alignerWafer.RouteTo(ll, llWaferStatus.eInSlot[placeCount]);
  1793. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(alignerWafer.currentMod, alignerWafer.currentSlot, ModuleName.EfemRobot, (int)freeHands[0], freeHands[0]) });
  1794. swapActions.Add(new MoveItem(ModuleName.EfemRobot, (int)freeHands[0], ll, llWaferStatus.eInSlot[placeCount], freeHands[0]));
  1795. validHands.Add(freeHands[0]);
  1796. placeCount++;
  1797. }
  1798. }
  1799. var armWafers = atmWafers.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToArray();
  1800. foreach (var wafer in armWafers)
  1801. {
  1802. if (CanWaferGotoLL(wafer, ll) && placeCount < llWaferStatus.eInSlot.Count)
  1803. {
  1804. wafer.RouteTo(ll, llWaferStatus.eInSlot[placeCount]);
  1805. swapActions.Add(new MoveItem(wafer.currentMod, wafer.currentSlot, ll, llWaferStatus.eInSlot[placeCount], (Hand)wafer.currentSlot));
  1806. if (!validHands.Contains((Hand)wafer.currentSlot))
  1807. validHands.Add((Hand)wafer.currentSlot);
  1808. placeCount++;
  1809. }
  1810. }
  1811. if (!validHands.Contains(Hand.Blade1) && WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, (int)Hand.Blade1) && _efemRobotSingleArmOption != 2)
  1812. validHands.Add(Hand.Blade1);
  1813. if (!validHands.Contains(Hand.Blade2) && WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, (int)Hand.Blade2) && _efemRobotSingleArmOption != 1)
  1814. validHands.Add(Hand.Blade2);
  1815. int pickCount = 0;
  1816. var returnActions = new List<MoveItem>();
  1817. foreach (var slot in llWaferStatus.eOutSlot)
  1818. {
  1819. if (pickCount < validHands.Count)
  1820. {
  1821. var outWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == ll && wafer.currentSlot == slot);
  1822. if (outWafer != null)
  1823. {
  1824. outWafer.RouteTo(outWafer.destMod, outWafer.destSlot);
  1825. swapActions.Add(new MoveItem(ll, llWaferStatus.eOutSlot[pickCount], ModuleName.EfemRobot, (int)validHands[pickCount], validHands[pickCount]));
  1826. returnActions.Add(new MoveItem(ModuleName.EfemRobot, (int)validHands[pickCount], outWafer.destMod, outWafer.destSlot, validHands[pickCount]));
  1827. pickCount++;
  1828. }
  1829. }
  1830. }
  1831. if (swapActions.Count > 0)
  1832. {
  1833. _efemSchdActions.Enqueue(swapActions);
  1834. }
  1835. foreach (var action in returnActions)
  1836. {
  1837. _efemSchdActions.Enqueue(new List<MoveItem> { action });
  1838. }
  1839. return _efemSchdActions.Count > 0;
  1840. }
  1841. private void RoutingATMWafers()
  1842. {
  1843. if (_efemSchdActions.Count > 0 || _curEfemAction.Count > 0)
  1844. {
  1845. RunSchdEFEMActions();
  1846. return;
  1847. }
  1848. if (_efemRobotStatus != RState.End)
  1849. return;
  1850. if(_LLSlotInOutOption == LLSlotInOutOpt.AllInAllOut)
  1851. {
  1852. if (_LLASlotNumber == 2)
  1853. {
  1854. Routing2SlotATMSystem();
  1855. }
  1856. else if (_LLASlotNumber == 4)
  1857. {
  1858. Routing4SlotATMSystem();
  1859. }
  1860. }
  1861. else
  1862. {
  1863. RoutingFixedSlotATMSystem();
  1864. }
  1865. }
  1866. private void Routing4SlotATMSystem()
  1867. {
  1868. var atmWaferCount = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod)).Count();
  1869. var efemRobotWafers = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToList();
  1870. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut) // don need check system wafer existence as no blocking risk
  1871. {
  1872. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  1873. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  1874. var inLLWaferStatus = GetLLReadyInOutSlots(inLL);
  1875. var outLLWaferStatus = GetLLReadyInOutSlots(outLL);
  1876. if (efemRobotWafers.Count() >= 1)
  1877. {
  1878. if (inLLWaferStatus.emptySlot.Count >= 2 && !IsLLReservedByTM(inLL)) // wait for in loadlock ready
  1879. {
  1880. if (_dictModuleTask[inLL].TimeToReady < 2)
  1881. {
  1882. ExchangeWaferWithLL(inLL);
  1883. }
  1884. }
  1885. if ((inLLWaferStatus.emptySlot.Count == 0 || (outLLWaferStatus.outSlot.Count >= 1 && _dictModuleTask[outLL].TimeToReady < 2)) && !IsLLReservedByTM(outLL)) // ready return wafer
  1886. {
  1887. ExchangeWaferWithLL(outLL);
  1888. }
  1889. }
  1890. else
  1891. {
  1892. if (((outLLWaferStatus.outSlot.Count >= 2 && _dictModuleTask[outLL].TimeToReady < 10) ||
  1893. (outLLWaferStatus.outSlot.Count == 1 && _dictModuleTask[outLL].TimeToReady < 2)) && !IsLLReservedByTM(outLL))
  1894. {
  1895. ExchangeWaferWithLL(outLL);
  1896. }
  1897. else if (inLLWaferStatus.emptySlot.Count >= 2)
  1898. {
  1899. if (((_dictModuleTask[inLL].TimeToReady > 20 || _dictModuleTask[inLL].Scheduler.IsVac) && atmWaferCount < 2) || atmWaferCount == 0)
  1900. {
  1901. ForwardATMWafers(inLL);
  1902. }
  1903. else if (atmWaferCount >= 2 && !IsLLReservedByTM(inLL))
  1904. {
  1905. ExchangeWaferWithLL(inLL);
  1906. }
  1907. }
  1908. }
  1909. }
  1910. else
  1911. {
  1912. RoutingSameLLInOutATMSystem();
  1913. }
  1914. }
  1915. private void Routing2SlotATMSystem()
  1916. {
  1917. var atmWaferCount = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod)).Count();
  1918. var efemRobotWafers = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod)).ToList();
  1919. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut) // don need check system wafer existence as no blocking risk
  1920. {
  1921. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  1922. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  1923. var inLLWaferStatus = GetLLReadyInOutSlots(inLL);
  1924. var outLLWaferStatus = GetLLReadyInOutSlots(outLL);
  1925. // whether can do double return in 5 second, single return with single arm option or LL under atm pressure
  1926. if ((outLLWaferStatus.outSlot.Count == 2 && _dictModuleTask[outLL].TimeToReady < 5 && (atmWaferCount < 2 || !_dictModuleTask[outLL].Scheduler.IsAtm)) ||
  1927. (outLLWaferStatus.outSlot.Count == 1 && ((_dictModuleTask[outLL].TimeToReady < 2 && _dictModuleTask[outLL].Scheduler.IsAtm)
  1928. || _efemRobotSingleArmOption != 0
  1929. || _lstWaferTasks.Where(wt => ModuleHelper.IsLoadPort(wt.destMod)).Count() == 1)))
  1930. {
  1931. if (ExchangeWaferWithLL(outLL))
  1932. return;
  1933. }
  1934. if(atmWaferCount == 0 || (atmWaferCount == 1 && inLLWaferStatus.emptySlot.Count == 2 && _efemRobotSingleArmOption == 0 && (outLLWaferStatus.outSlot.Count < 2 || _dictModuleTask[outLL].TimeToReady > 20)))
  1935. {
  1936. if (ForwardATMWafers(inLL))
  1937. return;
  1938. }
  1939. if((inLLWaferStatus.emptySlot.Count == 2 || (inLLWaferStatus.emptySlot.Count == 1 && _efemRobotSingleArmOption != 0) && _dictModuleTask[inLL].TimeToReady < 2))
  1940. {
  1941. ExchangeWaferWithLL(inLL);
  1942. }
  1943. }
  1944. else
  1945. {
  1946. RoutingSameLLInOutATMSystem();
  1947. }
  1948. }
  1949. private void RoutingFixedSlotATMSystem()
  1950. {
  1951. var atmWaferCount = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod)).Count();
  1952. var lls = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.Scheduler.IsOnline);
  1953. if ( atmWaferCount == 0 || (_efemRobotSingleArmOption == 0 && atmWaferCount == 1 ))
  1954. {
  1955. foreach(var ll in lls)
  1956. {
  1957. if (ForwardATMWafers(ll.Key))
  1958. return;
  1959. }
  1960. }
  1961. // try to match a ll swap action
  1962. var readyReturnLL = lls.Where(ll => (GetLLFixedReadyInOutSlots(ll.Key).eOutSlot.Count + GetLLFixedReadyInOutSlots(ll.Key).eInSlot.Count > 0) && ll.Value.TimeToReady <= 10)
  1963. .OrderByDescending(ll => GetLLFixedReadyInOutSlots(ll.Key).eInSlot.Count + GetLLFixedReadyInOutSlots(ll.Key).eOutSlot.Count)
  1964. .OrderBy(ll => ll.Value.TimeToReady);
  1965. foreach (var ll in readyReturnLL)
  1966. {
  1967. if (ExchangeWaferWithFixedSlotLL(ll.Key))
  1968. return;
  1969. }
  1970. }
  1971. private void RoutingSameLLInOutATMSystem()
  1972. {
  1973. var atmWaferCount = _lstWaferTasks.Where(wafer => ModuleHelper.IsEFEMRobot(wafer.currentMod) || ModuleHelper.IsAligner(wafer.currentMod)).Count();
  1974. var vacWaferCount = _lstWaferTasks.Where(task => ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod) || ModuleHelper.IsLoadLock(task.currentMod)).Count();
  1975. var preferWaferVacCount = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsOnline).Count() +
  1976. (LLInOutPath == SequenceLLInOutPath.AInAOut ? _LLASlotNumber / 2 : (LLInOutPath == SequenceLLInOutPath.BInBOut ? _LLBSlotNumber / 2 : _LLASlotNumber));
  1977. var inOutIndicator = vacWaferCount - preferWaferVacCount;
  1978. if (inOutIndicator < 0)
  1979. {
  1980. // ready to push in
  1981. var readyLLs = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) &&
  1982. ((mod.Key == ModuleName.LLA && LLInOutPath != SequenceLLInOutPath.BInBOut) || (mod.Key == ModuleName.LLB && LLInOutPath != SequenceLLInOutPath.AInAOut)) &&
  1983. mod.Value.TimeToReady <= 10 &&
  1984. !IsLLReservedByTM(mod.Key) &&
  1985. GetLLReadyInOutSlots(mod.Key).emptySlot.Count >= 1).
  1986. OrderBy(mod => mod.Value.TimeToReady).ToList();
  1987. if (atmWaferCount >= 1)
  1988. {
  1989. foreach(var ll in readyLLs)
  1990. {
  1991. if (ExchangeWaferWithLL(ll.Key))
  1992. return;
  1993. }
  1994. }
  1995. // forward wafer to system
  1996. if (atmWaferCount < 2)
  1997. {
  1998. var targetLL = readyLLs.Count > 0 ? readyLLs.First().Key : (LLInOutPath == SequenceLLInOutPath.BInBOut ? ModuleName.LLB : ModuleName.LLA);
  1999. ForwardATMWafers(targetLL);
  2000. }
  2001. if (_efemSchdActions.Count == 0)
  2002. {
  2003. var readyReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) &&
  2004. ((mod.Key == ModuleName.LLA && LLInOutPath != SequenceLLInOutPath.BInBOut) || (mod.Key == ModuleName.LLB && LLInOutPath != SequenceLLInOutPath.AInAOut)) &&
  2005. mod.Value.TimeToReady <= 5 &&
  2006. !IsLLReservedByTM(mod.Key) &&
  2007. GetLLReadyInOutSlots(mod.Key).outSlot.Count >= 1).
  2008. OrderByDescending(mod => GetLLReadyInOutSlots(mod.Key).outSlot.Count).ToList();
  2009. foreach(var ll in readyReturnLL)
  2010. {
  2011. if (ExchangeWaferWithLL(readyReturnLL.First().Key))
  2012. return;
  2013. }
  2014. }
  2015. }
  2016. else
  2017. {
  2018. // ready double return LL
  2019. var readyDoubleReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) &&
  2020. mod.Value.TimeToReady < 10 &&
  2021. !IsLLReservedByTM(mod.Key) &&
  2022. GetLLReadyInOutSlots(mod.Key).outSlot.Count > 1).
  2023. OrderBy(mod => mod.Value.TimeToReady).ToList();
  2024. foreach(var ll in readyDoubleReturnLL)
  2025. {
  2026. if (ExchangeWaferWithLL(ll.Key))
  2027. return;
  2028. }
  2029. // ready single return LL
  2030. var readySingleReturnLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) &&
  2031. mod.Value.TimeToReady < 2 &&
  2032. !IsLLReservedByTM(mod.Key) &&
  2033. GetLLReadyInOutSlots(mod.Key).outSlot.Count == 1).
  2034. OrderBy(mod => mod.Value.TimeToReady).ToList();
  2035. foreach (var ll in readySingleReturnLL)
  2036. {
  2037. if (ExchangeWaferWithLL(ll.Key))
  2038. return;
  2039. }
  2040. if (atmWaferCount == 0)
  2041. {
  2042. // ready to push in
  2043. var readyLLs = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) &&
  2044. ((mod.Key != ModuleName.LLA && LLInOutPath == SequenceLLInOutPath.BInBOut) || (mod.Key != ModuleName.LLB && LLInOutPath == SequenceLLInOutPath.AInAOut)) &&
  2045. mod.Value.TimeToReady <= 10 &&
  2046. !IsLLReservedByTM(mod.Key) &&
  2047. GetLLReadyInOutSlots(mod.Key).emptySlot.Count > 1).
  2048. OrderBy(mod => mod.Value.TimeToReady).ToList();
  2049. foreach(var ll in readyLLs)
  2050. {
  2051. if (ForwardATMWafers(ll.Key))
  2052. return;
  2053. }
  2054. }
  2055. }
  2056. }
  2057. private bool isReturnActionsDone(List<MoveItem> items)
  2058. {
  2059. foreach(var item in items)
  2060. {
  2061. if (WaferManager.Instance.CheckHasWafer(item.SourceModule, item.SourceSlot) || WaferManager.Instance.CheckNoWafer(item.DestinationModule, item.DestinationSlot))
  2062. return false;
  2063. }
  2064. return true;
  2065. }
  2066. private bool IsMovingActionsDone(List<MoveItem> actions)
  2067. {
  2068. foreach(var ac in actions)
  2069. {
  2070. var task = _lstWaferTasks.Find(wafer => (wafer.currentMod == ac.SourceModule && wafer.currentSlot == ac.SourceSlot) || (wafer.currentMod == ac.DestinationModule && wafer.currentSlot == ac.DestinationSlot));
  2071. if (task != null && task.movingStatus == RState.Running)
  2072. return false; ;
  2073. }
  2074. return true;
  2075. }
  2076. private bool IsLLReservedByTM(ModuleName ll)
  2077. {
  2078. foreach(var item in _curTmAction)
  2079. {
  2080. if (item.DestinationModule == ll || item.SourceModule == ll)
  2081. return true;
  2082. }
  2083. foreach(var action in _tmSchdActions)
  2084. {
  2085. foreach(var item in action)
  2086. {
  2087. if (item.DestinationModule == ll || item.SourceModule == ll)
  2088. return true;
  2089. }
  2090. }
  2091. return false;
  2092. }
  2093. private bool IsLLReservedByEFEM(ModuleName ll)
  2094. {
  2095. foreach (var item in _curEfemAction)
  2096. {
  2097. if (item.DestinationModule == ll || item.SourceModule == ll)
  2098. return true;
  2099. }
  2100. foreach (var action in _efemSchdActions)
  2101. {
  2102. foreach (var item in action)
  2103. {
  2104. if (item.DestinationModule == ll || item.SourceModule == ll)
  2105. return true;
  2106. }
  2107. }
  2108. return false;
  2109. }
  2110. private void RunSchdEFEMActions()
  2111. {
  2112. var efemRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot) as SchedulerEfemRobot;
  2113. if (efemRobot == null || !efemRobot.IsAvailable)
  2114. return;
  2115. if (_dictModuleTask[ModuleName.EfemRobot].IsIdle)
  2116. {
  2117. if(_efemSchdActions.Count > 0 )
  2118. {
  2119. if(_curEfemAction.Count == 0 || IsMovingActionsDone(_curEfemAction))
  2120. {
  2121. var nextActions = _efemSchdActions.First();
  2122. if (nextActions.Exists(action => !_lstWaferTasks.Exists(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot)))
  2123. return;
  2124. var nextModule = nextActions.First().Module;
  2125. if (ModuleHelper.IsLoadLock(nextModule) && !_dictModuleTask[nextModule].IsIdle)
  2126. return;
  2127. _curEfemAction = _efemSchdActions.Dequeue();
  2128. foreach (var action in _curEfemAction)
  2129. {
  2130. var waferTask = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot);
  2131. waferTask.MoveTo(action.DestinationModule, action.DestinationSlot);
  2132. //--2024-03-21 增加了PortJobWaferStart 上报事件 start--
  2133. if (ModuleHelper.IsLoadPort(action.SourceModule))
  2134. {
  2135. ControlJobInfo currentControlJob = GetLoadPortCurrentControlJob(action.SourceModule);
  2136. if(currentControlJob!=null)
  2137. {
  2138. _faCallback.JobWaferStart(currentControlJob, action.SourceSlot);
  2139. }
  2140. }
  2141. if(ModuleHelper.IsLoadPort(action.DestinationModule))
  2142. {
  2143. ControlJobInfo currentControlJob = GetLoadPortCurrentControlJob(action.DestinationModule);
  2144. if (currentControlJob != null)
  2145. {
  2146. _faCallback.JobWaferEnd(currentControlJob, action.SourceSlot);
  2147. }
  2148. }
  2149. //--2024-03-21 增加了PortJobWaferStart 上报事件 end--
  2150. }
  2151. (_dictModuleTask[ModuleName.EfemRobot].Scheduler as SchedulerEfemRobot).PostMoveItems(_curEfemAction.ToArray());
  2152. }
  2153. }
  2154. else if(_curEfemAction.Count >= 0 && IsMovingActionsDone(_curEfemAction)) // all scheduled actions done
  2155. {
  2156. _curEfemAction.Clear();
  2157. }
  2158. }
  2159. }
  2160. private void RunSchdTMActions()
  2161. {
  2162. if (_dictModuleTask[ModuleName.TMRobot].IsIdle)
  2163. {
  2164. if (_tmSchdActions.Count > 0)
  2165. {
  2166. if (_curTmAction.Count == 0 || IsMovingActionsDone(_curTmAction))
  2167. {
  2168. var nextActions = _tmSchdActions.First();
  2169. if (nextActions.Exists(action => !_lstWaferTasks.Exists(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot)))
  2170. return;
  2171. if (ModuleHelper.IsPm(nextActions.First().Module) && !_dictModuleTask[nextActions.First().Module].IsIdle) /// wait PMTask status update to idle
  2172. return;
  2173. _curTmAction = _tmSchdActions.Dequeue();
  2174. foreach(var action in _curTmAction)
  2175. {
  2176. var waferTask = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot);
  2177. waferTask.MoveTo(action.DestinationModule, action.DestinationSlot);
  2178. }
  2179. (_dictModuleTask[ModuleName.TMRobot].Scheduler as SchedulerTMRobot).SendMoveItems(_curTmAction.ToArray());
  2180. }
  2181. }
  2182. else if (_curTmAction.Count >= 0 && IsMovingActionsDone(_curTmAction)) // all scheduled actions done
  2183. {
  2184. _curTmAction.Clear();
  2185. }
  2186. }
  2187. }
  2188. private int TimeForNextModuleReady(WaferTask task)
  2189. {
  2190. if (ModuleHelper.IsPm(task.destMod) && (ModuleHelper.IsLoadLock(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod)))
  2191. return _dictModuleTask[task.destMod].TimeToReady;
  2192. var llbWaferStatus = GetLLReadyInOutSlots(ModuleName.LLB);
  2193. var llaWaferStatus = GetLLReadyInOutSlots(ModuleName.LLA);
  2194. if (ModuleHelper.IsLoadPort(task.destMod) && (ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod)))
  2195. {
  2196. if (task.llInOutPath == SequenceLLInOutPath.AInBOut || task.llInOutPath == SequenceLLInOutPath.BInBOut)
  2197. {
  2198. if(llbWaferStatus.emptySlot.Count > 0)
  2199. {
  2200. return _dictModuleTask[ModuleName.LLB].TimeToReady;
  2201. }
  2202. }
  2203. else if(task.llInOutPath == SequenceLLInOutPath.BInAOut || task.llInOutPath == SequenceLLInOutPath.AInAOut)
  2204. {
  2205. if(llaWaferStatus.emptySlot.Count > 0)
  2206. {
  2207. return _dictModuleTask[ModuleName.LLA].TimeToReady;
  2208. }
  2209. }
  2210. else if(task.llInOutPath == SequenceLLInOutPath.DInDOut)
  2211. {
  2212. if(llaWaferStatus.emptySlot.Count > 0 && llbWaferStatus.emptySlot.Count > 0)
  2213. {
  2214. if (_dictModuleTask[ModuleName.LLA].TimeToReady == _dictModuleTask[ModuleName.LLB].TimeToReady)
  2215. {
  2216. return _dictModuleTask[ModuleName.LLA].TimeToReady;
  2217. }
  2218. else
  2219. {
  2220. return Math.Min(_dictModuleTask[ModuleName.LLA].TimeToReady, _dictModuleTask[ModuleName.LLB].TimeToReady);
  2221. }
  2222. }
  2223. else if(llaWaferStatus.emptySlot.Count > 0)
  2224. {
  2225. return _dictModuleTask[ModuleName.LLA].TimeToReady;
  2226. }
  2227. else if(llbWaferStatus.emptySlot.Count > 0)
  2228. {
  2229. return _dictModuleTask[ModuleName.LLB].TimeToReady;
  2230. }
  2231. }
  2232. }
  2233. else if(ModuleHelper.IsPm(task.destMod) && (ModuleHelper.IsLoadLock(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod)))
  2234. {
  2235. if(_lstWaferTasks.Find(wafer => wafer.currentMod == task.destMod || wafer.routedMod == task.destMod) == null)
  2236. {
  2237. return _dictModuleTask[task.destMod].TimeToReady;
  2238. }
  2239. }
  2240. return int.MaxValue;
  2241. }
  2242. List<Hand> GetTMValidFreeHands(List<WaferTask> robotWafers, List<MoveItem> swapActions)
  2243. {
  2244. var hands = new List<Hand>();
  2245. for(int i = 0; i < 2; i++)
  2246. {
  2247. if(_tmRobotSingleArmOption == 0 || _tmRobotSingleArmOption == i + 1)
  2248. {
  2249. if ((!robotWafers.Exists(wafer => wafer.currentSlot == i) ||
  2250. ( robotWafers.Exists(wafer => wafer.currentSlot == i) &&
  2251. swapActions.Exists(item => item.SourceModule == ModuleName.TMRobot && item.SourceSlot == i))) &&
  2252. !swapActions.Exists(item => item.DestinationModule == ModuleName.TMRobot && item.DestinationSlot == i))
  2253. hands.Add((Hand)i);
  2254. }
  2255. }
  2256. return hands;
  2257. }
  2258. (ModuleName llPath, List<MoveItem> swapActions) FindBestLLSwapPlan(ModuleName ll, List<WaferTask> robotWafers)
  2259. {
  2260. ModuleName path = ll == ModuleName.System ? ModuleName.System : ll;
  2261. List<ModuleName> lls = ll == ModuleName.System ? new List<ModuleName> { ModuleName.LLA, ModuleName.LLB } : new List<ModuleName> { ll };
  2262. List<MoveItem> actions = new List<MoveItem>();
  2263. Dictionary<ModuleName, List<MoveItem>> llSwapActions = new Dictionary<ModuleName, List<MoveItem>>();
  2264. foreach (var loadlock in lls)
  2265. {
  2266. llSwapActions[loadlock] = new List<MoveItem>();
  2267. if (IsLLReservedByEFEM(loadlock))
  2268. continue;
  2269. var llWaferStatus = GetLLReadyInOutSlots(loadlock);
  2270. foreach(var wafer in robotWafers)
  2271. {
  2272. if(_dictModuleTask[loadlock].TimeToReady == 0 && !IsLLReservedByEFEM(loadlock) && CanWaferGotoLL(wafer,loadlock) && llWaferStatus.emptySlot.Count > llSwapActions[loadlock].Count)
  2273. {
  2274. llSwapActions[loadlock].Add(new MoveItem(ModuleName.TMRobot, wafer.currentSlot, loadlock, llWaferStatus.emptySlot[llSwapActions[loadlock].Count], (Hand)wafer.currentSlot));
  2275. }
  2276. }
  2277. int prePickTime = 20;
  2278. int prePickCount = 0;
  2279. var hands = GetTMValidFreeHands(robotWafers, llSwapActions[loadlock]);
  2280. foreach (var slot in llWaferStatus.inSlot)
  2281. {
  2282. var wafer = _lstWaferTasks.Find(task => task.currentMod == loadlock && task.currentSlot == slot);
  2283. if (wafer != null && _dictModuleTask[wafer.destMod].TimeToReady <= prePickTime && _dictModuleTask[wafer.destMod].Scheduler.IsOnline && hands.Count > prePickCount)
  2284. {
  2285. if(_lstWaferTasks.Exists(task => task.currentMod == wafer.destMod))
  2286. {
  2287. 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))
  2288. {
  2289. llSwapActions[loadlock].Add(new MoveItem(loadlock, slot, ModuleName.TMRobot, (int)hands[prePickCount], hands[prePickCount]));
  2290. prePickCount++;
  2291. break;
  2292. }
  2293. }
  2294. else
  2295. {
  2296. llSwapActions[loadlock].Add(new MoveItem(loadlock, slot, ModuleName.TMRobot, (int)hands[prePickCount], hands[prePickCount]));
  2297. prePickCount++;
  2298. }
  2299. }
  2300. }
  2301. }
  2302. var validateActions = llSwapActions.Where(item => item.Value.Count > 0).ToDictionary(k => k.Key, v => v.Value);
  2303. if(validateActions.Count == 1)
  2304. {
  2305. (path, actions) = (validateActions.First().Key, validateActions.First().Value);
  2306. }
  2307. else if(validateActions.Count == 2)
  2308. {
  2309. var sortedActions = validateActions.OrderByDescending(item => item.Value.Count + (_dictModuleTask[item.Key].Scheduler.IsVac ? 2 : 0)).ToDictionary(k => k.Key, v => v.Value);
  2310. (path, actions) = (sortedActions.First().Key, sortedActions.First().Value);
  2311. }
  2312. return (path, actions);
  2313. }
  2314. (List<WaferTask> pmWafers, ModuleName destLL, List<MoveItem> swapAction) FindBestReturnWafersPlan(Hand[] freeHands)
  2315. {
  2316. Dictionary<ModuleName, List<WaferTask>> returnWafers = new Dictionary<ModuleName, List<WaferTask>>();
  2317. Dictionary<ModuleName, List<MoveItem>> llSwapActions = new Dictionary<ModuleName, List<MoveItem>>();
  2318. Dictionary<ModuleName, int> llValues = new Dictionary<ModuleName, int>();
  2319. foreach(var ll in new List<ModuleName> { ModuleName.LLA, ModuleName.LLB})
  2320. {
  2321. var llWaferStatus = GetLLReadyInOutSlots(ll);
  2322. returnWafers[ll] = new List<WaferTask>();
  2323. llSwapActions[ll] = new List<MoveItem>();
  2324. llValues[ll] = 0;
  2325. if (IsLLReservedByEFEM(ll))
  2326. continue;
  2327. var readyReturnWafers = _lstWaferTasks.Where( wafer => ModuleHelper.IsPm(wafer.currentMod) &&
  2328. ModuleHelper.IsLoadPort(wafer.destMod) &&
  2329. wafer.movingStatus == RState.End &&
  2330. _dictModuleTask[wafer.currentMod].TimeToReady <= 2 &&
  2331. CanWaferGotoLL(wafer, ll)).Take(Math.Min(freeHands.Length, llWaferStatus.emptySlot.Count)).ToList();
  2332. int prePickTime = 10 * llSwapActions[ll].Count;
  2333. int handIndex = 0;
  2334. if(_dictModuleTask[ll].TimeToReady <= prePickTime)
  2335. {
  2336. foreach (var wafer in readyReturnWafers)
  2337. {
  2338. if(handIndex < freeHands.Length)
  2339. {
  2340. returnWafers[ll].Add(wafer);
  2341. llSwapActions[ll].Add(new MoveItem(ModuleName.TMRobot, (int)freeHands[handIndex], ll, llWaferStatus.emptySlot[handIndex], freeHands[handIndex]));
  2342. handIndex++;
  2343. }
  2344. }
  2345. int prePickCount = 0;
  2346. handIndex = 0;
  2347. foreach (var slot in llWaferStatus.inSlot)
  2348. {
  2349. var wafer = _lstWaferTasks.Find(task => task.currentMod == ll && task.currentSlot == slot);
  2350. if (wafer != null && _dictModuleTask[wafer.destMod].TimeToReady <= prePickTime && _dictModuleTask[wafer.destMod].Scheduler.IsOnline && freeHands.Length > prePickCount)
  2351. {
  2352. if (_lstWaferTasks.Exists(task => task.currentMod == wafer.destMod))
  2353. {
  2354. 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))
  2355. {
  2356. llSwapActions[ll].Add(new MoveItem(ll, slot, ModuleName.TMRobot, (int)freeHands[prePickCount], freeHands[prePickCount]));
  2357. prePickCount++;
  2358. break;
  2359. }
  2360. }
  2361. else
  2362. {
  2363. llSwapActions[ll].Add(new MoveItem(ll, slot, ModuleName.TMRobot, (int)freeHands[prePickCount], freeHands[prePickCount]));
  2364. prePickCount++;
  2365. }
  2366. }
  2367. }
  2368. llValues[ll] = returnWafers[ll].Count * 2 + llSwapActions[ll].Count + (_dictModuleTask[ll].Scheduler.IsVac && returnWafers[ll].Count > 0 ? 2 : 0);
  2369. }
  2370. }
  2371. var preferLL = llValues.OrderByDescending(v => v.Value).First().Key;
  2372. return (returnWafers[preferLL], preferLL, llSwapActions[preferLL]);
  2373. }
  2374. ModuleName FindTheBestMovePathWithFixSlot(IEnumerable<KeyValuePair<ModuleName, ModuleTask>> lls, List<WaferTask> pmWafers)
  2375. {
  2376. Dictionary<ModuleName, int> moveValues = new Dictionary<ModuleName, int>();
  2377. foreach(var ll in lls)
  2378. {
  2379. var llWaferStatus = GetLLFixedReadyInOutSlots(ll.Key);
  2380. moveValues[ll.Key] = Math.Min(pmWafers.Where(wt => CanWaferGotoLL(wt, ll.Key)).Count(), GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count) + llWaferStatus.tOutSlot.Count;
  2381. }
  2382. var preferLL = moveValues.OrderByDescending(v => v.Value).First().Key;
  2383. return moveValues[preferLL] > 0 ? preferLL : ModuleName.System;
  2384. }
  2385. void RoutingTMSwapActions(List<MoveItem> actions)
  2386. {
  2387. foreach (var action in actions)
  2388. {
  2389. if (ModuleHelper.IsTMRobot(action.SourceModule))
  2390. {
  2391. var waferTask = _lstWaferTasks.Find(wafer => wafer.currentMod == ModuleName.TMRobot && wafer.currentSlot == action.SourceSlot);
  2392. waferTask.RouteTo(action.DestinationModule, action.DestinationSlot);
  2393. }
  2394. else
  2395. {
  2396. var waferTask = _lstWaferTasks.Find(wafer => wafer.currentMod == action.SourceModule && wafer.currentSlot == action.SourceSlot);
  2397. waferTask.RouteTo(action.DestinationModule, action.DestinationSlot);
  2398. }
  2399. }
  2400. }
  2401. private bool ManaulReturnPMWafer()
  2402. {
  2403. var mReturnWafer = _qeReturnWafers.Peek();
  2404. if (ModuleHelper.IsPm((ModuleName)mReturnWafer.Station))
  2405. {
  2406. var hands = GetTMValidFreeHands(_lstWaferTasks.Where(wt => ModuleHelper.IsTMRobot(wt.currentMod)).ToList(), new List<MoveItem>());
  2407. var waferTask = _lstWaferTasks.Find(wt => wt.currentMod == (ModuleName)mReturnWafer.Station && wt.waferId == mReturnWafer.InnerId);
  2408. if (waferTask != null && hands.Count > 0)
  2409. {
  2410. waferTask.Return();
  2411. var outLL = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && CanWaferGotoLL(waferTask, mod.Key) && GetLLReadyInOutSlots(mod.Key).emptySlot.Count > 0 && !IsLLReservedByEFEM(mod.Key));
  2412. if (outLL.Count() > 0)
  2413. {
  2414. var slot = GetLLReadyInOutSlots(outLL.First().Key).emptySlot.First();
  2415. waferTask.RouteTo(outLL.First().Key, slot);
  2416. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(waferTask.currentMod, 0, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  2417. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), outLL.First().Key, slot, hands.First()) });
  2418. _qeReturnWafers.Dequeue();
  2419. return true;
  2420. }
  2421. }
  2422. }
  2423. return false;
  2424. }
  2425. private void RoutingVacWafers()
  2426. {
  2427. if(_tmSchdActions.Count > 0 || _curTmAction.Count > 0)
  2428. {
  2429. RunSchdTMActions();
  2430. return;
  2431. }
  2432. if(_tmRobotStatus == RState.End)
  2433. {
  2434. // handle manual return wafer if exist
  2435. if(_qeReturnWafers.Count > 0)
  2436. {
  2437. if (ManaulReturnPMWafer())
  2438. return;
  2439. }
  2440. if(_LLSlotInOutOption == LLSlotInOutOpt.AllInAllOut)
  2441. {
  2442. if (_LLASlotNumber == 2)
  2443. {
  2444. Routing2SlotVacSystem();
  2445. }
  2446. else if (_LLASlotNumber == 4)
  2447. {
  2448. Routing4SlotVacSystem();
  2449. }
  2450. }
  2451. else
  2452. {
  2453. RoutingFixedSlotVacSystem();
  2454. }
  2455. }
  2456. }
  2457. private void Routing2SlotVacSystem()
  2458. {
  2459. var robotWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && ModuleHelper.IsTMRobot(wafer.currentMod)).OrderBy(wafer => TimeForNextModuleReady(wafer)).ToList();
  2460. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut)
  2461. {
  2462. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  2463. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  2464. var inLLWaferStatus = GetLLReadyInOutSlots(inLL);
  2465. var outLLWaferStatus = GetLLReadyInOutSlots(outLL);
  2466. if(robotWafers.Count == 2)
  2467. {
  2468. int returnCount = 0;
  2469. foreach (var wafer in robotWafers)
  2470. {
  2471. if (ModuleHelper.IsPm(wafer.destMod))
  2472. {
  2473. if (_dictModuleTask[wafer.destMod].TimeToReady <= 5 &&
  2474. !_lstWaferTasks.Exists(waferT => waferT.currentMod == wafer.destMod) &&
  2475. !_lstWaferTasks.Exists(waferT => waferT.routedMod == wafer.destMod))
  2476. {
  2477. wafer.RouteTo(wafer.destMod, 0);
  2478. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  2479. }
  2480. }
  2481. else
  2482. {
  2483. if (_dictModuleTask[outLL].TimeToReady <= 5 && !IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > returnCount)
  2484. {
  2485. wafer.RouteTo(outLL, outLLWaferStatus.emptySlot[returnCount]);
  2486. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, outLL, outLLWaferStatus.emptySlot[returnCount], (Hand)wafer.currentSlot) });
  2487. returnCount++;
  2488. }
  2489. }
  2490. }
  2491. }
  2492. else if(robotWafers.Count == 1)
  2493. {
  2494. int freeHand = 1 - robotWafers.First().currentSlot;
  2495. if (ModuleHelper.IsPm(robotWafers.First().destMod))
  2496. {
  2497. if(_dictModuleTask[robotWafers.First().destMod].TimeToReady <= 5)
  2498. {
  2499. if(_lstWaferTasks.Exists(waferT => waferT.currentMod == robotWafers.First().destMod))
  2500. {
  2501. var pmActions = new List<MoveItem>();
  2502. var pmWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == robotWafers.First().destMod);
  2503. pmWafer.RouteTo(ModuleName.TMRobot, freeHand);
  2504. pmActions.Add(new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, freeHand, (Hand)freeHand));
  2505. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, pmWafer.currentMod, 0, (Hand)robotWafers.First().currentSlot));
  2506. _tmSchdActions.Enqueue(pmActions);
  2507. }
  2508. else
  2509. {
  2510. robotWafers.First().RouteTo(robotWafers.First().destMod, 0);
  2511. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, robotWafers.First().destMod, 0, (Hand)robotWafers.First().currentSlot) });
  2512. }
  2513. }
  2514. }
  2515. else
  2516. {
  2517. if (_dictModuleTask[outLL].TimeToReady <= 5 && !IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > 0)
  2518. {
  2519. robotWafers.First().RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2520. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, outLL, outLLWaferStatus.emptySlot[0], (Hand)robotWafers.First().currentSlot) });
  2521. }
  2522. }
  2523. if (_tmSchdActions.Count == 0 && _tmRobotSingleArmOption == 0)
  2524. {
  2525. // try push in one wafer
  2526. if (!IsLLReservedByEFEM(inLL) && _dictModuleTask[inLL].Scheduler.IsVac && inLLWaferStatus.inSlot.Count > 0)
  2527. {
  2528. foreach (var slot in inLLWaferStatus.inSlot)
  2529. {
  2530. var llWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == inLL && wafer.currentSlot == slot);
  2531. if (llWafer != null && !_lstWaferTasks.Exists(wt => wt.currentMod == llWafer.destMod) && _dictModuleTask[llWafer.destMod].TimeToReady < 15)
  2532. {
  2533. llWafer.RouteTo(llWafer.destMod, 0);
  2534. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(inLL, slot, ModuleName.TMRobot, freeHand, (Hand)freeHand) });
  2535. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, freeHand, llWafer.destMod, 0, (Hand)freeHand) });
  2536. return;
  2537. }
  2538. }
  2539. }
  2540. // try to return one wafer
  2541. if (!IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > 0)
  2542. {
  2543. var reayReturnWafer = _lstWaferTasks.Find(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod) && _dictModuleTask[wt.currentMod].TimeToReady <= 2);
  2544. if (reayReturnWafer != null)
  2545. {
  2546. reayReturnWafer.RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2547. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(reayReturnWafer.currentMod, 0, ModuleName.TMRobot, freeHand, (Hand)freeHand) });
  2548. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, freeHand, outLL, outLLWaferStatus.emptySlot[0], (Hand)freeHand) });
  2549. return;
  2550. }
  2551. }
  2552. }
  2553. }
  2554. else // no wafer on robot arm
  2555. {
  2556. var readyInPms = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && !_lstWaferTasks.Exists(wt => wt.currentMod == mod.Key) && mod.Value.Scheduler.IsOnline && mod.Value.TimeToReady <= 15);
  2557. var readyOutWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod) && _dictModuleTask[wt.currentMod].TimeToReady <= 2).ToList() ;
  2558. if(_tmRobotSingleArmOption != 0)
  2559. {
  2560. var hands = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2561. if (hands.Count == 0)
  2562. return;
  2563. if(!IsLLReservedByEFEM(inLL) && _dictModuleTask[inLL].TimeToReady <= 5 && inLLWaferStatus.inSlot.Count > 0 && readyInPms.Count() > 0)
  2564. {
  2565. foreach(var pm in readyInPms)
  2566. {
  2567. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && inLLWaferStatus.inSlot.Contains(wt.currentSlot) && wt.destMod == pm.Key);
  2568. if(llWafer != null)
  2569. {
  2570. llWafer.RouteTo(llWafer.destMod, 0);
  2571. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(inLL, llWafer.currentSlot, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  2572. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), llWafer.destMod, 0, hands.First()) });
  2573. return;
  2574. }
  2575. }
  2576. }
  2577. if(readyOutWafers.Count > 0 && !IsLLReservedByEFEM(outLL) && _dictModuleTask[outLL].TimeToReady < 50 && outLLWaferStatus.emptySlot.Count > 0)
  2578. {
  2579. var returnWafer = readyOutWafers.First();
  2580. returnWafer.RouteTo(outLL, outLLWaferStatus.emptySlot.First());
  2581. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(returnWafer.currentMod, 0, ModuleName.TMRobot, (int)hands.First(), hands.First()) });
  2582. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)hands.First(), outLL, outLLWaferStatus.emptySlot.First(), hands.First()) });
  2583. return;
  2584. }
  2585. return;
  2586. }
  2587. if(!IsLLReservedByEFEM(inLL) && _dictModuleTask[inLL].TimeToReady <= 5 && inLLWaferStatus.inSlot.Count > 0)
  2588. {
  2589. if(inLLWaferStatus.inSlot.Count == 2)
  2590. {
  2591. if(readyInPms.Count() > 0) // double move from LL to PM
  2592. {
  2593. int pickCount = 0;
  2594. var swapActions = new List<MoveItem>();
  2595. var singleActions = new List<MoveItem>();
  2596. foreach(var pm in readyInPms)
  2597. {
  2598. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && inLLWaferStatus.inSlot.Contains(wt.currentSlot) && wt.destMod == pm.Key);
  2599. if(llWafer != null)
  2600. {
  2601. llWafer.RouteTo(pm.Key, 0);
  2602. swapActions.Add(new MoveItem(inLL, llWafer.currentSlot, ModuleName.TMRobot, pickCount, (Hand)pickCount));
  2603. singleActions.Add(new MoveItem(ModuleName.TMRobot, pickCount, pm.Key, 0, (Hand)pickCount));
  2604. pickCount++;
  2605. }
  2606. }
  2607. if(pickCount == 1)
  2608. {
  2609. int remainSlot = swapActions.First().SourceSlot == inLLWaferStatus.inSlot[0] ? inLLWaferStatus.inSlot[1] : inLLWaferStatus.inSlot[0];
  2610. var remainWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && wt.currentSlot == remainSlot);
  2611. if (remainWafer != null)
  2612. {
  2613. remainWafer.RouteTo(ModuleName.TMRobot, 1);
  2614. swapActions.Add(new MoveItem(inLL, remainSlot, ModuleName.TMRobot, 1, Hand.Blade2));
  2615. }
  2616. }
  2617. if(swapActions.Count > 0)
  2618. {
  2619. _tmSchdActions.Enqueue(swapActions);
  2620. foreach(var ac in singleActions)
  2621. {
  2622. _tmSchdActions.Enqueue(new List<MoveItem> { ac });
  2623. }
  2624. }
  2625. }
  2626. else
  2627. {
  2628. foreach(var slot in inLLWaferStatus.inSlot)
  2629. {
  2630. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && wt.currentSlot == slot);
  2631. if (llWafer != null)
  2632. {
  2633. if(!_lstWaferTasks.Exists(wt => wt.currentMod == llWafer.destMod) && _dictModuleTask[llWafer.destMod].Scheduler.IsOnline)
  2634. {
  2635. var swapActions = new List<MoveItem>();
  2636. swapActions.Add(new MoveItem(inLL, slot, ModuleName.TMRobot, 0, Hand.Blade1));
  2637. int remainSlot = slot == inLLWaferStatus.inSlot[0] ? inLLWaferStatus.inSlot[1] : inLLWaferStatus.inSlot[0];
  2638. swapActions.Add(new MoveItem(inLL, remainSlot, ModuleName.TMRobot, 1, Hand.Blade2));
  2639. _tmSchdActions.Enqueue(swapActions);
  2640. llWafer.RouteTo(ModuleName.TMRobot, 0);
  2641. if (_dictModuleTask[llWafer.destMod].TimeToReady <= 30)
  2642. {
  2643. llWafer.RouteTo(llWafer.destMod, 0);
  2644. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, 0, llWafer.destMod, 0, Hand.Blade1) });
  2645. }
  2646. return;
  2647. }
  2648. }
  2649. }
  2650. }
  2651. }
  2652. else
  2653. {
  2654. var llWafer = _lstWaferTasks.Find(wt => wt.currentMod == inLL && wt.currentSlot == inLLWaferStatus.inSlot.First());
  2655. if (llWafer != null)
  2656. {
  2657. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(inLL, inLLWaferStatus.inSlot.First(), ModuleName.TMRobot, 0, Hand.Blade1) });
  2658. if (!_lstWaferTasks.Exists(wt => wt.currentMod == llWafer.destMod) && _dictModuleTask[llWafer.destMod].TimeToReady < 15)
  2659. {
  2660. llWafer.RouteTo(llWafer.destMod, 0);
  2661. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, 0, llWafer.destMod, 0, Hand.Blade1) });
  2662. }
  2663. }
  2664. }
  2665. }
  2666. if (_tmSchdActions.Count > 0)
  2667. return;
  2668. if(!IsLLReservedByEFEM(outLL) && outLLWaferStatus.emptySlot.Count > 0 && readyOutWafers.Count > 0)
  2669. {
  2670. if(readyOutWafers.Count >= 2 && outLLWaferStatus.emptySlot.Count == 2)
  2671. {
  2672. readyOutWafers[0].RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2673. readyOutWafers[1].RouteTo(outLL, outLLWaferStatus.emptySlot[1]);
  2674. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyOutWafers[0].currentMod, readyOutWafers[0].currentSlot, ModuleName.TMRobot, 0, Hand.Blade1) });
  2675. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyOutWafers[1].currentMod, readyOutWafers[1].currentSlot, ModuleName.TMRobot, 1, Hand.Blade2) });
  2676. var swapActions = new List<MoveItem>();
  2677. swapActions.Add(new MoveItem(ModuleName.TMRobot, 0, outLL, outLLWaferStatus.emptySlot[0], Hand.Blade1));
  2678. swapActions.Add(new MoveItem(ModuleName.TMRobot, 1, outLL, outLLWaferStatus.emptySlot[1], Hand.Blade2));
  2679. _tmSchdActions.Enqueue(swapActions);
  2680. }
  2681. else
  2682. {
  2683. var freeHands = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2684. readyOutWafers[0].RouteTo(outLL, outLLWaferStatus.emptySlot[0]);
  2685. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(readyOutWafers[0].currentMod, readyOutWafers[0].currentSlot, ModuleName.TMRobot, (int)freeHands[0], freeHands[0]) });
  2686. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)freeHands[0], outLL, outLLWaferStatus.emptySlot[0], freeHands[0]) });
  2687. }
  2688. }
  2689. }
  2690. }
  2691. else
  2692. {
  2693. if (robotWafers.Count == 2)
  2694. {
  2695. foreach (var wafer in robotWafers)
  2696. {
  2697. if (wafer.movingStatus == RState.End &&
  2698. ModuleHelper.IsPm(wafer.destMod) &&
  2699. _dictModuleTask[wafer.destMod].TimeToReady == 0 &&
  2700. !_lstWaferTasks.Exists(waferT => waferT.currentMod == wafer.destMod) &&
  2701. !_lstWaferTasks.Exists(waferT => waferT.routedMod == wafer.destMod))
  2702. {
  2703. wafer.RouteTo(wafer.destMod, 0);
  2704. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  2705. }
  2706. }
  2707. if (_tmSchdActions.Count == 0)
  2708. {
  2709. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2710. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2711. {
  2712. RoutingTMSwapActions(swapActions.swapActions);
  2713. _tmSchdActions.Enqueue(swapActions.swapActions);
  2714. }
  2715. }
  2716. }
  2717. else if (robotWafers.Count == 1)
  2718. {
  2719. if (ModuleHelper.IsPm(robotWafers[0].destMod) && _dictModuleTask[robotWafers[0].destMod].TimeToReady == 0)
  2720. {
  2721. var pmActions = new List<MoveItem>();
  2722. var pmWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == robotWafers[0].destMod && ModuleHelper.IsLoadPort(wafer.destMod));
  2723. if (pmWafer != null && string.IsNullOrWhiteSpace(pmWafer.wtwCleanRecipe))
  2724. {
  2725. int pickSlot = 1 - robotWafers[0].currentSlot;
  2726. pmWafer.RouteTo(ModuleName.TMRobot, pickSlot);
  2727. pmActions.Add(new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, pickSlot, (Hand)pickSlot));
  2728. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, pmWafer.currentMod, 0, (Hand)robotWafers[0].currentSlot));
  2729. _tmSchdActions.Enqueue(pmActions);
  2730. return;
  2731. }
  2732. if (!_lstWaferTasks.Exists(wafer => wafer.currentMod == robotWafers[0].destMod))
  2733. {
  2734. robotWafers[0].RouteTo(robotWafers[0].destMod, 0);
  2735. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, robotWafers[0].destMod, 0, (Hand)robotWafers[0].currentSlot));
  2736. _tmSchdActions.Enqueue(pmActions);
  2737. return;
  2738. }
  2739. }
  2740. // try to pick a wafer from LL
  2741. if (_tmSchdActions.Count == 0)
  2742. {
  2743. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2744. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2745. {
  2746. RoutingTMSwapActions(swapActions.swapActions);
  2747. _tmSchdActions.Enqueue(swapActions.swapActions);
  2748. return;
  2749. }
  2750. }
  2751. // try to return a wafer from PM
  2752. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2753. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2754. if (moveActions.pmWafers.Count > 0)
  2755. {
  2756. MoveItem placeAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2757. moveActions.pmWafers.First().RouteTo(placeAction.DestinationModule, placeAction.DestinationSlot);
  2758. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers.First().currentMod, 0, ModuleName.TMRobot, (int)freeHand[0], freeHand[0]) });
  2759. MoveItem pickAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.SourceModule));
  2760. if (pickAction != null)
  2761. {
  2762. var pickWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == pickAction.SourceModule && wafer.currentSlot == pickAction.SourceSlot);
  2763. pickWafer.RouteTo(pickAction.DestinationModule, pickAction.DestinationSlot);
  2764. }
  2765. _tmSchdActions.Enqueue(moveActions.swapAction);
  2766. }
  2767. }
  2768. else // robot arm is empty
  2769. {
  2770. // try return wafers from PM
  2771. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2772. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2773. if (moveActions.pmWafers.Count > 0)
  2774. {
  2775. int handIndex = 0;
  2776. var placeActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2777. foreach (var action in placeActions)
  2778. {
  2779. moveActions.pmWafers[handIndex].RouteTo(action.DestinationModule, action.DestinationSlot);
  2780. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers[handIndex].currentMod, 0, ModuleName.TMRobot, (int)freeHand[handIndex], freeHand[handIndex]) });
  2781. handIndex++;
  2782. }
  2783. var pickActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.SourceModule)).ToList();
  2784. foreach (var ac in pickActions)
  2785. {
  2786. var llWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == ac.SourceModule && wafer.currentSlot == ac.SourceSlot);
  2787. llWafer.RouteTo(ac.DestinationModule, ac.DestinationSlot);
  2788. }
  2789. _tmSchdActions.Enqueue(moveActions.swapAction);
  2790. }
  2791. if (_tmSchdActions.Count == 0)
  2792. {
  2793. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2794. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2795. {
  2796. RoutingTMSwapActions(swapActions.swapActions);
  2797. _tmSchdActions.Enqueue(swapActions.swapActions);
  2798. }
  2799. }
  2800. }
  2801. }
  2802. }
  2803. private void Routing4SlotVacSystem()
  2804. {
  2805. var robotWafers = _lstWaferTasks.Where(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && ModuleHelper.IsTMRobot(wafer.currentMod)).OrderBy(wafer => TimeForNextModuleReady(wafer)).ToList();
  2806. if (robotWafers.Count == 2)
  2807. {
  2808. foreach (var wafer in robotWafers)
  2809. {
  2810. if (wafer.movingStatus == RState.End &&
  2811. ModuleHelper.IsPm(wafer.destMod) &&
  2812. _dictModuleTask[wafer.destMod].TimeToReady == 0 &&
  2813. !_lstWaferTasks.Exists(waferT => waferT.currentMod == wafer.destMod) &&
  2814. !_lstWaferTasks.Exists(waferT => waferT.routedMod == wafer.destMod))
  2815. {
  2816. wafer.RouteTo(wafer.destMod, 0);
  2817. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  2818. }
  2819. }
  2820. if (_tmSchdActions.Count == 0)
  2821. {
  2822. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2823. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2824. {
  2825. RoutingTMSwapActions(swapActions.swapActions);
  2826. _tmSchdActions.Enqueue(swapActions.swapActions);
  2827. }
  2828. }
  2829. }
  2830. else if (robotWafers.Count == 1)
  2831. {
  2832. if (ModuleHelper.IsPm(robotWafers[0].destMod) && _dictModuleTask[robotWafers[0].destMod].TimeToReady == 0)
  2833. {
  2834. var pmActions = new List<MoveItem>();
  2835. var pmWafer = _lstWaferTasks.Find(wafer => (wafer.movingStatus == RState.End || wafer.movingStatus == RState.Init) && wafer.currentMod == robotWafers[0].destMod && ModuleHelper.IsLoadPort(wafer.destMod));
  2836. if (pmWafer != null && string.IsNullOrWhiteSpace(pmWafer.wtwCleanRecipe))
  2837. {
  2838. int pickSlot = 1 - robotWafers[0].currentSlot;
  2839. pmWafer.RouteTo(ModuleName.TMRobot, pickSlot);
  2840. pmActions.Add(new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, pickSlot, (Hand)pickSlot));
  2841. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, pmWafer.currentMod, 0, (Hand)robotWafers[0].currentSlot));
  2842. _tmSchdActions.Enqueue(pmActions);
  2843. return;
  2844. }
  2845. if (!_lstWaferTasks.Exists(wafer => wafer.currentMod == robotWafers[0].destMod))
  2846. {
  2847. robotWafers[0].RouteTo(robotWafers[0].destMod, 0);
  2848. pmActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers[0].currentSlot, robotWafers[0].destMod, 0, (Hand)robotWafers[0].currentSlot));
  2849. _tmSchdActions.Enqueue(pmActions);
  2850. return;
  2851. }
  2852. }
  2853. // try to pick a wafer from LL
  2854. if (_tmSchdActions.Count == 0)
  2855. {
  2856. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2857. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2858. {
  2859. RoutingTMSwapActions(swapActions.swapActions);
  2860. _tmSchdActions.Enqueue(swapActions.swapActions);
  2861. return;
  2862. }
  2863. }
  2864. // try to return a wafer from PM
  2865. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2866. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2867. if (moveActions.pmWafers.Count > 0)
  2868. {
  2869. MoveItem placeAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2870. moveActions.pmWafers.First().RouteTo(placeAction.DestinationModule, placeAction.DestinationSlot);
  2871. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers.First().currentMod, 0, ModuleName.TMRobot, (int)freeHand[0], freeHand[0]) });
  2872. MoveItem pickAction = moveActions.swapAction.Find(ac => ModuleHelper.IsLoadLock(ac.SourceModule));
  2873. if (pickAction != null)
  2874. {
  2875. var pickWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == pickAction.SourceModule && wafer.currentSlot == pickAction.SourceSlot);
  2876. pickWafer.RouteTo(pickAction.DestinationModule, pickAction.DestinationSlot);
  2877. }
  2878. _tmSchdActions.Enqueue(moveActions.swapAction);
  2879. }
  2880. }
  2881. else // robot arm is empty
  2882. {
  2883. // try return wafers from PM
  2884. var freeHand = GetTMValidFreeHands(robotWafers, new List<MoveItem>());
  2885. var moveActions = FindBestReturnWafersPlan(freeHand.ToArray());
  2886. if (moveActions.pmWafers.Count > 0)
  2887. {
  2888. int handIndex = 0;
  2889. var placeActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.DestinationModule));
  2890. foreach (var action in placeActions)
  2891. {
  2892. moveActions.pmWafers[handIndex].RouteTo(action.DestinationModule, action.DestinationSlot);
  2893. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(moveActions.pmWafers[handIndex].currentMod, 0, ModuleName.TMRobot, (int)freeHand[handIndex], freeHand[handIndex]) });
  2894. handIndex++;
  2895. }
  2896. var pickActions = moveActions.swapAction.Where(ac => ModuleHelper.IsLoadLock(ac.SourceModule)).ToList();
  2897. foreach (var ac in pickActions)
  2898. {
  2899. var llWafer = _lstWaferTasks.Find(wafer => wafer.currentMod == ac.SourceModule && wafer.currentSlot == ac.SourceSlot);
  2900. llWafer.RouteTo(ac.DestinationModule, ac.DestinationSlot);
  2901. }
  2902. _tmSchdActions.Enqueue(moveActions.swapAction);
  2903. }
  2904. if (_tmSchdActions.Count == 0)
  2905. {
  2906. var swapActions = FindBestLLSwapPlan(ModuleName.System, robotWafers);
  2907. if (swapActions.llPath != ModuleName.System && swapActions.swapActions.Count > 0)
  2908. {
  2909. RoutingTMSwapActions(swapActions.swapActions);
  2910. _tmSchdActions.Enqueue(swapActions.swapActions);
  2911. }
  2912. }
  2913. }
  2914. }
  2915. private void RoutingFixedSlotVacSystem()
  2916. {
  2917. var robotWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsTMRobot(wt.currentMod));
  2918. var lls = _dictModuleTask.Where(mod => ModuleHelper.IsLoadLock(mod.Key) && mod.Value.Scheduler.IsOnline);
  2919. if (_LLASlotNumber == 2)
  2920. {
  2921. if (robotWafers.Count() == 1)
  2922. {
  2923. if(ModuleHelper.IsPm(robotWafers.First().destMod))
  2924. {
  2925. var destPM = robotWafers.First().destMod;
  2926. if(_dictModuleTask[destPM].TimeToReady <= 5)
  2927. {
  2928. if(_lstWaferTasks.Exists(wt => wt.currentMod == destPM))
  2929. {
  2930. var pickHand = SelectTMPickArm(destPM);
  2931. if(pickHand != Hand.None)
  2932. {
  2933. var pmSwap = new List<MoveItem>();
  2934. pmSwap.Add(new MoveItem(destPM, 0, ModuleName.TMRobot, (int)pickHand, pickHand));
  2935. pmSwap.Add(new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, destPM, 0, (Hand)robotWafers.First().currentSlot));
  2936. _tmSchdActions.Enqueue(pmSwap);
  2937. return;
  2938. }
  2939. }
  2940. else
  2941. {
  2942. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, destPM, 0, (Hand)robotWafers.First().currentSlot) });
  2943. return;
  2944. }
  2945. }
  2946. }
  2947. else
  2948. {
  2949. var readySwapLL = lls.Where(ll => CanWaferGotoLL(robotWafers.First(), ll.Key) &&
  2950. GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count == 1 &&
  2951. GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count == 1 &&
  2952. ll.Value.TimeToReady <= 10);
  2953. if (readySwapLL.Count() > 0)
  2954. {
  2955. var destLL = readySwapLL.First().Key;
  2956. Hand pickHand = SelectTMPickArm(destLL);
  2957. if (pickHand != Hand.None)
  2958. {
  2959. Hand placeHand = (Hand)robotWafers.First().currentSlot;
  2960. var llSwap = new List<MoveItem>();
  2961. var llSlots = GetLLFixedReadyInOutSlots(destLL);
  2962. llSwap.Add(new MoveItem(destLL, llSlots.tOutSlot.First(), ModuleName.TMRobot, (int)pickHand, pickHand));
  2963. llSwap.Add(new MoveItem(ModuleName.TMRobot, (int)placeHand, destLL, llSlots.tInSlot.First(), placeHand));
  2964. _tmSchdActions.Enqueue(llSwap);
  2965. return;
  2966. }
  2967. }
  2968. var emptyLL = lls.Where(ll => CanWaferGotoLL(robotWafers.First(), ll.Key) &&
  2969. GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count == 1 &&
  2970. ll.Value.TimeToReady <= 2);
  2971. if (emptyLL.Count() > 0)
  2972. {
  2973. var destLL = emptyLL.First().Key;
  2974. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem( ModuleName.TMRobot,
  2975. robotWafers.First().currentSlot,
  2976. destLL,
  2977. GetLLFixedReadyInOutSlots(destLL).tInSlot.First(),
  2978. (Hand)robotWafers.First().currentSlot) });
  2979. return;
  2980. }
  2981. }
  2982. // try to return a wafer
  2983. var readyReturnWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod) && _dictModuleTask[wt.currentMod].TimeToReady <= 2);
  2984. foreach(var pmWafer in readyReturnWafers)
  2985. {
  2986. var readyLL = lls.Where(ll => CanWaferGotoLL(pmWafer, ll.Key) && GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count > 0 && ll.Value.TimeToReady <= 2).OrderBy(ll => ll.Value.Scheduler.IsVac ? 0 : 1);
  2987. if(readyLL.Count() > 0)
  2988. {
  2989. var validHand = (Hand)(1 - robotWafers.First().currentSlot);
  2990. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, (int)validHand, validHand) });
  2991. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)validHand , readyLL.First().Key, GetLLFixedReadyInOutSlots(readyLL.First().Key).tInSlot.First(), validHand) });
  2992. return;
  2993. }
  2994. }
  2995. // try to push in a wafer
  2996. var readyInLL = lls.Where(ll => GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count > 0 && ll.Value.TimeToReady <= 2).OrderBy(ll => ll.Value.Scheduler.IsVac ? 0 : 1);
  2997. foreach(var ll in readyInLL)
  2998. {
  2999. var waferStatus = GetLLFixedReadyInOutSlots(ll.Key);
  3000. var wafer = _lstWaferTasks.Find(wt => wt.currentMod == ll.Key && wt.currentSlot == waferStatus.tOutSlot.First());
  3001. if( wafer != null &&
  3002. ModuleHelper.IsPm(wafer.destMod) &&
  3003. !_lstWaferTasks.Exists(wt => wt.currentMod == wafer.destMod) &&
  3004. _dictModuleTask[wafer.destMod].TimeToReady <= 2)
  3005. {
  3006. var validHand = (Hand)(1 - robotWafers.First().currentSlot);
  3007. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(wafer.currentMod, wafer.currentSlot, ModuleName.TMRobot, (int)validHand, validHand) });
  3008. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)validHand, wafer.destMod, 0, validHand) });
  3009. return;
  3010. }
  3011. }
  3012. }
  3013. else if (robotWafers.Count() == 0)
  3014. {
  3015. var readyReturnWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod)).ToList();
  3016. if(readyReturnWafers.Count > 0)
  3017. {
  3018. // try to do a LL swap action
  3019. var readySwapLL = lls.Where(ll => GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count == 1 && GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count == 1 && ll.Value.TimeToReady <= 10);
  3020. foreach (var pmWafer in readyReturnWafers)
  3021. {
  3022. foreach (var ll in readySwapLL)
  3023. {
  3024. if (CanWaferGotoLL(pmWafer, ll.Key))
  3025. {
  3026. var returnHand = SelectTMPickArm(pmWafer.currentMod);
  3027. var pickHand = SelectTMPickArm(ll.Key);
  3028. var llWaferStatus = GetLLFixedReadyInOutSlots(ll.Key);
  3029. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(pmWafer.currentMod, pmWafer.currentSlot, ModuleName.TMRobot, (int)returnHand, returnHand) });
  3030. var llSwapActions = new List<MoveItem>();
  3031. llSwapActions.Add(new MoveItem(ModuleName.TMRobot, (int)returnHand, ll.Key, llWaferStatus.tInSlot.First(), returnHand));
  3032. llSwapActions.Add(new MoveItem(ll.Key, llWaferStatus.tOutSlot.First(), ModuleName.TMRobot, (int)pickHand, pickHand));
  3033. _tmSchdActions.Enqueue(llSwapActions);
  3034. return;
  3035. }
  3036. }
  3037. }
  3038. // just return to LL
  3039. var readyInLL = lls.Where(ll => GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count == 1 && ll.Value.TimeToReady <= 5);
  3040. foreach(var pmWafer in readyReturnWafers)
  3041. {
  3042. foreach(var ll in readyInLL)
  3043. {
  3044. if(CanWaferGotoLL(pmWafer, ll.Key))
  3045. {
  3046. var returnHand = SelectTMPickArm(pmWafer.currentMod);
  3047. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(pmWafer.currentMod, pmWafer.currentSlot, ModuleName.TMRobot, (int)returnHand, returnHand) });
  3048. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)returnHand, ll.Key, GetLLFixedReadyInOutSlots(ll.Key).tInSlot.First(), returnHand) });
  3049. return;
  3050. }
  3051. }
  3052. }
  3053. }
  3054. var readyPushInLLs = lls.Where(ll => GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count == 1 && ll.Value.TimeToReady <= 2);
  3055. if(readyPushInLLs.Count() > 0)
  3056. {
  3057. var destLL = readyPushInLLs.First().Key;
  3058. if(readyPushInLLs.Count() == 2 && readyPushInLLs.First().Value.Scheduler.IsAtm && readyPushInLLs.Last().Value.Scheduler.IsVac)
  3059. {
  3060. destLL = readyPushInLLs.Last().Key;
  3061. }
  3062. var pickHand = SelectTMPickArm(destLL);
  3063. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(destLL, GetLLFixedReadyInOutSlots(destLL).tOutSlot.First(), ModuleName.TMRobot, (int)pickHand, pickHand) });
  3064. return;
  3065. }
  3066. }
  3067. else
  3068. {
  3069. // should not go here
  3070. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, "Had better avoid picking two wafers on TM robot arms under Loadlock fixed slot pattern");
  3071. foreach(var wafer in robotWafers)
  3072. {
  3073. if(ModuleHelper.IsPm(wafer.destMod))
  3074. {
  3075. if(!_lstWaferTasks.Exists(wt => wt.currentMod == wafer.destMod) && _dictModuleTask[wafer.destMod].TimeToReady <= 2)
  3076. {
  3077. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  3078. return;
  3079. }
  3080. }
  3081. else
  3082. {
  3083. var readyInLL = lls.Where(ll => GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count == 1 && CanWaferGotoLL(wafer, ll.Key) && ll.Value.TimeToReady <= 2);
  3084. if(readyInLL.Count() > 0)
  3085. {
  3086. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, readyInLL.First().Key, GetLLFixedReadyInOutSlots(readyInLL.First().Key).tInSlot.First(), (Hand)wafer.currentSlot) });
  3087. return;
  3088. }
  3089. }
  3090. }
  3091. }
  3092. }
  3093. else // 4 slot Loadlock
  3094. {
  3095. if(robotWafers.Count() > 0)
  3096. {
  3097. if (robotWafers.Count() == 2)
  3098. {
  3099. if (robotWafers.All(wt => ModuleHelper.IsLoadPort(wt.destMod)))
  3100. {
  3101. var destLL = lls.Where(ll => robotWafers.All(wt => CanWaferGotoLL(wt, ll.Key)) &&
  3102. ll.Value.TimeToReady <= 10 &&
  3103. GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count == 2).
  3104. OrderByDescending(ll => GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count).
  3105. OrderBy(ll => ll.Value.TimeToReady);
  3106. if (destLL.Count() > 0)
  3107. {
  3108. var swapLL = destLL.First().Key;
  3109. var llWaferStatus = GetLLFixedReadyInOutSlots(swapLL);
  3110. var swapActions = new List<MoveItem>();
  3111. swapActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers.First().currentSlot, swapLL, llWaferStatus.tInSlot.First(), (Hand)robotWafers.First().currentSlot));
  3112. swapActions.Add(new MoveItem(ModuleName.TMRobot, robotWafers.Last().currentSlot, swapLL, llWaferStatus.tInSlot.Last(), (Hand)robotWafers.Last().currentSlot));
  3113. int pickCount = 0;
  3114. foreach (var slot in llWaferStatus.eOutSlot)
  3115. {
  3116. swapActions.Add(new MoveItem(swapLL, slot, ModuleName.TMRobot, pickCount, (Hand)pickCount));
  3117. }
  3118. _tmSchdActions.Enqueue(swapActions);
  3119. return;
  3120. }
  3121. }
  3122. foreach (var wafer in robotWafers)
  3123. {
  3124. if (ModuleHelper.IsPm(wafer.destMod))
  3125. {
  3126. if (!_lstWaferTasks.Exists(wt => wt.currentMod == wafer.destMod) && _dictModuleTask[wafer.destMod].TimeToReady <= 5)
  3127. {
  3128. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, wafer.destMod, 0, (Hand)wafer.currentSlot) });
  3129. return;
  3130. }
  3131. }
  3132. else
  3133. {
  3134. var placeLL = lls.Where(ll => CanWaferGotoLL(wafer, ll.Key) && ll.Value.TimeToReady <= 5 && GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count > 0).OrderBy(ll => ll.Value.TimeToReady);
  3135. if (placeLL.Count() > 0)
  3136. {
  3137. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, wafer.currentSlot, placeLL.First().Key, GetLLFixedReadyInOutSlots(placeLL.First().Key).tInSlot.First(), (Hand)wafer.currentSlot) });
  3138. return;
  3139. }
  3140. }
  3141. }
  3142. }
  3143. else if (robotWafers.Count() == 1)
  3144. {
  3145. var robotWafer = robotWafers.First();
  3146. if (ModuleHelper.IsPm(robotWafer.destMod))
  3147. {
  3148. if (_dictModuleTask[robotWafer.destMod].TimeToReady <= 5)
  3149. {
  3150. if (_lstWaferTasks.Exists(wt => wt.currentMod == robotWafer.destMod))
  3151. {
  3152. var pmSwap = new List<MoveItem>();
  3153. pmSwap.Add(new MoveItem(robotWafer.destMod, 0, ModuleName.TMRobot, 1 - robotWafer.currentSlot, (Hand)(1 - robotWafer.currentSlot)));
  3154. pmSwap.Add(new MoveItem(ModuleName.TMRobot, robotWafer.currentSlot, robotWafer.destMod, 0, (Hand)robotWafer.currentSlot));
  3155. _tmSchdActions.Enqueue(pmSwap);
  3156. return;
  3157. }
  3158. else
  3159. {
  3160. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, robotWafer.currentSlot, robotWafer.destMod, 0, (Hand)robotWafer.currentSlot) });
  3161. return;
  3162. }
  3163. }
  3164. }
  3165. else
  3166. {
  3167. var destLL = lls.Where(ll => CanWaferGotoLL(robotWafer, ll.Key) &&
  3168. ll.Value.TimeToReady <= 10 &&
  3169. GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count > 0).
  3170. OrderByDescending(ll => GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count).
  3171. OrderBy(ll => ll.Value.TimeToReady);
  3172. if (destLL.Count() > 0)
  3173. {
  3174. var swapLL = destLL.First().Key;
  3175. var llWaferStatus = GetLLFixedReadyInOutSlots(swapLL);
  3176. var llSwap = new List<MoveItem>();
  3177. llSwap.Add(new MoveItem(ModuleName.TMRobot, robotWafer.currentSlot, swapLL, llWaferStatus.tInSlot.First(), (Hand)robotWafer.currentSlot));
  3178. int pickCount = 0;
  3179. foreach (var slot in llWaferStatus.tOutSlot)
  3180. {
  3181. llSwap.Add(new MoveItem(swapLL, slot, ModuleName.TMRobot, pickCount, (Hand)pickCount));
  3182. pickCount++;
  3183. }
  3184. _tmSchdActions.Enqueue(llSwap);
  3185. return;
  3186. }
  3187. }
  3188. // try to return a wafer
  3189. var readyReturnWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsPm(wt.currentMod) && ModuleHelper.IsLoadPort(wt.destMod) && _dictModuleTask[wt.currentMod].TimeToReady <= 2);
  3190. foreach (var pmWafer in readyReturnWafers)
  3191. {
  3192. var readyLL = lls.Where(ll => CanWaferGotoLL(pmWafer, ll.Key) && GetLLFixedReadyInOutSlots(ll.Key).tInSlot.Count > 0 && ll.Value.TimeToReady <= 2).OrderBy(ll => ll.Value.Scheduler.IsVac ? 0 : 1);
  3193. if (readyLL.Count() > 0)
  3194. {
  3195. var validHand = (Hand)(1 - robotWafers.First().currentSlot);
  3196. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(pmWafer.currentMod, 0, ModuleName.TMRobot, (int)validHand, validHand) });
  3197. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)validHand, readyLL.First().Key, GetLLFixedReadyInOutSlots(readyLL.First().Key).tInSlot.First(), validHand) });
  3198. return;
  3199. }
  3200. }
  3201. // try to push in a wafer
  3202. var readyInLL = lls.Where(ll => GetLLFixedReadyInOutSlots(ll.Key).tOutSlot.Count > 0 && ll.Value.TimeToReady <= 2).OrderBy(ll => ll.Value.Scheduler.IsVac ? 0 : 1);
  3203. foreach (var ll in readyInLL)
  3204. {
  3205. var waferStatus = GetLLFixedReadyInOutSlots(ll.Key);
  3206. var wafer = _lstWaferTasks.Find(wt => wt.currentMod == ll.Key && wt.currentSlot == waferStatus.tOutSlot.First());
  3207. if (wafer != null &&
  3208. ModuleHelper.IsPm(wafer.destMod) &&
  3209. !_lstWaferTasks.Exists(wt => wt.currentMod == wafer.destMod) &&
  3210. _dictModuleTask[wafer.destMod].TimeToReady <= 2)
  3211. {
  3212. var validHand = (Hand)(1 - robotWafers.First().currentSlot);
  3213. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(wafer.currentMod, wafer.currentSlot, ModuleName.TMRobot, (int)validHand, validHand) });
  3214. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.TMRobot, (int)validHand, wafer.destMod, 0, validHand) });
  3215. return;
  3216. }
  3217. }
  3218. }
  3219. }
  3220. else // robot empty
  3221. {
  3222. var nearlyReadyReturnWafers = _lstWaferTasks.Where(wt => ModuleHelper.IsPm(wt.currentMod) && (ModuleHelper.IsLoadPort(wt.destMod) || _dictModuleTask[wt.currentMod].TimeToReady <= 10)).OrderBy(wt => _dictModuleTask[wt.currentMod].TimeToReady).ToList();
  3223. var readyLL = lls.Where(ll => ll.Value.TimeToReady <= 10);
  3224. if(readyLL.Count() > 0)
  3225. {
  3226. var llPath = FindTheBestMovePathWithFixSlot(readyLL, nearlyReadyReturnWafers);
  3227. if(ModuleHelper.IsLoadLock(llPath))
  3228. {
  3229. int returnCount = 0;
  3230. var llWaferStatus = GetLLFixedReadyInOutSlots(llPath);
  3231. var llSwapActions = new List<MoveItem>();
  3232. foreach(var wafer in nearlyReadyReturnWafers)
  3233. {
  3234. if(CanWaferGotoLL(wafer, llPath) && returnCount < Math.Min(llWaferStatus.tInSlot.Count, 2))
  3235. {
  3236. _tmSchdActions.Enqueue(new List<MoveItem> { new MoveItem(wafer.currentMod, 0, ModuleName.TMRobot, returnCount, (Hand)returnCount) });
  3237. llSwapActions.Add(new MoveItem(ModuleName.TMRobot, returnCount, llPath, llWaferStatus.tInSlot[returnCount], (Hand)returnCount));
  3238. returnCount++;
  3239. }
  3240. }
  3241. int pickCount = 0;
  3242. foreach(var slot in llWaferStatus.tOutSlot)
  3243. {
  3244. if (pickCount < 2)
  3245. {
  3246. llSwapActions.Add(new MoveItem(llPath, slot, ModuleName.TMRobot, pickCount, (Hand)pickCount));
  3247. pickCount++;
  3248. }
  3249. }
  3250. if(llSwapActions.Count > 0)
  3251. {
  3252. _tmSchdActions.Enqueue(llSwapActions);
  3253. }
  3254. return;
  3255. }
  3256. }
  3257. }
  3258. }
  3259. }
  3260. private bool IsTMRobotArmNotReserved(Hand hand)
  3261. {
  3262. if (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, (int)hand) || _lstWaferTasks.Exists(wt => wt.currentMod == ModuleName.TMRobot && wt.currentSlot == (int)hand))
  3263. return false;
  3264. foreach(var acs in _tmSchdActions)
  3265. {
  3266. foreach(var ac in acs)
  3267. {
  3268. if ((ac.DestinationModule == ModuleName.TMRobot || ac.SourceModule == ModuleName.TMRobot) && ac.DestinationSlot == (int)hand)
  3269. return false;
  3270. }
  3271. }
  3272. return true;
  3273. }
  3274. private Hand SelectTMPickArm(ModuleName mod)
  3275. {
  3276. switch (_tmRobotSingleArmOption)
  3277. {
  3278. case 0:
  3279. {
  3280. if (IsTMRobotArmNotReserved(Hand.Blade1))
  3281. return Hand.Blade1;
  3282. if (IsTMRobotArmNotReserved(Hand.Blade2))
  3283. return Hand.Blade2;
  3284. }
  3285. break;
  3286. case 1:
  3287. {
  3288. if (IsTMRobotArmNotReserved(Hand.Blade1))
  3289. return Hand.Blade1;
  3290. }
  3291. break;
  3292. case 2:
  3293. {
  3294. if (IsTMRobotArmNotReserved(Hand.Blade2))
  3295. return Hand.Blade2;
  3296. }
  3297. break;
  3298. // Blade1 In Blade2 Out
  3299. case 3:
  3300. {
  3301. if (ModuleHelper.IsLoadLock(mod) && IsTMRobotArmNotReserved(Hand.Blade1))
  3302. return Hand.Blade1;
  3303. if (ModuleHelper.IsPm(mod) && IsTMRobotArmNotReserved(Hand.Blade2))
  3304. return Hand.Blade2;
  3305. }
  3306. break;
  3307. // Blade2 In Blade1 Out
  3308. case 4:
  3309. {
  3310. if (ModuleHelper.IsLoadLock(mod) && IsTMRobotArmNotReserved(Hand.Blade2))
  3311. return Hand.Blade2;
  3312. if (ModuleHelper.IsPm(mod) && IsTMRobotArmNotReserved(Hand.Blade1))
  3313. return Hand.Blade1;
  3314. }
  3315. break;
  3316. }
  3317. return Hand.None;
  3318. }
  3319. private List<Hand> GetTMFreeHand()
  3320. {
  3321. var lstHands = new List<Hand>();
  3322. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0) && _tmRobotSingleArmOption != 2)
  3323. lstHands.Add(Hand.Blade1);
  3324. if (WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1) && _tmRobotSingleArmOption != 1)
  3325. lstHands.Add(Hand.Blade2);
  3326. return lstHands;
  3327. }
  3328. private List<Hand> GetEFEMFreeHand()
  3329. {
  3330. var lstHands = new List<Hand>();
  3331. if (WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, 0) && _efemRobotSingleArmOption != 2)
  3332. lstHands.Add(Hand.Blade1);
  3333. if (WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, 1) && _efemRobotSingleArmOption != 1)
  3334. lstHands.Add(Hand.Blade2);
  3335. return lstHands;
  3336. }
  3337. private bool CanWaferGotoLL(WaferTask task, ModuleName ll)
  3338. {
  3339. bool bVacWafer = ModuleHelper.IsPm(task.currentMod) || ModuleHelper.IsTMRobot(task.currentMod);
  3340. if (bVacWafer)
  3341. {
  3342. if (ModuleHelper.IsPm(task.destMod))
  3343. return false;
  3344. switch (task.llInOutPath)
  3345. {
  3346. case SequenceLLInOutPath.AInAOut:
  3347. case SequenceLLInOutPath.BInAOut:
  3348. return ll == ModuleName.LLA;
  3349. case SequenceLLInOutPath.AInBOut:
  3350. case SequenceLLInOutPath.BInBOut:
  3351. return ll == ModuleName.LLB;
  3352. case SequenceLLInOutPath.DInDOut:
  3353. return true;
  3354. }
  3355. }
  3356. else
  3357. {
  3358. if (ModuleHelper.IsLoadPort(task.destMod))
  3359. return false;
  3360. switch (task.llInOutPath)
  3361. {
  3362. case SequenceLLInOutPath.AInAOut:
  3363. case SequenceLLInOutPath.AInBOut:
  3364. return ll == ModuleName.LLA;
  3365. case SequenceLLInOutPath.BInAOut:
  3366. case SequenceLLInOutPath.BInBOut:
  3367. return ll == ModuleName.LLB;
  3368. case SequenceLLInOutPath.DInDOut:
  3369. return true;
  3370. }
  3371. }
  3372. return false;
  3373. }
  3374. private (List<int> inSlot, List<int> outSlot, List<int> emptySlot) GetLLReadyInOutSlots(ModuleName ll)
  3375. {
  3376. var readInSlots = new List<int>();
  3377. var readyOutSlots = new List<int>();
  3378. var emptySlots = new List<int>();
  3379. for(int slot = 0; slot < (ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber); slot++)
  3380. {
  3381. 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)))
  3382. {
  3383. emptySlots.Add(slot);
  3384. }
  3385. if(WaferManager.Instance.CheckHasWafer(ll, slot))
  3386. {
  3387. Guid waferID = WaferManager.Instance.GetWafer(ll, slot).InnerId;
  3388. var waferTask = _lstWaferTasks.Find(wafer => wafer.waferId == waferID);
  3389. if(waferTask != null && waferTask.movingStatus == RState.End)
  3390. {
  3391. if (ModuleHelper.IsPm(waferTask.destMod))
  3392. readInSlots.Add(slot);
  3393. else if (_dictModuleTask[ll].Scheduler.WaferArrivedTicks(slot) > waferTask.llDelayTime * 1000)
  3394. readyOutSlots.Add(slot);
  3395. }
  3396. }
  3397. }
  3398. return (readInSlots, readyOutSlots, emptySlots);
  3399. }
  3400. private (List<int> eInSlot, List<int> eOutSlot, List<int> tInSlot, List<int> tOutSlot) GetLLFixedReadyInOutSlots(ModuleName ll)
  3401. {
  3402. var eInSlot = new List<int>();
  3403. var eOutSlot = new List<int>();
  3404. var tInSlot = new List<int>();
  3405. var tOutSlot = new List<int>();
  3406. int SlotNumber = ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber;
  3407. for (int slot = 0; slot < SlotNumber; slot++)
  3408. {
  3409. 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)))
  3410. {
  3411. if ((slot >= SlotNumber / 2 && _LLSlotInOutOption == LLSlotInOutOpt.UpperInLowerOut) ||
  3412. (slot < SlotNumber / 2 && _LLSlotInOutOption == LLSlotInOutOpt.LowerInUpperOut))
  3413. {
  3414. eInSlot.Add(slot);
  3415. }
  3416. else
  3417. {
  3418. tInSlot.Add(slot);
  3419. }
  3420. }
  3421. if (WaferManager.Instance.CheckHasWafer(ll, slot))
  3422. {
  3423. var wafer = WaferManager.Instance.GetWafer(ll, slot);
  3424. var waferTask = _lstWaferTasks.Find(wt => wt.waferId == wafer.InnerId && wt.currentMod == ll && wt.currentSlot == slot);
  3425. if(waferTask == null)
  3426. {
  3427. LOG.Write(eEvent.ERR_ROUTER, ll, $"Routing failed, cannot find inner task associated with {wafer.WaferOrigin}");
  3428. }
  3429. else if (waferTask.movingStatus == RState.End)
  3430. {
  3431. if (ModuleHelper.IsPm(waferTask.destMod))
  3432. tOutSlot.Add(slot);
  3433. else if (_dictModuleTask[ll].Scheduler.WaferArrivedTicks(slot) > waferTask.llDelayTime * 1000)
  3434. eOutSlot.Add(slot);
  3435. }
  3436. }
  3437. }
  3438. return (eInSlot, eOutSlot, tInSlot, tOutSlot);
  3439. }
  3440. private (List<WaferInfo> wafers, List<int> emptySlots) GetLLWaferExistance(ModuleName ll)
  3441. {
  3442. var lstWafers = new List<WaferInfo>();
  3443. var lstEmptySlots = new List<int>();
  3444. for (int slot = 0; slot < (ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber); slot++)
  3445. {
  3446. var wafer = WaferManager.Instance.GetWafer(ll, slot);
  3447. if(wafer != null && !wafer.IsEmpty)
  3448. {
  3449. lstWafers.Add(wafer);
  3450. }
  3451. else
  3452. {
  3453. lstEmptySlots.Add(slot);
  3454. }
  3455. }
  3456. return (lstWafers, lstEmptySlots);
  3457. }
  3458. private void PrepareLLPressure()
  3459. {
  3460. var preaparedLLs = new List<ModuleName>();
  3461. var lls = _efemSchdActions.Where(acs => ModuleHelper.IsLoadLock(acs.First().Module)).Select(item => item.First().Module).ToList();
  3462. if (_curEfemAction.Count == 0 && _efemSchdActions.Count > 0 && ModuleHelper.IsLoadLock(_efemSchdActions.First().First().Module))
  3463. lls.Remove(_efemSchdActions.First().First().Module);
  3464. foreach(var ll in lls)
  3465. {
  3466. if(_dictModuleTask[ll].Scheduler.IsAvailable && _dictModuleTask[ll].Scheduler.IsVac && !preaparedLLs.Contains(ll))
  3467. {
  3468. (_dictModuleTask[ll].Scheduler as SchedulerLoadLock).PreVent();
  3469. preaparedLLs.Add(ll);
  3470. }
  3471. }
  3472. lls = _tmSchdActions.Where(acs => ModuleHelper.IsLoadLock(acs.First().Module)).Select(item => item.First().Module).ToList();
  3473. if(_curTmAction.Count == 0 && _tmSchdActions.Count > 0 && ModuleHelper.IsLoadLock(_tmSchdActions.First().First().Module))
  3474. lls.Remove(_tmSchdActions.First().First().Module);
  3475. foreach (var ll in lls)
  3476. {
  3477. if (_dictModuleTask[ll].Scheduler.IsAvailable && _dictModuleTask[ll].Scheduler.IsAtm && !preaparedLLs.Contains(ll))
  3478. {
  3479. (_dictModuleTask[ll].Scheduler as SchedulerLoadLock).PrePump();
  3480. preaparedLLs.Add(ll);
  3481. }
  3482. }
  3483. if (_efemSchdActions.Count > 0 || _curEfemAction.Count > 0 || _tmSchdActions.Count > 0 || _curTmAction.Count > 0)
  3484. return;
  3485. if (LLInOutPath == SequenceLLInOutPath.AInBOut || LLInOutPath == SequenceLLInOutPath.BInAOut)
  3486. {
  3487. var inLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLA : ModuleName.LLB;
  3488. var outLL = LLInOutPath == SequenceLLInOutPath.AInBOut ? ModuleName.LLB : ModuleName.LLA;
  3489. var inLLWaferCount = _lstWaferTasks.Where(wafer => wafer.currentMod == inLL).Count();
  3490. var outLLWaferCount = _lstWaferTasks.Where(wafer => wafer.currentMod == outLL).Count();
  3491. if (inLLWaferCount == 0 && !preaparedLLs.Contains(inLL))
  3492. {
  3493. (_dictModuleTask[inLL] as LoadlockTask).PreVent();
  3494. preaparedLLs.Add(inLL);
  3495. }
  3496. else if(inLLWaferCount == (LLInOutPath == SequenceLLInOutPath.AInBOut ? _LLASlotNumber : _LLBSlotNumber) && !preaparedLLs.Contains(inLL))
  3497. {
  3498. (_dictModuleTask[inLL] as LoadlockTask).PrePump();
  3499. preaparedLLs.Add(inLL);
  3500. }
  3501. if(outLLWaferCount == 0 && !preaparedLLs.Contains(outLL))
  3502. {
  3503. (_dictModuleTask[outLL] as LoadlockTask).PrePump();
  3504. preaparedLLs.Add(outLL);
  3505. }
  3506. else if(outLLWaferCount == (LLInOutPath == SequenceLLInOutPath.BInAOut ? _LLASlotNumber : _LLBSlotNumber) && !preaparedLLs.Contains(outLL))
  3507. {
  3508. (_dictModuleTask[outLL] as LoadlockTask).PreVent();
  3509. preaparedLLs.Add(outLL);
  3510. }
  3511. }
  3512. }
  3513. private void ReturnVacWafers()
  3514. {
  3515. if (_tmSchdActions.Count > 0 || _curTmAction.Count > 0)
  3516. {
  3517. RunSchdTMReturnActions();
  3518. return;
  3519. }
  3520. if (_tmRobotStatus != RState.End)
  3521. return;
  3522. var hands = GetTMFreeHand();
  3523. 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);
  3524. if(lls.Count() > 0)
  3525. {
  3526. // return robot wafers
  3527. int moveCount = 0;
  3528. var llActions = new List<MoveItem>();
  3529. var emptySlots = GetLLWaferExistance(lls.First().Key).emptySlots;
  3530. for(int arm = 0; arm < 2; arm++)
  3531. {
  3532. var wafer = WaferManager.Instance.GetWafer(ModuleName.TMRobot, arm);
  3533. if(wafer != null && !wafer.IsEmpty && moveCount < emptySlots.Count)
  3534. {
  3535. llActions.Add(new MoveItem(ModuleName.TMRobot, arm, lls.First().Key, emptySlots[moveCount], (Hand)arm));
  3536. moveCount++;
  3537. }
  3538. }
  3539. if(moveCount > 0)
  3540. {
  3541. _tmSchdActions.Enqueue(llActions);
  3542. return;
  3543. }
  3544. // return PM wafers
  3545. var PickActions = new List<MoveItem>();
  3546. var hasWaferPMs = _dictModuleTask.Where(mod => ModuleHelper.IsPm(mod.Key) && mod.Value.Scheduler.IsIdle && WaferManager.Instance.CheckHasWafer(mod.Key, 0));
  3547. foreach(var pm in hasWaferPMs)
  3548. {
  3549. if (moveCount < hands.Count && moveCount < emptySlots.Count)
  3550. {
  3551. PickActions.Add(new MoveItem(pm.Key, 0, ModuleName.TMRobot, (int)hands[moveCount], hands[moveCount]));
  3552. llActions.Add(new MoveItem(ModuleName.TMRobot, (int)hands[moveCount], lls.First().Key, emptySlots[moveCount], hands[moveCount]));
  3553. moveCount++;
  3554. }
  3555. }
  3556. if(moveCount > 0)
  3557. {
  3558. foreach(var ac in PickActions)
  3559. {
  3560. _tmSchdActions.Enqueue(new List<MoveItem> { ac });
  3561. }
  3562. _tmSchdActions.Enqueue(llActions);
  3563. }
  3564. }
  3565. }
  3566. private void ReturnAtmWafers()
  3567. {
  3568. if (_efemSchdActions.Count > 0 || _curEfemAction.Count > 0)
  3569. {
  3570. RunSchdEFEMReturnActions();
  3571. return;
  3572. }
  3573. if (_efemRobotStatus != RState.End)
  3574. return;
  3575. // return Robot Wafer
  3576. for(int i = 0; i < 2; i++)
  3577. {
  3578. var wafer = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, i);
  3579. if(wafer != null && !wafer.IsEmpty)
  3580. {
  3581. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.EfemRobot, i, (ModuleName)wafer.OriginStation, wafer.OriginSlot, (Hand)i) });
  3582. }
  3583. }
  3584. if (_efemSchdActions.Count > 0)
  3585. return;
  3586. var hands = GetEFEMFreeHand();
  3587. // return Aligner wafer
  3588. var aligner = ModuleHelper.InstalledModules.Where(mod => ModuleHelper.IsAligner(mod)).ToList();
  3589. if(aligner.Count > 0 && WaferManager.Instance.CheckHasWafer(aligner.First(), 0) && hands.Count > 0)
  3590. {
  3591. var wafer = WaferManager.Instance.GetWafer(aligner.First(), 0);
  3592. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(aligner.First(), 0, ModuleName.EfemRobot, (int)hands.First(), hands.First()) });
  3593. _efemSchdActions.Enqueue(new List<MoveItem> { new MoveItem(ModuleName.EfemRobot, (int)hands.First(), (ModuleName)wafer.OriginStation, wafer.OriginSlot, hands.First()) });
  3594. return;
  3595. }
  3596. // return Loadlock wafer
  3597. 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);
  3598. if(lls.Count() > 0)
  3599. {
  3600. int returnCount = 0;
  3601. var llActions = new List<MoveItem>();
  3602. var placActions = new List<MoveItem>();
  3603. foreach(var wafer in GetLLWaferExistance(lls.First().Key).wafers)
  3604. {
  3605. if(returnCount < hands.Count)
  3606. {
  3607. llActions.Add(new MoveItem(lls.First().Key, wafer.Slot, ModuleName.EfemRobot, (int)hands[returnCount], hands[returnCount]));
  3608. placActions.Add(new MoveItem(ModuleName.EfemRobot, (int)hands[returnCount], (ModuleName)wafer.OriginStation, wafer.OriginSlot, hands[returnCount]));
  3609. returnCount++;
  3610. }
  3611. }
  3612. if (returnCount > 0)
  3613. {
  3614. _efemSchdActions.Enqueue(llActions);
  3615. foreach (var ac in placActions)
  3616. {
  3617. _efemSchdActions.Enqueue(new List<MoveItem> { ac });
  3618. }
  3619. }
  3620. }
  3621. }
  3622. private void RunSchdEFEMReturnActions()
  3623. {
  3624. var efemRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot) as SchedulerEfemRobot;
  3625. if (efemRobot == null || !efemRobot.IsAvailable)
  3626. return;
  3627. if (_efemSchdActions.Count > 0)
  3628. {
  3629. if (_curEfemAction.Count == 0 || isReturnActionsDone(_curEfemAction))
  3630. {
  3631. var nextActions = _efemSchdActions.First();
  3632. var nextModule = nextActions.First().Module;
  3633. if (ModuleHelper.IsLoadLock(nextModule) && !_dictModuleTask[nextModule].IsIdle)
  3634. return;
  3635. _curEfemAction = _efemSchdActions.Dequeue();
  3636. efemRobot.PostMoveItems(_curEfemAction.ToArray());
  3637. }
  3638. }
  3639. else if (_curEfemAction.Count >= 0 && isReturnActionsDone(_curEfemAction)) // all scheduled actions done
  3640. {
  3641. _curEfemAction.Clear();
  3642. }
  3643. }
  3644. private void RunSchdTMReturnActions()
  3645. {
  3646. var tmRobot = Singleton<TransferModule>.Instance.GetScheduler(ModuleName.TMRobot) as SchedulerTMRobot;
  3647. if (tmRobot == null || !tmRobot.IsAvailable)
  3648. return;
  3649. if (_tmSchdActions.Count > 0)
  3650. {
  3651. if (_curTmAction.Count == 0 || isReturnActionsDone(_curTmAction))
  3652. {
  3653. var nextActions = _tmSchdActions.First();
  3654. var nextModule = nextActions.First().Module;
  3655. if (ModuleHelper.IsLoadLock(nextModule) && !_dictModuleTask[nextModule].IsIdle)
  3656. return;
  3657. _curTmAction = _tmSchdActions.Dequeue();
  3658. tmRobot.SendMoveItems(_curTmAction.ToArray());
  3659. }
  3660. }
  3661. else if (_curTmAction.Count >= 0 && isReturnActionsDone(_curTmAction)) // all scheduled actions done
  3662. {
  3663. _curTmAction.Clear();
  3664. }
  3665. }
  3666. #endregion
  3667. #region sequence/recipe operation
  3668. private bool CheckSequencePmReady(SequenceInfo seq, out string reason)
  3669. {
  3670. reason = "";
  3671. foreach(var pm in seq.PMs)
  3672. {
  3673. if (ModuleHelper.IsInstalled(pm) && (_dictModuleTask[pm].Scheduler.IsOnline || !Singleton<RouteManager>.Instance.IsAutoMode))
  3674. return true;
  3675. }
  3676. reason = $"Sequence {seq.Name} no valid PM, " + string.Join("|", seq.PMs);
  3677. return false;
  3678. }
  3679. private bool CheckSequenceKepler2200TemperatureReady(SequenceInfo seq)
  3680. {
  3681. for (int i = 0; i < seq.Steps.Count; i++)
  3682. {
  3683. SequenceStepInfo stepInfo = seq.Steps[i];
  3684. foreach (var module in stepInfo.StepModules)
  3685. {
  3686. if (ModuleHelper.IsPm(module))
  3687. {
  3688. string attr = $"{module}Recipe";
  3689. if (stepInfo.StepParameter.ContainsKey(attr)
  3690. && !string.IsNullOrWhiteSpace((string)stepInfo.StepParameter[attr]))
  3691. {
  3692. var recipeName = (string)stepInfo.StepParameter[attr];
  3693. var recipeContent =
  3694. RecipeFileManager.Instance.LoadRecipe($"{module}", recipeName, false, "Process");
  3695. var recipe = Recipe.Load(recipeContent);
  3696. float currentChamberTemperature;
  3697. switch (module)
  3698. {
  3699. case ModuleName.PMA:
  3700. currentChamberTemperature = Singleton<RouteManager>.Instance.PMA.ChamberTemperature;
  3701. break;
  3702. case ModuleName.PMB:
  3703. currentChamberTemperature = Singleton<RouteManager>.Instance.PMB.ChamberTemperature;
  3704. break;
  3705. case ModuleName.PMC:
  3706. currentChamberTemperature = Singleton<RouteManager>.Instance.PMC.ChamberTemperature;
  3707. break;
  3708. case ModuleName.PMD:
  3709. currentChamberTemperature = Singleton<RouteManager>.Instance.PMD.ChamberTemperature;
  3710. break;
  3711. default:
  3712. currentChamberTemperature = 0;
  3713. break;
  3714. }
  3715. if (recipe.Header.Temperature != null && recipe.Header.Temperature != "" && (currentChamberTemperature > Convert.ToSingle(recipe.Header.Temperature) + 10 || currentChamberTemperature < Convert.ToSingle(recipe.Header.Temperature) - 10))
  3716. {
  3717. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Start job失败,由于{module}腔体温度{currentChamberTemperature}与{recipeName} Recipe温度{recipe.Header.Temperature}不匹配");
  3718. return false;
  3719. }
  3720. }
  3721. }
  3722. }
  3723. }
  3724. return true;
  3725. }
  3726. private bool CheckSequenceRecipeFileValid(SequenceInfo seq, out string reason)
  3727. {
  3728. for (int i = 0; i < seq.Steps.Count; i++)
  3729. {
  3730. SequenceStepInfo stepInfo = seq.Steps[i];
  3731. foreach (var module in stepInfo.StepModules)
  3732. {
  3733. if (ModuleHelper.IsPm(module))
  3734. {
  3735. string attr = $"{module}Recipe";
  3736. if (stepInfo.StepParameter.ContainsKey(attr)
  3737. && !string.IsNullOrWhiteSpace((string)stepInfo.StepParameter[attr]))
  3738. {
  3739. var recipeName = (string)stepInfo.StepParameter[attr];
  3740. if (!string.IsNullOrWhiteSpace(recipeName))
  3741. {
  3742. var recipeContent =
  3743. RecipeFileManager.Instance.LoadRecipe($"{module}", recipeName, false, "Process");
  3744. if (string.IsNullOrWhiteSpace(recipeContent))
  3745. {
  3746. reason = $"Can not find recipe file{recipeName}";
  3747. return false;
  3748. }
  3749. }
  3750. }
  3751. }
  3752. }
  3753. }
  3754. reason = "";
  3755. return true;
  3756. }
  3757. private void UpdateLLInOutPathProperty()
  3758. {
  3759. if (_lstControlJobs.Count == 0)
  3760. return;
  3761. var inOutPaths = new List<SequenceLLInOutPath>();
  3762. foreach(var wt in _lstWaferTasks)
  3763. {
  3764. if (!inOutPaths.Contains(wt.llInOutPath))
  3765. inOutPaths.Add(wt.llInOutPath);
  3766. }
  3767. if (inOutPaths.Count == 1)
  3768. {
  3769. _LLInOutPath = inOutPaths[0];
  3770. }
  3771. else
  3772. _LLInOutPath = SequenceLLInOutPath.DInDOut;
  3773. }
  3774. private bool GetAllJobRecipe(ControlJobInfo cj,SequenceInfo seq)
  3775. {
  3776. for (int i = 0; i < seq.Steps.Count; i++)
  3777. {
  3778. SequenceStepInfo stepInfo = seq.Steps[i];
  3779. foreach (var module in stepInfo.StepModules)
  3780. {
  3781. if (ModuleHelper.IsPm(module))
  3782. {
  3783. string attr = $"{module}Recipe";
  3784. if (stepInfo.StepParameter.ContainsKey(attr)
  3785. && !string.IsNullOrWhiteSpace((string)stepInfo.StepParameter[attr]))
  3786. {
  3787. var recipeName = (string)stepInfo.StepParameter[attr];
  3788. var recipeContent =
  3789. RecipeFileManager.Instance.LoadRecipe($"{module}", recipeName, false, "Process");
  3790. var recipe = Recipe.Load(recipeContent);
  3791. if (recipe.Header.ChuckRecipe != null && recipe.Header.ChuckRecipe != "")
  3792. {
  3793. InUseRecipes.Add($"{module}.ChuckRecipe.{recipe.Header.ChuckRecipe}");
  3794. }
  3795. if (recipe.Header.DechuckRecipe != null && recipe.Header.DechuckRecipe != "")
  3796. {
  3797. InUseRecipes.Add($"{module}.DechuckRecipe.{recipe.Header.DechuckRecipe}");
  3798. }
  3799. InUseRecipes.Add($"{module}.Process.{recipeName}");
  3800. if (cj.PreJobClean != "")
  3801. {
  3802. InUseRecipes.Add($"{module}.Clean.{cj.PreJobClean}");
  3803. }
  3804. if (cj.PostJobClean != "")
  3805. {
  3806. InUseRecipes.Add($"{module}.Clean.{cj.PostJobClean}");
  3807. }
  3808. if (stepInfo.StepParameter["WTWClean"].ToString() != "")
  3809. {
  3810. InUseRecipes.Add($"{module}.Clean.{stepInfo.StepParameter["WTWClean"]}");
  3811. }
  3812. }
  3813. }
  3814. }
  3815. }
  3816. return true;
  3817. }
  3818. #endregion
  3819. }
  3820. }