SystemDispatcher.cs 149 KB

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