WaferTask.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.IOCore;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Equipment;
  6. using MECF.Framework.Common.Jobs;
  7. using MECF.Framework.Common.SubstrateTrackings;
  8. using MECF.Framework.Common.WaferHolder;
  9. using CyberX8_Core;
  10. using CyberX8_RT.Modules;
  11. using CyberX8_RT.Modules.Loader;
  12. using CyberX8_RT.Modules.PUF;
  13. using CyberX8_RT.Schedulers;
  14. using CyberX8_RT.Schedulers.EfemRobot;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using MECF.Framework.Common.RecipeCenter;
  21. using MECF.Framework.Common.Schedulers;
  22. using System.Windows;
  23. using MECF.Framework.RT.Core.Equipments;
  24. using CyberX8_RT.Modules.SRD;
  25. using CyberX8_RT.Schedulers.Puf;
  26. namespace CyberX8_RT.Dispatch
  27. {
  28. public enum WaferTaskState
  29. {
  30. Created,
  31. Processing,
  32. End,
  33. Paused,
  34. Error
  35. }
  36. /// <summary>
  37. /// WaferTask完成
  38. /// </summary>
  39. /// <param name="id"></param>
  40. public delegate void WaferTaskComplete(string id);
  41. /// <summary>
  42. /// WaferTask开始
  43. /// </summary>
  44. /// <param name="id"></param>
  45. public delegate void WaferTaskStart(string id);
  46. public class WaferTask
  47. {
  48. #region 内部变量
  49. /// <summary>
  50. /// 调度步骤
  51. /// </summary>
  52. private List<SchedulerSequence> _schedulerSequences;
  53. /// <summary>
  54. /// 当前步骤索引
  55. /// </summary>
  56. private int _currentSequenceIndex = 0;
  57. /// <summary>
  58. /// PUF实例
  59. /// </summary>
  60. private PUFEntity _pufEntity;
  61. /// <summary>
  62. /// 是否开始执行
  63. /// </summary>
  64. private bool _isStart = false;
  65. /// <summary>
  66. /// 暂停时的索引
  67. /// </summary>
  68. private int _pausedIndex = -1;
  69. /// <summary>
  70. /// 日志trigger
  71. /// </summary>
  72. private R_TRIG _sequenceConditionTrigger = new R_TRIG();
  73. /// <summary>
  74. /// 错误上升触发
  75. /// </summary>
  76. private R_TRIG _sequenceErrorTigger = new R_TRIG();
  77. #endregion
  78. #region 属性
  79. /// <summary>
  80. /// Wafer信息
  81. /// </summary>
  82. public string WaferId { get; set; }
  83. /// <summary>
  84. /// 状态
  85. /// </summary>
  86. public WaferTaskState State { get; private set; }
  87. /// <summary>
  88. /// 匹配WaferTask
  89. /// </summary>
  90. public string MateWaferTask { get; set; }
  91. /// <summary>
  92. /// 调度集合
  93. /// </summary>
  94. public List<SchedulerSequence> SchedulerSequences { get { return _schedulerSequences; } }
  95. #endregion
  96. #region 事件
  97. /// <summary>
  98. /// 任务完成事件
  99. /// </summary>
  100. public event WaferTaskComplete OnTaskComplete;
  101. /// <summary>
  102. /// 任务启动
  103. /// </summary>
  104. public event WaferTaskStart OnTaskStart;
  105. #endregion
  106. /// <summary>
  107. /// 构造函数
  108. /// </summary>
  109. /// <param name="waferInfo"></param>
  110. /// <param name="schedulerSequences"></param>
  111. public WaferTask(WaferInfo waferInfo,PUFEntity pufEntity, List<SchedulerSequence> schedulerSequences)
  112. {
  113. this.WaferId = waferInfo.WaferID;
  114. _schedulerSequences = schedulerSequences;
  115. State = WaferTaskState.Created;
  116. _pufEntity = pufEntity;
  117. }
  118. /// <summary>
  119. /// 执行
  120. /// </summary>
  121. public void Run()
  122. {
  123. if (State == WaferTaskState.Error)
  124. {
  125. return;
  126. }
  127. if (_currentSequenceIndex >= _schedulerSequences.Count)
  128. {
  129. State = WaferTaskState.End;
  130. if (OnTaskComplete != null)
  131. {
  132. OnTaskComplete(WaferId);
  133. }
  134. WaferTaskManager.Instance.RemoveWaferIdMatchWaferHolderTaskDic(WaferId);
  135. return;
  136. }
  137. SchedulerSequence sequence = _schedulerSequences[_currentSequenceIndex];
  138. if (_currentSequenceIndex == _schedulerSequences.Count - 1 && sequence.State == RState.End)
  139. {
  140. State = WaferTaskState.End;
  141. if(OnTaskComplete!=null)
  142. {
  143. OnTaskComplete(WaferId);
  144. }
  145. WaferTaskManager.Instance.RemoveWaferIdMatchWaferHolderTaskDic(WaferId);
  146. return;
  147. }
  148. if (!_isStart)
  149. {
  150. bool preCondition = CheckStartCondition();
  151. if (!preCondition)
  152. {
  153. return;
  154. }
  155. _isStart = true;
  156. }
  157. //暂停中
  158. if(sequence.State==RState.Init&&_currentSequenceIndex>=_pausedIndex&&_pausedIndex!=-1)
  159. {
  160. return;
  161. }
  162. if(sequence.IsWaitNotify)
  163. {
  164. if (sequence.State == RState.End)
  165. {
  166. _currentSequenceIndex++;
  167. }
  168. return;
  169. }
  170. if(sequence.MaterialType==MaterialType.WaferHolder)
  171. {
  172. if(sequence.State==RState.End)
  173. {
  174. _currentSequenceIndex++;
  175. }
  176. return;
  177. }
  178. if (_currentSequenceIndex < _schedulerSequences.Count)
  179. {
  180. if (sequence.State == RState.Init)
  181. {
  182. string reason = "";
  183. bool sequeceCondition = sequence.SchedulerModule.CheckPrecondition(_schedulerSequences, _currentSequenceIndex,sequence.Parameters,WaferId,ref reason);
  184. _sequenceConditionTrigger.CLK = !sequeceCondition;
  185. if (sequeceCondition)
  186. {
  187. bool result = sequence.SchedulerModule.RunProcess(sequence.Recipe,sequence.Parameters,sequence.SynchronousModuleMessages);
  188. if (result)
  189. {
  190. if (State == WaferTaskState.Created)
  191. {
  192. State = WaferTaskState.Processing;
  193. if(OnTaskStart!=null)
  194. {
  195. OnTaskStart(WaferId);
  196. }
  197. }
  198. sequence.State = RState.Running;
  199. sequence.StartTime = DateTime.Now;
  200. LOG.WriteLog(eEvent.EV_SEQUENCE, "System", $"{WaferId} Start {_currentSequenceIndex + 1} sequence {sequence.SchedulerModule.Module}");
  201. }
  202. }
  203. else
  204. {
  205. if (_sequenceConditionTrigger.Q)
  206. {
  207. LOG.WriteLog(eEvent.EV_SEQUENCE, "System", $"{WaferId} Start {_currentSequenceIndex + 1} sequence {sequence.SchedulerModule.Module} failed,{reason}");
  208. if (sequence.SchedulerModule.Module == ModuleName.EfemRobot)
  209. {
  210. AnalyseSchedulerSrdState(sequence);
  211. }
  212. }
  213. }
  214. }
  215. else if (sequence.State == RState.Running)
  216. {
  217. bool hasMatched=!string.IsNullOrEmpty(WaferTaskManager.Instance.GetMatchWaferIdByWaferId(this.WaferId));
  218. sequence.SchedulerModule.MonitorProcess(sequence,hasMatched);
  219. _sequenceErrorTigger.CLK = sequence.SchedulerModule.IsError;
  220. if (sequence.SchedulerModule.IsIdle)
  221. {
  222. sequence.EndTime = DateTime.Now;
  223. sequence.State = RState.End;
  224. sequence.ProcessMilliSeconds = sequence.EndTime.Subtract(sequence.StartTime).TotalMilliseconds;
  225. LOG.WriteLog(eEvent.EV_SEQUENCE, "System", $"{WaferId} {_currentSequenceIndex + 1} sequence complete, time length {sequence.ProcessMilliSeconds}");
  226. _currentSequenceIndex++;
  227. }
  228. }
  229. else if(sequence.State==RState.End)
  230. {
  231. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"{WaferId} {_currentSequenceIndex + 1} sequence is End");
  232. _currentSequenceIndex++;
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 分析错误状态下的调度
  238. /// </summary>
  239. private void AnalyseSchedulerSrdState(SchedulerSequence sequence)
  240. {
  241. if(!(sequence.Parameters is MoveItem))
  242. {
  243. return;
  244. }
  245. MoveItem moveItem = sequence.Parameters as MoveItem;
  246. bool exsitSRD = false;
  247. if (moveItem.DestinationType == ModuleType.SRD)
  248. {
  249. SRDEntity srdEntity1 = Singleton<RouteManager>.Instance.GetModule<SRDEntity>(ModuleName.SRD1.ToString());
  250. if (srdEntity1 != null&&(srdEntity1.IsBusy||srdEntity1.IsIdle)&&srdEntity1.IsAuto)
  251. {
  252. exsitSRD = true;
  253. return;
  254. }
  255. SRDEntity srdEntity2 = Singleton<RouteManager>.Instance.GetModule<SRDEntity>(ModuleName.SRD2.ToString());
  256. if (srdEntity2 != null && (srdEntity2.IsBusy || srdEntity2.IsIdle) && srdEntity2.IsAuto)
  257. {
  258. exsitSRD = true;
  259. return;
  260. }
  261. if (!exsitSRD)
  262. {
  263. WaferInfo waferInfo = WaferManager.Instance.GetWaferByWaferId(WaferId);
  264. if (waferInfo == null || waferInfo.IsEmpty)
  265. {
  266. return;
  267. }
  268. WaferManager.Instance.UpdateWaferProcessStatus(moveItem.SourceModule, moveItem.SourceSlot, EnumWaferProcessStatus.MisSrdProcess);
  269. MaterialTrackerManager.Instance.UpdateModuleMaterial(moveItem.SourceModule.ToString());
  270. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"{waferInfo.WaferID} status changed to {EnumWaferProcessStatus.MisSrdProcess}");
  271. moveItem.DestinationModule = (ModuleName)waferInfo.OriginStation;
  272. moveItem.DestinationSlot = waferInfo.OriginSlot;
  273. moveItem.DestinationType = ModuleType.LoadPort;
  274. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"{WaferId} has no avaible srd changed to {moveItem.DestinationModule}.{moveItem.DestinationSlot}");
  275. for(int i = _currentSequenceIndex + 1; i < _schedulerSequences.Count; i++)
  276. {
  277. SchedulerSequence item = _schedulerSequences[i];
  278. item.State = RState.End;
  279. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"{WaferId} skip {i + 1} sequence {item.ModuleType}");
  280. }
  281. }
  282. }
  283. }
  284. /// <summary>
  285. /// 释放资源
  286. /// </summary>
  287. public void Dispose()
  288. {
  289. _schedulerSequences.Clear();
  290. }
  291. /// <summary>
  292. /// 检验前置条件
  293. /// </summary>
  294. /// <returns></returns>
  295. private bool CheckStartCondition()
  296. {
  297. EfemEntity efemEntity = Singleton<RouteManager>.Instance.EFEM;
  298. if(!efemEntity.IsIdle)
  299. {
  300. return false;
  301. }
  302. SchedulerRobot schedulerEfemRobot = (SchedulerRobot)SchedulerManager.Instance.GetScheduler(ModuleName.EfemRobot);
  303. if(schedulerEfemRobot==null)
  304. {
  305. return false;
  306. }
  307. if(!schedulerEfemRobot.IsIdle)
  308. {
  309. return false;
  310. }
  311. if(WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot,0))
  312. {
  313. return false;
  314. }
  315. if(WaferManager.Instance.CheckHasWafer(ModuleName.Aligner1, 0))
  316. {
  317. return false;
  318. }
  319. if(!_pufEntity.IsIdle)
  320. {
  321. return false;
  322. }
  323. //PUF B面无Wafer
  324. if(WaferManager.Instance.CheckHasWafer(_pufEntity.Module,1))
  325. {
  326. return false;
  327. }
  328. //if(!WaferHolderManager.Instance.HasWaferHolder("Loader"))
  329. //{
  330. // return false;
  331. //}
  332. LoaderEntity loaderEntity = Singleton<RouteManager>.Instance.GetModule<LoaderEntity>(ModuleName.Loader1.ToString());
  333. if (loaderEntity == null)
  334. {
  335. return false;
  336. }
  337. if(!string.IsNullOrEmpty(MateWaferTask))
  338. {
  339. WaferTask mateTask=WaferTaskManager.Instance.GetWaferTask(MateWaferTask);
  340. if(mateTask!=null)
  341. {
  342. if(mateTask.State==WaferTaskState.Created)
  343. {
  344. return false;
  345. }
  346. }
  347. }
  348. else
  349. {
  350. if (loaderEntity.IsBusy && loaderEntity.State != (int)LOADERSTATE.WaitForUnload)
  351. {
  352. return false;
  353. }
  354. }
  355. //WaferHolderTask waferHolderTask = WaferTaskManager.Instance.GetWaferHolderTaskByWaferId(WaferId);
  356. //if (waferHolderTask != null)
  357. //{
  358. // if(waferHolderTask.State==WaferHolderTaskState.Created||waferHolderTask.State==WaferHolderTaskState.Error)
  359. // {
  360. // return false;
  361. // }
  362. //}
  363. return true;
  364. }
  365. /// <summary>
  366. /// 同步更新Loader工序完成
  367. /// </summary>
  368. public void UpdateLoaderSchedulerSequenceComplete()
  369. {
  370. SchedulerSequence schedulerSequence = _schedulerSequences[_currentSequenceIndex];
  371. if (schedulerSequence != null)
  372. {
  373. if (schedulerSequence.ModuleType == ModuleType.PUF&&schedulerSequence.IsWaitNotify)
  374. {
  375. PufSchedulerParameter pufParameter = (PufSchedulerParameter)schedulerSequence.Parameters;
  376. if (!pufParameter.IsForward)
  377. {
  378. schedulerSequence.StartTime = DateTime.Now;
  379. schedulerSequence.EndTime = DateTime.Now;
  380. schedulerSequence.State = RState.End;
  381. schedulerSequence.ProcessMilliSeconds = schedulerSequence.EndTime.Subtract(schedulerSequence.StartTime).TotalMilliseconds;
  382. LOG.WriteLog(eEvent.EV_SEQUENCE, "System", $"{WaferId} {_currentSequenceIndex + 1} puf sequence task synchronize notify complete");
  383. }
  384. }
  385. }
  386. }
  387. /// <summary>
  388. /// 暂停
  389. /// </summary>
  390. public void Pause()
  391. {
  392. _pausedIndex = _currentSequenceIndex;
  393. }
  394. /// <summary>
  395. /// 恢复
  396. /// </summary>
  397. public void Resume()
  398. {
  399. _pausedIndex = -1;
  400. }
  401. /// <summary>
  402. /// 移除SRD调度
  403. /// </summary>
  404. public void RemoveSrdScheduler()
  405. {
  406. WaferInfo waferInfo=WaferManager.Instance.GetWaferByWaferId(WaferId);
  407. if (waferInfo != null && waferInfo.WaferType == WaferType.Production&&waferInfo.ProcessJob!=null
  408. &&waferInfo.ProcessJob.SequenceRecipe!=null)
  409. {
  410. if (SequenceRecipeManager.Instance.IsContainedSrd(waferInfo.ProcessJob.SequenceRecipe))
  411. {
  412. for(int i=_currentSequenceIndex;i<_schedulerSequences.Count;i++)
  413. {
  414. SchedulerSequence item= _schedulerSequences[i];
  415. if(item.ModuleType==ModuleType.SRD&&i+1<_schedulerSequences.Count&&i>0)
  416. {
  417. SchedulerSequence preSequence = _schedulerSequences[i - 1];
  418. SchedulerSequence nextSequence = _schedulerSequences[i + 1];
  419. if (preSequence.ModuleName == ModuleName.EfemRobot&&nextSequence.ModuleName==ModuleName.EfemRobot)
  420. {
  421. MoveItem moveItem=preSequence.Parameters as MoveItem;
  422. MoveItem nextMoveItem=nextSequence.Parameters as MoveItem;
  423. moveItem.DestinationModule = nextMoveItem.DestinationModule;
  424. moveItem.DestinationSlot= nextMoveItem.DestinationSlot;
  425. moveItem.DestinationType= nextMoveItem.DestinationType;
  426. _schedulerSequences.RemoveAt(i + 1);
  427. _schedulerSequences.RemoveAt(i);
  428. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"Wafer {WaferId} remove SRD Scheduler");
  429. }
  430. break;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. /// <summary>
  437. /// 步骤是否进入Loader及之后
  438. /// </summary>
  439. /// <returns></returns>
  440. public bool IsSchedulerForward()
  441. {
  442. //0-EfemRobot,1-Aligner,2-EfemRobot,3-Puf(正向),4-PUF(工艺结束返回)
  443. return _currentSequenceIndex < 4;
  444. }
  445. }
  446. }