AutoCycle.cs 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  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. enum ModulePriority
  27. {
  28. High,
  29. Middle,
  30. Low,
  31. Stop,
  32. }
  33. public enum MovingStatus
  34. {
  35. Idle,
  36. Waiting,
  37. Moving,
  38. Moved,
  39. // PM Status
  40. WaitProcess,
  41. StartProcess,
  42. Processing,
  43. // Align Status
  44. WaitAlign,
  45. StartAlign,
  46. Aligning,
  47. }
  48. class ModuleFlag
  49. {
  50. public ModulePriority Priority { get; set; }
  51. public MovingStatus MovingStatus { get; set; }
  52. public bool InUsed { get; set; }
  53. public ModuleFlag(ModulePriority _priority)
  54. {
  55. Priority = _priority;
  56. MovingStatus = MovingStatus.Idle;
  57. InUsed = true;
  58. }
  59. }
  60. class AutoCycle : IRoutine
  61. {
  62. private List<ControlJobInfo> _lstControlJobs = new List<ControlJobInfo>();
  63. private List<ProcessJobInfo> _lstProcessJobs = new List<ProcessJobInfo>();
  64. private RState _cycleState = RState.Init;
  65. Dictionary<ModuleName, SchedulerModule> _atmSchedulers = new Dictionary<ModuleName, SchedulerModule>();
  66. Dictionary<ModuleName, ModuleFlag> _atmModules = new Dictionary<ModuleName, ModuleFlag>();
  67. Dictionary<ModuleName, SchedulerModule> _vacSchedulers = new Dictionary<ModuleName, SchedulerModule>();
  68. Dictionary<ModuleName, ModuleFlag> _vacModules = new Dictionary<ModuleName, ModuleFlag>();
  69. SchedulerTMRobot _tmRobot = (SchedulerTMRobot)Singleton<TransferModule>.Instance.GetScheduler(ModuleName.TMRobot);
  70. SchedulerEfemRobot _efemRobot = (SchedulerEfemRobot)Singleton<TransferModule>.Instance.GetScheduler(ModuleName.EfemRobot);
  71. Dictionary<SlotItem, long> _vacReadyOutSlots = new Dictionary<SlotItem, long>();
  72. Dictionary<SlotItem, long> _vacReadyInSlots = new Dictionary<SlotItem, long>();
  73. Dictionary<SlotItem, long> _atmReadyOutSlots = new Dictionary<SlotItem, long>();
  74. Dictionary<SlotItem, long> _atmReadyInSlots = new Dictionary<SlotItem, long>();
  75. List<int> _LLAInSlot = new List<int> { 0, 1, 2, 3 };
  76. List<int> _LLAOutSlot = new List<int> { 0, 1, 2, 3 };
  77. List<int> _LLBInSlot = new List<int> { 0, 1, 2, 3 };
  78. List<int> _LLBOutSlot = new List<int> { 0, 1, 2, 3 };
  79. int _LLASlotNumber = 4;
  80. int _LLBSlotNumber = 4;
  81. int _efemRobotSingleArmOption = 0;
  82. SequenceLLInOutPath _LLInOutPath = SequenceLLInOutPath.DInDOut;
  83. List<MoveItem> _movingItems = new List<MoveItem>();
  84. List<MoveItem> _efemMovingItems = new List<MoveItem>();
  85. Dictionary<SlotItem, Guid> _vacWaferTargets = new Dictionary<SlotItem, Guid>();
  86. Dictionary<SlotItem, Guid> _atmWaferTargets = new Dictionary<SlotItem, Guid>();
  87. R_TRIG _vacMoveFinishTrig = new R_TRIG();
  88. R_TRIG _atmMoveFinishTrig = new R_TRIG();
  89. private SchedulerFACallback _faCallback;
  90. private SchedulerDBCallback _dbCallback;
  91. private bool _isCycleMode;
  92. private int _cycleSetPoint = 0;
  93. private int _cycledCount = 0;
  94. private int _cycledWafer = 0;
  95. private float _throughput = 0.0f;
  96. private Stopwatch _cycleWatch = new Stopwatch();
  97. public bool HasJobRunning => _lstControlJobs.Count > 0;
  98. #region public interface
  99. public AutoCycle()
  100. {
  101. _faCallback = new SchedulerFACallback();
  102. _dbCallback = new SchedulerDBCallback();
  103. InitModules();
  104. _LLASlotNumber = SC.GetValue<int>("LLA.SlotNumber");
  105. _LLBSlotNumber = SC.GetValue<int>("LLB.SlotNumber");
  106. _LLAInSlot.RemoveAll(item => item >= _LLASlotNumber);
  107. _LLAOutSlot.RemoveAll(item => item >= _LLASlotNumber);
  108. _LLBInSlot.RemoveAll(item => item >= _LLBSlotNumber);
  109. _LLBOutSlot.RemoveAll(item => item >= _LLBSlotNumber);
  110. _efemRobotSingleArmOption = SC.GetValue<int>("EFEM.SingleArmOption");
  111. DATA.Subscribe("Scheduler.CycledCount", () => _cycledCount,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  112. DATA.Subscribe("Scheduler.CycledWafer", () => _cycledWafer, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  113. DATA.Subscribe("Scheduler.CycleSetPoint", () => _cycleSetPoint, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  114. DATA.Subscribe("Scheduler.Throughput", () => _throughput, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  115. DATA.Subscribe("Scheduler.PjIdList", () => Array.ConvertAll(_lstProcessJobs.ToArray(), x => x.InnerId.ToString()).ToList());
  116. }
  117. public RState Start(params object[] objs)
  118. {
  119. _isCycleMode = SC.GetValue<bool>("System.IsCycleMode");
  120. _cycleSetPoint = _isCycleMode ? SC.GetValue<int>("System.CycleCount") : 0;
  121. _cycledWafer = 0;
  122. _cycledCount = 0;
  123. _throughput = 0;
  124. _cycleWatch.Stop();
  125. return RState.Running;
  126. }
  127. public RState Monitor()
  128. {
  129. if(_cycleState == RState.Running)
  130. {
  131. prelude();
  132. driveAtmSystem();
  133. driveVacSystem();
  134. epilogue();
  135. }
  136. return _cycleState;
  137. }
  138. public void Abort()
  139. {
  140. }
  141. public void CreateJob(Dictionary<string, object> param)
  142. {
  143. _isCycleMode = SC.GetValue<bool>("System.IsCycleMode");
  144. _cycleSetPoint = _isCycleMode ? SC.GetValue<int>("System.CycleCount") : 0;
  145. string[] slotSequence = (string[])param["SlotSequence"];
  146. string jobId = (string)param["JobId"];
  147. string module = (string)param["Module"];
  148. //bool autoStart = (bool)param["AutoStart"];
  149. string lotId = jobId;
  150. if (param.ContainsKey("LotId"))
  151. lotId = (string)param["LotId"];
  152. if (slotSequence.Length != SC.GetValue<int>("EFEM.LoadPort.SlotNumber"))
  153. {
  154. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"slot sequence parameter not valid, length is {slotSequence.Length}, should be {SC.GetValue<int>("EFEM.LoadPort.SlotNumber")}");
  155. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  156. return;
  157. }
  158. if (!ModuleHelper.IsLoadPort(ModuleHelper.Converter(module)))
  159. {
  160. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"{module} should be LoadPort");
  161. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  162. return;
  163. }
  164. if (string.IsNullOrEmpty(jobId))
  165. {
  166. jobId = "CJ_Local_" + module;
  167. }
  168. if (_lstControlJobs.Exists(x => x.Name == jobId))
  169. {
  170. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"{jobId} already created");
  171. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  172. return;
  173. }
  174. SchedulerLoadPort lp = _atmSchedulers[ModuleHelper.Converter(module)] as SchedulerLoadPort;
  175. if (!lp.CheckReadyRunJob())
  176. {
  177. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"{module} not ready");
  178. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  179. return;
  180. }
  181. ControlJobInfo cj = new ControlJobInfo();
  182. cj.Name = jobId;
  183. cj.Module = module;
  184. cj.LotName = lotId;
  185. cj.LotInnerId = Guid.NewGuid();
  186. cj.LotWafers = new List<WaferInfo>();
  187. cj.SetState(EnumControlJobState.WaitingForStart);
  188. Dictionary<string, bool[]> seqSlot = new Dictionary<string, bool[]>();
  189. Dictionary<string, List<Tuple<ModuleName, int>>> seqSlotWafers = new Dictionary<string, List<Tuple<ModuleName, int>>>();
  190. Dictionary<string, string> indexSequence = new Dictionary<string, string>();
  191. bool enableGroupBySequence = SC.GetValue<bool>("Scheduler.GroupWaferBySequence");
  192. for (int i = 0; i < SC.GetValue<int>("EFEM.LoadPort.SlotNumber"); i++)
  193. {
  194. if (string.IsNullOrEmpty(slotSequence[i]) || string.IsNullOrEmpty(slotSequence[i].Trim()))
  195. continue;
  196. string groupName = enableGroupBySequence ? slotSequence[i].Trim() : i.ToString();
  197. indexSequence[groupName] = slotSequence[i];
  198. if (!seqSlot.ContainsKey(groupName))
  199. {
  200. seqSlot[groupName] = new bool[SC.GetValue<int>("EFEM.LoadPort.SlotNumber")];
  201. }
  202. if (!seqSlotWafers.ContainsKey(groupName))
  203. {
  204. seqSlotWafers[groupName] = new List<Tuple<ModuleName, int>>();
  205. }
  206. seqSlot[groupName][i] = true;
  207. if (!WaferManager.Instance.CheckHasWafer(module, i))
  208. {
  209. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"job wafer: {module} slot {i + 1} not in the carrier");
  210. return;
  211. }
  212. if (!WaferManager.Instance.CheckWafer(ModuleHelper.Converter(module), i, WaferStatus.Normal))
  213. {
  214. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"job wafer: {module} slot {i + 1} status is {WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).Status}");
  215. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  216. return;
  217. }
  218. if (WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).ProcessState != EnumWaferProcessStatus.Idle)
  219. {
  220. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"job wafer: {module} slot {i + 1} process status is {WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).ProcessState}");
  221. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  222. return;
  223. }
  224. cj.LotWafers.Add(WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i));
  225. WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).SequenceName = slotSequence[i];
  226. seqSlotWafers[groupName].Add(Tuple.Create(ModuleHelper.Converter(module), i));
  227. cj.JobWaferSize = WaferManager.Instance.GetWafer(ModuleHelper.Converter(module), i).Size;
  228. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Assigned wafer job, wafer {module}.{i + 1}, sequence: {slotSequence[i]}");
  229. }
  230. if (seqSlotWafers.Count == 0)
  231. {
  232. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"job has not assign wafer");
  233. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  234. return;
  235. }
  236. List<ProcessJobInfo> pjs = new List<ProcessJobInfo>();
  237. string[] seqs = seqSlot.Keys.ToArray();
  238. for (int i = 0; i < seqs.Length; i++)
  239. {
  240. ProcessJobInfo pj = new ProcessJobInfo();
  241. pj.Name = jobId + "_" + (i + 1);
  242. pj.Sequence = SequenceInfoHelper.GetInfo(indexSequence[seqs[i]]);
  243. pj.ControlJobName = cj.Name;
  244. pj.LotName = lotId;
  245. pj.SlotWafers = seqSlotWafers[seqs[i]];
  246. pj.SetState(EnumProcessJobState.Queued);
  247. if (!CheckSequencePmReady(pj.Sequence, null, out _, out string reason))
  248. {
  249. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"no valid chamber for the {reason}");
  250. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  251. return;
  252. }
  253. if (!RouteManager.IsATMMode && !CheckSequenceRecipeFileValid(pj.Sequence, out reason))
  254. {
  255. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"recipe file not valid in the sequence, {reason}");
  256. _faCallback.JobCreateFailed(module, lotId, jobId, "");
  257. return;
  258. }
  259. pjs.Add(pj);
  260. }
  261. _dbCallback.LotUpdate(cj);
  262. foreach (var pj in pjs)
  263. {
  264. cj.ProcessJobNameList.Add(pj.Name);
  265. _lstProcessJobs.Add(pj);
  266. }
  267. _lstControlJobs.Add(cj);
  268. //AssociatedPMWithLP(cj);
  269. _faCallback.JobCreated(cj, GetFirstProcessJob(cj));
  270. }
  271. public bool CheckAllJobDone()
  272. {
  273. foreach (var cj in _lstControlJobs)
  274. {
  275. if (cj.State == EnumControlJobState.Executing || cj.State == EnumControlJobState.Paused)
  276. return false;
  277. }
  278. return true;
  279. }
  280. public bool CheckJobJustDone(out string sJobName)
  281. {
  282. foreach (var cj in _lstControlJobs)
  283. {
  284. if (cj.State == EnumControlJobState.Completed && !cj.BeenPosted)
  285. {
  286. //LP;WaferSize;Lot;Number;Start;End;
  287. sJobName = $"{cj.Module};{cj.JobWaferSize};{cj.LotName};{cj.LotWafers.Count};{cj.StartTime:T};{cj.EndTime:T}";
  288. cj.BeenPosted = true;
  289. return true;
  290. }
  291. }
  292. sJobName = "NULL";
  293. return false;
  294. }
  295. public ProcessJobInfo GetFirstProcessJob(ControlJobInfo cj)
  296. {
  297. foreach (var pj in _lstProcessJobs)
  298. {
  299. if (pj.ControlJobName == cj.Name)
  300. return pj;
  301. }
  302. return null;
  303. }
  304. public void AbortJob(string jobName)
  305. {
  306. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  307. if (cj == null)
  308. {
  309. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"abort job rejected, not found job with id {jobName}");
  310. return;
  311. }
  312. List<ProcessJobInfo> pjAbortList = new List<ProcessJobInfo>();
  313. foreach (var pj in _lstProcessJobs)
  314. {
  315. if (pj.ControlJobName == cj.Name)
  316. {
  317. pj.SetState(EnumProcessJobState.Aborting);
  318. pjAbortList.Add(pj);
  319. int unprocessed = 0;
  320. int aborted = 0;
  321. WaferInfo[] wafers = WaferManager.Instance.GetWaferByProcessJob(pj.Name);
  322. foreach (var waferInfo in wafers)
  323. {
  324. waferInfo.ProcessJob = null;
  325. waferInfo.NextSequenceStep = 0;
  326. if (waferInfo.ProcessState != EnumWaferProcessStatus.Completed)
  327. unprocessed++;
  328. }
  329. JobDataRecorder.EndPJ(pj.InnerId.ToString(), aborted, unprocessed);
  330. }
  331. }
  332. _faCallback.JobAborted(cj, GetFirstProcessJob(cj));
  333. _dbCallback.LotFinished(cj);
  334. foreach (var pj in pjAbortList)
  335. {
  336. _lstProcessJobs.Remove(pj);
  337. }
  338. _lstControlJobs.Remove(cj);
  339. }
  340. public void StopJob(string jobName)
  341. {
  342. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  343. if (cj == null)
  344. {
  345. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"stop job rejected, not found job with id {jobName}");
  346. return;
  347. }
  348. foreach (var pj in _lstProcessJobs)
  349. {
  350. if (pj.ControlJobName == cj.Name)
  351. {
  352. pj.SetState(EnumProcessJobState.Stopping);
  353. }
  354. }
  355. _faCallback.JobStopped(cj, GetFirstProcessJob(cj));
  356. _dbCallback.LotFinished(cj);
  357. }
  358. public void ResumeJob(string jobName)
  359. {
  360. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  361. if (cj == null)
  362. {
  363. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"resume job rejected, not found job with id {jobName}");
  364. return;
  365. }
  366. if (cj.State == EnumControlJobState.Paused)
  367. {
  368. cj.SetState(EnumControlJobState.Executing);
  369. }
  370. _faCallback.JobResumed(cj, GetFirstProcessJob(cj));
  371. }
  372. public void PauseJob(string jobName)
  373. {
  374. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  375. if (cj == null)
  376. {
  377. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"pause job rejected, not found job with id {jobName}");
  378. return;
  379. }
  380. if (cj.State == EnumControlJobState.Executing)
  381. {
  382. cj.SetState(EnumControlJobState.Paused);
  383. }
  384. _faCallback.JobPaused(cj, GetFirstProcessJob(cj));
  385. }
  386. public void StartJob(string jobName)
  387. {
  388. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == jobName);
  389. if (cj == null)
  390. {
  391. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"start job rejected, not found job with id {jobName}");
  392. return;
  393. }
  394. if (cj.State == EnumControlJobState.WaitingForStart)
  395. {
  396. cj.SetState(EnumControlJobState.Executing);
  397. //PreJobClean(cj);
  398. (_atmSchedulers[ModuleHelper.Converter(cj.Module)] as SchedulerLoadPort).NoteJobStart();
  399. cj.StartTime = DateTime.Now;
  400. _dbCallback.LotCreated(cj);
  401. _faCallback.JobStarted(cj, GetFirstProcessJob(cj));
  402. //if (!_blockingWatcher.IsRunning)
  403. // _blockingWatcher.Restart();
  404. //ResetTraceFlag();
  405. }
  406. if(!_cycleWatch.IsRunning)
  407. {
  408. _cycleWatch.Restart();
  409. }
  410. _cycleState = RState.Running;
  411. }
  412. public void Clear()
  413. {
  414. _vacWaferTargets.Clear();
  415. _atmWaferTargets.Clear();
  416. _movingItems.Clear();
  417. _efemMovingItems.Clear();
  418. _vacMoveFinishTrig.RST = true;
  419. _atmMoveFinishTrig.RST = true;
  420. _efemRobot.ResetTask();
  421. _tmRobot.ResetTask();
  422. foreach (var module in _vacSchedulers)
  423. {
  424. module.Value.ResetTask();
  425. _vacModules[module.Key].MovingStatus = MovingStatus.Idle;
  426. }
  427. foreach (var module in _atmSchedulers)
  428. {
  429. module.Value.ResetTask();
  430. _atmModules[module.Key].MovingStatus = MovingStatus.Idle;
  431. }
  432. _lstControlJobs.Clear();
  433. _lstProcessJobs.Clear();
  434. _cycleState = RState.End;
  435. }
  436. #endregion
  437. private void InitModules()
  438. {
  439. foreach(var module in new ModuleName[]{ ModuleName.LP1, ModuleName.LP2, ModuleName.LP3,
  440. ModuleName.Aligner1, ModuleName.Aligner2, ModuleName.Cooling1, ModuleName.Cooling2})
  441. {
  442. if(ModuleHelper.IsInstalled(module))
  443. {
  444. if(ModuleHelper.IsLoadPort(module))
  445. {
  446. _atmSchedulers[module] = Singleton<TransferModule>.Instance.GetScheduler(module);
  447. _atmModules[module] = new ModuleFlag(ModulePriority.Middle);
  448. }
  449. else if(ModuleHelper.IsAligner(module) || ModuleHelper.IsCooling(module))
  450. {
  451. _atmSchedulers[module] = Singleton<TransferModule>.Instance.GetScheduler(module);
  452. _atmModules[module] = new ModuleFlag(ModulePriority.Middle);
  453. }
  454. }
  455. }
  456. foreach (var module in new ModuleName[] { ModuleName.PMA, ModuleName.PMB, ModuleName.PMC,
  457. ModuleName.PMD, ModuleName.PME, ModuleName.PMF})
  458. {
  459. if (ModuleHelper.IsInstalled(module))
  460. {
  461. _vacSchedulers[module] = Singleton<TransferModule>.Instance.GetScheduler(module);
  462. _vacModules[module] = new ModuleFlag(ModulePriority.Middle);
  463. }
  464. }
  465. foreach (var module in new ModuleName[] { ModuleName.LLA, ModuleName.LLB })
  466. {
  467. if (ModuleHelper.IsInstalled(module))
  468. {
  469. var llScheduler = Singleton<TransferModule>.Instance.GetScheduler(module);
  470. _vacSchedulers[module] = llScheduler;
  471. _vacModules[module] = new ModuleFlag(ModulePriority.Middle);
  472. _atmSchedulers[module] = llScheduler;
  473. _atmModules[module] = new ModuleFlag(ModulePriority.Middle);
  474. }
  475. }
  476. }
  477. private void prelude()
  478. {
  479. }
  480. private void epilogue()
  481. {
  482. CheckWaferArrived();
  483. ProcessPMTask();
  484. ProcessAlignerTask();
  485. UpdateProcessJobStatus();
  486. UpdateControlJobStatus();
  487. StartNewJob();
  488. UpdateLLInOutPathProperty();
  489. }
  490. private void driveVacSystem()
  491. {
  492. PumpingTMRobotTask();
  493. ProcessTMRobotTask();
  494. RuningTMRobotTask();
  495. }
  496. #region Vacuum System
  497. private void PumpingTMRobotTask()
  498. {
  499. if (!_tmRobot.IsAvailable)
  500. return;
  501. foreach(var mod in _vacSchedulers)
  502. {
  503. if (mod.Value.IsAvailable && _vacModules[mod.Key].MovingStatus == MovingStatus.Idle)
  504. {
  505. if(ModuleHelper.IsLoadLock(mod.Key))
  506. {
  507. var inSlots = mod.Key == ModuleName.LLA ? _LLAInSlot : _LLBInSlot;
  508. foreach(var slot in inSlots)
  509. {
  510. if (IsLoadLockReservedByEFEM(mod.Key))
  511. continue;
  512. if(WaferManager.Instance.CheckHasWafer(mod.Key, slot) && IsVacWaferReadyOut(mod.Key, slot))
  513. {
  514. _vacReadyOutSlots.Add(new SlotItem(mod.Key, slot), mod.Value.WaferArrivedTicks(slot));
  515. }
  516. }
  517. var outSlots = mod.Key == ModuleName.LLA ? _LLAOutSlot : _LLBOutSlot;
  518. foreach(var slot in outSlots)
  519. {
  520. if (IsLoadLockReservedByEFEM(mod.Key))
  521. continue;
  522. if (WaferManager.Instance.CheckNoWafer(mod.Key, slot))
  523. {
  524. _vacReadyInSlots.Add(new SlotItem(mod.Key, slot), mod.Value.WaferArrivedTicks(slot));
  525. }
  526. }
  527. }
  528. else
  529. {
  530. if(WaferManager.Instance.CheckHasWafer(mod.Key, 0) && IsVacWaferReadyOut(mod.Key, 0)) // processed?
  531. {
  532. _vacReadyOutSlots.Add(new SlotItem(mod.Key, 0), mod.Value.WaferArrivedTicks(0));
  533. }
  534. else if(WaferManager.Instance.CheckNoWafer(mod.Key, 0))
  535. {
  536. _vacReadyInSlots.Add(new SlotItem(mod.Key, 0), mod.Value.WaferArrivedTicks(0));
  537. }
  538. }
  539. }
  540. }
  541. _vacReadyOutSlots = _vacReadyOutSlots.OrderByDescending(item => item.Value).ToDictionary(k => k.Key, v => v.Value);
  542. }
  543. bool SearchWaferDestination(SlotItem outSlot, out SlotItem destSlot)
  544. {
  545. destSlot = new SlotItem(ModuleName.System, -1);
  546. var wafer = WaferManager.Instance.GetWafer(outSlot.Module, outSlot.Slot);
  547. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null || wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  548. return false;
  549. foreach (var next_module in wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules)
  550. {
  551. var validSlots = GetModuleValidSlots(next_module);
  552. foreach (var nSlot in validSlots)
  553. {
  554. if (_movingItems.Exists(mItem => mItem.DestinationModule == next_module && mItem.DestinationSlot == nSlot))
  555. continue;
  556. foreach(var inItem in _vacReadyInSlots)
  557. {
  558. if(inItem.Key.Module == next_module && inItem.Key.Slot == nSlot && _vacModules[next_module].MovingStatus != MovingStatus.Moving)
  559. {
  560. destSlot.Module = next_module;
  561. destSlot.Slot = nSlot;
  562. return true;
  563. }
  564. }
  565. }
  566. }
  567. return false;
  568. }
  569. List<int> GetModuleValidSlots(ModuleName mod)
  570. {
  571. var validSlot = new List<int>();
  572. if(ModuleHelper.IsLoadLock(mod))
  573. {
  574. if(!IsLoadLockReservedByEFEM(mod))
  575. {
  576. var inSlots = mod == ModuleName.LLA ? _LLAInSlot : _LLBInSlot;
  577. foreach (var slot in inSlots)
  578. {
  579. if (WaferManager.Instance.CheckNoWafer(mod, slot) && !_movingItems.Exists(item => item.DestinationModule == mod && item.DestinationSlot == slot))
  580. validSlot.Add(slot);
  581. if (WaferManager.Instance.CheckHasWafer(mod, slot) && _movingItems.Exists(item => item.SourceModule == mod && item.SourceSlot == slot))
  582. validSlot.Add(slot);
  583. }
  584. }
  585. }
  586. else if(ModuleHelper.IsPm(mod))
  587. {
  588. if (WaferManager.Instance.CheckNoWafer(mod, 0) && !_movingItems.Exists(item => item.DestinationModule == mod))
  589. validSlot.Add(0);
  590. if (WaferManager.Instance.CheckHasWafer(mod, 0) && _movingItems.Exists(item => item.SourceModule == mod))
  591. validSlot.Add(0);
  592. }
  593. return validSlot.Distinct().ToList();
  594. }
  595. private int GetModuleSlotCount(ModuleName mod)
  596. {
  597. int nSlotCount = 1;
  598. if(ModuleHelper.IsLoadLock(mod))
  599. {
  600. nSlotCount = mod == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber;
  601. }
  602. else if(ModuleHelper.IsLoadPort(mod))
  603. {
  604. nSlotCount = SC.GetValue<int>("EFEM.LoadPort.SlotNumber");
  605. }
  606. else if(ModuleHelper.IsTMRobot(mod) || ModuleHelper.IsEFEMRobot(mod))
  607. {
  608. nSlotCount = 2;
  609. }
  610. return nSlotCount;
  611. }
  612. List<MoveItem> SearchWaitInSlots(ModuleName inModule)
  613. {
  614. List<MoveItem> inSlots = new List<MoveItem>();
  615. var validSlots = GetModuleValidSlots(inModule);
  616. foreach(var slot in _vacReadyOutSlots)
  617. {
  618. if (slot.Key.Module == inModule || _vacModules[slot.Key.Module].MovingStatus != MovingStatus.Idle)
  619. continue;
  620. if (IsLoadLockReservedByEFEM(slot.Key.Module))
  621. continue;
  622. var wafer = WaferManager.Instance.GetWafer(slot.Key.Module, slot.Key.Slot);
  623. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null || wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  624. continue;
  625. foreach (var next_module in wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules)
  626. {
  627. if(next_module == inModule && validSlots.Count > 0 && !_movingItems.Exists(item => item.SourceModule == slot.Key.Module && item.SourceSlot == slot.Key.Slot))
  628. {
  629. if(!inSlots.Exists(item => item.SourceModule == slot.Key.Module && item.SourceSlot == slot.Key.Slot))
  630. {
  631. inSlots.Add(new MoveItem(slot.Key.Module, slot.Key.Slot, inModule, validSlots.First(), Hand.None));
  632. }
  633. validSlots.RemoveAt(0);
  634. }
  635. }
  636. }
  637. return inSlots;
  638. }
  639. private void UpdateItemMovingStatus(MoveItem moveItem, MovingStatus status = MovingStatus.Moving)
  640. {
  641. if(!ModuleHelper.IsTMRobot(moveItem.DestinationModule))
  642. {
  643. _vacModules[moveItem.DestinationModule].MovingStatus = status;
  644. }
  645. if(!ModuleHelper.IsTMRobot(moveItem.SourceModule))
  646. {
  647. _vacModules[moveItem.SourceModule].MovingStatus = status;
  648. }
  649. }
  650. private void UpdateModuleMovingStatus(ModuleName module, MovingStatus status = MovingStatus.Moving)
  651. {
  652. if (!ModuleHelper.IsTMRobot(module))
  653. {
  654. _vacModules[module].MovingStatus = status;
  655. }
  656. }
  657. private void ProcessTMRobotTask()
  658. {
  659. if(_tmRobot.IsAvailable)
  660. {
  661. if(WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 0) || WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 1))
  662. {
  663. return;
  664. }
  665. foreach(var slot in _vacReadyOutSlots)
  666. {
  667. if (_vacModules[slot.Key.Module].MovingStatus != MovingStatus.Idle)
  668. continue;
  669. if (ModuleHelper.IsLoadLock(slot.Key.Module))
  670. {
  671. if (IsLoadLockReservedByEFEM(slot.Key.Module))
  672. continue;
  673. if(SearchWaferDestination(slot.Key, out SlotItem destSlot))
  674. {
  675. _movingItems.Add(new MoveItem(slot.Key.Module, slot.Key.Slot, destSlot.Module, destSlot.Slot, Hand.None));
  676. UpdateModuleMovingStatus(slot.Key.Module);
  677. UpdateModuleMovingStatus(destSlot.Module);
  678. // check whether match double pick pattern
  679. foreach(var item in _vacReadyOutSlots)
  680. {
  681. if(item.Key.Module == slot.Key.Module && item.Key.Slot != slot.Key.Slot)
  682. {
  683. if (SearchWaferDestination(item.Key, out SlotItem destSlot_2))
  684. {
  685. _movingItems.Add(new MoveItem(item.Key.Module, item.Key.Slot, destSlot_2.Module, destSlot_2.Slot, Hand.None));
  686. UpdateModuleMovingStatus(item.Key.Module);
  687. UpdateModuleMovingStatus(destSlot_2.Module);
  688. break;
  689. }
  690. }
  691. }
  692. // check whether match swap pattern
  693. int inCount = 0;
  694. var in_slots = SearchWaitInSlots(slot.Key.Module);
  695. foreach(var in_slot in in_slots)
  696. {
  697. if(_vacModules[in_slot.DestinationModule].MovingStatus == MovingStatus.Idle && inCount < 2)
  698. {
  699. _movingItems.Add(in_slot);
  700. UpdateItemMovingStatus(in_slot);
  701. inCount++;
  702. }
  703. }
  704. return;
  705. }
  706. }
  707. else // PM
  708. {
  709. if(SearchWaferDestination(slot.Key, out SlotItem destSlot))
  710. {
  711. _movingItems.Add(new MoveItem(slot.Key.Module, slot.Key.Slot, destSlot.Module, destSlot.Slot, Hand.None));
  712. UpdateModuleMovingStatus(slot.Key.Module);
  713. UpdateModuleMovingStatus(destSlot.Module);
  714. // check whether match swap pattern
  715. int swapCount = 0;
  716. var in_slots = SearchWaitInSlots(slot.Key.Module);
  717. foreach(var swap_slot in in_slots)
  718. {
  719. if(_vacModules[swap_slot.SourceModule].MovingStatus == MovingStatus.Idle && swapCount < 1)
  720. {
  721. _movingItems.Add(swap_slot);
  722. UpdateItemMovingStatus(swap_slot);
  723. swapCount++;
  724. }
  725. }
  726. if(swapCount == 0)
  727. {
  728. // check whether match double move pattern
  729. int inCount = 0;
  730. var same_dest = SearchWaitInSlots(destSlot.Module);
  731. foreach(var in_slot in same_dest)
  732. {
  733. if(_vacModules[in_slot.SourceModule].MovingStatus == MovingStatus.Idle && inCount < 1)
  734. {
  735. _movingItems.Add(in_slot);
  736. UpdateItemMovingStatus(in_slot);
  737. inCount++;
  738. }
  739. }
  740. }
  741. return;
  742. }
  743. }
  744. }
  745. _vacReadyOutSlots.Clear();
  746. _vacReadyInSlots.Clear();
  747. }
  748. }
  749. private void RuningTMRobotTask()
  750. {
  751. if(_tmRobot.IsAvailable)
  752. {
  753. if(_tmRobot.PostMoveItems(_movingItems.ToArray()))
  754. {
  755. foreach(var item in _movingItems)
  756. {
  757. var wafer = WaferManager.Instance.GetWafer(item.SourceModule, item.SourceSlot);
  758. if (wafer.IsEmpty)
  759. {
  760. // post alarm
  761. _cycleState = RState.Failed;
  762. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Cannot run TM moving task as Get {item.SourceModule}{item.SourceModule} Wafer Info failed");
  763. return;
  764. }
  765. var slot = new SlotItem(item.DestinationModule, item.DestinationSlot);
  766. _vacWaferTargets[slot] = wafer.InnerId;
  767. }
  768. }
  769. }
  770. }
  771. private void ProcessPMTask()
  772. {
  773. foreach(var mod in _vacModules)
  774. {
  775. if(ModuleHelper.IsPm(mod.Key))
  776. {
  777. if(_vacSchedulers[mod.Key].IsAvailable)
  778. {
  779. switch (mod.Value.MovingStatus)
  780. {
  781. case MovingStatus.WaitProcess:
  782. {
  783. _vacSchedulers[mod.Key].EventWaferArrived?.Invoke(this, new WaferMoveArgs(ModuleName.TMRobot, 0, mod.Key, 0));
  784. mod.Value.MovingStatus = MovingStatus.StartProcess;
  785. }
  786. break;
  787. case MovingStatus.Processing:
  788. mod.Value.MovingStatus = MovingStatus.Idle;
  789. break;
  790. }
  791. }
  792. else
  793. {
  794. switch(mod.Value.MovingStatus)
  795. {
  796. case MovingStatus.StartProcess:
  797. mod.Value.MovingStatus = MovingStatus.Processing;
  798. break;
  799. }
  800. }
  801. }
  802. }
  803. }
  804. private Hand GetTMRobotFreeHand()
  805. {
  806. var blade1HasWafer = WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 0);
  807. var blade2HasWafer = WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 1);
  808. if (blade1HasWafer && blade2HasWafer)
  809. return Hand.None;
  810. else if (!blade1HasWafer && !blade2HasWafer)
  811. return Hand.Both;
  812. else if (blade1HasWafer)
  813. return Hand.Blade2;
  814. else
  815. return Hand.Blade1;
  816. }
  817. private bool IsVacWaferReadyOut(ModuleName mod, int slot)
  818. {
  819. var wafer = WaferManager.Instance.GetWafer(mod, slot);
  820. if (wafer.IsEmpty)
  821. return false;
  822. if (wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  823. return false;
  824. if (wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  825. {
  826. // should not go here
  827. //LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Wafer:{wafer.WaferOrigin} NextSequenceStep {wafer.NextSequenceStep} exceeds {wafer.ProcessJob.Sequence.Name} max steps number");
  828. return false;
  829. }
  830. if (wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules.Contains(mod))
  831. return false;
  832. foreach(var module in wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules)
  833. {
  834. if (ModuleHelper.IsLoadLock(module) || ModuleHelper.IsPm(module))
  835. return true;
  836. }
  837. return false;
  838. }
  839. #endregion Vacuum System
  840. #region Atm System
  841. private void driveAtmSystem()
  842. {
  843. PumpingEFEMRobotTask();
  844. RuningEFEMRobotTask();
  845. }
  846. private void PumpingEFEMRobotTask()
  847. {
  848. if (!_efemRobot.IsAvailable)
  849. return;
  850. foreach(var scheduler in _atmSchedulers)
  851. {
  852. if (!scheduler.Value.IsAvailable)
  853. continue;
  854. if(ModuleHelper.IsAligner(scheduler.Key))
  855. {
  856. if(_atmModules[scheduler.Key].MovingStatus != MovingStatus.Idle)
  857. {
  858. ProcessAlignerTask();
  859. if (_atmModules[scheduler.Key].MovingStatus != MovingStatus.Idle)
  860. return;
  861. }
  862. if (ProcessAlignerEFEMRobotTask(scheduler.Key))
  863. return;
  864. }
  865. else if (ModuleHelper.IsLoadLock(scheduler.Key))
  866. {
  867. if (ProcessLLEFEMRobotTask(scheduler.Key))
  868. return;
  869. }
  870. else if (ModuleHelper.IsCooling(scheduler.Key))
  871. {
  872. if (ProcessCoolingEFEMRobotTask(scheduler.Key))
  873. return;
  874. }
  875. else if (ModuleHelper.IsLoadPort(scheduler.Key))
  876. {
  877. if (ProcessLPEFEMRobotTask(scheduler.Key))
  878. return;
  879. }
  880. else
  881. {
  882. // should not go here
  883. LOG.Write(eEvent.ERR_ROUTER, $"Wrong Module: {scheduler.Key} in ATM System");
  884. }
  885. }
  886. }
  887. List<int> GetEfemRobotWaferReadyInHands(ModuleName mod)
  888. {
  889. var slots = new List<int>();
  890. for(int i = 0; i < 2; i++)
  891. {
  892. if (_efemRobotSingleArmOption != 0 && _efemRobotSingleArmOption != i + 1)
  893. continue;
  894. var wafer = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, i);
  895. if (wafer.IsEmpty)
  896. continue;
  897. if (wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  898. continue;
  899. if (wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  900. {
  901. // should not go here
  902. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Wafer:{wafer.WaferOrigin} NextSequenceStep {wafer.NextSequenceStep} exceeds {wafer.ProcessJob.Sequence.Name} max steps number");
  903. continue;
  904. }
  905. if (wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules.Contains(mod))
  906. {
  907. slots.Add(i);
  908. }
  909. }
  910. return slots;
  911. }
  912. bool IsAtmWaferReadyOut(ModuleName mod, int slot)
  913. {
  914. var wafer = WaferManager.Instance.GetWafer(mod, slot);
  915. if (wafer.IsEmpty)
  916. return false;
  917. if (wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  918. return false;
  919. if (wafer.NextSequenceStep == wafer.ProcessJob.Sequence.Steps.Count)
  920. {
  921. // need return
  922. return true;
  923. }
  924. else if(wafer.NextSequenceStep > wafer.ProcessJob.Sequence.Steps.Count)
  925. {
  926. // should not go here
  927. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Wafer:{wafer.WaferOrigin} NextSequenceStep {wafer.NextSequenceStep} exceeds {wafer.ProcessJob.Sequence.Name} max steps number");
  928. return false;
  929. }
  930. if (wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules.Contains(mod))
  931. return false;
  932. if(ModuleHelper.IsLoadLock(mod))
  933. {
  934. foreach (var module in wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules)
  935. {
  936. if (ModuleHelper.IsPm(module))
  937. return false;
  938. }
  939. }
  940. return true;
  941. }
  942. private bool ProcessLLEFEMRobotTask(ModuleName ll)
  943. {
  944. if (_vacModules[ll].MovingStatus != MovingStatus.Idle || IsLoadLockReservedByTM(ll))
  945. return false;
  946. var robotSlots = GetEfemRobotWaferReadyInHands(ll);
  947. var inSlots = ll == ModuleName.LLA ? _LLAInSlot : _LLBInSlot;
  948. foreach(var slot in inSlots)
  949. {
  950. if(robotSlots.Count >= 1)
  951. {
  952. if(WaferManager.Instance.CheckNoWafer(ll, slot))
  953. {
  954. _efemMovingItems.Add(new MoveItem(ModuleName.EfemRobot, robotSlots.First(), ll, slot, (Hand)robotSlots.First()));
  955. robotSlots.RemoveAt(0);
  956. }
  957. }
  958. }
  959. var slotNumber = ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber;
  960. Dictionary<int, long> slot_delays = new Dictionary<int, long>();
  961. for (int i = 0; i < slotNumber; i++)
  962. {
  963. if(IsAtmWaferReadyOut(ll, i))
  964. {
  965. slot_delays[i] = _atmSchedulers[ll].WaferArrivedTicks(i);
  966. }
  967. }
  968. slot_delays = slot_delays.OrderByDescending(item => item.Value).ToDictionary(k => k.Key, v => v.Value);
  969. if(slot_delays.Count > 0)
  970. {
  971. var robotHand = GetEFEMRobotFreeHand();
  972. if (robotHand != Hand.None)
  973. {
  974. _efemMovingItems.Add(new MoveItem(ll, slot_delays.ElementAt(0).Key, ModuleName.EfemRobot, (int)robotHand, robotHand));
  975. }
  976. if(slot_delays.Count > 1)
  977. {
  978. var secondHand = GetEFEMRobotFreeHand();
  979. if(secondHand != Hand.None)
  980. {
  981. _efemMovingItems.Add(new MoveItem(ll, slot_delays.ElementAt(1).Key, ModuleName.EfemRobot, (int)secondHand, secondHand));
  982. }
  983. }
  984. }
  985. return _efemMovingItems.Count > 0;
  986. }
  987. private Tuple<int, int> GetPMWaferExistence()
  988. {
  989. int exist = 0;
  990. int empty = 0;
  991. foreach (var module in _vacModules)
  992. {
  993. if (ModuleHelper.IsPm(module.Key) && _vacSchedulers[module.Key].IsOnline && module.Value.InUsed)
  994. {
  995. if (WaferManager.Instance.CheckHasWafer(module.Key, 0))
  996. exist++;
  997. else
  998. empty++;
  999. }
  1000. }
  1001. return new Tuple<int, int>(exist, empty);
  1002. }
  1003. private int GetTMRobotWaferCount()
  1004. {
  1005. return (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 0) ? 1 : 0) + (WaferManager.Instance.CheckHasWafer(ModuleName.TMRobot, 1) ? 1 : 0);
  1006. }
  1007. private int GetEfemRoborWaferCount()
  1008. {
  1009. return (WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 0) ? 1 : 0) + (WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 1) ? 1 : 0);
  1010. }
  1011. private bool IsLoadLockReservedByEFEM(ModuleName ll)
  1012. {
  1013. if (_efemMovingItems.Exists(item => item.DestinationModule == ll || item.SourceModule == ll))
  1014. return true;
  1015. return false;
  1016. }
  1017. private bool IsLoadLockReservedByTM(ModuleName ll)
  1018. {
  1019. List<ModuleName> unfinished = new List<ModuleName>();
  1020. foreach (var item in _vacWaferTargets)
  1021. {
  1022. var moving = _movingItems.Find(m_item => m_item.DestinationModule == item.Key.Module && m_item.DestinationSlot == item.Key.Slot);
  1023. if (!unfinished.Contains(moving.SourceModule))
  1024. unfinished.Add(moving.SourceModule);
  1025. if (!unfinished.Contains(moving.DestinationModule))
  1026. unfinished.Add(moving.DestinationModule);
  1027. }
  1028. return unfinished.Contains(ll);
  1029. }
  1030. private Tuple<int, int> GetLLProcessStatusCount(ModuleName ll)
  1031. {
  1032. int processedCount = 0;
  1033. int unprocessCount = 0;
  1034. int slotCount = ll == ModuleName.LLA ? _LLASlotNumber : _LLBSlotNumber;
  1035. if(ModuleHelper.IsLoadLock(ll))
  1036. {
  1037. for(int i = 0; i < slotCount;i++)
  1038. {
  1039. if(WaferManager.Instance.CheckHasWafer(ll, i))
  1040. {
  1041. if(CheckWaferNeedProcessAndPMAvailable(ll, i))
  1042. unprocessCount++;
  1043. else
  1044. processedCount++;
  1045. }
  1046. }
  1047. }
  1048. return new Tuple<int, int>(processedCount, unprocessCount);
  1049. }
  1050. private int GetAtmInerWaferCount()
  1051. {
  1052. int nCount = 0;
  1053. foreach(var atmModule in _atmModules)
  1054. {
  1055. if (!ModuleHelper.IsEFEMRobot(atmModule.Key) && !ModuleHelper.IsLoadPort(atmModule.Key) && WaferManager.Instance.CheckHasWafer(atmModule.Key, 0))
  1056. nCount++;
  1057. }
  1058. return nCount;
  1059. }
  1060. private int CanPushInWaferNumber()
  1061. {
  1062. if (_tmRobot.RobotStatus == RState.Running)
  1063. return 0;
  1064. var llaWaferStatus = GetLLProcessStatusCount(ModuleName.LLA);
  1065. var llbWaferStatus = GetLLProcessStatusCount(ModuleName.LLB);
  1066. var pmWaferStatus = GetPMWaferExistence();
  1067. if(_LLInOutPath == SequenceLLInOutPath.AInBOut)
  1068. {
  1069. return _LLASlotNumber + pmWaferStatus.Item2 - llaWaferStatus.Item1 - llaWaferStatus.Item2
  1070. - GetTMRobotWaferCount() - GetAtmInerWaferCount() - GetEfemRoborWaferCount();
  1071. }
  1072. else if(_LLInOutPath == SequenceLLInOutPath.BInAOut)
  1073. {
  1074. return _LLBSlotNumber + pmWaferStatus.Item2 - llbWaferStatus.Item1 - llbWaferStatus.Item2
  1075. - GetTMRobotWaferCount() - GetAtmInerWaferCount() - GetEfemRoborWaferCount();
  1076. }
  1077. else if(_LLInOutPath == SequenceLLInOutPath.AInAOut || (_LLInOutPath == SequenceLLInOutPath.DInDOut && _vacSchedulers[ModuleName.LLA].IsOnline && !_vacSchedulers[ModuleName.LLB].IsOnline))
  1078. {
  1079. return llaWaferStatus.Item1 > 0 ? 0 :
  1080. _LLASlotNumber / 2 + pmWaferStatus.Item2 - llaWaferStatus.Item2
  1081. - GetTMRobotWaferCount() - GetEfemRoborWaferCount() - GetAtmInerWaferCount();
  1082. }
  1083. else if (_LLInOutPath == SequenceLLInOutPath.BInBOut || (_LLInOutPath == SequenceLLInOutPath.DInDOut && !_vacSchedulers[ModuleName.LLA].IsOnline && _vacSchedulers[ModuleName.LLB].IsOnline))
  1084. {
  1085. return llbWaferStatus.Item1 > 0 ? 0 :
  1086. _LLBSlotNumber / 2 + pmWaferStatus.Item2 - llbWaferStatus.Item2
  1087. -GetTMRobotWaferCount() - GetEfemRoborWaferCount() - GetAtmInerWaferCount();
  1088. }
  1089. else if(_LLInOutPath == SequenceLLInOutPath.DInDOut && _vacSchedulers[ModuleName.LLA].IsOnline && _vacSchedulers[ModuleName.LLB].IsOnline)
  1090. {
  1091. return llaWaferStatus.Item1 + llbWaferStatus.Item1 > 1 ? 0 :
  1092. (_LLASlotNumber + _LLBSlotNumber) / 2 + pmWaferStatus.Item2
  1093. - llaWaferStatus.Item1 - llaWaferStatus.Item2 - llbWaferStatus.Item1 - llbWaferStatus.Item2
  1094. - GetTMRobotWaferCount() - GetEfemRoborWaferCount() - GetAtmInerWaferCount();
  1095. }
  1096. else
  1097. {
  1098. _cycleState = RState.Failed;
  1099. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, "Both LLA/LLB offline, stop runing.");
  1100. return 0;
  1101. }
  1102. }
  1103. private bool IsForwardPathBlocking(ModuleName mod, int slot)
  1104. {
  1105. var wafer = WaferManager.Instance.GetWafer(mod, slot);
  1106. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null || wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  1107. return false;
  1108. foreach (var next_module in wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules)
  1109. {
  1110. var SlotNumber = GetModuleSlotCount(next_module);
  1111. for(int i = 0; i < SlotNumber; i++)
  1112. {
  1113. if (WaferManager.Instance.CheckNoWafer(next_module, i))
  1114. return false;
  1115. }
  1116. }
  1117. return true;
  1118. }
  1119. private bool ProcessLPEFEMRobotTask(ModuleName lp)
  1120. {
  1121. // check return
  1122. for(int i = 0; i < 2; i++)
  1123. {
  1124. if (_efemRobotSingleArmOption != 0 && _efemRobotSingleArmOption != i + 1)
  1125. continue;
  1126. if(WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, i))
  1127. {
  1128. var returnWafer = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, i);
  1129. if (returnWafer.IsEmpty)
  1130. continue;
  1131. if (returnWafer.ProcessJob == null || returnWafer.ProcessJob.Sequence == null)
  1132. continue;
  1133. if (returnWafer.NextSequenceStep >= returnWafer.ProcessJob.Sequence.Steps.Count && lp == (ModuleName)returnWafer.OriginStation) // need return
  1134. {
  1135. _efemMovingItems.Add(new MoveItem(ModuleName.EfemRobot, i, lp, returnWafer.OriginSlot, (Hand)i));
  1136. }
  1137. }
  1138. }
  1139. var canPushInWafers = CanPushInWaferNumber();
  1140. if(_efemMovingItems.Count == 0 && canPushInWafers > 0)
  1141. {
  1142. var outSlots = GetNextWaferInJobQueue(lp);
  1143. var hand = GetEFEMRobotFreeHand();
  1144. if (hand != Hand.None && outSlots.Count > 0 && !IsForwardPathBlocking(lp, outSlots[0]))
  1145. {
  1146. _efemMovingItems.Add(new MoveItem(lp, outSlots[0], ModuleName.EfemRobot, (int)hand, hand));
  1147. }
  1148. if(canPushInWafers > 1)
  1149. {
  1150. hand = GetEFEMRobotFreeHand();
  1151. if (hand != Hand.None && outSlots.Count > 1 && !IsForwardPathBlocking(lp, outSlots[1]))
  1152. {
  1153. _efemMovingItems.Add(new MoveItem(lp, outSlots[1], ModuleName.EfemRobot, (int)hand, hand));
  1154. }
  1155. }
  1156. }
  1157. return _efemMovingItems.Count > 0;
  1158. }
  1159. private List<int> GetNextWaferInJobQueue(ModuleName lp)
  1160. {
  1161. var inSlots = new List<int>();
  1162. foreach (var cj in _lstControlJobs)
  1163. {
  1164. if (lp != ModuleName.System && (cj.Module != lp.ToString()))
  1165. continue;
  1166. if (cj.State == EnumControlJobState.Executing)
  1167. {
  1168. foreach (var pj in _lstProcessJobs)
  1169. {
  1170. if (pj.ControlJobName == cj.Name && pj.State == EnumProcessJobState.Processing)
  1171. {
  1172. foreach (var pjSlotWafer in pj.SlotWafers)
  1173. {
  1174. if (CheckWaferNeedProcessAndPMAvailable(pjSlotWafer.Item1, pjSlotWafer.Item2))
  1175. {
  1176. var wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1177. if(!wafer.IsEmpty && wafer.NextSequenceStep == 0)
  1178. {
  1179. inSlots.Add(pjSlotWafer.Item2);
  1180. if (inSlots.Count >= 2)
  1181. return inSlots;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. return inSlots;
  1190. }
  1191. private bool ProcessAlignerEFEMRobotTask(ModuleName aligner)
  1192. {
  1193. if(WaferManager.Instance.CheckHasWafer(aligner, 0) && IsAtmWaferReadyOut(aligner, 0) && _atmModules[aligner].MovingStatus == MovingStatus.Idle)
  1194. {
  1195. var hand = GetEFEMRobotFreeHand();
  1196. if(hand != Hand.None)
  1197. {
  1198. _efemMovingItems.Add(new MoveItem(aligner, 0, ModuleName.EfemRobot, (int)hand, hand));
  1199. }
  1200. //// check with whether another robot arm wafer wait go to aligner
  1201. //var robotSlots = GetEfemRobotWaferReadyInHands(aligner);
  1202. //if (robotSlots.Count > 0)
  1203. //{
  1204. // _efemMovingItems.Add(new MoveItem(ModuleName.EfemRobot, robotSlots.First(), aligner, 0, (Hand)robotSlots.First()));
  1205. //}
  1206. }
  1207. else if(WaferManager.Instance.CheckNoWafer(aligner, 0))
  1208. {
  1209. var robotSlots = GetEfemRobotWaferReadyInHands(aligner);
  1210. if(robotSlots.Count > 0)
  1211. {
  1212. _efemMovingItems.Add(new MoveItem(ModuleName.EfemRobot, robotSlots.First(), aligner, 0, (Hand)robotSlots.First()));
  1213. }
  1214. }
  1215. return _efemMovingItems.Count > 0;
  1216. }
  1217. private bool ProcessCoolingEFEMRobotTask(ModuleName cooling)
  1218. {
  1219. if (WaferManager.Instance.CheckHasWafer(cooling, 0) && IsAtmWaferReadyOut(cooling, 0))
  1220. {
  1221. var hand = GetEFEMRobotFreeHand();
  1222. if (hand != Hand.None)
  1223. {
  1224. _efemMovingItems.Add(new MoveItem(cooling, 0, ModuleName.EfemRobot, (int)hand, hand));
  1225. }
  1226. }
  1227. else
  1228. {
  1229. var robotSlots = GetEfemRobotWaferReadyInHands(cooling);
  1230. if (robotSlots.Count > 0)
  1231. {
  1232. _efemMovingItems.Add(new MoveItem(cooling, 0, ModuleName.EfemRobot, robotSlots.First(), (Hand)robotSlots.First()));
  1233. }
  1234. }
  1235. return _efemMovingItems.Count > 0;
  1236. }
  1237. private void RuningEFEMRobotTask()
  1238. {
  1239. if (_efemRobot.IsAvailable && _efemMovingItems.Count > 0)
  1240. {
  1241. if (_efemRobot.PostMoveItems(_efemMovingItems.ToArray()))
  1242. {
  1243. foreach (var item in _efemMovingItems)
  1244. {
  1245. var wafer = WaferManager.Instance.GetWafer(item.SourceModule, item.SourceSlot);
  1246. if (wafer.IsEmpty)
  1247. {
  1248. // post alarm
  1249. _cycleState = RState.Failed;
  1250. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Cannot run EFEM moving task as Get {item.SourceModule}{item.SourceModule} Wafer Info failed");
  1251. return;
  1252. }
  1253. var slot = new SlotItem(item.DestinationModule, item.DestinationSlot);
  1254. _atmWaferTargets[slot] = wafer.InnerId;
  1255. if(ModuleHelper.IsLoadLock(item.Module))
  1256. {
  1257. _vacModules[item.Module].MovingStatus = MovingStatus.Moving;
  1258. }
  1259. }
  1260. }
  1261. }
  1262. }
  1263. private Hand GetEFEMRobotFreeHand()
  1264. {
  1265. for(int i = 0; i < 2; i++)
  1266. {
  1267. if (_efemRobotSingleArmOption != 0 && _efemRobotSingleArmOption != i + 1)
  1268. continue;
  1269. if ((WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, i) &&
  1270. !_efemMovingItems.Exists(item => item.DestinationModule == ModuleName.EfemRobot && item.DestinationSlot == i)) ||
  1271. (WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, i) &&
  1272. _efemMovingItems.Exists(item => item.SourceModule == ModuleName.EfemRobot && item.SourceSlot == i) &&
  1273. !_efemMovingItems.Exists(item => item.DestinationModule == ModuleName.EfemRobot && item.DestinationSlot == i)))
  1274. return (Hand)i;
  1275. }
  1276. return Hand.None;
  1277. }
  1278. private void ProcessAlignerTask()
  1279. {
  1280. foreach(var align in _atmModules)
  1281. {
  1282. if(ModuleHelper.IsAligner(align.Key))
  1283. {
  1284. switch(align.Value.MovingStatus)
  1285. {
  1286. case MovingStatus.WaitAlign:
  1287. if(_efemRobot.IsAvailable)
  1288. {
  1289. WaferInfo wafer = WaferManager.Instance.GetWafer(align.Key, 0);
  1290. if(!wafer.IsEmpty)
  1291. {
  1292. string attr = "AlignAngle";
  1293. string angle = string.Empty;
  1294. int step = wafer.NextSequenceStep;
  1295. if (!wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules.Contains(align.Key))
  1296. {
  1297. step = wafer.NextSequenceStep - 1;
  1298. }
  1299. if (wafer.ProcessJob.Sequence.Steps[step].StepParameter.ContainsKey(attr))
  1300. {
  1301. angle = (string)wafer.ProcessJob.Sequence.Steps[step].StepParameter[attr];
  1302. if (_efemRobot.Align(float.Parse(angle)))
  1303. {
  1304. align.Value.MovingStatus = MovingStatus.StartAlign;
  1305. return;
  1306. }
  1307. }
  1308. }
  1309. align.Value.MovingStatus = MovingStatus.Idle;
  1310. }
  1311. break;
  1312. case MovingStatus.StartAlign:
  1313. if(!_efemRobot.IsAvailable)
  1314. {
  1315. align.Value.MovingStatus = MovingStatus.Aligning;
  1316. }
  1317. break;
  1318. case MovingStatus.Aligning:
  1319. {
  1320. if(_efemRobot.IsAvailable)
  1321. {
  1322. align.Value.MovingStatus = MovingStatus.Idle;
  1323. }
  1324. }
  1325. break;
  1326. }
  1327. }
  1328. }
  1329. }
  1330. #endregion Atm System
  1331. #region Sequence validation
  1332. private bool CheckSequencePmReady(SequenceInfo seq, List<ModuleName> pmOccupied, out List<ModuleName> pmUsed, out string reason)
  1333. {
  1334. pmUsed = new List<ModuleName>();
  1335. reason = "";
  1336. bool pmIsReady = true;
  1337. for (int i = 0; i < seq.Steps.Count; i++)
  1338. {
  1339. SequenceStepInfo stepInfo = seq.Steps[i];
  1340. bool hasPm = false;
  1341. foreach (var module in stepInfo.StepModules)
  1342. {
  1343. if (!ModuleHelper.IsPm(module))
  1344. {
  1345. hasPm = true;
  1346. break;
  1347. }
  1348. if (ModuleHelper.IsInstalled(module) && (pmOccupied == null || !pmOccupied.Contains(module)))
  1349. {
  1350. hasPm = true;
  1351. }
  1352. if (!pmUsed.Contains(module))
  1353. pmUsed.Add(module);
  1354. }
  1355. if (pmIsReady && !hasPm)
  1356. {
  1357. reason = $"Step {i + 1} no valid PM, " + string.Join("|", stepInfo.StepModules);
  1358. pmIsReady = false;
  1359. }
  1360. }
  1361. return pmIsReady;
  1362. }
  1363. private bool CheckSequenceRecipeFileValid(SequenceInfo seq, out string reason)
  1364. {
  1365. for (int i = 0; i < seq.Steps.Count; i++)
  1366. {
  1367. SequenceStepInfo stepInfo = seq.Steps[i];
  1368. foreach (var module in stepInfo.StepModules)
  1369. {
  1370. if (ModuleHelper.IsPm(module))
  1371. {
  1372. string attr = $"{module}Recipe";
  1373. if (stepInfo.StepParameter.ContainsKey(attr)
  1374. && !string.IsNullOrEmpty((string)stepInfo.StepParameter[attr]))
  1375. {
  1376. var recipeName = (string)stepInfo.StepParameter[attr];
  1377. if (!string.IsNullOrEmpty(recipeName))
  1378. {
  1379. var recipeContent =
  1380. RecipeFileManager.Instance.LoadRecipe($"{module}", recipeName, false);
  1381. if (string.IsNullOrEmpty(recipeContent))
  1382. {
  1383. reason = $"Can not find recipe file{recipeName}";
  1384. return false;
  1385. }
  1386. }
  1387. }
  1388. }
  1389. }
  1390. }
  1391. reason = "";
  1392. return true;
  1393. }
  1394. private bool CheckWaferNeedProcessAndPMAvailable(ModuleName waferModule, int waferSlot, ModuleName processIn = ModuleName.System)
  1395. {
  1396. WaferInfo wafer = WaferManager.Instance.GetWafer(waferModule, waferSlot);
  1397. if (wafer.IsEmpty)
  1398. return false;
  1399. if (wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  1400. return false;
  1401. if (wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  1402. return false;
  1403. for (int i = wafer.NextSequenceStep; i < wafer.ProcessJob.Sequence.Steps.Count; i++)
  1404. {
  1405. var hasPmAvailable = false;
  1406. foreach (var stepModule in wafer.ProcessJob.Sequence.Steps[i].StepModules)
  1407. {
  1408. if (!ModuleHelper.IsPm(stepModule))
  1409. break;
  1410. if (!_vacSchedulers.Keys.Contains(stepModule))
  1411. continue;
  1412. var pm = _vacSchedulers[stepModule] as SchedulerPM;
  1413. if (pm != null)
  1414. {
  1415. if (_vacModules[stepModule].InUsed)
  1416. {
  1417. hasPmAvailable = true;
  1418. break;
  1419. }
  1420. }
  1421. }
  1422. if (!hasPmAvailable)
  1423. continue;
  1424. if (processIn == ModuleName.System)
  1425. return true;
  1426. if (wafer.ProcessJob.Sequence.Steps[i].StepModules
  1427. .Contains(processIn))
  1428. return true;
  1429. }
  1430. return false;
  1431. }
  1432. private List<ModuleName> GetPmUsedInRunningPj()
  1433. {
  1434. var pmUsed = new List<ModuleName>();
  1435. foreach (var cj in _lstControlJobs)
  1436. {
  1437. if (cj.State != EnumControlJobState.Executing && cj.State != EnumControlJobState.Paused)
  1438. continue;
  1439. foreach (var pj in _lstProcessJobs)
  1440. {
  1441. if (pj.ControlJobName == cj.Name && (pj.State == EnumProcessJobState.Processing || pj.State == EnumProcessJobState.Paused))
  1442. {
  1443. for (int i = 0; i < pj.Sequence.Steps.Count; i++)
  1444. {
  1445. SequenceStepInfo stepInfo = pj.Sequence.Steps[i];
  1446. foreach (var module in stepInfo.StepModules)
  1447. {
  1448. if (ModuleHelper.IsPm(module) && !pmUsed.Contains(module))
  1449. pmUsed.Add(module);
  1450. }
  1451. }
  1452. }
  1453. }
  1454. }
  1455. return pmUsed;
  1456. }
  1457. private List<SchedulerPM> GetPmNeeded(ControlJobInfo cj)
  1458. {
  1459. List<SchedulerPM> result = new List<SchedulerPM>();
  1460. foreach (var pj in _lstProcessJobs)
  1461. {
  1462. if (pj.ControlJobName != cj.Name)
  1463. continue;
  1464. var seq = pj.Sequence;
  1465. for (int i = 0; i < seq.Steps.Count; i++)
  1466. {
  1467. SequenceStepInfo stepInfo = seq.Steps[i];
  1468. foreach (var module in stepInfo.StepModules)
  1469. {
  1470. if (ModuleHelper.IsPm(module) && _vacSchedulers.ContainsKey(module))
  1471. {
  1472. var pm = _vacSchedulers[module] as SchedulerPM;
  1473. if (pm != null && !result.Contains(pm))
  1474. {
  1475. result.Add(pm);
  1476. }
  1477. }
  1478. }
  1479. }
  1480. }
  1481. return result;
  1482. }
  1483. private void UpateSequenceStep(List<MoveItem> items)
  1484. {
  1485. foreach(var item in items)
  1486. {
  1487. if(!ModuleHelper.IsTMRobot(item.DestinationModule) && !ModuleHelper.IsEFEMRobot(item.DestinationModule))
  1488. {
  1489. var wafer = WaferManager.Instance.GetWafer(item.DestinationModule, item.DestinationSlot);
  1490. if(!wafer.IsEmpty && wafer.ProcessJob != null)
  1491. {
  1492. if (wafer.NextSequenceStep < wafer.ProcessJob.Sequence.Steps.Count && wafer.ProcessJob.Sequence.Steps[wafer.NextSequenceStep].StepModules.Contains(item.DestinationModule))
  1493. wafer.NextSequenceStep++;
  1494. }
  1495. else
  1496. {
  1497. // should not go here
  1498. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"UpateSequenceStep() failed, location: {item.DestinationModule}:{item.DestinationSlot + 1}, NextSequenceStep: {wafer.NextSequenceStep}");
  1499. }
  1500. }
  1501. }
  1502. }
  1503. private void CheckWaferArrived()
  1504. {
  1505. if (_cycleState != RState.Running)
  1506. return;
  1507. foreach (var mod in _vacSchedulers.Append(new KeyValuePair<ModuleName, SchedulerModule>(ModuleName.TMRobot, _tmRobot)))
  1508. {
  1509. var tars = _vacWaferTargets.Where(item => item.Key.Module == mod.Key).ToArray();
  1510. foreach (var tar in tars)
  1511. {
  1512. var wafer = WaferManager.Instance.GetWafer(tar.Key.Module, tar.Key.Slot);
  1513. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  1514. continue;
  1515. if (wafer.InnerId == tar.Value)
  1516. {
  1517. // wafer arrive
  1518. _vacWaferTargets.Remove(tar.Key);
  1519. if (!ModuleHelper.IsTMRobot(tar.Key.Module))
  1520. _vacSchedulers[tar.Key.Module].WaferArrived(tar.Key.Slot);
  1521. //LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"wafer {wafer.WaferOrigin}: {wafer.InnerId} arrived {tar.Key.Module}{tar.Key.Slot + 1}");
  1522. if (ModuleHelper.IsPm(tar.Key.Module))
  1523. {
  1524. _vacModules[tar.Key.Module].MovingStatus = MovingStatus.WaitProcess;
  1525. }
  1526. }
  1527. }
  1528. if(ModuleHelper.IsLoadLock(mod.Key))
  1529. {
  1530. List<ModuleName> unfinished = new List<ModuleName>();
  1531. foreach(var item in _vacWaferTargets)
  1532. {
  1533. var moving = _movingItems.Find(m_item => m_item.DestinationModule == item.Key.Module && m_item.DestinationSlot == item.Key.Slot);
  1534. if (!unfinished.Contains(moving.SourceModule))
  1535. unfinished.Add(moving.SourceModule);
  1536. if (!unfinished.Contains(moving.DestinationModule))
  1537. unfinished.Add(moving.DestinationModule);
  1538. }
  1539. if(!unfinished.Contains(mod.Key) && mod.Value.IsAvailable)
  1540. {
  1541. _vacModules[mod.Key].MovingStatus = MovingStatus.Idle;
  1542. }
  1543. }
  1544. }
  1545. _vacMoveFinishTrig.CLK = _vacWaferTargets.Count == 0 && _tmRobot.RobotStatus != RState.Running;
  1546. if (_vacMoveFinishTrig.Q)
  1547. {
  1548. foreach (var item in _movingItems)
  1549. {
  1550. if (_vacModules[item.DestinationModule].MovingStatus == MovingStatus.Moving)
  1551. {
  1552. _vacModules[item.DestinationModule].MovingStatus = MovingStatus.Idle;
  1553. }
  1554. if (_vacModules[item.SourceModule].MovingStatus == MovingStatus.Moving)
  1555. {
  1556. _vacModules[item.SourceModule].MovingStatus = MovingStatus.Idle;
  1557. }
  1558. }
  1559. UpateSequenceStep(_movingItems);
  1560. _movingItems.Clear();
  1561. }
  1562. foreach (var mod in _atmSchedulers.Append(new KeyValuePair<ModuleName, SchedulerModule>(ModuleName.EfemRobot, _efemRobot)))
  1563. {
  1564. var tars = _atmWaferTargets.Where(item => item.Key.Module == mod.Key).ToArray();
  1565. foreach (var tar in tars)
  1566. {
  1567. var wafer = WaferManager.Instance.GetWafer(tar.Key.Module, tar.Key.Slot);
  1568. if (wafer.IsEmpty || wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  1569. continue;
  1570. if (wafer.InnerId == tar.Value)
  1571. {
  1572. //LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"wafer {wafer.WaferOrigin}: {wafer.InnerId} arrived {tar.Key.Module}{tar.Key.Slot + 1}");
  1573. // wafer arrive
  1574. if (!ModuleHelper.IsLoadPort(tar.Key.Module) && !ModuleHelper.IsEFEMRobot(tar.Key.Module))
  1575. _atmSchedulers[tar.Key.Module].WaferArrived(tar.Key.Slot);
  1576. _atmWaferTargets.Remove(tar.Key);
  1577. if (ModuleHelper.IsAligner(tar.Key.Module))
  1578. {
  1579. _atmModules[tar.Key.Module].MovingStatus = MovingStatus.WaitAlign;
  1580. }
  1581. }
  1582. }
  1583. }
  1584. _atmMoveFinishTrig.CLK = _atmWaferTargets.Count == 0 && _efemRobot.RobotStatus != RState.Running;
  1585. if (_atmMoveFinishTrig.Q)
  1586. {
  1587. foreach (var item in _efemMovingItems)
  1588. {
  1589. if (ModuleHelper.IsLoadLock(item.Module))
  1590. {
  1591. _vacModules[item.Module].MovingStatus = MovingStatus.Idle;
  1592. }
  1593. }
  1594. UpateSequenceStep(_efemMovingItems);
  1595. _efemMovingItems.Clear();
  1596. }
  1597. }
  1598. private void PreJobClean(ControlJobInfo cj)
  1599. {
  1600. if (cj.IsPreJobCleanDone)
  1601. return;
  1602. cj.IsPreJobCleanDone = true;
  1603. List<SchedulerPM> pms = GetPmNeeded(cj);
  1604. foreach (var pm in pms)
  1605. {
  1606. pm.PreJobClean();
  1607. }
  1608. }
  1609. private bool ActiveProcessJob(ProcessJobInfo pj)
  1610. {
  1611. foreach (var pjSlotWafer in pj.SlotWafers)
  1612. {
  1613. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1614. wafer.ProcessJob = pj;
  1615. wafer.NextSequenceStep = 0;
  1616. WaferDataRecorder.SetPjInfo(wafer.InnerId.ToString(), pj.InnerId.ToString());
  1617. }
  1618. ControlJobInfo cj = _lstControlJobs.Find(x => x.Name == pj.ControlJobName);
  1619. CarrierInfo carrier = CarrierManager.Instance.GetCarrier(cj.Module);
  1620. JobDataRecorder.StartPJ(pj.InnerId.ToString(), carrier.InnerId.ToString(), cj.InnerId.ToString(), pj.Name, cj.Module, cj.Module, pj.SlotWafers.Count);
  1621. pj.SetState(EnumProcessJobState.Processing);
  1622. PreJobClean(cj);
  1623. return true;
  1624. }
  1625. private void StartNewJob()
  1626. {
  1627. ControlJobInfo cjActived = null;
  1628. List<ModuleName> pmOccupied = GetPmUsedInRunningPj();
  1629. foreach (var cj in _lstControlJobs)
  1630. {
  1631. if (cj.State != EnumControlJobState.Executing)
  1632. continue;
  1633. cjActived = cj;
  1634. foreach (var pjName in cjActived.ProcessJobNameList)
  1635. {
  1636. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1637. if (pj == null)
  1638. {
  1639. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"Not find pj named {pjName} in {cjActived.Name}");
  1640. continue;
  1641. }
  1642. if (pj.State == EnumProcessJobState.Queued)
  1643. {
  1644. if (CheckSequencePmReady(pj.Sequence, pmOccupied, out var pmUsed, out string reason))
  1645. {
  1646. ActiveProcessJob(pj);
  1647. foreach (var moduleName in pmUsed)
  1648. {
  1649. if (!pmOccupied.Contains(moduleName))
  1650. pmOccupied.Add(moduleName);
  1651. }
  1652. }
  1653. break;
  1654. }
  1655. }
  1656. }
  1657. }
  1658. private static bool IsWaferNeedGotoModule(WaferInfo wafer, ModuleName module)
  1659. {
  1660. if (wafer.IsEmpty)
  1661. return false;
  1662. if (wafer.ProcessJob == null || wafer.ProcessJob.Sequence == null)
  1663. return false;
  1664. if (wafer.NextSequenceStep >= wafer.ProcessJob.Sequence.Steps.Count)
  1665. return false;
  1666. for (int i = wafer.NextSequenceStep; i < wafer.ProcessJob.Sequence.Steps.Count; i++)
  1667. {
  1668. if (wafer.ProcessJob.Sequence.Steps[i].StepModules
  1669. .Contains(module))
  1670. return true;
  1671. }
  1672. return false;
  1673. }
  1674. private bool CheckAllWaferReturned(ProcessJobInfo pj, bool checkAllProcessed)
  1675. {
  1676. bool allWaferReturn = true;
  1677. for (int i = 0; i < pj.SlotWafers.Count; ++i)
  1678. {
  1679. WaferInfo wafer = WaferManager.Instance.GetWafer(pj.SlotWafers[i].Item1, pj.SlotWafers[i].Item2);
  1680. if (wafer.IsEmpty)
  1681. continue;
  1682. foreach(var mod in _vacModules)
  1683. {
  1684. if(ModuleHelper.IsPm(mod.Key))
  1685. {
  1686. if (checkAllProcessed && IsWaferNeedGotoModule(wafer, mod.Key))
  1687. {
  1688. mod.Value.InUsed = true;
  1689. allWaferReturn = false;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. return allWaferReturn;
  1695. }
  1696. private void UpdateProcessJobStatus()
  1697. {
  1698. if (_efemRobot.RobotStatus == RState.Running)
  1699. return;
  1700. foreach(var mod in _vacModules)
  1701. {
  1702. if (ModuleHelper.IsPm(mod.Key))
  1703. {
  1704. mod.Value.InUsed = false;
  1705. }
  1706. }
  1707. foreach (var pj in _lstProcessJobs)
  1708. {
  1709. if (pj.State == EnumProcessJobState.Processing)
  1710. {
  1711. if (CheckAllWaferReturned(pj, true))
  1712. {
  1713. pj.SetState(EnumProcessJobState.ProcessingComplete);
  1714. JobDataRecorder.EndPJ(pj.InnerId.ToString(), 0, 0);
  1715. }
  1716. }
  1717. else if (pj.State == EnumProcessJobState.Stopping)
  1718. {
  1719. if (CheckAllWaferReturned(pj, false))
  1720. {
  1721. pj.SetState(EnumProcessJobState.ProcessingComplete);
  1722. JobDataRecorder.EndPJ(pj.InnerId.ToString(), 0, 0);
  1723. }
  1724. }
  1725. }
  1726. }
  1727. private void CompleteJobClean(ControlJobInfo cj)
  1728. {
  1729. List<SchedulerPM> pms = GetPmNeeded(cj);
  1730. foreach (var pm in pms)
  1731. {
  1732. pm.CompleteJobClean();
  1733. }
  1734. }
  1735. private void UpdateControlJobStatus()
  1736. {
  1737. if (_lstControlJobs.Count == 0)
  1738. return;
  1739. if (_efemRobot.RobotStatus == RState.Running)
  1740. return;
  1741. bool allControlJobComplete = true;
  1742. List<ControlJobInfo> cjRemoveList = new List<ControlJobInfo>();
  1743. foreach (var cj in _lstControlJobs)
  1744. {
  1745. if (!Singleton<TransferModule>.Instance.GetScheduler(ModuleHelper.Converter(cj.Module)).IsAvailable)
  1746. {
  1747. allControlJobComplete = false;
  1748. continue;
  1749. }
  1750. if (cj.State == EnumControlJobState.Executing)
  1751. {
  1752. bool allPjCompleted = true;
  1753. foreach (var pjName in cj.ProcessJobNameList)
  1754. {
  1755. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1756. if (pj == null)
  1757. {
  1758. LOG.Write( eEvent.ERR_ROUTER, ModuleName.System, $"Not find pj named {pjName} in {cj.Name}");
  1759. continue;
  1760. }
  1761. if (pj.State != EnumProcessJobState.Complete && pj.State != EnumProcessJobState.ProcessingComplete)
  1762. {
  1763. allPjCompleted = false;
  1764. break;
  1765. }
  1766. if (allPjCompleted)
  1767. {
  1768. pj.SlotWafers.ForEach(p => {
  1769. if (ModuleHelper.IsLoadPort(p.Item1))
  1770. {
  1771. allPjCompleted = allPjCompleted && WaferManager.Instance.CheckHasWafer(ModuleHelper.Converter(p.Item1.ToString()), p.Item2);
  1772. }
  1773. });
  1774. }
  1775. }
  1776. if (allPjCompleted)
  1777. {
  1778. cj.SetState(EnumControlJobState.Completed);
  1779. cj.EndTime = DateTime.Now;
  1780. if (!(_isCycleMode && _cycledCount + 1 < _cycleSetPoint))
  1781. {
  1782. CompleteJobClean(cj);
  1783. }
  1784. _faCallback.JobFinished(cj, GetFirstProcessJob(cj));
  1785. _dbCallback.LotFinished(cj);
  1786. if (!(_isCycleMode && _cycledCount < _cycleSetPoint))
  1787. (Singleton<TransferModule>.Instance.GetScheduler(ModuleHelper.Converter(cj.Module)) as SchedulerLoadPort).NoteJobComplete();
  1788. }
  1789. }
  1790. LoadportCassetteState state = (LoadportCassetteState)DATA.Poll($"{cj.Module}.CassetteState");
  1791. if (cj.State == EnumControlJobState.Completed && state != LoadportCassetteState.Normal)
  1792. {
  1793. cjRemoveList.Add(cj);
  1794. }
  1795. allControlJobComplete = allControlJobComplete && cj.State == EnumControlJobState.Completed;
  1796. }
  1797. if (_isCycleMode && _cycledCount < (_isCycleMode ? _cycleSetPoint : 0))
  1798. {
  1799. int countPerCycle = 0;
  1800. int countProcessed = 0;
  1801. foreach (var pj in _lstProcessJobs)
  1802. {
  1803. foreach (var pjSlotWafer in pj.SlotWafers)
  1804. {
  1805. countPerCycle++;
  1806. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1807. if (!wafer.IsEmpty && !IsWaferNeedGotoModule(wafer, ModuleName.PMA) && !IsWaferNeedGotoModule(wafer, ModuleName.PMB) && !IsWaferNeedGotoModule(wafer, ModuleName.PMC) && !IsWaferNeedGotoModule(wafer, ModuleName.PMD))
  1808. countProcessed++;
  1809. }
  1810. }
  1811. int totalCycleWafer = _cycledCount * countPerCycle + countProcessed;
  1812. if(_cycledWafer != totalCycleWafer)
  1813. {
  1814. _cycledWafer = totalCycleWafer;
  1815. if(_cycledWafer >= 25)
  1816. {
  1817. _throughput = (float)(_cycledWafer / _cycleWatch.Elapsed.TotalHours);
  1818. }
  1819. else
  1820. {
  1821. _throughput = 0;
  1822. }
  1823. }
  1824. if (allControlJobComplete)
  1825. {
  1826. _cycledCount++;
  1827. if (_cycledCount < _cycleSetPoint)
  1828. {
  1829. foreach (var cj in _lstControlJobs)
  1830. {
  1831. cj.SetState(EnumControlJobState.Executing);
  1832. cj.LotInnerId = Guid.NewGuid();
  1833. _dbCallback.LotCreated(cj);
  1834. }
  1835. foreach (var pj in _lstProcessJobs)
  1836. {
  1837. pj.SetState(EnumProcessJobState.Queued);
  1838. pj.InnerId = Guid.NewGuid();
  1839. foreach (var pjSlotWafer in pj.SlotWafers)
  1840. {
  1841. WaferInfo wafer = WaferManager.Instance.GetWafer(pjSlotWafer.Item1, pjSlotWafer.Item2);
  1842. wafer.ProcessJob = null;
  1843. wafer.NextSequenceStep = 0;
  1844. wafer.ProcessState = EnumWaferProcessStatus.Idle;
  1845. }
  1846. }
  1847. }
  1848. else
  1849. _cycleState = RState.End;
  1850. }
  1851. }
  1852. foreach (var cj in cjRemoveList)
  1853. {
  1854. List<ProcessJobInfo> pjRemoveList = new List<ProcessJobInfo>();
  1855. foreach (var pj in _lstProcessJobs)
  1856. {
  1857. if (pj.ControlJobName == cj.Name)
  1858. pjRemoveList.Add(pj);
  1859. }
  1860. foreach (var pj in pjRemoveList)
  1861. {
  1862. _lstProcessJobs.Remove(pj);
  1863. }
  1864. _lstControlJobs.Remove(cj);
  1865. }
  1866. }
  1867. private void UpdateLLInOutPathProperty()
  1868. {
  1869. if (_lstControlJobs.Count == 0)
  1870. return;
  1871. List<SequenceLLInOutPath> inOutPaths = new List<SequenceLLInOutPath>();
  1872. foreach (var cj in _lstControlJobs)
  1873. {
  1874. if (cj.State == EnumControlJobState.Executing)
  1875. {
  1876. bool allPjCompleted = true;
  1877. foreach (var pjName in cj.ProcessJobNameList)
  1878. {
  1879. var pj = _lstProcessJobs.Find(x => x.Name == pjName);
  1880. if (pj == null || pj.State != EnumProcessJobState.Processing)
  1881. continue;
  1882. if (!inOutPaths.Exists(item => item == pj.Sequence.LLInOutPath))
  1883. inOutPaths.Add(pj.Sequence.LLInOutPath);
  1884. }
  1885. }
  1886. }
  1887. if (inOutPaths.Count == 1)
  1888. {
  1889. _LLInOutPath = inOutPaths[0];
  1890. }
  1891. }
  1892. private static WaferInfo GetCloneWafer(ModuleName mod, int slot)
  1893. {
  1894. var Wafer = WaferManager.Instance.GetWafer(mod, slot);
  1895. if (Wafer.IsEmpty || Wafer.ProcessJob == null || Wafer.ProcessJob.Sequence == null)
  1896. return Wafer;
  1897. var cloneWafer = SerializeHelper.DeepCopyJson(Wafer);
  1898. if (Wafer.IsEmpty || Wafer.ProcessJob == null || Wafer.ProcessJob.Sequence == null)
  1899. return Wafer;
  1900. return cloneWafer;
  1901. }
  1902. #endregion
  1903. }
  1904. }