IOWalkingAxisNcd01.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Device.Unit;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Fsm;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.OperationCenter;
  9. using Aitex.Core.Util;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.WalkingAixs
  17. {
  18. public class IOWalkingAxisNcd01:WalkingAxisBaseDevice
  19. {
  20. private R_TRIG _trigMonitorError = new R_TRIG();
  21. public IOWalkingAxisNcd01(string module,string name,IoSensor[] dis,IoTrigger[] dos):base(module,name)
  22. {
  23. _diBusy = dis[0];
  24. _diOnTarget = dis[1];
  25. _diReady = dis[2];
  26. _diOnError = dis[3];
  27. _diHomeSensor = dis[4];
  28. _diPosFeedback1 = dis[5];
  29. _diPosFeedback2 = dis[6];
  30. _diPosFeedback3 = dis[7];
  31. _diPosFeedback4 = dis[8];
  32. _diPosFeedback5 = dis[9];
  33. _doServoOn = dos[0];
  34. _doStartMoving = dos[1];
  35. _doPosSet0 = dos[2];
  36. _doPosSet1 = dos[3];
  37. _doPosSet2 = dos[4];
  38. _doResetAlarm = dos[5];
  39. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  40. DATA.Subscribe($"{Name}.PosFeedBack1", () => _diPosFeedback1.Value);
  41. DATA.Subscribe($"{Name}.PosFeedBack2", () => _diPosFeedback2.Value);
  42. DATA.Subscribe($"{Name}.PosFeedBack3", () => _diPosFeedback3.Value);
  43. DATA.Subscribe($"{Name}.PosFeedBack4", () => _diPosFeedback4.Value);
  44. DATA.Subscribe($"{Name}.PosFeedBack5", () => _diPosFeedback5.Value);
  45. DATA.Subscribe($"{Name}.Ready", () => _diReady.Value);
  46. DATA.Subscribe($"{Name}.OnTarget", () => _diOnTarget.Value);
  47. DATA.Subscribe($"{Name}.OnError", () => _diOnError.Value);
  48. DATA.Subscribe($"{Name}.HomeSensor", () => _diHomeSensor.Value);
  49. DATA.Subscribe($"{Name}.DoStartMoving", () => _doStartMoving.Value);
  50. DATA.Subscribe($"{Name}.DoPosSet0", () => _doPosSet0.Value);
  51. DATA.Subscribe($"{Name}.DoPosSet1", () => _doPosSet1.Value);
  52. DATA.Subscribe($"{Name}.DoPosSet2", () => _doPosSet2.Value);
  53. DATA.Subscribe($"{Name}.DoResetAlarm", () => _doResetAlarm.Value);
  54. DATA.Subscribe($"{Name}.Status", () => DeviceState.ToString());
  55. OP.Subscribe($"{Name}.SetDo", (method, args) =>
  56. {
  57. SetDo(method, args);
  58. return true;
  59. });
  60. }
  61. private void SetDo(string method, object[] args)
  62. {
  63. bool value = args[1].ToString() == "ON";
  64. switch(args[0])
  65. {
  66. case "_doStartMoving":
  67. _doStartMoving.SetTrigger(value, out _);
  68. break;
  69. case "_doPosSet0":
  70. _doPosSet0.SetTrigger(value, out _);
  71. break;
  72. case "_doPosSet1":
  73. _doPosSet1.SetTrigger(value, out _);
  74. break;
  75. case "_doPosSet2":
  76. _doPosSet2.SetTrigger(value, out _);
  77. break;
  78. case "_doResetAlarm":
  79. _doResetAlarm.SetTrigger(value, out _);
  80. break;
  81. }
  82. }
  83. private readonly IoSensor _diPosFeedback1;
  84. private readonly IoSensor _diPosFeedback2;
  85. private readonly IoSensor _diPosFeedback3;
  86. private readonly IoSensor _diPosFeedback4;
  87. private readonly IoSensor _diPosFeedback5;
  88. private readonly IoSensor _diBusy;
  89. private readonly IoSensor _diReady;
  90. private readonly IoSensor _diOnTarget;
  91. private readonly IoSensor _diOnError;
  92. private readonly IoSensor _diHomeSensor;
  93. private readonly IoTrigger _doServoOn;
  94. private readonly IoTrigger _doStartMoving;
  95. private readonly IoTrigger _doPosSet0;
  96. private readonly IoTrigger _doPosSet1;
  97. private readonly IoTrigger _doPosSet2;
  98. private readonly IoTrigger _doResetAlarm;
  99. public override void Monitor()
  100. {
  101. _trigMonitorError.CLK = _diOnError.Value;
  102. if (_trigMonitorError.Q)
  103. OnError("ErrorSignal");
  104. }
  105. protected override bool fStop(object[] param)
  106. {
  107. _doServoOn.SetTrigger(false, out _);
  108. _doStartMoving.SetTrigger(false, out _);
  109. _doPosSet0.SetTrigger(false, out _);
  110. _doPosSet1.SetTrigger(false, out _);
  111. _doPosSet2.SetTrigger(false, out _);
  112. _doResetAlarm.SetTrigger(false, out _);
  113. return base.fStop(param);
  114. }
  115. protected override bool fMonitorInit(object[] param)
  116. {
  117. IsBusy = false;
  118. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(TimeLimitHome))
  119. {
  120. _doStartMoving.SetTrigger(false, out _);
  121. OnError("InitTimeout");
  122. return true;
  123. }
  124. if (_diHomeSensor.Value && _diOnTarget.Value && _diReady.Value)
  125. {
  126. _doStartMoving.SetTrigger(false, out _);
  127. EV.PostInfoLog("WalkingAxis", $"{Name} init complete.");
  128. return true;
  129. }
  130. return false;
  131. }
  132. protected override bool fMonitorMove(object[] param)
  133. {
  134. IsBusy = false;
  135. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(TimeLimitMove))
  136. {
  137. _doStartMoving.SetTrigger(false, out _);
  138. OnError("MoveTimeout");
  139. return true;
  140. }
  141. if (_diOnTarget.Value && _diReady.Value && GetCurrentStation() == TargetStation)
  142. {
  143. _doStartMoving.SetTrigger(false, out _);
  144. return true;
  145. }
  146. return false;
  147. }
  148. protected override bool fStartInit(object[] param)
  149. {
  150. _doServoOn.SetTrigger(true, out _);
  151. _doResetAlarm.SetTrigger(false, out _);
  152. _doStartMoving.SetTrigger(false, out _);
  153. _doPosSet0.SetTrigger(true, out _);
  154. _doPosSet1.SetTrigger(false, out _);
  155. _doPosSet2.SetTrigger(false, out _);
  156. Thread.Sleep(200);
  157. _doStartMoving.SetTrigger(true, out _);
  158. _dtActionStart = DateTime.Now;
  159. return true;
  160. }
  161. protected override bool fInitComplete(object[] param)
  162. {
  163. IsBusy = false;
  164. return true;
  165. }
  166. protected override bool fStartMove(object[] param)
  167. {
  168. int stationIndex = (int)param[0];
  169. if (stationIndex > 7 || stationIndex <0) return false;
  170. _doPosSet0.SetTrigger((stationIndex & 0x1) == 0x1, out _);
  171. _doPosSet1.SetTrigger((stationIndex & 0x2) == 0x2, out _);
  172. _doPosSet2.SetTrigger((stationIndex & 0x4) == 0x4, out _);
  173. Thread.Sleep(200);
  174. _doStartMoving.SetTrigger(true, out _);
  175. Thread.Sleep(500);
  176. _dtActionStart = DateTime.Now;
  177. return true;
  178. }
  179. protected override bool fMoveComplete(object[] param)
  180. {
  181. IsBusy = false;
  182. _doStartMoving.SetTrigger(false, out _);
  183. _doPosSet0.SetTrigger(false, out _);
  184. _doPosSet1.SetTrigger(false, out _);
  185. _doPosSet2.SetTrigger(false, out _);
  186. return true;
  187. }
  188. protected override bool fStartReset(object[] param)
  189. {
  190. _doServoOn.SetTrigger(true, out _);
  191. _doStartMoving.SetTrigger(false, out _);
  192. _doPosSet0.SetTrigger(false, out _);
  193. _doPosSet1.SetTrigger(false, out _);
  194. _doPosSet2.SetTrigger(false, out _);
  195. _doResetAlarm.SetTrigger(true, out _);
  196. _dtActionStart = DateTime.Now;
  197. return true;
  198. }
  199. protected override bool fMonitorResetting(object[] param)
  200. {
  201. IsBusy = false;
  202. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(TimeLimitMove))
  203. {
  204. _doResetAlarm.SetTrigger(false, out _);
  205. OnError("ResetTimeout");
  206. return true;
  207. }
  208. if (_diOnError.Value) return false;
  209. if (!_diReady.Value) return false;
  210. _doResetAlarm.SetTrigger(false, out _);
  211. return true;
  212. }
  213. public override bool IsReady()
  214. {
  215. if (_diOnError.Value) return false;
  216. if (!_diReady.Value) return false;
  217. return base.IsReady();
  218. }
  219. public override int GetCurrentStation()
  220. {
  221. if (_diPosFeedback1.Value && !_diPosFeedback2.Value && !_diPosFeedback3.Value && !_diPosFeedback4.Value && !_diPosFeedback5.Value)
  222. return 1;
  223. if (!_diPosFeedback1.Value && _diPosFeedback2.Value && !_diPosFeedback3.Value && !_diPosFeedback4.Value && !_diPosFeedback5.Value)
  224. return 2;
  225. if (!_diPosFeedback1.Value && !_diPosFeedback2.Value && _diPosFeedback3.Value && !_diPosFeedback4.Value && !_diPosFeedback5.Value)
  226. return 3;
  227. if (!_diPosFeedback1.Value && !_diPosFeedback2.Value && !_diPosFeedback3.Value && _diPosFeedback4.Value && !_diPosFeedback5.Value)
  228. return 4;
  229. if (!_diPosFeedback1.Value && !_diPosFeedback2.Value && !_diPosFeedback3.Value && !_diPosFeedback4.Value && _diPosFeedback5.Value)
  230. return 5;
  231. return 0;
  232. }
  233. private AITServoMotorData DeviceData
  234. {
  235. get
  236. {
  237. return new AITServoMotorData()
  238. {
  239. DiPosFeedBack1 = _diPosFeedback1.Value,
  240. DiPosFeedBack2 = _diPosFeedback2.Value,
  241. DiPosFeedBack3 = _diPosFeedback3.Value,
  242. DiReady = _diReady.Value,
  243. DiOnTarget = _diOnTarget.Value,
  244. DiOnError = _diOnError.Value,
  245. DiOnHomeSensor = _diHomeSensor.Value,
  246. DoStart = _doStartMoving.Value,
  247. DoPos1 = _doPosSet0.Value,
  248. DoPos2 = _doPosSet1.Value,
  249. DoPos3 = _doPosSet2.Value,
  250. DoReset = _doResetAlarm.Value,
  251. CurrentStatus = DeviceState.ToString(),
  252. //State = _state,
  253. };
  254. }
  255. }
  256. }
  257. }