IoCylinder.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", (s, objects) => SetCylinder(true, out _));
  121. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", (s, objects) => SetCylinder(false, out _));
  122. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.SetState}", (out string reason, int time, object[] param) =>
  123. {
  124. bool isUp = (string)param[0] == "Up" ? true : false;
  125. SetCylinder(isUp, out reason);
  126. return true;
  127. });
  128. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}",
  129. (out string reason, int time, object[] param) => SetCylinder(true, out reason));
  130. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}",
  131. (out string reason, int time, object[] param) => SetCylinder(false, out reason));
  132. return true;
  133. }
  134. public void Terminate()
  135. {
  136. OFFSetPoint = false;
  137. ONSetPoint = false;
  138. }
  139. public bool SetCylinder(bool isOpen, out string reason)
  140. {
  141. reason = "";
  142. if (Name == "LoadLockArm")
  143. {
  144. if (isOpen && State == CylinderState.Open)
  145. {
  146. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"检查到气缸 {Name} 已经开");
  147. return true;
  148. }
  149. if (!isOpen && State == CylinderState.Close)
  150. {
  151. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"检查到气缸 {Name} 已经关");
  152. return true;
  153. }
  154. }
  155. ONSetPoint = isOpen;
  156. reason += _error;
  157. reason += " ";
  158. OFFSetPoint = !isOpen;
  159. reason += _error;
  160. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  161. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{(isOpen ? "打开" : "关闭")} 气缸 {Name}");
  162. if (Name == "LoadLockArm")
  163. _timer.Start(10 * 1000);
  164. else if (Name == "SlitDoor")
  165. _timer.Start(5 * 1000);
  166. else
  167. _timer.Start(2000);
  168. return true;
  169. }
  170. public void Monitor()
  171. {
  172. try
  173. {
  174. if (_timer.IsTimeout())
  175. {
  176. _timer.Stop();
  177. if (State != _operation)
  178. {
  179. if (_operation == CylinderState.Open)
  180. {
  181. if (!_doON.Check(true, out var reason))
  182. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}气缸信号无法打开, interlock, " + reason);
  183. else
  184. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}气缸信号仍然关闭");
  185. }
  186. else
  187. {
  188. if (!_doON.Check(false, out var reason))
  189. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}气缸信号无法关闭, interlock, " + reason);
  190. else
  191. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}气缸信号仍然打开");
  192. }
  193. }
  194. _operation = (CylinderState)SetPoint;
  195. }
  196. else if (_timer.IsIdle())
  197. {
  198. _trigReset.CLK = SetPoint != (int)_operation; // fire event only check at first, SetPoint set by interlock
  199. m_SetPoint = SetPoint;
  200. if (_trigReset.Q)
  201. {
  202. if (_operation == CylinderState.Open)
  203. {
  204. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, !_doON.Check(true, out var reason)
  205. ? $"气缸信号 {Display} was Close,Reason:{reason}"
  206. : $"气缸信号 {Display} was Close,Reason PLC kept");
  207. }
  208. else
  209. {
  210. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, !_doON.Check(false, out var reason)
  211. ? $"气缸信号 {Display} was Open,Reason:{reason}"
  212. : $"气缸信号 {Display} was Open,Reason PLC Kept");
  213. }
  214. _operation = (CylinderState)SetPoint;
  215. }
  216. }
  217. _trigError.CLK = State == CylinderState.Error;
  218. if (_trigError.Q)
  219. {
  220. //EV.PostAlarmLog(Module, "气缸状态错误");
  221. }
  222. //if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))
  223. //{
  224. // OFFSetPoint = false;
  225. // ONSetPoint = false;
  226. //}
  227. }
  228. catch (Exception ex)
  229. {
  230. LOG.WriteExeption(ex);
  231. }
  232. }
  233. public void Reset()
  234. {
  235. _trigReset.RST = true;
  236. _trigError.RST = true;
  237. }
  238. }
  239. }