IoCylinder.cs 12 KB

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