SystemDispatcher.cs 149 KB

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