WalkingAxisBaseDevice.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Fsm;
  4. using Aitex.Core.RT.DataCenter;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Aitex.Core.RT.OperationCenter;
  11. using MECF.Framework.Common.Event;
  12. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.WalkingAixs
  13. {
  14. public abstract class WalkingAxisBaseDevice : Entity, IDevice
  15. {
  16. public bool IsBusy { get; private set; }
  17. protected WalkingAxisBaseDevice(string module, string name)
  18. : base()
  19. {
  20. Module = module;
  21. Name = name;
  22. InitializeMotion();
  23. }
  24. private void InitializeMotion()
  25. {
  26. BuildTransitionTable();
  27. SubscribeDataVariable();
  28. SubscribeOperation();
  29. Running = true;
  30. }
  31. public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged;
  32. public bool HasAlarm { get; }
  33. public string Module { get; set; }
  34. public string Name { get; set ; }
  35. public WalkingAxisState DeviceState { get => (WalkingAxisState)fsm.State; }
  36. public abstract int GetCurrentStation();
  37. public bool IsArrivedTarget
  38. {
  39. get { return (TargetStation == GetCurrentStation()); }
  40. }
  41. public int TargetStation { get; private set; }
  42. public abstract void Monitor();
  43. public void Reset()
  44. {
  45. }
  46. public enum WalkingAxisMsg
  47. {
  48. Reset,
  49. Init,
  50. StartMove,
  51. Stop,
  52. Error,
  53. ActionDone,
  54. }
  55. public enum WalkingAxisState
  56. {
  57. Init,
  58. Initializing,
  59. Idle,
  60. Moving,
  61. ERROR,
  62. Stopped,
  63. Busy,
  64. }
  65. private void BuildTransitionTable()
  66. {
  67. fsm = new StateMachine<WalkingAxisBaseDevice>(Module + Name + ".StateMachine", (int)WalkingAxisState.Init, 50);
  68. AnyStateTransition(WalkingAxisMsg.Error, fError, WalkingAxisState.ERROR);
  69. AnyStateTransition(WalkingAxisMsg.Stop, fStop, WalkingAxisState.Stopped);
  70. Transition(WalkingAxisState.Init, WalkingAxisMsg.Init, fStartInit, WalkingAxisState.Initializing);
  71. Transition(WalkingAxisState.Initializing, FSM_MSG.TIMER, fMonitorInit, WalkingAxisState.Initializing);
  72. Transition(WalkingAxisState.Initializing, WalkingAxisMsg.ActionDone, fInitComplete, WalkingAxisState.Idle);
  73. Transition(WalkingAxisState.Idle, WalkingAxisMsg.Init, fStartInit, WalkingAxisState.Initializing);
  74. Transition(WalkingAxisState.Idle, WalkingAxisMsg.StartMove, fStartMove, WalkingAxisState.Moving);
  75. Transition(WalkingAxisState.Moving, FSM_MSG.TIMER, fMonitorMove, WalkingAxisState.Moving);
  76. Transition(WalkingAxisState.Moving, WalkingAxisMsg.ActionDone, fMoveComplete, WalkingAxisState.Idle);
  77. Transition(WalkingAxisState.ERROR, WalkingAxisMsg.Reset, fStartReset, WalkingAxisState.Init);
  78. Transition(WalkingAxisState.Stopped, WalkingAxisMsg.Reset, fStartReset, WalkingAxisState.Init);
  79. Transition(WalkingAxisState.Idle, WalkingAxisMsg.Reset, fStartReset, WalkingAxisState.Idle);
  80. Transition(WalkingAxisState.Init, WalkingAxisMsg.Reset, fStartReset, WalkingAxisState.Idle);
  81. }
  82. public virtual bool IsReady()
  83. {
  84. return ((!IsBusy) && (DeviceState == WalkingAxisState.Idle));
  85. }
  86. private void SubscribeDataVariable()
  87. {
  88. DATA.Subscribe($"{Module}.{Name}.State", () => DeviceState.ToString());
  89. }
  90. private void SubscribeOperation()
  91. {
  92. OP.Subscribe($"{Module}.{Name}.Home", (string cmd, object[] param) =>
  93. {
  94. string reason = "";
  95. if (!Home(null))
  96. {
  97. EV.PostWarningLog(Module, $"{Name} can not home, {reason}");
  98. return false;
  99. }
  100. EV.PostInfoLog(Module, $"{Name} home");
  101. return true;
  102. });
  103. OP.Subscribe($"{Module}.{Name}.Reset", (string cmd, object[] param) =>
  104. {
  105. string reason = "";
  106. if (!Reset(null))
  107. {
  108. EV.PostWarningLog(Module, $"{Name} can not clear alarm, {reason}");
  109. return false;
  110. }
  111. EV.PostInfoLog(Module, $"{Name} reset alarm");
  112. return true;
  113. });
  114. OP.Subscribe($"{Module}.{Name}.MoveTo", (string cmd, object[] param) =>
  115. {
  116. string reason = "";
  117. if (!MoveTo(param))
  118. {
  119. EV.PostWarningLog(Module, $"{Name} can not move to {param[0].ToString()}, {reason}");
  120. return false;
  121. }
  122. EV.PostInfoLog(Module, $"{Name} move to {param[0]}");
  123. return true;
  124. });
  125. OP.Subscribe($"{Module}.{Name}.Stop", (string cmd, object[] param) =>
  126. {
  127. string reason = "";
  128. if (!Stop(null))
  129. {
  130. EV.PostWarningLog(Module, $"{Name} can not Stop, {reason}");
  131. return false;
  132. }
  133. EV.PostInfoLog(Module, $"{Name} Stop");
  134. return true;
  135. });
  136. }
  137. protected virtual bool fStop(object[] param)
  138. {
  139. IsBusy = false;
  140. return true;
  141. }
  142. protected abstract bool fStartReset(object[] param);
  143. protected abstract bool fMonitorMove(object[] param);
  144. protected abstract bool fStartMove(object[] param);
  145. protected abstract bool fMonitorInit(object[] param);
  146. protected abstract bool fStartInit(object[] param);
  147. protected virtual bool fError(object[] param)
  148. {
  149. return true;
  150. }
  151. protected virtual bool fInitComplete(object[] param)
  152. {
  153. IsBusy = false;
  154. return true;
  155. }
  156. protected virtual bool fMoveComplete(object[] param)
  157. {
  158. IsBusy = false;
  159. return true;
  160. }
  161. public virtual bool Home(object[] param)
  162. {
  163. IsBusy = true;
  164. return CheckToPostMessage((int)WalkingAxisMsg.Init, param);
  165. }
  166. public virtual bool MoveTo(object[] param)
  167. {
  168. int temp;
  169. if (int.TryParse(param[0].ToString(), out temp))
  170. {
  171. TargetStation = temp;
  172. IsBusy = true;
  173. return CheckToPostMessage((int)WalkingAxisMsg.StartMove, new object[] { temp});
  174. }
  175. return false;
  176. }
  177. public virtual bool Reset(object[] param)
  178. {
  179. IsBusy = false;
  180. return CheckToPostMessage((int)WalkingAxisMsg.Reset, param);
  181. }
  182. public virtual bool Stop(object[] param)
  183. {
  184. IsBusy = true;
  185. return CheckToPostMessage((int)WalkingAxisMsg.Stop, param);
  186. }
  187. public virtual bool OnActionDone(object[] param)
  188. {
  189. IsBusy = false;
  190. return CheckToPostMessage((int)WalkingAxisMsg.ActionDone, param);
  191. }
  192. public virtual bool OnError(object[] param)
  193. {
  194. return CheckToPostMessage((int)WalkingAxisMsg.Error, param);
  195. }
  196. public bool CheckToPostMessage(int msg, params object[] args)
  197. {
  198. if (!fsm.FindTransition(fsm.State, msg))
  199. {
  200. EV.PostWarningLog(Name, $"{Name} is in { (WalkingAxisState)fsm.State} state,can not do {(WalkingAxisMsg)msg}");
  201. return false;
  202. }
  203. fsm.PostMsg(msg, args);
  204. return true;
  205. }
  206. public bool Check(int msg, out string reason, params object[] args)
  207. {
  208. if (!fsm.FindTransition(fsm.State, msg))
  209. {
  210. reason = String.Format("{0} is in {1} state,can not do {2}", Name, (WalkingAxisState)fsm.State, (WalkingAxisMsg)msg);
  211. return false;
  212. }
  213. reason = "";
  214. return true;
  215. }
  216. }
  217. }