SystemDispatcher.cs 149 KB

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