SystemDispatcher.cs 154 KB

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