SETMEntity.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 RState RobotStatus
  98. {
  99. get
  100. {
  101. if (_robot.Status != RState.Running)
  102. {
  103. if (_robotWatch.ElapsedMilliseconds < 100)
  104. return RState.Running;
  105. else
  106. return _robot.Status;
  107. }
  108. else
  109. return RState.Running;
  110. }
  111. }
  112. public bool IsOnline { get; internal set; }
  113. //public bool IsTMVac => _tm.IsTMVac;
  114. //public bool IsTMATM => _tm.IsTMATM;
  115. #endregion
  116. #region 私有变量
  117. private readonly HongHuTM _tm;
  118. private readonly ITransferRobot _robot;
  119. private readonly IPreAlign _vpa;
  120. private readonly SEMFHomeRoutine _homeRoutine;
  121. private readonly SEMFPickRoutine _pickRoutine;
  122. private readonly SEMFPlaceRoutine _placeRoutine;
  123. //private readonly
  124. private readonly Stopwatch _robotWatch = new Stopwatch();
  125. #endregion
  126. public SETMEntity()
  127. {
  128. _tm = DEVICE.GetDevice<HongHuTM>("SETM");
  129. _robot = new HongHuVR();
  130. _vpa = new HongHuVPA(ModuleName.VPA);
  131. _robotWatch = new Stopwatch();
  132. _homeRoutine = new SEMFHomeRoutine(_tm,_robot, _vpa);
  133. _pickRoutine = new SEMFPickRoutine(_tm,_robot, _vpa);
  134. _placeRoutine = new SEMFPlaceRoutine(_tm, _robot, _vpa);
  135. //routine
  136. InitFsmMap();
  137. }
  138. private void InitFsmMap()
  139. {
  140. fsm = new StateMachine<SETMEntity>("SETM", (int)STATE.Init, 50);
  141. AnyStateTransition(MSG.Error, fnError, STATE.Error);
  142. AnyStateTransition(MSG.Online, fnOnline, FSM_STATE.SAME);
  143. AnyStateTransition(MSG.Offline, fnOffline, FSM_STATE.SAME);
  144. AnyStateTransition(MSG.Home, fnHome, STATE.Initializing);
  145. //Home
  146. Transition(STATE.Initializing, FSM_MSG.TIMER, fnHomeTimeout, STATE.Idle);
  147. Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  148. //Pick
  149. Transition(STATE.Idle, MSG.Pick, fnStartPick, STATE.Picking);
  150. Transition(STATE.Picking, FSM_MSG.TIMER, fnPickTimeout, STATE.Idle);
  151. Transition(STATE.Picking, MSG.Abort, fnAbortPick, STATE.Idle);
  152. //Place
  153. Transition(STATE.Idle, MSG.Place, fnStartPlace, STATE.Placing);
  154. Transition(STATE.Placing, FSM_MSG.TIMER, fnPlaceTimeout, STATE.Idle);
  155. Transition(STATE.Placing, MSG.Abort, fnAbortPlace, STATE.Idle);
  156. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  157. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  158. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  159. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  160. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  161. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  162. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  163. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  164. //Transition(STATE.Initializing, MSG.Abort, fnAbortHome, STATE.Idle);
  165. Running = true;
  166. }
  167. protected override bool Init()
  168. {
  169. OP.Subscribe("SETM.Home", (cmd, args) => { PostMsg(MSG.Home); return true; });
  170. OP.Subscribe("SETM.Pick", (cmd, args) => { PostMsg(MSG.Pick, args[0], args[1],Hand.Blade1); return true; });
  171. OP.Subscribe("SETM.Place", (cmd, args) => { PostMsg(MSG.Place, args[0], args[1], Hand.Blade1); return true; });
  172. return true;
  173. }
  174. private bool fnAbortPlace(object[] param)
  175. {
  176. return true;
  177. }
  178. private bool fnPlaceTimeout(object[] param)
  179. {
  180. RState ret = _placeRoutine.Monitor();
  181. if (ret == RState.Failed || ret == RState.Timeout)
  182. {
  183. PostMsg(MSG.Error);
  184. return false;
  185. }
  186. return ret == RState.End;
  187. }
  188. private bool fnStartPlace(object[] param)
  189. {
  190. return _placeRoutine.Start(param) == RState.Running;
  191. }
  192. private bool fnAbortPick(object[] param)
  193. {
  194. _pickRoutine.Abort();
  195. return true;
  196. }
  197. private bool fnStartPick(object[] param)
  198. {
  199. return _pickRoutine.Start(param) == RState.Running;
  200. }
  201. private bool fnPickTimeout(object[] param)
  202. {
  203. RState ret = _pickRoutine.Monitor();
  204. if (ret == RState.Failed || ret == RState.Timeout)
  205. {
  206. PostMsg(MSG.Error);
  207. return false;
  208. }
  209. return ret == RState.End;
  210. }
  211. private bool fnAbortHome(object[] param)
  212. {
  213. _homeRoutine.Abort();
  214. return true;
  215. }
  216. private bool fnHome(object[] param)
  217. {
  218. if (fsm.State == (int)STATE.Init && param.Length > 0)//带参home
  219. {
  220. return false;
  221. }
  222. else
  223. return _homeRoutine.Start(param) == RState.Running;
  224. }
  225. private bool fnHomeTimeout(object[] param)
  226. {
  227. RState ret = _homeRoutine.Monitor();
  228. if (ret == RState.Failed || ret == RState.Timeout)
  229. {
  230. PostMsg(MSG.Error);
  231. return false;
  232. }
  233. return ret == RState.End;
  234. }
  235. private bool fnOffline(object[] param)
  236. {
  237. throw new NotImplementedException();
  238. }
  239. private bool fnOnline(object[] param)
  240. {
  241. throw new NotImplementedException();
  242. }
  243. private bool fnError(object[] param)
  244. {
  245. return true;
  246. }
  247. public bool Check(int msg, out string reason, params object[] args)
  248. {
  249. reason = "";
  250. return true;
  251. }
  252. public bool CheckAcked(int msg)
  253. {
  254. return fsm.CheckExecuted(msg);
  255. }
  256. public bool CheckToPostMessage(int msg, params object[] args)
  257. {
  258. if (!fsm.FindTransition(fsm.State, msg))
  259. {
  260. LOG.Write(eEvent.WARN_FSM_WARN, ModuleName.TM, $"TM is in {(STATE)fsm.State} state,can not do {(MSG)msg}");
  261. return false;
  262. }
  263. Running = true;
  264. fsm.PostMsg(msg, args);
  265. return true;
  266. }
  267. public int Invoke(string function, params object[] args)
  268. {
  269. switch (function)
  270. {
  271. case "Home":
  272. CheckToPostMessage((int)MSG.Home);
  273. return (int)MSG.Home;
  274. }
  275. return (int)FSM_MSG.NONE;
  276. }
  277. }
  278. }