using System; using System.Collections.Generic; using Aitex.Core.Common; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.Fsm; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.Routine; using Aitex.Core.Util; using Aitex.Core.Utilities; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.SubstrateTrackings; using MECF.Framework.RT.Core.Applications; using MECF.Framework.RT.Core.IoProviders; using MECF.Framework.RT.ModuleLibrary.Commons; using MECF.Framework.RT.ModuleLibrary.SystemModules.Routines; namespace MECF.Framework.RT.ModuleLibrary.SystemModules { public enum RtState { Init, Initializing, Idle, Transfer, AutoRunning, AutoIdle, ReturnAllWafer, Error, } public class EquipmentManager : FsmDevice { public enum MSG { MoveWafer, ReturnAllWafer, Stop, HOME, RESET, ABORT, ERROR, ToInit, SetAutoMode, SetManualMode, CreateJob, PauseJob, ResumeJob, StartJob, StopJob, AbortJob, JobDone, ModuleError, Map, } public static Dictionary Modules { get; set; } public bool IsAutoMode { get { return FsmState == (int)RtState.AutoRunning || FsmState == (int)RtState.AutoIdle; } } public bool IsInit { get { return FsmState == (int)RtState.Init; } } public bool IsIdle { get { return FsmState == (int)RtState.Idle; } } public bool IsAlarm { get { return FsmState == (int)RtState.Error; } } public bool IsRunning { get { return !IsAlarm && !IsIdle && !IsInit && (FsmState != (int)RtState.AutoIdle); } } private bool _isInited; protected IRoutine _manualTransfer; protected IAutoTransfer _auto; protected IRoutine _homeAll; protected IRoutine _returnAll; private List _modules; public EquipmentManager() { Module = "System"; Name = "System"; Modules = new Dictionary(); _modules = new List() { "System" }; } public override bool Initialize() { InitDevices(); InitModules(); EnumLoop.ForEach((item) => { MapState((int)item, item.ToString()); }); EnumLoop.ForEach((item) => { MapMessage((int)item, item.ToString()); }); EnableFsm(100, RtState.Init); BuildTransitionTable(); SubscribeDataVariable(); SubscribeOperation(); InitRoutine(); Singleton.Instance.OnAlarmEvent += Instance_OnAlarmEvent; return true; } protected void BuildModules(params ModuleFsmDevice[] modules) { if (modules != null && modules.Length > 0) { foreach (var moduleFsmDevice in modules) { Modules[ModuleHelper.Converter(moduleFsmDevice.Module)] = moduleFsmDevice; } foreach (var modulesKey in Modules.Keys) { _modules.Add(modulesKey.ToString()); } foreach (var modulesValue in Modules.Values) { modulesValue.Initialize(); } } } protected virtual void InitRoutine() { } protected virtual void InitModules() { } protected virtual void InitDevices() { } //private PeriodicJob _job1; //Stopwatch _sw = new Stopwatch(); //private bool OnJob1() //{ // _sw.Restart(); // BufferModule buffer = Modules[ModuleName.Buffer] as BufferModule; // if (!buffer.CheckToPostMessage(BufferModule.MSG.InTransfer)) // { // System.Diagnostics.Trace.Assert(false) // System.Diagnostics.Trace.WriteLine("fail to in transfer"); // } // if (buffer.IsIdle) // { // System.Diagnostics.Trace.Assert(false); // System.Diagnostics.Trace.WriteLine("!!!==========>IsIdle"); // } // //System.Diagnostics.Trace.Assert(buffer.IsIdle, $"in {buffer.StringFsmStatus}"); // while (buffer.IsIdle) // { // Thread.Sleep(5); // } // //buffer.NoteTransferStop(ModuleName.EfemRobot, Hand.Blade1, 0, EnumTransferType.Place); // //Thread.Sleep(15); // if (!buffer.CheckToPostMessage(BufferModule.MSG.TransferComplete)) // { // System.Diagnostics.Trace.Assert(false); // System.Diagnostics.Trace.WriteLine("fail to complete"); // } // //if (!buffer.IsIdle) // //{ // // System.Diagnostics.Trace.WriteLine("not idle"); // //} // System.Diagnostics.Trace.WriteLine($" === {_sw.ElapsedMilliseconds}"); // while (!buffer.IsIdle) // { // Thread.Sleep(5); // } // return true; //} private void BuildTransitionTable() { //Init sequence Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing); Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing); Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing); Transition(RtState.Error, MSG.ToInit, null, RtState.Init); Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle); Transition(RtState.Initializing, MSG.ERROR, fError, RtState.Error); Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init); //Reset AnyStateTransition(MSG.RESET, fStartReset, RtState.Idle); AnyStateTransition(MSG.ERROR, fError, RtState.Error); AnyStateTransition((int)FSM_MSG.ALARM, fError, (int)RtState.Error); //Auto/manual sequence Transition(RtState.Idle, MSG.SetAutoMode, fStartAutoTransfer, RtState.AutoIdle); Transition(RtState.AutoRunning, FSM_MSG.TIMER, fAutoTransfer, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.ABORT, FsmAbort, RtState.AutoIdle); Transition(RtState.AutoRunning, MSG.JobDone, null, RtState.AutoIdle); Transition(RtState.AutoRunning, MSG.CreateJob, FsmCreateJob, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.StartJob, FsmStartJob, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.PauseJob, FsmPauseJob, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.ResumeJob, FsmResumeJob, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.StopJob, FsmStopJob, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.AbortJob, FsmAbortJob, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.ModuleError, FsmModuleError, RtState.AutoRunning); Transition(RtState.AutoRunning, MSG.Map, FsmMap, RtState.AutoRunning); EnterExitTransition(RtState.AutoRunning, null, FSM_MSG.NONE, fExitAutoTransfer); Transition(RtState.AutoIdle, FSM_MSG.TIMER, FsmMonitorAutoIdle, RtState.AutoIdle); Transition(RtState.AutoIdle, MSG.SetManualMode, FsmStartSetManualMode, RtState.Idle); Transition(RtState.AutoIdle, MSG.CreateJob, FsmCreateJob, RtState.AutoIdle); Transition(RtState.AutoIdle, MSG.StartJob, FsmStartJob, RtState.AutoRunning); Transition(RtState.AutoIdle, MSG.ABORT, FsmAbort, RtState.AutoIdle); Transition(RtState.AutoIdle, MSG.AbortJob, FsmAbortJob, RtState.AutoIdle); Transition(RtState.AutoIdle, MSG.Map, FsmMap, RtState.AutoIdle); //return all wafer Transition(RtState.Idle, MSG.ReturnAllWafer, FsmStartReturnAllWafer, RtState.ReturnAllWafer); Transition(RtState.ReturnAllWafer, FSM_MSG.TIMER, FsmMonitorReturnAllWafer, RtState.Idle); Transition(RtState.ReturnAllWafer, MSG.ABORT, FsmAbortReturnAllWafer, RtState.Idle); //Transfer sequence Transition(RtState.Idle, MSG.MoveWafer, fStartTransfer, RtState.Transfer); Transition(RtState.Transfer, FSM_MSG.TIMER, fTransfer, RtState.Idle); EnterExitTransition(RtState.Transfer, null, FSM_MSG.NONE, fExitTransfer); Transition(RtState.Transfer, MSG.ABORT, FsmAbort, RtState.Idle); } void SubscribeDataVariable() { DATA.Subscribe("Rt.Status", () => StringFsmStatus); DATA.Subscribe("System.IsIdle", () => IsIdle || IsInit); DATA.Subscribe("System.IsAlarm", () => IsAlarm); DATA.Subscribe("System.IsBusy", () => IsRunning); DATA.Subscribe("System.IsAutoRunning", () => IsRunning); DATA.Subscribe("System.Modules", () => _modules); } void SubscribeOperation() { OP.Subscribe("Create8InchWafer", InvokeCreate8InchWafer); OP.Subscribe("Create12InchWafer", InvokeCreate12InchWafer); OP.Subscribe("DeleteWafer", InvokeDeleteWafer); OP.Subscribe("ReturnWafer", InvokeReturnWafer); OP.Subscribe("System.ReturnAllWafer", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.ReturnAllWafer); }); OP.Subscribe("System.MoveWafer", (string cmd, object[] args) => { if (!Enum.TryParse((string)args[0], out ModuleName source)) { EV.PostWarningLog(Name, $"Parameter source {(string)args[0]} not valid"); return false; } if (!Enum.TryParse((string)args[2], out ModuleName destination)) { EV.PostWarningLog(Name, $"Parameter destination {(string)args[1]} not valid"); return false; } if (args.Length > 8) { return CheckToPostMessage((int)MSG.MoveWafer, source, (int)args[1], destination, (int)args[3], (bool)args[4], (int)args[5], (bool)args[6], (int)args[7]); } else if (args.Length > 5) { return CheckToPostMessage((int)MSG.MoveWafer, source, (int)args[1], destination, (int)args[3], (bool)args[4]); } return CheckToPostMessage((int)MSG.MoveWafer, source, (int)args[1], destination, (int)args[3]); }); OP.Subscribe("System.HomeAll", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.HOME); }); OP.Subscribe("System.Abort", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.ABORT); }); OP.Subscribe("System.Reset", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.RESET); }); OP.Subscribe("System.SetAutoMode", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.SetAutoMode); }); OP.Subscribe("System.SetManualMode", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.SetManualMode); }); OP.Subscribe("System.CreateJob", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.CreateJob, args[0]); }); OP.Subscribe("System.StartJob", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.StartJob, args[0]); }); OP.Subscribe("System.PauseJob", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.PauseJob, args[0]); }); OP.Subscribe("System.ResumeJob", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.ResumeJob, args[0]); }); OP.Subscribe("System.StopJob", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.StopJob, args[0]); }); OP.Subscribe("System.AbortJob", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.AbortJob, args[0]); }); OP.Subscribe("System.ShutDown", InvokeShutDown); OP.Subscribe("System.MapWafer", InvokeEfemMap); } private void Instance_OnAlarmEvent(EventItem obj) { } #region Init private bool FsmStartHome(object[] objs) { _isInited = false; return _homeAll.Start() == Result.RUN; } private bool FsmMonitorHome(object[] objs) { Result ret = _homeAll.Monitor(); if (ret == Result.DONE) { _isInited = true; return true; } if (ret == Result.FAIL) { PostMsg(MSG.ERROR); } return false; } private bool fError(object[] objs) { if (FsmState == (int)RtState.Transfer) { } return true; } #endregion #region AutoTransfer private bool FsmMonitorAutoIdle(object[] param) { Result ret = _auto.Monitor(); if (!_auto.CheckAllJobDone()) { return false; } return ret == Result.DONE; } private bool FsmStartSetManualMode(object[] objs) { if (_auto.HasJobRunning) { EV.PostWarningLog("System", "Can not change to manual mode, abort running job first"); return false; } return true; } private bool fStartAutoTransfer(object[] objs) { Result ret = _auto.Start(objs); return ret == Result.RUN; } private bool fAutoTransfer(object[] objs) { Result ret = _auto.Monitor(); if (_auto.CheckAllJobDone()) { if (!CheckToPostMessage((int)MSG.JobDone)) return false; } return ret == Result.DONE; } private bool fExitAutoTransfer(object[] objs) { return true; } private bool fAbortAutoTransfer(object[] objs) { return true; } #endregion #region return all wafer private bool FsmAbortReturnAllWafer(object[] param) { return true; } private bool FsmMonitorReturnAllWafer(object[] param) { return _returnAll.Monitor() == Result.DONE; } private bool FsmStartReturnAllWafer(object[] param) { return _returnAll.Start() == Result.RUN; } #endregion #region Transfer private bool fStartTransfer(object[] objs) { Result ret = _manualTransfer.Start(objs); if (ret == Result.FAIL || ret == Result.DONE) return false; return ret == Result.RUN; } private bool fTransfer(object[] objs) { Result ret = _manualTransfer.Monitor(); if (ret == Result.FAIL) { PostMsg(MSG.ERROR); return false; } return ret == Result.DONE; } private bool fExitTransfer(object[] objs) { return true; } private bool fAbortTransfer(object[] objs) { return true; } #endregion #region reset private bool fStartReset(object[] objs) { EV.ClearAlarmEvent(); //Singleton.Instance.PostMsg(DeviceEntity.MSG.RESET); IoProviderManager.Instance.Reset(); foreach (var modulesValue in Modules.Values) { modulesValue.Reset(); } if (FsmState == (int)RtState.Error) { if (!_isInited) { PostMsg(MSG.ToInit); return false; } return true; } return false; } #endregion private bool FsmCreateJob(object[] param) { _auto.CreateJob((Dictionary)param[0]); return true; } private bool FsmAbortJob(object[] param) { _auto.AbortJob((string)param[0]); return true; } private bool FsmStopJob(object[] param) { _auto.StopJob((string)param[0]); //CheckToPostMessage((int)MSG.StopJob);// return true; } private bool FsmResumeJob(object[] param) { _auto.ResumeJob((string)param[0]); return true; } private bool FsmPauseJob(object[] param) { _auto.PauseJob((string)param[0]); return true; } private bool FsmStartJob(object[] param) { return _auto.StartJob((string)param[0]); } private bool FsmAbort(object[] param) { if (FsmState == (int)RtState.Transfer) { // _manualTransfer.Clear(); } if (FsmState == (int)RtState.AutoRunning) { _auto.Clear(); } if (FsmState == (int)RtState.Initializing) { _homeAll.Abort(); } return true; } private bool FsmModuleError(object[] param) { _auto.ModuleError((string)param[0]); return true; } private bool FsmMap(object[] param) { _auto.Map((string)param[0]); return true; } private bool InvokeReturnWafer(string arg1, object[] args) { ModuleName target = ModuleHelper.Converter(args[0].ToString()); int slot = (int)args[1]; if (ModuleHelper.IsLoadPort(target)) { EV.PostInfoLog("System", string.Format("Wafer already at LoadPort {0} {1}, return operation is not valid", target.ToString(), slot + 1)); return false; } if (!WaferManager.Instance.IsWaferSlotLocationValid(target, slot)) { EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", target.ToString(), slot.ToString())); return false; } WaferInfo wafer = WaferManager.Instance.GetWafer(target, slot); if (wafer.IsEmpty) { EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, return operation is not valid", target.ToString(), slot + 1)); return false; } return CheckToPostMessage((int)MSG.MoveWafer, target, slot, (ModuleName)wafer.OriginStation, wafer.OriginSlot, false, 0, false, 0); } private bool InvokeDeleteWafer(string arg1, object[] args) { ModuleName chamber = ModuleHelper.Converter(args[0].ToString()); int slot = (int)args[1]; if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot)) { if (WaferManager.Instance.CheckHasWafer(chamber, slot)) { WaferManager.Instance.DeleteWafer(chamber, slot); EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1); } else { EV.PostInfoLog("System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1)); } } else { EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString())); return false; } return true; } private bool InvokeCreate8InchWafer(string arg1, object[] args) { ModuleName chamber = ModuleHelper.Converter(args[0].ToString()); int slot = (int)args[1]; WaferStatus state = WaferStatus.Normal; //if (ModuleHelper.IsLoadPort(chamber)) //{ // var lp = Modules[chamber] as LoadPortModule; // if (lp.LPDevice.CassetteState != LoadportCassetteState.Normal) // { // EV.PostWarningLog("System", $"Can not create wafer at {chamber}.{slot + 1}, Cassette not placed."); // return false; // } //} if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot)) { if (WaferManager.Instance.CheckHasWafer(chamber, slot)) { EV.PostInfoLog("System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot)); } else if (WaferManager.Instance.CreateWafer(chamber, slot, state,WaferSize.WS8) != null) { EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferCreate, chamber.ToString(), slot + 1, state.ToString()); } } else { EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString())); return false; } return true; } private bool InvokeCreate12InchWafer(string arg1, object[] args) { ModuleName chamber = ModuleHelper.Converter(args[0].ToString()); int slot = (int)args[1]; WaferStatus state = WaferStatus.Normal; //if (ModuleHelper.IsLoadPort(chamber)) //{ // var lp = Modules[chamber] as LoadPortModule; // if (lp.LPDevice.CassetteState != LoadportCassetteState.Normal) // { // EV.PostWarningLog("System", $"Can not create wafer at {chamber}.{slot + 1}, Cassette not placed."); // return false; // } //} if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot)) { if (WaferManager.Instance.CheckHasWafer(chamber, slot)) { EV.PostInfoLog("System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot)); } else if (WaferManager.Instance.CreateWafer(chamber, slot, state,WaferSize.WS12) != null) { EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferCreate, chamber.ToString(), slot + 1, state.ToString()); } } else { EV.PostWarningLog("System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString())); return false; } return true; } private bool InvokeShutDown(string arg1, object[] arg2) { if (IsAlarm || IsIdle || IsInit) { EV.PostWarningLog(Module, $"System start shut down"); EV.PostKickoutMessage("ShutDown"); RtApplication.Instance.Terminate(); return true; } EV.PostWarningLog(Module, $"System in {StringFsmStatus} mode, can not shut down"); return false; } private bool InvokeEfemMap(string arg1, object[] arg2) { ModuleName target = ModuleHelper.Converter((string)arg2[0]); if (!ModuleHelper.IsLoadPort(target)) { EV.PostWarningLog("System", $"Invalid map target {target}"); return false; } for (int i = 0; i < 25; i++) { WaferInfo wafer = WaferManager.Instance.GetWafer(target, i); if (wafer.IsEmpty) continue; if (wafer.ProcessState == EnumWaferProcessStatus.Completed || wafer.ProcessState == EnumWaferProcessStatus.Failed) { EV.PostWarningLog("System", $"{target} wafer is processed, can not map again"); return false; } } if (IsAutoMode) { _auto.Map((string)arg2[0]); return true; } //EfemModule efem = Modules[ModuleName.EfemRobot] as EfemModule; //if (!efem.Map(ModuleHelper.Converter((string) arg2[0]), out string reason)) //{ // EV.PostWarningLog(Module, reason); //} return true; } private void OnModuleError(string module) { if (FsmState == (int)RtState.AutoRunning) { ModuleName mod = ModuleHelper.Converter(module); PostMsg(MSG.ModuleError, module); } } public override void Monitor() { base.Monitor(); try { } catch (Exception ex) { LOG.Write(ex); } } } }