IoMfc3.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.RT.OperationCenter;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.RT.Tolerance;
  11. using Aitex.Core.Util;
  12. using MECF.Framework.Common.Event;
  13. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs;
  14. namespace Aitex.Core.RT.Device.Unit
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public class IoMfc3 : BaseDevice, IDevice, IMfc
  20. {
  21. public string Unit
  22. {
  23. get; set;
  24. }
  25. public double Scale
  26. {
  27. get
  28. {
  29. if (_scN2Scale == null || _scScaleFactor == null)
  30. return 0;
  31. return _scN2Scale.DoubleValue * _scScaleFactor.DoubleValue;
  32. }
  33. }
  34. public double SetPoint
  35. {
  36. get
  37. {
  38. if (_aoFlowL == null || _aoFlowH == null)
  39. return 0;
  40. short valueL = _aoFlowL.Value;
  41. short valueH = _aoFlowH.Value;
  42. UInt32 tmp = (ushort)valueL + (UInt32)(((ushort)valueH) << 16);
  43. double value = tmp > Int32.MaxValue ? (double)((~tmp + 1) * -1) : (double)tmp;
  44. return value;
  45. }
  46. set
  47. {
  48. if (_aoFlowL != null && _aoFlowH != null)
  49. {
  50. _aoFlowL.Value = (short)((int)value & 0xFFFF);
  51. _aoFlowH.Value = (short)((int)value >> 16 & 0xFFFF);
  52. }
  53. }
  54. }
  55. public double DefaultSetPoint
  56. {
  57. get
  58. {
  59. if (_scDefaultSetPoint != null)
  60. return _scDefaultSetPoint.DoubleValue;
  61. return 0;
  62. }
  63. }
  64. public double FeedBack
  65. {
  66. get
  67. {
  68. if (_aiFlowL == null || _aiFlowH == null)
  69. return 0;
  70. short valueL = _aiFlowL.Value;
  71. short valueH = _aiFlowH.Value;
  72. UInt32 tmp = (ushort)valueL + (UInt32)(((ushort)valueH) << 16);
  73. double value = tmp > Int32.MaxValue ? (double)((~tmp + 1) * -1) : (double)tmp;
  74. return (_scRegulationFactor != null && _scRegulationFactor.DoubleValue > 0.001) ? value / _scRegulationFactor.DoubleValue : value;
  75. }
  76. }
  77. public bool EnableAlarm
  78. {
  79. get
  80. {
  81. if (_scEnableAlarm != null)
  82. return _scEnableAlarm.BoolValue;
  83. return false;
  84. }
  85. }
  86. public double AlarmRange
  87. {
  88. get
  89. {
  90. if (_scAlarmRange != null)
  91. return _scAlarmRange.DoubleValue;
  92. return 0;
  93. }
  94. }
  95. public double AlarmTime
  96. {
  97. get
  98. {
  99. if (_scAlarmTime != null)
  100. return _scAlarmTime.IntValue;
  101. return 0;
  102. }
  103. }
  104. public double WarningRange
  105. {
  106. get
  107. {
  108. if (_scWarningRange != null)
  109. return _scWarningRange.DoubleValue;
  110. return 0;
  111. }
  112. }
  113. public double WarningTime
  114. {
  115. get
  116. {
  117. if (_scWarningTime != null)
  118. return _scWarningTime.IntValue;
  119. return 0;
  120. }
  121. }
  122. private AITMfcData DeviceData
  123. {
  124. get
  125. {
  126. AITMfcData data = new AITMfcData()
  127. {
  128. UniqueName = $"{Module}.{Name}",
  129. Type = "MFC",
  130. Module = Module,
  131. DeviceName = Name,
  132. DeviceSchematicId = DeviceID,
  133. DisplayName = DisplayName,
  134. FeedBack = FeedBack,
  135. SetPoint = SetPoint,
  136. Scale = Scale,
  137. IsWarning = !AlarmToleranceWarning.IsAcknowledged,
  138. IsError = !AlarmToleranceAlarm.IsAcknowledged,
  139. };
  140. return data;
  141. }
  142. }
  143. public string DisplayName
  144. {
  145. get
  146. {
  147. if (_scGasName != null)
  148. return _scGasName.StringValue;
  149. return Display;
  150. }
  151. }
  152. private DeviceTimer _rampTimer = new DeviceTimer();
  153. private double _rampTarget;
  154. private double _rampInitValue;
  155. private int _rampTime;
  156. private ToleranceChecker _toleranceCheckerWarning = new ToleranceChecker();
  157. private ToleranceChecker _toleranceCheckerAlarm = new ToleranceChecker();
  158. private AIAccessor _aiFlowL;
  159. private AIAccessor _aiFlowH;
  160. private AOAccessor _aoFlowL;
  161. private AOAccessor _aoFlowH;
  162. private DOAccessor _doOpen;
  163. private DOAccessor _doClose;
  164. protected SCConfigItem _scGasName;
  165. protected SCConfigItem _scEnable;
  166. private SCConfigItem _scN2Scale;
  167. private SCConfigItem _scScaleFactor;
  168. private SCConfigItem _scAlarmRange;
  169. private SCConfigItem _scEnableAlarm;
  170. private SCConfigItem _scAlarmTime;
  171. private SCConfigItem _scDefaultSetPoint;
  172. private SCConfigItem _scRegulationFactor;
  173. protected SCConfigItem _scWarningTime;
  174. protected SCConfigItem _scWarningRange;
  175. public AlarmEventItem AlarmToleranceWarning { get; set; }
  176. public AlarmEventItem AlarmToleranceAlarm { get; set; }
  177. private bool _isFloatAioType = false;
  178. public IoMfc3(string module, XmlElement node, string ioModule = "")
  179. {
  180. Unit = node.GetAttribute("unit");
  181. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  182. base.Name = node.GetAttribute("id");
  183. base.Display = node.GetAttribute("display");
  184. base.DeviceID = node.GetAttribute("schematicId");
  185. _aiFlowL = ParseAiNode("aiFlowL", node, ioModule);
  186. _aiFlowH = ParseAiNode("aiFlowH", node, ioModule);
  187. _aoFlowL = ParseAoNode("aoFlowL", node, ioModule);
  188. _aoFlowH = ParseAoNode("aoFlowH", node, ioModule);
  189. _doOpen = ParseDoNode("doOpen", node, ioModule);
  190. _doClose = ParseDoNode("doClose", node, ioModule);
  191. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType")=="float");
  192. string scBasePath = node.GetAttribute("scBasePath");
  193. if (string.IsNullOrEmpty(scBasePath))
  194. scBasePath = $"{Module}.{Name}";
  195. else
  196. {
  197. scBasePath = scBasePath.Replace("{module}", Module);
  198. }
  199. _scGasName = SC.GetConfigItem($"{scBasePath}.{Name}.GasName");
  200. _scEnable = SC.GetConfigItem($"{scBasePath}.{Name}.Enable");
  201. _scN2Scale = ParseScNode("scN2Scale", node, ioModule, $"{scBasePath}.{Name}.N2Scale");
  202. _scScaleFactor = ParseScNode("scScaleFactor", node, ioModule, $"{scBasePath}.{Name}.ScaleFactor");
  203. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule, $"{scBasePath}.{Name}.AlarmRange");
  204. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{scBasePath}.{Name}.EnableAlarm");
  205. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule, $"{scBasePath}.{Name}.AlarmTime");
  206. _scDefaultSetPoint = ParseScNode("scDefaultSetPoint", node, ioModule, $"{scBasePath}.{Name}.DefaultSetPoint");
  207. _scWarningRange = SC.GetConfigItem($"{scBasePath}.{Name}.WarningRange");
  208. _scWarningTime = SC.GetConfigItem($"{scBasePath}.{Name}.WarningTime");
  209. _scRegulationFactor = ParseScNode("scFlowRegulationFactor", node, ioModule, $"{scBasePath}.{Name}.RegulationFactor");
  210. }
  211. public bool Initialize()
  212. {
  213. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  214. DATA.Subscribe($"{Module}.{Name}.Feedback", () => FeedBack);
  215. DATA.Subscribe($"{Module}.{Name}.SetPoint", () => SetPoint);
  216. OP.Subscribe($"{Module}.{Name}.Ramp", (out string reason, int time, object[] param) =>
  217. {
  218. double target = Convert.ToDouble(param[0].ToString());
  219. if (target < 0 || target > Scale)
  220. {
  221. reason = $"set {Display} value {target} out of range [0, {Scale}] {Unit}";
  222. return false;
  223. }
  224. Ramp(target, time);
  225. if (time > 0)
  226. {
  227. reason = $"{Display} ramp to {target} {Unit} in {time} seconds";
  228. }
  229. else
  230. {
  231. reason = $"{Display} ramp to {target} {Unit}";
  232. }
  233. return true;
  234. });
  235. AlarmToleranceWarning = SubscribeAlarm($"{Module}.{Name}.ToleranceWarning", "", ResetWarningChecker, EventLevel.Warning);
  236. AlarmToleranceAlarm = SubscribeAlarm($"{Module}.{Name}.ToleranceAlarm", "", ResetAlarmChecker);
  237. return true;
  238. }
  239. public void Monitor()
  240. {
  241. MonitorRamping();
  242. MonitorTolerance();
  243. }
  244. public bool ResetWarningChecker()
  245. {
  246. _toleranceCheckerWarning.Reset(WarningTime);
  247. return true;
  248. }
  249. public bool ResetAlarmChecker()
  250. {
  251. _toleranceCheckerAlarm.Reset(AlarmTime);
  252. return true;
  253. }
  254. public void Reset()
  255. {
  256. AlarmToleranceWarning.Reset();
  257. AlarmToleranceAlarm.Reset();
  258. }
  259. public void Terminate()
  260. {
  261. Ramp(DefaultSetPoint, 0);
  262. }
  263. public bool Ramp(double flowSetPoint, int time, out string reason)
  264. {
  265. if (HasAlarm)
  266. {
  267. reason = $"{DisplayName} in error status, can not flow";
  268. return false;
  269. }
  270. if (flowSetPoint < 0 || flowSetPoint > Scale)
  271. {
  272. reason = $"{DisplayName} range is [0, {Scale}], can not flow {flowSetPoint}";
  273. return false;
  274. }
  275. if (time > 0)
  276. {
  277. EV.PostInfoLog(Module, $"Set {DisplayName} flow to {flowSetPoint} {Unit} in {time/1000:F0} seconds");
  278. }
  279. else
  280. {
  281. EV.PostInfoLog(Module, $"Set {DisplayName} flow to {flowSetPoint} {Unit}");
  282. }
  283. Ramp(flowSetPoint, time);
  284. reason = string.Empty;
  285. return true;
  286. }
  287. public void Ramp(int time)
  288. {
  289. Ramp(DefaultSetPoint, time);
  290. }
  291. public void Ramp(double target, int time)
  292. {
  293. target = Math.Max(0, target);
  294. target = Math.Min(Scale, target);
  295. _rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值.零漂问题
  296. _rampTime = time;
  297. _rampTarget = target;
  298. _rampTimer.Start(_rampTime);
  299. }
  300. public void StopRamp()
  301. {
  302. Ramp(SetPoint, 0);
  303. }
  304. private void MonitorRamping()
  305. {
  306. if (!_rampTimer.IsIdle())
  307. {
  308. if (_rampTimer.IsTimeout() || _rampTime == 0)
  309. {
  310. _rampTimer.Stop();
  311. SetPoint = _rampTarget;
  312. }
  313. else
  314. {
  315. SetPoint = _rampInitValue + (_rampTarget - _rampInitValue) * _rampTimer.GetElapseTime() / _rampTime;
  316. }
  317. }
  318. }
  319. public bool SetOpen(bool isOpen, out string reason)
  320. {
  321. reason = string.Empty;
  322. if (_doOpen != null)
  323. {
  324. if (!_doOpen.SetValue(isOpen, out reason))
  325. {
  326. LOG.Write(reason);
  327. return false;
  328. }
  329. }
  330. if (_doClose != null)
  331. {
  332. if (!_doClose.SetValue(!isOpen, out reason))
  333. {
  334. LOG.Write(reason);
  335. return false;
  336. }
  337. }
  338. return true;
  339. }
  340. private void MonitorTolerance()
  341. {
  342. if (!EnableAlarm || SetPoint < 0.01)
  343. {
  344. _toleranceCheckerWarning.RST = true;
  345. _toleranceCheckerAlarm.RST = true;
  346. return;
  347. }
  348. _toleranceCheckerWarning.Monitor(FeedBack, (SetPoint - Math.Abs(WarningRange)), (SetPoint + Math.Abs(WarningRange)), WarningTime);
  349. if (_toleranceCheckerWarning.Trig)
  350. {
  351. AlarmToleranceWarning.Description = $"{Display} flow out of range {WarningRange} {Unit} in {WarningTime:F0} seconds";
  352. AlarmToleranceWarning.Set();
  353. }
  354. _toleranceCheckerAlarm.Monitor(FeedBack, (SetPoint - Math.Abs(AlarmRange)), (SetPoint + Math.Abs(AlarmRange)), AlarmTime);
  355. if (_toleranceCheckerAlarm.Trig)
  356. {
  357. AlarmToleranceAlarm.Description = $"{Display} flow out of range {AlarmRange} {Unit} in {AlarmTime:F0} seconds";
  358. AlarmToleranceAlarm.Set();
  359. }
  360. }
  361. }
  362. }