SystemDispatcher.cs 221 KB

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