IoThrottleValve.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.Util;
  9. namespace Aitex.Core.RT.Device.Unit
  10. {
  11. public class IoThrottleValve : BaseDevice, IDevice
  12. {
  13. public struct Context
  14. {
  15. public string tvName;
  16. public string aoPressureModeName;
  17. public string aoPressureSetPointName;
  18. public string aoPositionSetPointName;
  19. public string aiPressureFeedbackName;
  20. public string aiPositionFeedbackName;
  21. public string aiStateName;
  22. };
  23. public PressureCtrlMode PressureMode
  24. {
  25. get
  26. {
  27. if (_aoPressureMode == null)
  28. return PressureCtrlMode.TVPositionCtrl;
  29. return Math.Abs(_aoPressureMode.Value - 2) < 0.1 ? PressureCtrlMode.TVPositionCtrl : PressureCtrlMode.TVPressureCtrl;
  30. }
  31. set
  32. {
  33. if (_aoPositionSetPoint == null || _aoPressureSetPoint == null || _aoPressureMode == null)
  34. return;
  35. float setpoint = value == PressureCtrlMode.TVPositionCtrl ? 2 : 1;
  36. if (Math.Abs(_aoPressureMode.Value - setpoint) > 0.01)
  37. {
  38. if (value == PressureCtrlMode.TVPositionCtrl)
  39. {
  40. _aoPositionSetPoint.Value = (short)PositionFeedback;
  41. }
  42. else
  43. {
  44. _aoPressureSetPoint.Value = (short)PressureFeedback;
  45. }
  46. _aoPressureMode.Value = (short)setpoint;
  47. }
  48. }
  49. }
  50. public int State
  51. {
  52. get
  53. {
  54. return _aiState == null ? 1 : (int)_aiState.Value;
  55. }
  56. }
  57. [Subscription(AITThrottleValvePropertyName.TVPositionSetPoint)]
  58. public float PositionSetpoint
  59. {
  60. get
  61. {
  62. return _aoPositionSetPoint == null ? 0 : _aoPositionSetPoint.Value;
  63. }
  64. set
  65. {
  66. if (_aoPositionSetPoint == null)
  67. return;
  68. _aoPositionSetPoint.Value = (short)value;
  69. }
  70. }
  71. [Subscription(AITThrottleValvePropertyName.TVPosition)]
  72. public float PositionFeedback
  73. {
  74. get
  75. {
  76. return _aiPositionFeedback == null ? 0 : _aiPositionFeedback.Value;
  77. }
  78. }
  79. [Subscription(AITThrottleValvePropertyName.TVPressureSetPoint)]
  80. public float PressureSetpoint
  81. {
  82. get
  83. {
  84. return _aoPressureSetPoint == null ? 0 : _aoPressureSetPoint.Value;
  85. }
  86. set
  87. {
  88. if (_aoPressureSetPoint == null)
  89. return;
  90. _aoPressureSetPoint.Value = (short)value;
  91. }
  92. }
  93. private AITThrottleValveData DeviceData
  94. {
  95. get
  96. {
  97. AITThrottleValveData data = new AITThrottleValveData()
  98. {
  99. DeviceName = Name,
  100. DeviceSchematicId = DeviceID,
  101. DisplayName = Display,
  102. Mode = (int)PressureMode,
  103. PositionFeedback = PositionFeedback,
  104. PositionSetPoint = PositionSetpoint,
  105. PressureFeedback = PressureFeedback,
  106. PressureSetPoint = PressureSetpoint,
  107. State = State,
  108. };
  109. return data;
  110. }
  111. }
  112. [Subscription(AITThrottleValvePropertyName.TVPressure)]
  113. public float PressureFeedback
  114. {
  115. get
  116. {
  117. return _aiPressureFeedback == null ? 0 : _aiPressureFeedback.Value;
  118. }
  119. }
  120. public bool IsPumpMode
  121. {
  122. get; set;
  123. }
  124. public bool IsIndependent
  125. {
  126. get; set;
  127. }
  128. public bool IsOffline
  129. {
  130. get
  131. {
  132. return _diOffline != null && _diOffline.RawData;
  133. }
  134. }
  135. private DIAccessor _diOffline;
  136. private AIAccessor _aiPressureFeedback = null;
  137. private AIAccessor _aiPositionFeedback = null;
  138. private AOAccessor _aoPressureSetPoint = null;
  139. private AOAccessor _aoPositionSetPoint = null;
  140. private AOAccessor _aoPressureMode = null;
  141. private AIAccessor _aiState = null;
  142. private R_TRIG _tvStatusAlmTrig = new R_TRIG();
  143. private R_TRIG _trigOffline = new R_TRIG();
  144. public IoThrottleValve(string module, XmlElement node, string ioModule = "")
  145. {
  146. base.Module = module;
  147. base.Name = node.GetAttribute("id");
  148. base.Display = node.GetAttribute("display");
  149. base.DeviceID = node.GetAttribute("schematicId");
  150. _aiPositionFeedback = ParseAiNode("aiPositionFeedback", node);
  151. _aiPressureFeedback = ParseAiNode("aiPressureFeedback", node);
  152. _aoPositionSetPoint = ParseAoNode("aoPositionSetPoint", node);
  153. _aoPressureSetPoint = ParseAoNode("aoPressureSetPoint", node);
  154. _aiState = ParseAiNode("aiState", node);
  155. _aoPressureMode = ParseAoNode("aoPressureMode", node);
  156. _diOffline = ParseDiNode("diOffline", node);
  157. }
  158. public bool Initialize()
  159. {
  160. DATA.Subscribe(string.Format("Device.{0}.{1}", Module, Name), () => DeviceData);
  161. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  162. PressureMode = PressureCtrlMode.TVPressureCtrl;
  163. DEVICE.Register(String.Format("{0}.{1}", Name, AITThrottleValveOperation.SetMode), (out string reason, int time, object[] param) =>
  164. {
  165. PressureMode = (PressureCtrlMode)Enum.Parse(typeof(PressureCtrlMode), (string)param[0], true);
  166. reason = string.Format("Throttle valve set to {0} mode", PressureMode);
  167. return true;
  168. });
  169. DEVICE.Register(String.Format("{0}.{1}", Name, AITThrottleValveOperation.SetPosition), (out string reason, int time, object[] param) =>
  170. {
  171. double target = Convert.ToDouble((string)param[0]);
  172. if (PressureMode != PressureCtrlMode.TVPositionCtrl)
  173. {
  174. reason = "Throttle valve in pressure mode, can not set position";
  175. return false;
  176. }
  177. PositionSetpoint = (float)target;
  178. reason = string.Format("position set to {0}%", target.ToString("F1"));
  179. return true;
  180. });
  181. DEVICE.Register(String.Format("{0}.{1}", Name, AITThrottleValveOperation.SetPressure), (out string reason, int time, object[] param) =>
  182. {
  183. if (PressureMode == PressureCtrlMode.TVPositionCtrl)
  184. {
  185. reason = "Throttle valve is in positon conrol mode, can not set pressure";
  186. return false;
  187. }
  188. double target = Convert.ToDouble((string)param[0]);
  189. PressureSetpoint = (float)target;
  190. reason = string.Format("pressure set {0} mTorr", target);
  191. return true;
  192. });
  193. return true;
  194. }
  195. public void Terminate()
  196. {
  197. }
  198. public void Monitor()
  199. {
  200. try
  201. {
  202. _tvStatusAlmTrig.CLK = State != 1;
  203. if (_tvStatusAlmTrig.Q)
  204. {
  205. EV.PostMessage(Module, EventEnum.ThrottleValveAbnormal, Module);
  206. }
  207. _trigOffline.CLK = IsOffline;
  208. if (_trigOffline.Q)
  209. {
  210. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Throttle Valve Offline");
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. LOG.Write(ex);
  216. }
  217. }
  218. public void Reset()
  219. {
  220. _tvStatusAlmTrig.RST = true;
  221. _trigOffline.RST = true;
  222. }
  223. public void SetPositionMode(int position)
  224. {
  225. PressureMode = PressureCtrlMode.TVPositionCtrl;
  226. PositionSetpoint = (float)position;
  227. }
  228. }
  229. }