EfemEntity.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using Aitex.Core.Common;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.Fsm;
  8. using Aitex.Core.RT.OperationCenter;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.Utilities;
  11. using Aitex.Core.Util;
  12. using Aitex.Core.RT.Log;
  13. using Aitex.Sorter.Common;
  14. using MECF.Framework.Common.Equipment;
  15. using MECF.Framework.Common.Schedulers;
  16. using MECF.Framework.Common.SubstrateTrackings;
  17. using PunkHPX8_Core;
  18. using PunkHPX8_RT;
  19. using PunkHPX8_RT.Devices;
  20. using PunkHPX8_RT.Devices.YASKAWA;
  21. using PunkHPX8_RT.Devices.EFEM;
  22. using PunkHPX8_RT.Modules.LPs;
  23. using PunkHPX8_RT.Modules.EFEM;
  24. using System.Reflection;
  25. using PunkHPX8_RT.Modules.Transporter;
  26. using MECF.Framework.Common.Alarm;
  27. using MECF.Framework.Common.CommonData;
  28. using Aitex.Core.RT.Routine;
  29. using MECF.Framework.Common.IOCore;
  30. using MECF.Framework.Common.Beckhoff.ModuleIO;
  31. namespace PunkHPX8_RT.Modules
  32. {
  33. class EfemEntity : Entity, IEntity, IModuleEntity
  34. {
  35. #region 常量
  36. private const string VACUUM_VALUE = "VacuumValue";
  37. private const string DOOR_UNLOCK = "DoorUnlock";
  38. private const string DOOR_LOCKED = "DoorLocked";
  39. #endregion
  40. //private int _bigWafer = 0;
  41. //private int _midWafer = 0;
  42. //private int _smallWafer = 0;
  43. private bool _isVacuume = false;
  44. private bool _isPause = false;
  45. private bool _isResume = false;
  46. public enum STATE
  47. {
  48. Unknown, // 0
  49. Initializing, // 1
  50. Idle, // 2
  51. Error, // 3
  52. Picking, // 4
  53. Placing, // 5
  54. Aligning, // 6
  55. Mapping, // 7
  56. Init, // 8
  57. Orgshing, // 9
  58. Lifting, // 10
  59. InitingAL, // 11
  60. InitingRB, // 12
  61. Extending, // 13
  62. Retracting, // 14
  63. //SettingLamp, // 15
  64. Swapping,
  65. Gotoing,
  66. Gripping,
  67. Ungripping,
  68. Fliping,
  69. Vacuuming,
  70. Retrying,
  71. ConfirmCompleting,
  72. ErrorPicking,
  73. ErrorPlacing,
  74. RobotCycleing,
  75. ErrorInitingAL,
  76. }
  77. public enum MSG
  78. {
  79. HomeAll, // 0
  80. Pick, // 1
  81. Place, // 2
  82. Align, // 3
  83. ActionDone, // 4
  84. MoveCmd, // 6
  85. Recover, // 8
  86. Goto, // 9
  87. Error, // 10
  88. Online, // 11
  89. CommReady, // 12
  90. Lift, // 13
  91. HomeAL, // 14
  92. HomeRB, // 15
  93. Extend, // 16
  94. Retract, // 17
  95. PMLiftPinUp, // 18
  96. PMLiftPinDown, // 19
  97. TurnOffBuzzer,
  98. Abort,
  99. GotoMap,
  100. Map,
  101. ToInit,
  102. Cool,
  103. Swap,
  104. Grip,
  105. Ungrip,
  106. Flip,
  107. LiftActionDone,
  108. Offline,
  109. SetRobotSpeed,
  110. MapDummy,
  111. Vacuum,
  112. CloseBuzzer,
  113. RobotPowerOn,
  114. Retry,
  115. ConfirmComplete,
  116. BackroundCmd=999,
  117. Load,
  118. Unload,
  119. Dock,
  120. Undock,
  121. Clamp,
  122. Unclamp,
  123. ReadCarrierId,
  124. WriteCarrierID,
  125. ReadTagData,
  126. WriteTagData,
  127. RobotCycle,
  128. }
  129. public enum EfemType
  130. {
  131. FutureEfem = 1,
  132. SunWayEFEM =2
  133. }
  134. public bool IsIdle
  135. {
  136. get { return fsm.State == (int)STATE.Idle; }
  137. }
  138. public bool IsError
  139. {
  140. get { return fsm.State == (int)STATE.Error; }
  141. }
  142. public bool IsInit
  143. {
  144. get { return fsm.State == (int)STATE.Unknown || fsm.State == (int)STATE.Init; }
  145. }
  146. public bool IsBusy
  147. {
  148. get { return !IsInit && !IsError && !IsIdle; }
  149. }
  150. public bool IsAuto { get; } = true;
  151. /// <summary>
  152. /// 是否为工程模式
  153. /// </summary>
  154. public bool IsEngineering { get; } = false;
  155. /// <summary>
  156. /// 是否为产品模式
  157. /// </summary>
  158. public bool IsProduction { get; } = true;
  159. /// <summary>
  160. /// 模块名称
  161. /// </summary>
  162. public ModuleName Module { get; private set; }
  163. public bool IsHomed
  164. {
  165. get { return _isHomed; }
  166. }
  167. public bool IsOnline { get; internal set; }
  168. public bool IsDisable { get; internal set; }
  169. public RState RobotStatus
  170. {
  171. get
  172. {
  173. if (_efem.Status != RState.Running)
  174. {
  175. if (_robotWatch.ElapsedMilliseconds < 200)
  176. return RState.Running;
  177. else
  178. return _efem.Status;
  179. }
  180. else
  181. return RState.Running;
  182. }
  183. }
  184. public double VacuumValue
  185. {
  186. get { return _vacuumValue; }
  187. }
  188. public bool Check(int msg, out string reason, params object[] args)
  189. {
  190. throw new NotImplementedException();
  191. }
  192. // Fields
  193. private readonly string Name;
  194. private readonly EfemBase _efem;
  195. private readonly LoadPortModule[] _lpms = new LoadPortModule[3];
  196. private readonly DummyDevice[] _dummies = new DummyDevice[2];
  197. private readonly EfemType _efemType;
  198. public EfemBase EfemDevice => _efem;
  199. public EfemType EFEMType => _efemType;
  200. // routine
  201. private readonly EfemPickRoutine _pickRoutine;
  202. private readonly EfemPlaceRoutine _placeRoutine;
  203. private readonly EfemSwapRoutine _swapRoutine;
  204. private readonly EfemHomeRoutine _homeRoutine;
  205. private readonly EFEMAlignRoutine _alignRoutine;
  206. private readonly EfemMapDummyRoutine _mapDummyRoutine;
  207. private readonly EfemVacuumRoutine _vacuumRoutine;
  208. private readonly CycleRobotCycleNewRoutine _cycleRobotCycleRoutine;
  209. private readonly EfemSafeOpenRoutine _efemSafeOpenRoutine;
  210. private readonly EfemSafeCloseRoutine _efemSafeCloseRoutine;
  211. private string LiftMessage;
  212. private Stopwatch _robotWatch = new Stopwatch();
  213. private R_TRIG _robotIdleTrigger = new R_TRIG();
  214. private bool _isHomed = false;
  215. private EfemAutoMessageProcessor _autoMessageProcessor;
  216. private STATE _errorPreState;
  217. private IRoutine _currentRoutine;
  218. private int _currentCycleTimes;
  219. /// <summary>
  220. /// 变量是否初始化字典
  221. /// </summary>
  222. private Dictionary<string, bool> _variableInitializeDic = new Dictionary<string, bool>();
  223. /// <summary>
  224. /// 真空数值
  225. /// </summary>
  226. private double _vacuumValue;
  227. private bool _doorUnlock;
  228. private bool _doorLocked;
  229. // Constructor
  230. public EfemEntity()
  231. {
  232. Module = ModuleName.EFEM;
  233. Name = ModuleName.EFEM.ToString();
  234. InitFsmMap();
  235. _efemType = (EfemType)SC.GetValue<int>($"EFEM.EfemType");
  236. _efem = new SunWayRobot();
  237. _homeRoutine = new EfemHomeRoutine(_efem);
  238. _pickRoutine = new EfemPickRoutine(_efem);
  239. _placeRoutine = new EfemPlaceRoutine(_efem);
  240. _swapRoutine = new EfemSwapRoutine(_efem);
  241. _alignRoutine = new EFEMAlignRoutine(_efem);
  242. _mapDummyRoutine = new EfemMapDummyRoutine(_efem);
  243. _vacuumRoutine = new EfemVacuumRoutine(_efem);
  244. _cycleRobotCycleRoutine = new CycleRobotCycleNewRoutine(_efem);
  245. _autoMessageProcessor =new EfemAutoMessageProcessor(_efem);
  246. _efemSafeOpenRoutine = new EfemSafeOpenRoutine(_efem);
  247. _efemSafeCloseRoutine = new EfemSafeCloseRoutine(_efem);
  248. }
  249. public LoadPortModule GetLoadportModule(int lpNumber)
  250. {
  251. if (lpNumber < 0|| _lpms.Length<=lpNumber)
  252. {
  253. return null;
  254. }
  255. return _lpms[lpNumber];
  256. }
  257. public DummyDevice GetDummyDevice(int dummyNumber)
  258. {
  259. if (dummyNumber < 0||_dummies.Length<=dummyNumber)
  260. {
  261. return null;
  262. }
  263. return _dummies[dummyNumber];
  264. }
  265. protected override bool Init()
  266. {
  267. _lpms[0] = new LoadPortModule(ModuleName.LP1, _efem);
  268. _lpms[1] = new LoadPortModule(ModuleName.LP2, _efem);
  269. _lpms[2] = new LoadPortModule(ModuleName.LP3, _efem);
  270. _dummies[0] = new DummyDevice(ModuleName.Dummy1);
  271. _dummies[1] = new DummyDevice(ModuleName.Dummy2);
  272. _lpms[0].Initialize();
  273. _lpms[1].Initialize();
  274. _lpms[2].Initialize();
  275. BeckhoffIoSubscribeUpdateVariable(VACUUM_VALUE);
  276. BeckhoffIoSubscribeUpdateVariable(DOOR_UNLOCK);
  277. BeckhoffIoSubscribeUpdateVariable(DOOR_LOCKED);
  278. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAll); return true; });
  279. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.RobotCycle}", (cmd, args) => {
  280. PostMsg(MSG.RobotCycle,args); return true; });
  281. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.ClearError}", (cmd, args) => { PostMsg(MSG.Recover); return true; });
  282. OP.Subscribe($"{ModuleName.EFEM}.{EfemOperation.TurnOffBuzzer}", (cmd, args) => { PostMsg(MSG.CloseBuzzer); return true; });
  283. OP.Subscribe($"{ModuleName.EFEM}.Online", (cmd, args) =>
  284. {
  285. PostMsg(MSG.Online, args[0]); return true;
  286. });
  287. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Pick}", (cmd, args) => { CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_COMMON_FAILED,"EFEM",(int)MSG.Pick, args[0]); return true; });
  288. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Place}", (cmd, args) => { CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_COMMON_FAILED, "EFEM", (int)MSG.Place, args[0]); return true; });
  289. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Swap}", (cmd, args) => { PostMsg(MSG.Swap, args[0]); return true; });
  290. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.DummyMap}", (cmd, args) => { CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_COMMON_FAILED, "EFEM", (int)MSG.MapDummy); return true; });
  291. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Speed}", (cmd, args) => {
  292. return _efem.SetRobotSpeed(ModuleName.EfemRobot, (int)args[0]); });
  293. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Vacuum}", (cmd, args) => {
  294. return _efem.Vacuum(ModuleName.EfemRobot, (bool)args[1]); });
  295. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.PowerOn}", (cmd, args) => {
  296. return _efem.RobotPowerOn(ModuleName.EfemRobot, (bool)args[0]); });
  297. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Abort}", (cmd, args) => { PostMsg(MSG.Abort); return true; });
  298. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeRB); return true; });
  299. OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Grip}", (cmd, args) =>
  300. {
  301. bool isGrip = ((string)args[0]).ToLower() == "on";
  302. PostMsg(isGrip ? MSG.Grip : MSG.Ungrip, args[1]);
  303. return true;
  304. });
  305. //OP.Subscribe($"{ModuleName.EfemRobot}.{EfemOperation.Flip}", (cmd, args) => { PostMsg(MSG.Flip, args[0]); return true; });
  306. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Home}", (cmd, args) => { PostMsg(MSG.HomeAL, ModuleName.Aligner1); return true; });
  307. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Align}", (cmd, args) => {
  308. int alignerFlatType = SC.GetValue<int>("EFEM.Aligner1.AlignerPlatType");
  309. CheckToPostMessage<STATE,MSG>(eEvent.ERR_EFEM_COMMON_FAILED,ModuleName.EFEM.ToString(),(int)MSG.Align, ModuleName.Aligner1, args[0], args[1],alignerFlatType); return true; });
  310. //OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Vacuum}", (cmd, args) => { CheckToPostMessage<STATE,MSG>(eEvent.ERR_EFEM_COMMON_FAILED,ModuleName.EFEM.ToString(),(int)MSG.Vacuum, args); return true; });
  311. OP.Subscribe($"{ModuleName.Aligner1}.{EfemOperation.Vacuum}", (cmd, args) => { return VacuumAction(args); });
  312. OP.Subscribe($"{Name}.DoorUnlock", (cmd, args) => { return DoorUnlock(args); });
  313. OP.Subscribe($"{Name}.SafeOpen", (cmd, args) => { return SafeOpenAction(args); });
  314. OP.Subscribe($"{Name}.SafeClose", (cmd, args) => { return SafeCloseAction(args); });
  315. OP.Subscribe($"{Name}.Pause", (cmd, args) => { return Pause(args); });
  316. OP.Subscribe($"{Name}.Resume", (cmd, args) => { return Resume(args); });
  317. DATA.Subscribe($"{Name}.FsmState", () => ((STATE)fsm.State).ToString(),SubscriptionAttribute.FLAG.IgnoreSaveDB);
  318. DATA.Subscribe($"{Name}.FsmPrevState", () => ((STATE)fsm.PrevState).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  319. DATA.Subscribe($"{Name}.FsmLastMessage", GetFsmLastMessage, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  320. DATA.Subscribe($"{Name}.RobotMoveAction", () => (_efem.TMRobotMoveInfo), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  321. DATA.Subscribe($"{Name}.State", () => ((STATE)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  322. DATA.Subscribe($"{Name}.IsAlarm", () => IsError, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  323. DATA.Subscribe($"{Name}.IsInit", () => IsInit, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  324. DATA.Subscribe($"{Name}.IsIdle", () => IsIdle, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  325. DATA.Subscribe($"{Name}.IsBusy", () => IsBusy, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  326. DATA.Subscribe($"{Name}.IsOnline", () => IsOnline, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  327. DATA.Subscribe($"{Name}.IsHomed", () => _isHomed, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  328. DATA.Subscribe($"{Name}.RobotSpeed",()=>IsIdle?SC.GetValue<int>("EFEM.DefaultMoveSpeedInPercent"):0);
  329. DATA.Subscribe($"{Name}.CurrentRobotCycleTime",()=>_currentCycleTimes, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  330. DATA.Subscribe($"{Name}.VacuumValue",()=>_vacuumValue, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  331. DATA.Subscribe($"{Name}.DoorUnlock", () => _doorUnlock, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  332. DATA.Subscribe($"{Name}.DoorLocked", () => _doorLocked, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  333. _robotWatch.Restart();
  334. return true;
  335. }
  336. /// <summary>
  337. /// 订阅IO变量
  338. /// </summary>
  339. /// <param name="variable"></param>
  340. private void BeckhoffIoSubscribeUpdateVariable(string variable)
  341. {
  342. _variableInitializeDic[variable] = false;
  343. IOModuleManager.Instance.SubscribeModuleVariable($"{Module}", variable, UpdateVariableValue);
  344. }
  345. /// 更新变量数值
  346. /// </summary>
  347. /// <param name="variable"></param>
  348. /// <param name="value"></param>
  349. private void UpdateVariableValue(string variable, object value)
  350. {
  351. _variableInitializeDic[variable] = true;
  352. if (variable == VACUUM_VALUE)
  353. {
  354. _vacuumValue = (double)value;
  355. }
  356. else if (variable == DOOR_UNLOCK)
  357. {
  358. _doorUnlock = (bool)value;
  359. }
  360. else if (variable == DOOR_LOCKED)
  361. {
  362. _doorLocked = (bool)value;
  363. }
  364. }
  365. private void InitFsmMap()
  366. {
  367. fsm = new StateMachine<EfemEntity>("EFEM", (int)STATE.Unknown, 50);
  368. fsm.EnableRepeatedMsg(true);
  369. AnyStateTransition(FSM_MSG.TIMER, fnMonitor, FSM_STATE.SAME);
  370. AnyStateTransition(MSG.TurnOffBuzzer, fnTurnOffBuzzer, FSM_STATE.SAME);
  371. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  372. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  373. AnyStateTransition(MSG.Abort, fnAbortRobot, FSM_STATE.SAME);
  374. AnyStateTransition(MSG.ToInit, fnToInit, STATE.Init);
  375. AnyStateTransition(MSG.BackroundCmd, fnBackroundCommand, FSM_STATE.SAME);
  376. AnyStateTransition(MSG.CloseBuzzer, fnCloseBuzzer, FSM_STATE.SAME);
  377. AnyStateTransition(MSG.CloseBuzzer, fnCloseBuzzer, FSM_STATE.SAME);
  378. Transition(STATE.Unknown,MSG.CommReady, fnCommReady, STATE.Init);
  379. //Error
  380. Transition(STATE.Error, MSG.Recover, fnRecover, STATE.Idle);
  381. // Home
  382. Transition(STATE.Unknown, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  383. Transition(STATE.Init, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  384. Transition(STATE.Idle, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  385. Transition(STATE.Error, MSG.HomeAll, fnHomeAll, STATE.Initializing);
  386. Transition(STATE.Initializing, FSM_MSG.TIMER, fnHomingTimeout, STATE.Idle);
  387. // Home Robot
  388. Transition(STATE.Error, MSG.HomeRB, fnHomeRobot, STATE.InitingRB);
  389. Transition(STATE.Idle, MSG.HomeRB, fnHomeRobot, STATE.InitingRB);
  390. Transition(STATE.InitingRB, FSM_MSG.TIMER, fnHomingRobotTimeout, STATE.Idle);
  391. // Home Aligner
  392. Transition(STATE.Idle, MSG.HomeAL, fnHomeAligner, STATE.InitingAL);
  393. Transition(STATE.InitingAL, FSM_MSG.TIMER, fnHomingAlignTimeout, STATE.Idle);
  394. Transition(STATE.Error, MSG.HomeAL, fnHomeAligner, STATE.ErrorInitingAL);
  395. Transition(STATE.ErrorInitingAL, FSM_MSG.TIMER, fnHomingAlignTimeout, STATE.Error);
  396. // Pick wafer
  397. Transition(STATE.Idle, MSG.Pick, FnStartPick, STATE.Picking);
  398. Transition(STATE.Picking, FSM_MSG.TIMER, FnPickTimeout, STATE.Idle);
  399. Transition(STATE.Picking, MSG.Abort, FnAbortPick, STATE.Idle);
  400. //Robot Cycle
  401. Transition(STATE.Idle, MSG.RobotCycle, FnStartRobotCycle, STATE.RobotCycleing);
  402. Transition(STATE.RobotCycleing, FSM_MSG.TIMER, FnRobotCycleTimeout, STATE.Idle);
  403. Transition(STATE.RobotCycleing, MSG.Abort, FnAbortRobotCycle, STATE.Idle);
  404. Transition(STATE.Error, MSG.Pick, FnErrorStartPick, STATE.ErrorPicking);
  405. Transition(STATE.ErrorPicking, FSM_MSG.TIMER, FnErrorPickTimeout, STATE.Error);
  406. Transition(STATE.ErrorPicking, MSG.Abort, FnErrorAbortPick, STATE.Error);
  407. // Place wafer
  408. Transition(STATE.Idle, MSG.Place, FnStartPlace, STATE.Placing);
  409. Transition(STATE.Placing, FSM_MSG.TIMER, FnPlaceTimeout, STATE.Idle);
  410. Transition(STATE.Placing, MSG.Abort, FnAbortPlace, STATE.Idle);
  411. Transition(STATE.Error, MSG.Place, FnErrorStartPlace, STATE.ErrorPlacing);
  412. Transition(STATE.ErrorPlacing, FSM_MSG.TIMER, FnErrorPlaceTimeout, STATE.Error);
  413. Transition(STATE.ErrorPlacing, MSG.Abort, FnErrorAbortPlace, STATE.Error);
  414. // Swap wafer with LL sequence
  415. Transition(STATE.Idle, MSG.Swap, FnStartSwap, STATE.Swapping);
  416. Transition(STATE.Swapping, FSM_MSG.TIMER, FnSwapTimeout, STATE.Idle);
  417. Transition(STATE.Swapping, MSG.Abort, FnAbortSwap, STATE.Idle);
  418. // Goto
  419. Transition(STATE.Idle, MSG.Goto, fnGoto, STATE.Gotoing);
  420. Transition(STATE.Gotoing, MSG.ActionDone, fnActionDone, STATE.Idle);
  421. // Map
  422. Transition(STATE.Idle, MSG.Map, fnMap, STATE.Mapping);
  423. Transition(STATE.Mapping, MSG.ActionDone, fnActionDone, STATE.Idle);
  424. // Grip
  425. Transition(STATE.Idle, MSG.Grip, fnGrip, STATE.Gripping);
  426. Transition(STATE.Gripping, MSG.ActionDone, fnActionDone, STATE.Idle);
  427. // Ungrip
  428. Transition(STATE.Idle, MSG.Ungrip, fnUngrip, STATE.Ungripping);
  429. Transition(STATE.Ungripping, MSG.ActionDone, fnActionDone, STATE.Idle);
  430. Transition(STATE.Idle, MSG.Lift, fnLift, STATE.Lifting);
  431. //Transition(STATE.Lifting, MSG.LiftActionDone, fnActionDone, STATE.Idle);
  432. Transition(STATE.Lifting, FSM_MSG.TIMER, fnLiftTimeout, STATE.Idle);
  433. Transition(STATE.Idle, MSG.Align, fnAlign, STATE.Aligning);
  434. Transition(STATE.Aligning, FSM_MSG.TIMER, fnAlignTimeout, STATE.Idle);
  435. Transition(STATE.Aligning, MSG.ActionDone, fnActionDone, STATE.Idle);
  436. //MapDummy
  437. Transition(STATE.Idle, MSG.MapDummy, MapDummy, STATE.Mapping);
  438. Transition(STATE.Mapping, FSM_MSG.TIMER, MapDummyTimeout, STATE.Idle);
  439. //Resume
  440. Transition(STATE.Idle, MSG.MapDummy, MapDummy, STATE.Mapping);
  441. //Vacuum
  442. //Transition(STATE.Init, MSG.Vacuum, VacuumAction, STATE.Vacuuming);
  443. //Transition(STATE.Idle, MSG.Vacuum, VacuumAction, STATE.Vacuuming);
  444. //Transition(STATE.Error, MSG.Vacuum, VacuumAction, STATE.Vacuuming);
  445. //Transition(STATE.Vacuuming, FSM_MSG.TIMER, VacuumActionTimeout, STATE.Idle);
  446. //Retry
  447. Transition(STATE.Error, MSG.Retry, NullFunc, STATE.Retrying);
  448. Transition(STATE.Retrying, FSM_MSG.TIMER, EfemRetry, STATE.Retrying);
  449. Transition(STATE.Retrying, MSG.Align, RetryAlign, STATE.Aligning);
  450. Transition(STATE.Retrying, MSG.Pick, RetryPick, STATE.Picking);
  451. Transition(STATE.Retrying, MSG.Place, RetryPlace, STATE.Placing);
  452. //ConfirmComplete
  453. Transition(STATE.Init, MSG.ConfirmComplete, ClearModuleAlarm, STATE.Init);
  454. Transition(STATE.Idle, MSG.ConfirmComplete, ClearModuleAlarm, STATE.Idle);
  455. Transition(STATE.Error, MSG.ConfirmComplete, NullFunc, STATE.ConfirmCompleting);
  456. Transition(STATE.ConfirmCompleting, FSM_MSG.TIMER, ConfirmComplete, STATE.ConfirmCompleting);
  457. Transition(STATE.ConfirmCompleting, MSG.Align, ConfirmAlign, STATE.Idle);
  458. Transition(STATE.ConfirmCompleting, MSG.Pick, ConfirmPickup, STATE.Idle);
  459. Transition(STATE.ConfirmCompleting, MSG.Place, ConfirmPlace, STATE.Idle);
  460. EnumLoop<STATE>.ForEach((item) => { fsm.MapState((int)item, item.ToString()); });
  461. EnumLoop<MSG>.ForEach((item) => { fsm.MapMessage((int)item, item.ToString()); });
  462. Running = true;
  463. }
  464. private bool fnCommReady(object[] param)
  465. {
  466. return true;
  467. }
  468. private bool fnCloseBuzzer(object[] param)
  469. {
  470. return _efem.CloseBuzzer();
  471. }
  472. /// <summary>
  473. ///
  474. /// </summary>
  475. /// <param name="param"></param>
  476. /// <returns></returns>
  477. private bool EnterPause(object[] param)
  478. {
  479. return true;
  480. }
  481. private bool EnterResume(object[] param)
  482. {
  483. return true;
  484. }
  485. private bool fnResumeTimeout(object[] param)
  486. {
  487. _currentRoutine = _efemSafeCloseRoutine;
  488. RState ret = _efemSafeCloseRoutine.Monitor();
  489. if (ret == RState.Failed || ret == RState.Timeout)
  490. {
  491. _currentRoutine = null;
  492. PostMsg(MSG.Error);
  493. return false;
  494. }
  495. bool result = ret == RState.End;
  496. if (result)
  497. {
  498. _currentRoutine = null;
  499. }
  500. return result;
  501. }
  502. private bool fnHomeAll(object[] param)
  503. {
  504. _isHomed = false;
  505. return _homeRoutine.Start(ModuleName.EFEM) == RState.Running;
  506. }
  507. private bool fnHomingTimeout(object[] param)
  508. {
  509. _currentRoutine = _homeRoutine;
  510. RState ret = _homeRoutine.Monitor();
  511. if (ret == RState.Failed || ret == RState.Timeout)
  512. {
  513. _currentRoutine = null;
  514. PostMsg(MSG.Error);
  515. return false;
  516. }
  517. bool result= ret == RState.End;
  518. if(result)
  519. {
  520. _isHomed= true;
  521. RecoverLPStatus();
  522. _currentRoutine = null;
  523. }
  524. return result;
  525. }
  526. private void RecoverLPStatus()
  527. {
  528. _lpms[0].PostMsg(LoadPortModule.MSG.EnterIdle);
  529. _lpms[1].PostMsg(LoadPortModule.MSG.EnterIdle);
  530. _lpms[2].PostMsg(LoadPortModule.MSG.EnterIdle);
  531. }
  532. private bool Resume(object[] param)
  533. {
  534. return _efem.Resume();
  535. }
  536. private bool Pause(object[] param)
  537. {
  538. return _efem.Pause();
  539. }
  540. private bool fnHomeRobot(object[] param)
  541. {
  542. return _homeRoutine.Start(ModuleName.EfemRobot) == RState.Running;
  543. }
  544. private bool fnHomingRobotTimeout(object[] param)
  545. {
  546. _currentRoutine = _homeRoutine;
  547. RState ret = _homeRoutine.Monitor();
  548. if (ret == RState.Failed || ret == RState.Timeout)
  549. {
  550. PostMsg(MSG.Error);
  551. _currentRoutine = _homeRoutine;
  552. return false;
  553. }
  554. bool result = ret == RState.End;
  555. if (result)
  556. {
  557. _currentRoutine = null;
  558. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Idle.ToString());
  559. }
  560. return result;
  561. }
  562. private bool fnHomeAligner(object[] param)
  563. {
  564. // module
  565. ModuleName unit = ModuleName.EFEM;
  566. if (param[0] is string s1)
  567. unit = ModuleNameString.ToEnum(s1);
  568. else if (param[0] is ModuleName mod)
  569. unit = mod;
  570. else
  571. throw new ArgumentException("Argument error");
  572. return _homeRoutine.Start(unit) == RState.Running;
  573. }
  574. private bool fnHomingAlignTimeout(object[] param)
  575. {
  576. _currentRoutine = _homeRoutine;
  577. RState ret = _homeRoutine.Monitor();
  578. if (ret == RState.Failed || ret == RState.Timeout)
  579. {
  580. _currentRoutine = null;
  581. PostMsg(MSG.Error);
  582. return false;
  583. }
  584. bool result = ret == RState.End;
  585. if (result)
  586. {
  587. _currentRoutine = null;
  588. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Idle.ToString());
  589. }
  590. return result;
  591. }
  592. private bool fnActionDone(object[] param)
  593. {
  594. return false;
  595. }
  596. public bool CheckToPostMessage(int msg, params object[] args)
  597. {
  598. if (!fsm.FindTransition(fsm.State, msg))
  599. {
  600. LOG.Write(eEvent.WARN_EFEM_COMMON_WARN, ModuleName.EFEM, $"{Name} is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  601. return false;
  602. }
  603. Running = true;
  604. fsm.PostMsg(msg, args);
  605. return true;
  606. }
  607. private bool fnMonitor(object[] param)
  608. {
  609. if (_isVacuume)
  610. {
  611. bool result = VacuumActionTimeout();
  612. if (result)
  613. {
  614. _isVacuume = false;
  615. }
  616. }
  617. if (_isPause)
  618. {
  619. bool result = SafeOpenActionTimeout();
  620. if (result)
  621. {
  622. _isPause = false;
  623. }
  624. }
  625. if (_isResume)
  626. {
  627. bool result = SafeCloseActionTimeout();
  628. if (result)
  629. {
  630. _isResume = false;
  631. }
  632. }
  633. // robot idle check
  634. _robotIdleTrigger.CLK = _efem.Status != RState.Running;
  635. if (_robotIdleTrigger.Q)
  636. {
  637. _robotWatch.Restart();
  638. LOG.WriteLog(eEvent.EV_EFEM_ROBOT, Module.ToString(), $"robot status changed to {_efem.Status}");
  639. }
  640. _autoMessageProcessor.Monitor();
  641. return true;
  642. }
  643. private bool fnOnline(object[] param)
  644. {
  645. bool bOnlineFlag = (bool)param[0];
  646. if (_efem is EfemBase efem)
  647. {
  648. efem.SetOnline(bOnlineFlag);
  649. }
  650. IsOnline = bOnlineFlag;
  651. return true;
  652. }
  653. private string GetFsmLastMessage()
  654. {
  655. int msg = fsm.LastMsg;
  656. if (msg >= (int)MSG.HomeAll && msg <= (int)MSG.Error)
  657. return ((MSG)msg).ToString();
  658. if (msg == (int)FSM_MSG.TIMER)
  659. return "Timer";
  660. return msg.ToString();
  661. }
  662. private bool fnError(object[] param)
  663. {
  664. _errorPreState = (STATE)fsm.State;
  665. string error = (param != null && param.Length != 0) ? param[0].ToString() : "";
  666. if (Singleton<RouteManager>.Instance.IsAutoMode)
  667. {
  668. int msg = 0;
  669. if (_errorPreState == STATE.Picking)
  670. {
  671. msg = (int)MSG.Pick;
  672. }
  673. else if (_errorPreState == STATE.Aligning)
  674. {
  675. msg = (int)MSG.Align;
  676. }
  677. else if (_errorPreState == STATE.Placing)
  678. {
  679. msg = (int)MSG.Place;
  680. }
  681. else
  682. {
  683. error = $"{error}\r\n Plase Home Robot back to idle state";
  684. return true;
  685. }
  686. AlarmList alarmList = new AlarmList(Module.ToString(), ((STATE)fsm.State).ToString(), msg,
  687. error, 0, (int)AlarmType.Error);
  688. AlarmListManager.Instance.AddAlarm(alarmList);
  689. }
  690. return true;
  691. }
  692. private bool fnToInit(object[] param)
  693. {
  694. return true;
  695. }
  696. private bool fnRecover(object[] param)
  697. {
  698. bool result=_efem.ClearError();
  699. if (result)
  700. {
  701. PostMsg(MSG.ToInit);
  702. }
  703. return result;
  704. }
  705. private bool fnAbortRobot(object[] param)
  706. {
  707. //_efem.ExecuteAction();
  708. if (_currentRoutine != null)
  709. {
  710. _currentRoutine.Abort();
  711. }
  712. _efem.Halt();
  713. return true;
  714. }
  715. private bool fnBackroundCommand(object[] param)
  716. {
  717. _autoMessageProcessor.SendAutoCommandMessage(param);
  718. return true;
  719. }
  720. private bool fnRobotPowerOn(object[] param)
  721. {
  722. return _efem.RobotPowerOn(ModuleName.EfemRobot, (bool)param[0]);
  723. }
  724. private bool fnSetLED(object[] param)
  725. {
  726. LightType light = (LightType)param[0];
  727. LightStatus st = (LightStatus)param[1];
  728. return _efem.SetLamp(light, st);
  729. }
  730. private bool fnTurnOffBuzzer(object[] param)
  731. {
  732. return false;
  733. }
  734. private bool FnStartPick(object[] param)
  735. {
  736. return _pickRoutine.Start(param) == RState.Running;
  737. }
  738. private bool FnStartRobotCycle(object[] param)
  739. {
  740. return _cycleRobotCycleRoutine.Start(param) == RState.Running;
  741. }
  742. private bool FnErrorStartPick(object[] param)
  743. {
  744. if (!_isHomed)
  745. {
  746. LOG.WriteLog(eEvent.ERR_EFEM_ROBOT, Module.ToString(), "EFEM is not homed");
  747. return false;
  748. }
  749. _efem.Reset();
  750. return _pickRoutine.Start(param) == RState.Running;
  751. }
  752. /// <summary>
  753. /// Retry Pick
  754. /// </summary>
  755. /// <param name="param"></param>
  756. /// <returns></returns>
  757. private bool RetryPick(object[] param)
  758. {
  759. int stepIndex = (int)param[0];
  760. return _pickRoutine.Retry(stepIndex) == RState.Running;
  761. }
  762. private bool FnPickTimeout(object[] param)
  763. {
  764. _currentRoutine = _pickRoutine;
  765. RState ret = _pickRoutine.Monitor();
  766. if (ret == RState.Failed || ret == RState.Timeout)
  767. {
  768. PostMsg(MSG.Error,_pickRoutine.ErrorMsg);
  769. _currentRoutine = null;
  770. return false;
  771. }
  772. if (ret == RState.End)
  773. {
  774. _currentRoutine = null;
  775. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Picking.ToString());
  776. }
  777. return ret == RState.End;
  778. }
  779. private bool FnRobotCycleTimeout(object[] param)
  780. {
  781. RState ret = _cycleRobotCycleRoutine.Monitor();
  782. if (ret == RState.Failed || ret == RState.Timeout)
  783. {
  784. PostMsg(MSG.Error);
  785. return false;
  786. }
  787. _currentCycleTimes = _cycleRobotCycleRoutine.GetCurrentCycle();
  788. return ret == RState.End;
  789. }
  790. private bool FnErrorPickTimeout(object[] param)
  791. {
  792. RState ret = _pickRoutine.Monitor();
  793. if (ret == RState.Failed || ret == RState.Timeout)
  794. {
  795. PostMsg(MSG.Error, _pickRoutine.ErrorMsg);
  796. return false;
  797. }
  798. return ret == RState.End;
  799. }
  800. /// <summary>
  801. /// 确认Pick是否完成
  802. /// </summary>
  803. /// <param name="param"></param>
  804. /// <returns></returns>
  805. private bool ConfirmPickup(object[] param)
  806. {
  807. int stepIdex = (int)param[0];
  808. bool result = _pickRoutine.CheckCompleteCondition(stepIdex);
  809. if (!result)
  810. {
  811. PostMsg(MSG.Error,_pickRoutine.ErrorMsg);
  812. }
  813. else
  814. {
  815. if (Singleton<RouteManager>.Instance.IsAutoRunning)
  816. {
  817. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Picking.ToString());
  818. }
  819. }
  820. return result;
  821. }
  822. private bool FnAbortPick(object[] param)
  823. {
  824. _pickRoutine.Abort();
  825. return true;
  826. }
  827. private bool FnAbortRobotCycle(object[] param)
  828. {
  829. _cycleRobotCycleRoutine.Abort();
  830. return true;
  831. }
  832. private bool FnErrorAbortPick(object[] param)
  833. {
  834. _pickRoutine.Abort();
  835. return true;
  836. }
  837. private bool FnStartPlace(object[] param)
  838. {
  839. return _placeRoutine.Start(param) == RState.Running;
  840. }
  841. private bool FnErrorStartPlace(object[] param)
  842. {
  843. if(!_isHomed)
  844. {
  845. LOG.WriteLog(eEvent.ERR_EFEM_ROBOT, Module.ToString(), "EFEM is not homed");
  846. return false;
  847. }
  848. _efem.Reset();
  849. return _placeRoutine.Start(param) == RState.Running;
  850. }
  851. /// <summary>
  852. /// Retry Place
  853. /// </summary>
  854. /// <param name="param"></param>
  855. /// <returns></returns>
  856. private bool RetryPlace(object[] param)
  857. {
  858. int stepIndex = (int)param[0];
  859. return _placeRoutine.Retry(stepIndex) == RState.Running;
  860. }
  861. private bool FnPlaceTimeout(object[] param)
  862. {
  863. _currentRoutine = _placeRoutine;
  864. RState ret = _placeRoutine.Monitor();
  865. if (ret == RState.Failed || ret == RState.Timeout)
  866. {
  867. _currentRoutine = null;
  868. PostMsg(MSG.Error,_placeRoutine.ErrorMsg);
  869. return false;
  870. }
  871. if (ret == RState.End)
  872. {
  873. _currentRoutine = null;
  874. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Placing.ToString());
  875. }
  876. return ret == RState.End;
  877. }
  878. private bool FnErrorPlaceTimeout(object[] param)
  879. {
  880. RState ret = _placeRoutine.Monitor();
  881. if (ret == RState.Failed || ret == RState.Timeout)
  882. {
  883. PostMsg(MSG.Error, _placeRoutine.ErrorMsg);
  884. return false;
  885. }
  886. return ret == RState.End;
  887. }
  888. /// <summary>
  889. /// 确认Place是否完成
  890. /// </summary>
  891. /// <param name="param"></param>
  892. /// <returns></returns>
  893. private bool ConfirmPlace(object[] param)
  894. {
  895. int stepIdex = (int)param[0];
  896. bool result = _placeRoutine.CheckCompleteCondition(stepIdex);
  897. if (!result)
  898. {
  899. PostMsg(MSG.Error, _placeRoutine.ErrorMsg);
  900. }
  901. else
  902. {
  903. if (Singleton<RouteManager>.Instance.IsAutoRunning)
  904. {
  905. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Picking.ToString());
  906. }
  907. }
  908. return result;
  909. }
  910. private bool FnAbortPlace(object[] param)
  911. {
  912. _placeRoutine.Abort();
  913. return true;
  914. }
  915. private bool FnErrorAbortPlace(object[] param)
  916. {
  917. _placeRoutine.Abort();
  918. return true;
  919. }
  920. private bool FnStartSwap(object[] param)
  921. {
  922. return _swapRoutine.Start(param) == RState.Running;
  923. }
  924. private bool FnSwapTimeout(object[] param)
  925. {
  926. RState ret = _swapRoutine.Monitor();
  927. if (ret == RState.Failed || ret == RState.Timeout)
  928. {
  929. PostMsg(MSG.Error);
  930. return false;
  931. }
  932. return ret == RState.End;
  933. }
  934. private bool FnAbortSwap(object[] param)
  935. {
  936. _swapRoutine.Abort();
  937. return true;
  938. }
  939. private bool fnGoto(object[] param)
  940. {
  941. // module
  942. ModuleName unit = ModuleName.EFEM;
  943. if (param[0] is string s1)
  944. unit = ModuleNameString.ToEnum(s1);
  945. else if (param[0] is ModuleName mod)
  946. unit = mod;
  947. else
  948. throw new ArgumentException("Argument error");
  949. _efem.Goto(unit, Hand.Blade1);
  950. return true;
  951. }
  952. private bool fnLift(object[] param)
  953. {
  954. // module
  955. ModuleName unit = ModuleName.EFEM;
  956. if (param[0] is string s1)
  957. unit = ModuleNameString.ToEnum(s1);
  958. else if (param[0] is ModuleName mod)
  959. unit = mod;
  960. else
  961. throw new ArgumentException("Argument error");
  962. bool isUp = true;
  963. if (param.Length > 1)
  964. {
  965. isUp = (bool) param[1];
  966. }
  967. if (isUp)
  968. {
  969. if (!_efem.SetPinUp(unit))
  970. return false;
  971. }
  972. else
  973. {
  974. if (!_efem.SetPinDown(unit))
  975. return false;
  976. }
  977. LiftMessage = isUp ? "Up" : "Down";
  978. return true;
  979. }
  980. private bool fnLiftTimeout(object[] param)
  981. {
  982. if (LiftMessage == "Up")
  983. {
  984. return _efem.LiftIsDown == false && _efem.LiftIsUp == true;
  985. }
  986. else if (LiftMessage == "Down")
  987. {
  988. return _efem.LiftIsDown == true && _efem.LiftIsUp == false;
  989. }
  990. return false;
  991. }
  992. private bool fnAlign(object[] param)
  993. {
  994. return _alignRoutine.Start(param) == RState.Running;
  995. }
  996. private bool fnAlignTimeout(object[] param)
  997. {
  998. _currentRoutine = _alignRoutine;
  999. RState ret = _alignRoutine.Monitor();
  1000. if (ret == RState.Failed || ret == RState.Timeout)
  1001. {
  1002. _currentRoutine = null;
  1003. PostMsg(MSG.Error,_alignRoutine.ErrorMsg);
  1004. return false;
  1005. }
  1006. if (ret == RState.End)
  1007. {
  1008. _currentRoutine = null;
  1009. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Aligning.ToString());
  1010. }
  1011. return ret == RState.End;
  1012. }
  1013. /// <summary>
  1014. /// Retry Align
  1015. /// </summary>
  1016. /// <param name="param"></param>
  1017. /// <returns></returns>
  1018. private bool RetryAlign(object[] param)
  1019. {
  1020. int stepIndex = (int)param[0];
  1021. return _alignRoutine.Retry(stepIndex) == RState.Running;
  1022. }
  1023. /// <summary>
  1024. /// 确认Align是否完成
  1025. /// </summary>
  1026. /// <param name="param"></param>
  1027. /// <returns></returns>
  1028. private bool ConfirmAlign(object[] param)
  1029. {
  1030. int stepIdex = (int)param[0];
  1031. bool result = _alignRoutine.CheckCompleteCondition(stepIdex);
  1032. if (!result)
  1033. {
  1034. PostMsg(MSG.Error, _alignRoutine.ErrorMsg);
  1035. }
  1036. else
  1037. {
  1038. if (Singleton<RouteManager>.Instance.IsAutoRunning)
  1039. {
  1040. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), STATE.Aligning.ToString());
  1041. }
  1042. }
  1043. return result;
  1044. }
  1045. private bool MapDummyTimeout(object[] param)
  1046. {
  1047. RState ret = _mapDummyRoutine.Monitor();
  1048. if (ret == RState.Failed || ret == RState.Timeout)
  1049. {
  1050. PostMsg(MSG.Error);
  1051. return false;
  1052. }
  1053. return ret == RState.End;
  1054. }
  1055. private bool VacuumActionTimeout()
  1056. {
  1057. RState ret = _vacuumRoutine.Monitor();
  1058. if (ret == RState.Failed || ret == RState.Timeout)
  1059. {
  1060. LOG.Write(eEvent.ERR_EFEM_COMMON_FAILED, ModuleName.EFEM, "Vacuum");
  1061. return false;
  1062. }
  1063. return ret == RState.End;
  1064. }
  1065. private bool SafeOpenActionTimeout()
  1066. {
  1067. RState ret = _efemSafeOpenRoutine.Monitor();
  1068. if (ret == RState.Failed || ret == RState.Timeout)
  1069. {
  1070. LOG.Write(eEvent.ERR_EFEM_COMMON_FAILED, ModuleName.EFEM, "SafeOpen Failed");
  1071. return false;
  1072. }
  1073. return ret == RState.End;
  1074. }
  1075. private bool SafeCloseActionTimeout()
  1076. {
  1077. RState ret = _efemSafeCloseRoutine.Monitor();
  1078. if (ret == RState.Failed || ret == RState.Timeout)
  1079. {
  1080. LOG.Write(eEvent.ERR_EFEM_COMMON_FAILED, ModuleName.EFEM, "SafeClose Failed");
  1081. return false;
  1082. }
  1083. return ret == RState.End;
  1084. }
  1085. private bool fnMap(object[] param)
  1086. {
  1087. // module
  1088. ModuleName unit = ModuleName.EFEM;
  1089. if (param[0] is string s1)
  1090. unit = ModuleNameString.ToEnum(s1);
  1091. else if (param[0] is ModuleName mod)
  1092. unit = mod;
  1093. else
  1094. throw new ArgumentException("Argument error");
  1095. if (!_efem.Map(unit))
  1096. return false;
  1097. return true;
  1098. }
  1099. private bool MapDummy(object[] param)
  1100. {
  1101. return _mapDummyRoutine.Start()==RState.Running;
  1102. }
  1103. private bool VacuumAction(object[] param)
  1104. {
  1105. ModuleName vacuumModule = (ModuleName)param[0];
  1106. bool vacuum=(bool)param[1];
  1107. bool result = _vacuumRoutine.Start(vacuumModule, vacuum) == RState.Running;
  1108. if (result)
  1109. {
  1110. _isVacuume = true;
  1111. }
  1112. return result;
  1113. }
  1114. private bool SafeOpenAction(object[] param)
  1115. {
  1116. bool result = _efemSafeOpenRoutine.Start() == RState.Running;
  1117. if (result)
  1118. {
  1119. _isPause = true;
  1120. }
  1121. return result;
  1122. }
  1123. private bool SafeCloseAction(object[] param)
  1124. {
  1125. bool result = _efemSafeCloseRoutine.Start() == RState.Running;
  1126. if (result)
  1127. {
  1128. _isResume = true;
  1129. }
  1130. return result;
  1131. }
  1132. public bool DoorUnlock(object[] param)
  1133. {
  1134. bool unlock = (bool)param[0];
  1135. string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{DOOR_UNLOCK}");
  1136. if (!string.IsNullOrEmpty(ioName))
  1137. {
  1138. return IOModuleManager.Instance.WriteIoValue(ioName, unlock);
  1139. }
  1140. return true;
  1141. }
  1142. private bool fnGrip(object[] param)
  1143. {
  1144. Hand arm = (Hand)Enum.Parse(typeof(Hand), (string)param[0]);
  1145. if (!_efem.Grip(arm, true))
  1146. return false;
  1147. return true;
  1148. }
  1149. public int SendEfemAutoCommand(params object[] args)
  1150. {
  1151. if (CheckToPostMessage((int)MSG.BackroundCmd, args))
  1152. return (int)MSG.BackroundCmd;
  1153. return (int)FSM_MSG.NONE;
  1154. }
  1155. private bool fnUngrip(object[] param)
  1156. {
  1157. Hand arm = (Hand)Enum.Parse(typeof(Hand), (string)param[0]);
  1158. if (!_efem.Grip(arm, false))
  1159. return false;
  1160. return true;
  1161. }
  1162. #region EfemRetry
  1163. /// <summary>
  1164. /// Retry
  1165. /// </summary>
  1166. /// <param name="param"></param>
  1167. /// <returns></returns>
  1168. private bool EfemRetry(object[] param)
  1169. {
  1170. AlarmList alarmList = AlarmListManager.Instance.GetAlarmListByModule(Module.ToString());
  1171. if (alarmList != null)
  1172. {
  1173. CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_ROBOT, Module.ToString(), alarmList.ModuleCmd,
  1174. alarmList.ModuleStep);
  1175. }
  1176. return false;
  1177. }
  1178. #endregion
  1179. #region ConfirmComplete
  1180. /// <summary>
  1181. /// 确认是否完成
  1182. /// </summary>
  1183. /// <param name="param"></param>
  1184. /// <returns></returns>
  1185. private bool ConfirmComplete(object[] param)
  1186. {
  1187. AlarmList alarmList = AlarmListManager.Instance.GetAlarmListByModule(Module.ToString());
  1188. if (alarmList != null)
  1189. {
  1190. if (alarmList.ModuleState == STATE.Picking.ToString())
  1191. {
  1192. CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_ROBOT, Module.ToString(), (int)MSG.Pick, alarmList.ModuleStep);
  1193. }
  1194. else if (alarmList.ModuleState == STATE.Placing.ToString())
  1195. {
  1196. CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_ROBOT, Module.ToString(), (int)MSG.Place, alarmList.ModuleStep);
  1197. }
  1198. else if (alarmList.ModuleState == STATE.Aligning.ToString())
  1199. {
  1200. CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_ROBOT, Module.ToString(), (int)MSG.Align, alarmList.ModuleStep);
  1201. }
  1202. else
  1203. {
  1204. PostMsg(PUFSTATE.Error);
  1205. }
  1206. }
  1207. return false;
  1208. }
  1209. /// <summary>
  1210. /// 清除报警
  1211. /// </summary>
  1212. /// <param name="param"></param>
  1213. /// <returns></returns>
  1214. private bool ClearModuleAlarm(object[] param)
  1215. {
  1216. AlarmList alarmList = AlarmListManager.Instance.GetAlarmListByModule(Module.ToString());
  1217. if (alarmList != null)
  1218. {
  1219. AlarmListManager.Instance.CheckModuleAlamAndRemove(Module.ToString(), "");
  1220. }
  1221. return true;
  1222. }
  1223. #endregion
  1224. public int Invoke(string function, params object[] args)
  1225. {
  1226. switch (function)
  1227. {
  1228. case "HomeAll":
  1229. if (IsIdle)
  1230. {
  1231. return (int)FSM_MSG.NONE;
  1232. }
  1233. if (CheckToPostMessage((int)MSG.HomeAll))
  1234. {
  1235. return (int)MSG.HomeAll;
  1236. }
  1237. else
  1238. {
  1239. return (int)FSM_MSG.NONE;
  1240. }
  1241. case "Retry":
  1242. if (CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_ROBOT, Module.ToString(), (int)MSG.Retry, args))
  1243. {
  1244. return (int)MSG.Retry;
  1245. }
  1246. else
  1247. {
  1248. return (int)FSM_MSG.NONE;
  1249. }
  1250. case "ConfirmComplete":
  1251. if (CheckToPostMessage<STATE, MSG>(eEvent.ERR_EFEM_ROBOT, Module.ToString(), (int)MSG.ConfirmComplete, args))
  1252. {
  1253. return (int)MSG.ConfirmComplete;
  1254. }
  1255. else
  1256. {
  1257. return (int)FSM_MSG.NONE;
  1258. }
  1259. }
  1260. return (int)FSM_MSG.NONE;
  1261. }
  1262. public bool CheckAcked(int msg)
  1263. {
  1264. return fsm.CheckExecuted(msg);
  1265. }
  1266. internal void InvokeReset()
  1267. {
  1268. if (fsm.State == (int) STATE.Error)
  1269. {
  1270. PostMsg((int)MSG.Recover);
  1271. }
  1272. }
  1273. public int InvokeAlign(string module, int reserv, float time)
  1274. {
  1275. if (CheckToPostMessage((int)MSG.Align, module, reserv, time))
  1276. return (int)MSG.Align;
  1277. return (int)FSM_MSG.NONE;
  1278. }
  1279. public int InvokeLiftDown(string module)
  1280. {
  1281. if (CheckToPostMessage((int)MSG.Lift, module, false))
  1282. return (int)MSG.Lift;
  1283. return (int)FSM_MSG.NONE;
  1284. }
  1285. public int InvokePick(ModuleName source, int slot, Hand hand, WaferSize size)
  1286. {
  1287. if (CheckToPostMessage((int)MSG.Pick, source, slot, hand, size))
  1288. return (int)MSG.Pick;
  1289. return (int)FSM_MSG.NONE;
  1290. }
  1291. public int InvokeGoto(ModuleName source, int slot)
  1292. {
  1293. if (CheckToPostMessage((int)MSG.Goto, source, slot))
  1294. return (int)MSG.Goto;
  1295. return (int)FSM_MSG.NONE;
  1296. }
  1297. public int InvokePlace(ModuleName target, int slot, Hand hand, WaferSize size)
  1298. {
  1299. if (CheckToPostMessage((int)MSG.Place, target, slot, hand, size))
  1300. return (int)MSG.Place;
  1301. return (int)FSM_MSG.NONE;
  1302. }
  1303. public int InvokePickAndPlace(ModuleName targetModule, Hand pickHand, int pickSlot, Hand placeHand, int placeSlot, WaferSize size)
  1304. {
  1305. if (CheckToPostMessage((int)MSG.Swap, targetModule, pickSlot, pickHand, placeHand, placeSlot, size))
  1306. return (int)MSG.Swap;
  1307. return (int)FSM_MSG.NONE;
  1308. }
  1309. public int InvokeMap(string target )
  1310. {
  1311. if (CheckToPostMessage((int)MSG.Map, target ))
  1312. return (int)MSG.Map;
  1313. return (int)FSM_MSG.NONE;
  1314. }
  1315. public int InvokeFlip(Hand hand)
  1316. {
  1317. if (CheckToPostMessage((int)MSG.Flip, hand))
  1318. return (int)MSG.Flip;
  1319. return (int)FSM_MSG.NONE;
  1320. }
  1321. public bool IsPrepareTransferReady(ModuleName module, EnumTransferType type, int slot)
  1322. {
  1323. //if (type == EnumTransferType.Pick)
  1324. //{
  1325. // //需要补充:判断LP 放好了,而且已经map过。
  1326. // return _efem[module].HasCassette && _efem[module].IsMapped;
  1327. //}
  1328. //else if (type == EnumTransferType.Place)
  1329. //{
  1330. // //需要补充:判断LP 放好了,而且已经map过。
  1331. // return _efem[module].HasCassette && _efem[module].IsMapped;
  1332. //}
  1333. return false;
  1334. }
  1335. internal bool CheckReadyRunNewJob(ModuleName module)
  1336. {
  1337. //???
  1338. return true;
  1339. }
  1340. internal bool CheckReadyTransfer(ModuleName module)
  1341. {
  1342. //return _efem[module].HasCassette && _efem[module].IsMapped;
  1343. return true;
  1344. }
  1345. internal bool CheckPlaced(ModuleName module)
  1346. {
  1347. //return _efem[module].HasCassette;
  1348. return true;
  1349. }
  1350. internal void NoteJobStart(ModuleName module)
  1351. {
  1352. //_efem[module].NoteJobStart();
  1353. }
  1354. internal void NoteJobComplete(ModuleName module)
  1355. {
  1356. //_efem[module].NoteJobComplete();
  1357. }
  1358. }
  1359. }