AutoCycle.cs 82 KB

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