SystemDispatcher.cs 196 KB

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