IoCylinder.cs 9.4 KB

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