IoCylinder.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Device;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.IOCore;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.Util;
  11. namespace Venus_RT.Devices
  12. {
  13. public class IoCylinder : BaseDevice, IDevice
  14. {
  15. private readonly DIAccessor _diON;
  16. private readonly DIAccessor _diOFF;
  17. private readonly DOAccessor _doON;
  18. private readonly DOAccessor _doOFF;
  19. private CylinderState _operation;
  20. private readonly DeviceTimer _timer = new DeviceTimer();
  21. private readonly R_TRIG _trigReset = new R_TRIG();
  22. private readonly R_TRIG _trigError = new R_TRIG();
  23. public bool EnableOpen { get; set; }
  24. public bool EnableClose { get; set; }
  25. private string _error = null;
  26. public int SetPoint
  27. {
  28. get
  29. {
  30. if (_doON.Value && _doOFF.Value) return (int)CylinderState.Error;
  31. if (_doON.Value && !_doOFF.Value) return (int)CylinderState.Open;
  32. if (!_doON.Value && _doOFF.Value) return (int)CylinderState.Close;
  33. if (!_doON.Value && !_doOFF.Value) return (int)CylinderState.Unknown;
  34. return (int)CylinderState.Unknown;
  35. }
  36. }
  37. public CylinderState State
  38. {
  39. get
  40. {
  41. if (_diON != null && _diOFF != null)
  42. {
  43. if (ONFeedback && _diOFF.Value)
  44. return CylinderState.Error;
  45. if (ONFeedback && !_diOFF.Value)
  46. return CylinderState.Open;
  47. if (!ONFeedback && _diOFF.Value)
  48. return CylinderState.Close;
  49. if (!ONFeedback && !_diOFF.Value)
  50. return CylinderState.Unknown;
  51. }
  52. return CylinderState.Unknown;
  53. }
  54. }
  55. public bool ONFeedback
  56. {
  57. get { return _diON != null && _diON.Value; }
  58. }
  59. public bool OFFFeedback
  60. {
  61. get { return _diOFF != null && _diOFF.Value; }
  62. }
  63. public bool ONSetPoint
  64. {
  65. get
  66. {
  67. return _doON != null && _doON.Value;
  68. }
  69. private set
  70. {
  71. if (_doON != null && _doON.Check(value, out _error))
  72. _doON.Value = value;
  73. }
  74. }
  75. public bool OFFSetPoint
  76. {
  77. get
  78. {
  79. return _doOFF != null && _doOFF.Value;
  80. }
  81. private set
  82. {
  83. if (_doOFF != null && _doOFF.Check(value, out _error))
  84. _doOFF.Value = value;
  85. }
  86. }
  87. private AITCylinderData DeviceData
  88. {
  89. get
  90. {
  91. return new AITCylinderData
  92. {
  93. Module = Module,
  94. DeviceName = Name,
  95. DeviceSchematicId = DeviceID,
  96. DisplayName = Display,
  97. OpenFeedback = ONFeedback,
  98. CloseFeedback = OFFFeedback,
  99. OpenSetPoint = ONSetPoint,
  100. CloseSetPoint = OFFSetPoint
  101. };
  102. }
  103. }
  104. public IoCylinder(string module, XmlElement node, string ioModule = "")
  105. {
  106. base.Module = module;
  107. base.Name = node.GetAttribute("id");
  108. base.Display = node.GetAttribute("display");
  109. base.DeviceID = node.GetAttribute("schematicId");
  110. _operation = CylinderState.Unknown;
  111. _diON = ParseDiNode("diON", node, ioModule);
  112. _diOFF = ParseDiNode("diOFF", node, ioModule);
  113. _doON = ParseDoNode("doON", node, ioModule);
  114. _doOFF = ParseDoNode("doOFF", node, ioModule);
  115. }
  116. public bool Initialize()
  117. {
  118. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  119. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", (s, objects) => SetCylinder(true, out _));
  120. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", (s, objects) => SetCylinder(false, out _));
  121. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.SetState}", (out string reason, int time, object[] param) =>
  122. {
  123. bool isUp = (string)param[0] == "Up" ? true : false;
  124. SetCylinder(isUp, out reason);
  125. return true;
  126. });
  127. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}",
  128. (out string reason, int time, object[] param) => SetCylinder(true, out reason));
  129. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}",
  130. (out string reason, int time, object[] param) => SetCylinder(false, out reason));
  131. return true;
  132. }
  133. public void Terminate()
  134. {
  135. OFFSetPoint = false;
  136. ONSetPoint = false;
  137. }
  138. public bool SetCylinder(bool isOpen, out string reason)
  139. {
  140. reason = "";
  141. if (Name == "LoadLockArm")
  142. {
  143. if (isOpen && State == CylinderState.Open)
  144. {
  145. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"检查到气缸 {Name} 已经开");
  146. return true;
  147. }
  148. if (!isOpen && State == CylinderState.Close)
  149. {
  150. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"检查到气缸 {Name} 已经关");
  151. return true;
  152. }
  153. }
  154. ONSetPoint = isOpen;
  155. reason += _error;
  156. reason += " ";
  157. OFFSetPoint = !isOpen;
  158. reason += _error;
  159. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  160. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{(isOpen ? "打开" : "关闭")} 气缸 {Name}");
  161. if (Name == "LoadLockArm")
  162. _timer.Start(10 * 1000);
  163. else if (Name == "SlitDoor")
  164. _timer.Start(5 * 1000);
  165. else
  166. _timer.Start(2000);
  167. return true;
  168. }
  169. public void Monitor()
  170. {
  171. try
  172. {
  173. if (_timer.IsTimeout())
  174. {
  175. _timer.Stop();
  176. if (State != _operation)
  177. {
  178. if (_operation == CylinderState.Open)
  179. {
  180. if (!_doON.Check(true, out var reason))
  181. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "气缸信号无法打开, interlock, " + reason);
  182. else
  183. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "气缸信号仍然关闭");
  184. }
  185. else
  186. {
  187. if (!_doON.Check(false, out var reason))
  188. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "气缸信号无法关闭, interlock, " + reason);
  189. else
  190. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "气缸信号仍然打开");
  191. }
  192. }
  193. _operation = (CylinderState)SetPoint;
  194. }
  195. else if (_timer.IsIdle())
  196. {
  197. _trigReset.CLK = SetPoint != (int)_operation; // fire event only check at first, SetPoint set by interlock
  198. if (_trigReset.Q)
  199. {
  200. if (_operation == CylinderState.Open)
  201. {
  202. LOG.Write(eEvent.ERR_DEVICE_INFO, Module,!_doON.Check(true, out var reason)
  203. ? $"气缸信号 {Display} was Close,Reason:{reason}"
  204. : $"气缸信号 {Display} was Close,Reason PLC kept");
  205. }
  206. else
  207. {
  208. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, !_doON.Check(false, out var reason)
  209. ? $"气缸信号 {Display} was Open,Reason:{reason}"
  210. : $"气缸信号 {Display} was Open,Reason PLC Kept");
  211. }
  212. _operation = (CylinderState)SetPoint;
  213. }
  214. }
  215. _trigError.CLK = State == CylinderState.Error;
  216. if (_trigError.Q)
  217. {
  218. //EV.PostAlarmLog(Module, "气缸状态错误");
  219. }
  220. //if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))
  221. //{
  222. // OFFSetPoint = false;
  223. // ONSetPoint = false;
  224. //}
  225. }
  226. catch (Exception ex)
  227. {
  228. //LOG.Write(ex);
  229. }
  230. }
  231. public void Reset()
  232. {
  233. _trigReset.RST = true;
  234. _trigError.RST = true;
  235. }
  236. }
  237. }