IoMfc.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.SCCore;
  8. using Aitex.Core.RT.Tolerance;
  9. using Aitex.Core.Util;
  10. namespace Aitex.Core.RT.Device.Unit
  11. {
  12. [Obsolete]
  13. public class IoMfc : BaseDevice, IDevice
  14. {
  15. public string Unit
  16. {
  17. get; set;
  18. }
  19. //[Subscription(AITMfcDataPropertyName.Scale)]
  20. public double Scale
  21. {
  22. get
  23. {
  24. if (_scN2Scale == null || _scScaleFactor == null)
  25. return 0;
  26. return _scN2Scale.IntValue * _scScaleFactor.IntValue;
  27. }
  28. }
  29. [Subscription(AITMfcDataPropertyName.SetPoint)]
  30. public double SetPoint
  31. {
  32. get
  33. {
  34. if (_aoFlow != null)
  35. {
  36. return _aoFlow.Value;
  37. }
  38. return 0;
  39. }
  40. set
  41. {
  42. if (_aoFlow != null)
  43. {
  44. _aoFlow.Value = (short)value;
  45. }
  46. }
  47. }
  48. [Subscription(AITMfcDataPropertyName.DefaultSetPoint)]
  49. public double DefaultSetPoint
  50. {
  51. get
  52. {
  53. if (_scDefaultSetPoint != null)
  54. return _scDefaultSetPoint.DoubleValue;
  55. return 0;
  56. }
  57. }
  58. [Subscription(AITMfcDataPropertyName.FeedBack)]
  59. public double FeedBack
  60. {
  61. get
  62. {
  63. if (_aiFlow != null)
  64. return (_scRegulationFactor != null && _scRegulationFactor.DoubleValue > 0) ? _aiFlow.Value / _scRegulationFactor.DoubleValue : _aiFlow.Value;
  65. return 0;
  66. }
  67. }
  68. //[Subscription(AITMfcDataPropertyName.IsOutOfTolerance)]
  69. public bool IsOutOfTolerance
  70. {
  71. get
  72. {
  73. return _toleranceChecker.Result;
  74. }
  75. }
  76. //[Subscription(AITMfcDataPropertyName.IsEnableAlarm)]
  77. public bool EnableAlarm
  78. {
  79. get
  80. {
  81. if (_scEnableAlarm != null)
  82. return _scEnableAlarm.BoolValue;
  83. return false;
  84. }
  85. }
  86. //[Subscription(AITMfcDataPropertyName.AlarmRange)]
  87. public double AlarmRange
  88. {
  89. get
  90. {
  91. if (_scAlarmRange != null)
  92. return _scAlarmRange.DoubleValue;
  93. return 0;
  94. }
  95. }
  96. //[Subscription(AITMfcDataPropertyName.AlarmTime)]
  97. public double AlarmTime
  98. {
  99. get
  100. {
  101. if (_scAlarmTime != null)
  102. return _scAlarmTime.DoubleValue;
  103. return 0;
  104. }
  105. }
  106. //[Subscription(AITMfcDataPropertyName.IsOffline)]
  107. public bool IsOffline
  108. {
  109. get
  110. {
  111. if (_diOffline != null)
  112. return _diOffline.Value;
  113. return false;
  114. }
  115. }
  116. public string DisplayName
  117. {
  118. get
  119. {
  120. if (_scGasName != null)
  121. return _scGasName.StringValue;
  122. return Display;
  123. }
  124. }
  125. private DeviceTimer rampTimer = new DeviceTimer();
  126. private double rampTarget;
  127. private double rampInitValue;
  128. private int rampTime;
  129. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  130. private AIAccessor _aiFlow;
  131. private AOAccessor _aoFlow;
  132. private AOAccessor _aoRange;
  133. private DIAccessor _diOffline;
  134. private SCConfigItem _scGasName;
  135. private SCConfigItem _scEnable;
  136. private SCConfigItem _scN2Scale;
  137. private SCConfigItem _scScaleFactor;
  138. private SCConfigItem _scAlarmRange;
  139. private SCConfigItem _scEnableAlarm;
  140. private SCConfigItem _scAlarmTime;
  141. private SCConfigItem _scDefaultSetPoint;
  142. private SCConfigItem _scRegulationFactor;
  143. private R_TRIG _trigOffline = new R_TRIG();
  144. private string _uniqueName;
  145. public IoMfc(string module, XmlElement node, string ioModule = "")
  146. {
  147. Unit = node.GetAttribute("unit");
  148. base.Module = module;
  149. base.Name = node.GetAttribute("id");
  150. base.Display = node.GetAttribute("display");
  151. base.DeviceID = node.GetAttribute("schematicId");
  152. _aoRange = ParseAoNode("aoRange", node, ioModule);
  153. _diOffline = ParseDiNode("diOffline", node, ioModule);
  154. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  155. _aoFlow = ParseAoNode("aoFlow", node, ioModule);
  156. _scGasName = ParseScNode("scGasName", node, ioModule);
  157. _scEnable = ParseScNode("scEnable", node, ioModule);
  158. _scN2Scale = ParseScNode("scN2Scale", node, ioModule);
  159. _scScaleFactor = ParseScNode("scScaleFactor", node, ioModule);
  160. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule);
  161. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule);
  162. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule);
  163. _scDefaultSetPoint = ParseScNode("scDefaultSetPoint", node, ioModule);
  164. _scRegulationFactor = ParseScNode("scFlowRegulationFactor", node, ioModule);
  165. _uniqueName = $"{Module}.{Name}";
  166. }
  167. public bool Initialize()
  168. {
  169. DATA.Subscribe($"{Module}.{Name}", () =>
  170. {
  171. AITMfcData data = new AITMfcData()
  172. {
  173. Type = "MFC",
  174. UniqueName = _uniqueName,
  175. DeviceName = Name,
  176. DeviceSchematicId = DeviceID,
  177. DisplayName = DisplayName,
  178. FeedBack = FeedBack,
  179. SetPoint = SetPoint,
  180. Scale = Scale,
  181. IsOffline = IsOffline,
  182. };
  183. return data;
  184. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  185. DATA.Subscribe($"{Module}.{Name}.FeedBack", ()=>FeedBack);
  186. DEVICE.Register($"{Module}.{Name}.{AITMfcOperation.Ramp}", (out string reason, int time, object[] param) =>
  187. {
  188. double target = Convert.ToDouble((string)param[0]);
  189. target = Math.Min(target, Scale);
  190. target = Math.Max(target, 0);
  191. Ramp(target, time);
  192. reason = string.Format("{0} ramp to {1}{2}", Display, target, Unit);
  193. return true;
  194. });
  195. //@AAA use recipe
  196. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  197. {
  198. double target = Convert.ToDouble((string)param[0]);
  199. target = Math.Min(target, Scale);
  200. target = Math.Max(target, 0);
  201. Ramp(target, time);
  202. reason = string.Format("{0} ramp to {1}{2}", Display, target, Unit);
  203. return true;
  204. });
  205. return true;
  206. }
  207. public void Monitor()
  208. {
  209. Ramping();
  210. CheckTolerance();
  211. if (_aoRange != null)
  212. _aoRange.Value = (short)Scale;
  213. _trigOffline.CLK = IsOffline;
  214. if (_trigOffline.Q)
  215. {
  216. EV.PostMessage(Module, EventEnum.DefaultAlarm, string.Format("{0} is offline", DisplayName));
  217. }
  218. }
  219. public void Reset()
  220. {
  221. _toleranceChecker.Reset(AlarmTime);
  222. _trigOffline.RST = true;
  223. }
  224. public void Terminate()
  225. {
  226. Ramp(DefaultSetPoint, 0);
  227. }
  228. public void Ramp(int time)
  229. {
  230. Ramp(DefaultSetPoint, time);
  231. }
  232. public void Ramp(double target, int time)
  233. {
  234. target = Math.Max(0, target);
  235. target = Math.Min(Scale, target);
  236. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值。零漂问题
  237. rampTime = time;
  238. rampTarget = target;
  239. rampTimer.Start(rampTime);
  240. }
  241. public void StopRamp()
  242. {
  243. Ramp(SetPoint, 0);
  244. }
  245. private void Ramping()
  246. {
  247. if (rampTimer.IsTimeout() || rampTime == 0)
  248. {
  249. SetPoint = rampTarget;
  250. }
  251. else
  252. {
  253. SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;
  254. }
  255. }
  256. private void CheckTolerance()
  257. {
  258. if (!EnableAlarm)
  259. return;
  260. _toleranceChecker.Monitor(FeedBack, (SetPoint - Math.Abs(AlarmRange)), (SetPoint + Math.Abs(AlarmRange)), AlarmTime);
  261. if (_toleranceChecker.Trig)
  262. {
  263. EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
  264. String.Format("Out of range in {0} seconds", AlarmTime.ToString("0")));
  265. }
  266. }
  267. }
  268. }