IoCylinder.cs 11 KB

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