SystemDispatcher.cs 148 KB

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