EfemEntity.cs 55 KB

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