LoadPortEntity.cs 12 KB

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