SystemDispatcher.cs 153 KB

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