MfcBase.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using Aitex.Core.Common.DeviceData;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Device;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.IOCore;
  12. using Aitex.Core.RT.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.RT.Tolerance;
  15. using Aitex.Core.Util;
  16. namespace MECF.Framework.Common.Device.Bases
  17. {
  18. public abstract class MfcBase : BaseDevice, IDevice
  19. {
  20. public string Unit
  21. {
  22. get; set;
  23. }
  24. public float Scale
  25. {
  26. get
  27. {
  28. if (_scN2Scale == null || _scScaleFactor == null)
  29. return 0;
  30. return (float)( _scN2Scale.DoubleValue * _scScaleFactor.DoubleValue);
  31. }
  32. }
  33. public virtual float SetPoint { get; set; }
  34. public virtual float FeedBack { get; set; }
  35. public bool IsOutOfTolerance
  36. {
  37. get
  38. {
  39. return _toleranceChecker.Result;
  40. }
  41. }
  42. public bool EnableAlarm
  43. {
  44. get
  45. {
  46. if (_scEnableAlarm != null)
  47. return _scEnableAlarm.BoolValue;
  48. return false;
  49. }
  50. }
  51. public double AlarmRange
  52. {
  53. get
  54. {
  55. if (_scAlarmRange != null)
  56. return _scAlarmRange.DoubleValue;
  57. return 0;
  58. }
  59. }
  60. public double AlarmTime
  61. {
  62. get
  63. {
  64. if (_scAlarmTime != null)
  65. return _scAlarmTime.IntValue;
  66. return 0;
  67. }
  68. }
  69. public string DisplayName
  70. {
  71. get
  72. {
  73. if (_scGasName != null)
  74. return _scGasName.StringValue;
  75. return Display;
  76. }
  77. }
  78. public virtual AITMfcData DeviceData
  79. {
  80. get
  81. {
  82. AITMfcData data = new AITMfcData()
  83. {
  84. UniqueName = _uniqueName,
  85. Type = "MFC",
  86. DeviceName = Name,
  87. DeviceSchematicId = DeviceID,
  88. DisplayName = DisplayName,
  89. FeedBack = FeedBack,
  90. SetPoint = SetPoint,
  91. Scale = Scale,
  92. };
  93. return data;
  94. }
  95. }
  96. private DeviceTimer rampTimer = new DeviceTimer();
  97. private float rampTarget;
  98. private float rampInitValue;
  99. private int rampTime;
  100. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  101. private SCConfigItem _scGasName;
  102. private SCConfigItem _scEnable;
  103. private SCConfigItem _scN2Scale;
  104. private SCConfigItem _scScaleFactor;
  105. private SCConfigItem _scAlarmRange;
  106. private SCConfigItem _scEnableAlarm;
  107. private SCConfigItem _scAlarmTime;
  108. private SCConfigItem _scDefaultSetPoint;
  109. private SCConfigItem _scRegulationFactor;
  110. private R_TRIG _trigOffline = new R_TRIG();
  111. private string _uniqueName;
  112. public MfcBase( ):base()
  113. {
  114. }
  115. public MfcBase(string module, XmlElement node, string ioModule = "")
  116. {
  117. Unit = node.GetAttribute("unit");
  118. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  119. base.Name = node.GetAttribute("id");
  120. base.Display = node.GetAttribute("display");
  121. base.DeviceID = node.GetAttribute("schematicId");
  122. _scGasName = ParseScNode("scGasName", node);
  123. _scEnable = ParseScNode("scEnable", node);
  124. _scN2Scale = ParseScNode("scN2Scale", node, ioModule, $"{Module}.{Name}.N2Scale");
  125. _scScaleFactor = ParseScNode("scScaleFactor", node, ioModule, $"{Module}.{Name}.ScaleFactor");
  126. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule, $"{Module}.{Name}.AlarmRange");
  127. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{Module}.{Name}.EnableAlarm");
  128. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule, $"{Module}.{Name}.AlarmTime");
  129. _scDefaultSetPoint = ParseScNode("scDefaultSetPoint", node);
  130. _scRegulationFactor = ParseScNode("scFlowRegulationFactor", node, ioModule, $"{Module}.{Name}.RegulationFactor");
  131. _uniqueName = $"{Module}.{Name}";
  132. }
  133. public virtual bool Initialize()
  134. {
  135. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  136. DATA.Subscribe($"{Module}.{Name}.FlowFeedback", () => FeedBack);
  137. DATA.Subscribe($"{Module}.{Name}.FlowSetPoint", () => SetPoint);
  138. OP.Subscribe($"{Module}.{Name}.SetMfcFlow", (function, args) =>
  139. {
  140. SetMfcFlow(Convert.ToSingle(args[0]));
  141. return true;
  142. });
  143. OP.Subscribe($"{Module}.{Name}.Ramp", (function, args) =>
  144. {
  145. SetMfcFlow(Convert.ToSingle(args[0]));
  146. return true;
  147. });
  148. InitSc();
  149. return true;
  150. }
  151. public virtual void InitSc()
  152. {
  153. _scGasName = SC.GetConfigItem($"{ScBasePath}.{Name}.GasName");
  154. _scEnable = SC.GetConfigItem($"{ScBasePath}.{Name}.Enable");
  155. _scN2Scale = SC.GetConfigItem($"{ScBasePath}.{Name}.N2Scale");
  156. _scScaleFactor = SC.GetConfigItem($"{ScBasePath}.{Name}.ScaleFactor");
  157. _scEnableAlarm = SC.GetConfigItem($"{ScBasePath}.{Name}.EnableAlarm");
  158. _scAlarmRange = SC.GetConfigItem($"{ScBasePath}.{Name}.AlarmRange");
  159. _scAlarmTime = SC.GetConfigItem($"{ScBasePath}.{Name}.AlarmTime");
  160. }
  161. public virtual void SetMfcFlow(float flow)
  162. {
  163. }
  164. public virtual bool SetMfcFlow(int time, float flow, out string reason)
  165. {
  166. reason = "";
  167. return true;
  168. }
  169. private bool InvokeRamp(string method, object[] args)
  170. {
  171. float target = Convert.ToSingle((string)(args[0].ToString()));
  172. target = Math.Min(target, Scale);
  173. target = Math.Max(target, 0);
  174. int time = 0;
  175. if (args.Length >= 2)
  176. time = Convert.ToInt32((string)(args[1].ToString()));
  177. Ramp(target, time);
  178. EV.PostInfoLog(Module, $"{_uniqueName} ramp to {target}{Unit} in {time} seconds");
  179. return true;
  180. }
  181. public virtual void Monitor()
  182. {
  183. MonitorRamping();
  184. MonitorTolerance();
  185. }
  186. public virtual void Reset()
  187. {
  188. _toleranceChecker.Reset(AlarmTime);
  189. _trigOffline.RST = true;
  190. }
  191. public virtual void Terminate()
  192. {
  193. Ramp(0, 0);
  194. }
  195. public void Ramp(int time)
  196. {
  197. Ramp(0, time);
  198. }
  199. public void Ramp(float target, int time)
  200. {
  201. target = Math.Max(0, target);
  202. target = Math.Min(Scale, target);
  203. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值。零漂问题
  204. rampTime = time;
  205. rampTarget = target;
  206. rampTimer.Start(rampTime);
  207. }
  208. public void StopRamp()
  209. {
  210. Ramp(SetPoint, 0);
  211. }
  212. private void MonitorRamping()
  213. {
  214. if (rampTimer.IsTimeout() || rampTime == 0)
  215. {
  216. SetPoint = rampTarget;
  217. }
  218. else
  219. {
  220. SetPoint = (float)(rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime);
  221. }
  222. }
  223. private void MonitorTolerance()
  224. {
  225. if (!EnableAlarm)
  226. return;
  227. _toleranceChecker.Monitor(FeedBack, (SetPoint - Math.Abs(AlarmRange)), (SetPoint + Math.Abs(AlarmRange)), AlarmTime);
  228. if (_toleranceChecker.Trig)
  229. {
  230. EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
  231. String.Format("Out of range in {0} seconds", AlarmTime.ToString("0")));
  232. }
  233. }
  234. }
  235. }