AutoCycle.cs 83 KB

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