LoadPortEntity.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Fsm;
  3. using Aitex.Core.RT.OperationCenter;
  4. using Aitex.Core.RT.Routine;
  5. using Aitex.Core.Util;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
  8. using System;
  9. using athosRT.FSM;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Diagnostics;
  15. using System.Threading;
  16. using athosRT.tool;
  17. using OP = Common.OP.OP;
  18. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  19. using Aitex.Core.RT.Device;
  20. namespace athosRT.Modules.LPs
  21. {
  22. public class LoadPortEntity : Entity, IEntity
  23. {
  24. private HomeRoutine homeRoutine = null;
  25. private LoadFoupRoutine loadRoutine = null;
  26. private UnloadFoupRoutine unloadRoutine = null;
  27. private ReadAndLoadRoutine readAndLoadRoutine = null;
  28. private ModuleName _chamber;
  29. private LoadPortBaseDevice _lp;
  30. private ModuleFsmDevice MFD = new ModuleFsmDevice();
  31. public string Name { get; set; }
  32. public bool IsIdle => fsm.State == (int)STATE.IDLE;
  33. public bool IsRunning => fsm.State != (int)STATE.INIT && fsm.State != (int)STATE.IDLE && fsm.State != (int)STATE.ERROR;
  34. public int FsmState => fsm.State;
  35. public bool IsAlarm => false;
  36. public bool IsWarning => false;
  37. public bool IsLoaded => _lp.IsLoaded;
  38. public bool IsDoorOpen => _lp.DoorState == Aitex.Sorter.Common.FoupDoorState.Open;
  39. public bool IsPresent => _lp.IsPresent;
  40. public bool IsReady => _lp.IsReady();
  41. public RobotBaseDevice _robot { get; set; }
  42. public LoadPortEntity(string name, RobotBaseDevice robot)
  43. {
  44. Name = name;
  45. _lp = DEVICE.GetDevice<LoadPortBaseDevice>(name);
  46. _chamber = (ModuleName)Enum.Parse(typeof(ModuleName), this.Name);
  47. fsm = new StateMachine<LoadPortEntity>(this.Name, 0, 50);
  48. AnyStateTransition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.MSG.ERROR, new FsmFunc(this.fError), LoadPortEntity.STATE.ERROR);
  49. AnyStateTransition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.MSG.ABORT, new FsmFunc(this.fError), LoadPortEntity.STATE.IDLE);
  50. AnyStateTransition(268435443, new FsmFunc(this.fError), 8);
  51. AnyStateTransition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.MSG.RESET, new FsmFunc(this.fInit), LoadPortEntity.STATE.IDLE);
  52. Transition<LoadPortEntity.STATE, FSM_MSG>(LoadPortEntity.STATE.INIT, FSM_MSG.TIMER, new FsmFunc(this.fInit), LoadPortEntity.STATE.IDLE);
  53. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.INIT, LoadPortEntity.MSG.ERROR, new FsmFunc(this.fError), LoadPortEntity.STATE.ERROR);
  54. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.ERROR, LoadPortEntity.MSG.HOME, new FsmFunc(this.fStartHome), LoadPortEntity.STATE.HOME);
  55. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.HOME, new FsmFunc(this.fStartHome), LoadPortEntity.STATE.HOME);
  56. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.INIT, LoadPortEntity.MSG.HOME, new FsmFunc(this.fStartHome), LoadPortEntity.STATE.HOME);
  57. Transition<LoadPortEntity.STATE, FSM_MSG>(LoadPortEntity.STATE.HOME, FSM_MSG.TIMER, new FsmFunc(this.fHome), LoadPortEntity.STATE.IDLE);
  58. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.Load, new FsmFunc(this.fStartLoad), LoadPortEntity.STATE.LOAD);
  59. Transition<LoadPortEntity.STATE, FSM_MSG>(LoadPortEntity.STATE.LOAD, FSM_MSG.TIMER, new FsmFunc(this.fLoad), LoadPortEntity.STATE.IDLE);
  60. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.Unload, new FsmFunc(this.fStartUnload), LoadPortEntity.STATE.UNLOAD);
  61. Transition<LoadPortEntity.STATE, FSM_MSG>(LoadPortEntity.STATE.UNLOAD, FSM_MSG.TIMER, new FsmFunc(this.fUnload), LoadPortEntity.STATE.IDLE);
  62. Transition<LoadPortEntity.STATE, LoadPortEntity.MSG>(LoadPortEntity.STATE.IDLE, LoadPortEntity.MSG.ReadAndLoad, new FsmFunc(this.fStartReadAndLoad), LoadPortEntity.STATE.ReadAndLoad);
  63. Transition<LoadPortEntity.STATE, FSM_MSG>(LoadPortEntity.STATE.ReadAndLoad, FSM_MSG.TIMER, new FsmFunc(this.fReadAndLoad), LoadPortEntity.STATE.IDLE);
  64. Running = true;
  65. _robot = robot;
  66. }
  67. public bool Check(int msg, out string reason, object[] objs)
  68. {
  69. bool flag = true;
  70. reason = "";
  71. if (msg != 3 || this.fsm.State == 1)
  72. return flag;
  73. reason = string.Format("{0} is busy {1},can't excute {2}", (object)this.Name, (object)this.State2String(this.fsm.State), (object)this.Msg2String(msg));
  74. return false;
  75. }
  76. private string Msg2String(int msg)
  77. {
  78. string str = "";
  79. switch (msg)
  80. {
  81. case 6:
  82. str = "复位";
  83. break;
  84. case 7:
  85. str = "终止";
  86. break;
  87. }
  88. return str;
  89. }
  90. private string State2String(int state)
  91. {
  92. string str = "";
  93. switch (state)
  94. {
  95. case 0:
  96. case 9:
  97. str = "初始化";
  98. break;
  99. case 1:
  100. str = "就绪";
  101. break;
  102. case 8:
  103. str = "错误";
  104. break;
  105. }
  106. return str;
  107. }
  108. protected override bool Init()
  109. {
  110. this.loadRoutine = new LoadFoupRoutine(_chamber);
  111. this.unloadRoutine = new UnloadFoupRoutine(_chamber, _robot);
  112. this.homeRoutine = new HomeRoutine(_chamber, _robot);
  113. this.readAndLoadRoutine = new ReadAndLoadRoutine(_chamber);
  114. OP.Subscribe(string.Format("{0}.LPReset", (object)this._chamber), (Func<string, object[], bool>)((cmd, args) => CheckToPostMsg(LoadPortEntity.MSG.RESET)));
  115. OP.Subscribe(string.Format("{0}.Load", (object)this._chamber), (Func<string, object[], bool>)((cmd, args) => CheckToPostMsg(LoadPortEntity.MSG.Load)));
  116. OP.Subscribe(string.Format("{0}.UnLoad", (object)this._chamber), (Func<string, object[], bool>)((cmd, args) => CheckToPostMsg(LoadPortEntity.MSG.Unload)));
  117. OP.Subscribe(string.Format("{0}.Home", (object)this._chamber), (Func<string, object[], bool>)((cmd, args) => CheckToPostMsg(LoadPortEntity.MSG.HOME)));
  118. OP.Subscribe(string.Format("{0}.Abort", (object)this._chamber), (Func<string, object[], bool>)((cmd, args) => CheckToPostMsg(LoadPortEntity.MSG.ABORT)));
  119. return true;
  120. }
  121. protected override void Term()
  122. {
  123. }
  124. private bool fStartHome(object[] objs)
  125. {
  126. Running = false;
  127. RState ret = MFD.StartRoutine(homeRoutine);
  128. if (ret == RState.Failed || ret == RState.End)
  129. return false;
  130. //_isInit = false;
  131. return ret == RState.Running;
  132. }
  133. private bool fHome(object[] objs)
  134. {
  135. RState ret = MFD.MonitorRoutine();
  136. Trace.WriteLine("执行情况"+ret);
  137. if (ret == RState.Failed)
  138. {
  139. PostMsg(MSG.ERROR);
  140. return false;
  141. }
  142. if (ret == RState.End)
  143. {
  144. //_isInit = true;
  145. return true;
  146. }
  147. return false;
  148. }
  149. private bool fStartLoad(object[] objs)
  150. {
  151. Running = false;
  152. switch (this.loadRoutine.Start(objs))
  153. {
  154. case RState.End:
  155. return false;
  156. case RState.Failed:
  157. return false;
  158. default:
  159. return true;
  160. }
  161. }
  162. private bool fLoad(object[] objs)
  163. {
  164. switch (this.loadRoutine.Monitor())
  165. {
  166. case RState.End:
  167. this.Running = true;
  168. return true;
  169. case RState.Failed:
  170. this.PostMsg<LoadPortEntity.MSG>(LoadPortEntity.MSG.ERROR);
  171. return true;
  172. default:
  173. return false;
  174. }
  175. }
  176. private bool fStartUnload(object[] objs)
  177. {
  178. this.Running = false;
  179. switch (this.unloadRoutine.Start(objs))
  180. {
  181. case RState.End:
  182. return false;
  183. case RState.Failed:
  184. return false;
  185. default:
  186. return true;
  187. }
  188. }
  189. private bool fUnload(object[] objs)
  190. {
  191. switch (this.unloadRoutine.Monitor())
  192. {
  193. case RState.End:
  194. this.Running = true;
  195. return true;
  196. case RState.Failed:
  197. this.PostMsg<LoadPortEntity.MSG>(LoadPortEntity.MSG.ERROR);
  198. return true;
  199. default:
  200. return false;
  201. }
  202. }
  203. private bool fStartReadAndLoad(object[] objs)
  204. {
  205. this.Running = false;
  206. switch (this.readAndLoadRoutine.Start(objs))
  207. {
  208. case RState.End:
  209. return false;
  210. case RState.Failed:
  211. return false;
  212. default:
  213. return true;
  214. }
  215. }
  216. private bool fReadAndLoad(object[] objs)
  217. {
  218. switch (this.readAndLoadRoutine.Monitor())
  219. {
  220. case RState.End:
  221. this.Running = true;
  222. return true;
  223. case RState.Failed:
  224. this.PostMsg<LoadPortEntity.MSG>(LoadPortEntity.MSG.ERROR);
  225. return false;
  226. default:
  227. return false;
  228. }
  229. }
  230. private bool fInit(object[] objs)
  231. {
  232. Trace.WriteLine("finit开始");
  233. return true;
  234. }
  235. private bool fError(object[] objs)
  236. {
  237. //出现错误的时候向上报 告知上层FSM处于错误状态
  238. Trace.WriteLine("fError开始");
  239. Singleton<RouteManager1>.Instance.CheckToPostMsg(RouteManager1.MSG.ERROR);
  240. return true;
  241. }
  242. private bool fAbort()
  243. {
  244. return true;
  245. }
  246. public bool CheckToPostMsg(LoadPortEntity.MSG msg)
  247. {
  248. Trace.WriteLine("当前状态机状态:"+this.fsm.State);
  249. if (!this.fsm.FindTransition(this.fsm.State, (int)msg))
  250. {
  251. Trace.WriteLine("当前状态不能接受消息:"+msg.ToString());
  252. LogObject.Error(_chamber.ToString(), $"当前状态{fsm.State}不能接受消息:{msg}");
  253. //EV.PostMessage<EventEnum>("System", EventEnum.DefaultWarning, (object)string.Format("{0} is in {1} state,can not do {2}", (object)this.Name, (object)(LoadPortEntity.STATE)this.fsm.State, (object)msg));
  254. return false;
  255. }
  256. LogObject.Info(_chamber.ToString(),"当前状态能接受消息:" + msg.ToString());
  257. this.PostMsg<LoadPortEntity.MSG>(msg);
  258. return true;
  259. }
  260. public enum STATE
  261. {
  262. INIT,
  263. IDLE,
  264. HOME,
  265. LOAD,
  266. UNLOAD,
  267. READ,
  268. WRITE,
  269. ReadAndLoad,
  270. ERROR,
  271. Reset,
  272. }
  273. public enum MSG
  274. {
  275. HOME,
  276. ReadRFID,
  277. WriteRFID,
  278. Load,
  279. Unload,
  280. ReadAndLoad,
  281. RESET,
  282. ABORT,
  283. ERROR,
  284. }
  285. }
  286. }