SystemDispatcher.cs 155 KB

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