SETMEntity.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Fsm;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Sorter.Common;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.SubstrateTrackings;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Venus_Core;
  16. using Venus_RT.Devices;
  17. using Venus_RT.Devices.PreAligner;
  18. using Venus_RT.Devices.VCE;
  19. using Venus_RT.Modules.PMs;
  20. namespace Venus_RT.Modules.TM.VenusEntity
  21. {
  22. public class SETMEntity : Entity, IModuleEntity
  23. {
  24. public enum STATE
  25. {
  26. Unknown,
  27. Init,
  28. Initializing,
  29. InitializingRB,
  30. Idle,
  31. Error,
  32. Pumping,
  33. Venting,
  34. Purging,
  35. Leakchecking,
  36. Picking,
  37. Placing,
  38. Swaping,
  39. PMPicking,
  40. PMPlacing,
  41. PMSwaping,
  42. Aligning,
  43. Mapping,
  44. Extending,
  45. Retracting,
  46. Swapping,
  47. Gotoing,
  48. ControllingPressure
  49. }
  50. public enum MSG
  51. {
  52. Home,
  53. RobotHome,
  54. Online,
  55. Offline,
  56. Pump,
  57. Vent,
  58. Purge,
  59. CyclePurge,
  60. LeakCheck,
  61. Pick,
  62. Place,
  63. Swap,
  64. DoublePick,
  65. DoublePlace,
  66. DoubleSwap,
  67. PMPick,
  68. PMPlace,
  69. PMSwap,
  70. Extend,
  71. Retract,
  72. TMCycle,
  73. ControlPressure,
  74. Error,
  75. Abort,
  76. AbortControlPressure
  77. }
  78. #region 公开变量
  79. public bool IsIdle
  80. {
  81. get { return fsm.State == (int)STATE.Idle; }
  82. }
  83. public bool IsError
  84. {
  85. get { return fsm.State == (int)STATE.Error; }
  86. }
  87. public bool IsInit
  88. {
  89. get { return fsm.State == (int)STATE.Unknown || fsm.State == (int)STATE.Init; }
  90. }
  91. public bool IsBusy
  92. {
  93. get { return !IsInit && !IsError && !IsIdle; }
  94. }
  95. public bool VCEIsATM => true;
  96. public bool TMIsATM => true;
  97. public bool IsPMASlitDoorClosed => _tm.PMASlitDoorClosed;
  98. public bool IsPMBSlitDoorClosed => _tm.PMBSlitDoorClosed;
  99. public bool IsPMCSlitDoorClosed => _tm.PMCSlitDoorClosed;
  100. public RState RobotStatus
  101. {
  102. get
  103. {
  104. if (_robot.Status != RState.Running)
  105. {
  106. if (_robotWatch.ElapsedMilliseconds < 100)
  107. return RState.Running;
  108. else
  109. return _robot.Status;
  110. }
  111. else
  112. return RState.Running;
  113. }
  114. }
  115. public bool IsOnline { get; internal set; }
  116. //public bool IsTMVac => _tm.IsTMVac;
  117. //public bool IsTMATM => _tm.IsTMATM;
  118. #endregion
  119. #region 私有变量
  120. private readonly HongHuTM _tm;
  121. private readonly ITransferRobot _robot;
  122. private readonly IPreAlign _vpa;
  123. private readonly SEMFHomeRoutine _homeRoutine;
  124. private readonly SEMFPickRoutine _pickRoutine;
  125. private readonly SEMFPlaceRoutine _placeRoutine;
  126. private readonly SEMFVentRoutine _ventRoutine;
  127. private readonly SEMFPumpRoutine _pumpRoutine;
  128. private readonly SEMFPMPickRoutine _pickpmRoutine;
  129. private readonly SEMFPMPlaceRoutine _placepmRoutine;
  130. //private readonly
  131. private readonly Stopwatch _robotWatch = new Stopwatch();
  132. #endregion
  133. public SETMEntity()
  134. {
  135. _tm = DEVICE.GetDevice<HongHuTM>("SETM");
  136. if(ModuleHelper.IsInstalled(ModuleName.TMRobot))
  137. _robot = new HongHuVR();
  138. _vpa = new HongHuVPA(ModuleName.VPA);
  139. _robotWatch = new Stopwatch();
  140. _homeRoutine = new SEMFHomeRoutine(_tm,_robot, _vpa);
  141. _pickRoutine = new SEMFPickRoutine(_tm,_robot, _vpa);
  142. _placeRoutine = new SEMFPlaceRoutine(_tm, _robot, _vpa);
  143. _pumpRoutine = new SEMFPumpRoutine(_tm, ModuleName.SETM);
  144. _ventRoutine = new SEMFVentRoutine(_tm, ModuleName.SETM);
  145. _pickpmRoutine = new SEMFPMPickRoutine(_tm, _robot);
  146. _placepmRoutine = new SEMFPMPlaceRoutine(_tm, _robot);
  147. InitFsmMap();
  148. }
  149. protected override bool Init()
  150. {
  151. OP.Subscribe("SETM.Home", (cmd, args) => { PostMsg(MSG.Home); return true; });
  152. OP.Subscribe("SETM.Pick", (cmd, args) => { PostMsg(MSG.Pick, args[0], args[1], Hand.Blade1); return true; });
  153. OP.Subscribe("SETM.Place", (cmd, args) => { PostMsg(MSG.Place, args[0], args[1], Hand.Blade1); return true; });
  154. OP.Subscribe("SETM.PumpDown", (cmd, args) => { PostMsg(MSG.Pump); return true; });
  155. OP.Subscribe("SETM.Vent", (cmd, args) => { PostMsg(MSG.Vent); return true; });
  156. return true;
  157. }
  158. private void InitFsmMap()
  159. {
  160. fsm = new StateMachine<SETMEntity>("SETM", (int)STATE.Init, 50);
  161. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  162. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  163. AnyStateTransition(MSG.Offline, fnOffline, FSM_STATE.SAME);
  164. AnyStateTransition(MSG.Home, fnHome, STATE.Initializing);
  165. //Home
  166. Transition(STATE.Initializing, FSM_MSG.TIMER, fnHomeTimeout, STATE.Idle);
  167. Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  168. //Pick
  169. Transition(STATE.Idle, MSG.Pick, fnStartPick, STATE.Picking);
  170. Transition(STATE.Picking, FSM_MSG.TIMER, fnPickTimeout, STATE.Idle);
  171. Transition(STATE.Picking, MSG.Abort, fnAbortPick, STATE.Idle);
  172. //Place
  173. Transition(STATE.Idle, MSG.Place, fnStartPlace, STATE.Placing);
  174. Transition(STATE.Placing, FSM_MSG.TIMER, fnPlaceTimeout, STATE.Idle);
  175. Transition(STATE.Placing, MSG.Abort, fnAbortPlace, STATE.Idle);
  176. //Pump
  177. Transition(STATE.Idle, MSG.Pump, fnStartPump, STATE.Pumping);
  178. Transition(STATE.Pumping, FSM_MSG.TIMER, fnPumpTimeout, STATE.Idle);
  179. Transition(STATE.Pumping, MSG.Abort, fnAbortPump, STATE.Idle);
  180. //Vent
  181. Transition(STATE.Idle, MSG.Vent, fnStartVent, STATE.Venting);
  182. Transition(STATE.Venting, FSM_MSG.TIMER, fnVentTimeout, STATE.Idle);
  183. Transition(STATE.Venting, MSG.Abort, fnAbortVent, STATE.Idle);
  184. //PMPick
  185. Transition(STATE.Idle, MSG.PMPick, fnStartPMPick, STATE.PMPicking);
  186. Transition(STATE.PMPicking, FSM_MSG.TIMER, fnPMPickTimeout, STATE.Idle);
  187. Transition(STATE.PMPicking, MSG.Abort, fnAbortPMPick, STATE.Idle);
  188. //PMPlace
  189. Transition(STATE.Idle, MSG.PMPlace, fnStartPMPlace, STATE.PMPlacing);
  190. Transition(STATE.PMPlacing, FSM_MSG.TIMER, fnPMPlaceTimeout, STATE.Idle);
  191. Transition(STATE.PMPlacing, MSG.Abort, fnAbortPMPlace, STATE.Idle);
  192. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  193. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  194. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  195. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  196. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  197. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  198. Running = true;
  199. }
  200. private bool fnAbortPMPlace(object[] param)
  201. {
  202. _placepmRoutine.Abort();
  203. return true;
  204. }
  205. private bool fnPMPlaceTimeout(object[] param)
  206. {
  207. RState ret = _placepmRoutine.Monitor();
  208. if (ret == RState.Failed || ret == RState.Timeout)
  209. {
  210. PostMsg(MSG.Error);
  211. return false;
  212. }
  213. return ret == RState.End;
  214. }
  215. private bool fnStartPMPlace(object[] param)
  216. {
  217. return _placepmRoutine.Start(param) == RState.Running;
  218. }
  219. private bool fnAbortPMPick(object[] param)
  220. {
  221. _pickpmRoutine.Abort();
  222. return true;
  223. }
  224. private bool fnPMPickTimeout(object[] param)
  225. {
  226. RState ret = _pickpmRoutine.Monitor();
  227. if (ret == RState.Failed || ret == RState.Timeout)
  228. {
  229. PostMsg(MSG.Error);
  230. return false;
  231. }
  232. return ret == RState.End;
  233. }
  234. private bool fnStartPMPick(object[] param)
  235. {
  236. return _pickpmRoutine.Start(param) == RState.Running;
  237. }
  238. private bool fnAbortVent(object[] param)
  239. {
  240. _ventRoutine.Abort();
  241. return true;
  242. }
  243. private bool fnVentTimeout(object[] param)
  244. {
  245. RState ret = _ventRoutine.Monitor();
  246. if (ret == RState.Failed || ret == RState.Timeout)
  247. {
  248. PostMsg(MSG.Error);
  249. return false;
  250. }
  251. return ret == RState.End;
  252. }
  253. private bool fnStartVent(object[] param)
  254. {
  255. return _ventRoutine.Start(param) == RState.Running;
  256. }
  257. private bool fnAbortPump(object[] param)
  258. {
  259. _pumpRoutine.Abort();
  260. return true;
  261. }
  262. private bool fnPumpTimeout(object[] param)
  263. {
  264. RState ret = _pumpRoutine.Monitor();
  265. if (ret == RState.Failed || ret == RState.Timeout)
  266. {
  267. PostMsg(MSG.Error);
  268. return false;
  269. }
  270. return ret == RState.End;
  271. }
  272. private bool fnStartPump(object[] param)
  273. {
  274. return _pumpRoutine.Start(param) == RState.Running;
  275. }
  276. private bool fnAbortPlace(object[] param)
  277. {
  278. return true;
  279. }
  280. private bool fnPlaceTimeout(object[] param)
  281. {
  282. RState ret = _placeRoutine.Monitor();
  283. if (ret == RState.Failed || ret == RState.Timeout)
  284. {
  285. PostMsg(MSG.Error);
  286. return false;
  287. }
  288. return ret == RState.End;
  289. }
  290. private bool fnStartPlace(object[] param)
  291. {
  292. return _placeRoutine.Start(param) == RState.Running;
  293. }
  294. private bool fnAbortPick(object[] param)
  295. {
  296. _pickRoutine.Abort();
  297. return true;
  298. }
  299. private bool fnStartPick(object[] param)
  300. {
  301. return _pickRoutine.Start(param) == RState.Running;
  302. }
  303. private bool fnPickTimeout(object[] param)
  304. {
  305. RState ret = _pickRoutine.Monitor();
  306. if (ret == RState.Failed || ret == RState.Timeout)
  307. {
  308. PostMsg(MSG.Error);
  309. return false;
  310. }
  311. return ret == RState.End;
  312. }
  313. private bool fnAbortHome(object[] param)
  314. {
  315. _homeRoutine.Abort();
  316. return true;
  317. }
  318. private bool fnHome(object[] param)
  319. {
  320. if (fsm.State == (int)STATE.Init && param.Length > 0)//带参home
  321. {
  322. return false;
  323. }
  324. else
  325. return _homeRoutine.Start(param) == RState.Running;
  326. }
  327. private bool fnHomeTimeout(object[] param)
  328. {
  329. RState ret = _homeRoutine.Monitor();
  330. if (ret == RState.Failed || ret == RState.Timeout)
  331. {
  332. PostMsg(MSG.Error);
  333. return false;
  334. }
  335. return ret == RState.End;
  336. }
  337. private bool fnOffline(object[] param)
  338. {
  339. throw new NotImplementedException();
  340. }
  341. private bool fnOnline(object[] param)
  342. {
  343. throw new NotImplementedException();
  344. }
  345. private bool fnError(object[] param)
  346. {
  347. return true;
  348. }
  349. public bool Check(int msg, out string reason, params object[] args)
  350. {
  351. reason = "";
  352. return true;
  353. }
  354. public bool CheckAcked(int msg)
  355. {
  356. return fsm.CheckExecuted(msg);
  357. }
  358. public bool CheckToPostMessage(int msg, params object[] args)
  359. {
  360. if (!fsm.FindTransition(fsm.State, msg))
  361. {
  362. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  363. return false;
  364. }
  365. Running = true;
  366. fsm.PostMsg(msg, args);
  367. return true;
  368. }
  369. public int Invoke(string function, params object[] args)
  370. {
  371. switch (function)
  372. {
  373. case "Home":
  374. CheckToPostMessage((int)MSG.Home);
  375. return (int)MSG.Home;
  376. }
  377. return (int)FSM_MSG.NONE;
  378. }
  379. }
  380. }