MfcBase.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. using MECF.Framework.Common.DBCore;
  17. namespace MECF.Framework.Common.Device.Bases
  18. {
  19. public abstract class MfcBase : BaseDevice, IDevice
  20. {
  21. public string Unit
  22. {
  23. get; set;
  24. }
  25. public virtual float Scale
  26. {
  27. get
  28. {
  29. if (_scN2Scale == null || _scScaleFactor == null)
  30. return 100;
  31. return (float)(_scN2Scale.DoubleValue * _scScaleFactor.DoubleValue);
  32. }
  33. set
  34. {
  35. }
  36. }
  37. public virtual float MinimumFlow
  38. {
  39. get;
  40. set;
  41. }
  42. public virtual float SetPoint { get; set; }
  43. public virtual float FeedBack { get; set; }
  44. public bool IsOutOfTolerance
  45. {
  46. get
  47. {
  48. return _toleranceAlarmChecker.Result;
  49. }
  50. }
  51. public virtual bool EnableAlarm
  52. {
  53. get
  54. {
  55. if (_scEnableAlarm != null)
  56. return _scEnableAlarm.BoolValue;
  57. return false;
  58. }
  59. }
  60. public virtual double AlarmTime
  61. {
  62. get
  63. {
  64. if (_scAlarmTime != null)
  65. return _scAlarmTime.IntValue != 0 ? _scAlarmTime.IntValue : _scAlarmTime.DoubleValue;
  66. return 10;
  67. }
  68. }
  69. public virtual double AlarmRange
  70. {
  71. get
  72. {
  73. if (_currentAlarmRange > 0)
  74. return _currentAlarmRange;
  75. if (_scAlarmRange != null)
  76. return _scAlarmRange.DoubleValue;
  77. return 0;
  78. }
  79. }
  80. public virtual double WarningTime
  81. {
  82. get
  83. {
  84. if (_scWarningTime != null)
  85. return _scWarningTime.DoubleValue;
  86. return 0;
  87. }
  88. }
  89. public virtual double WarningRange
  90. {
  91. get
  92. {
  93. if (_currentWarningRange > 0)
  94. return _currentWarningRange;
  95. if (_scWarningRange != null)
  96. return _scWarningRange.DoubleValue;
  97. return 0;
  98. }
  99. }
  100. public virtual double FineTuningValue
  101. {
  102. get
  103. {
  104. if (_scFineTuningEnable == null || !_scFineTuningEnable.BoolValue)
  105. return 1;
  106. if (_currentFineTuningValue != 0)
  107. return 1 + _currentFineTuningValue / 100;
  108. return _scFineTuningValue != null ? 1 + _scFineTuningValue.DoubleValue / 100 : 1;
  109. }
  110. }
  111. public string DisplayName
  112. {
  113. get
  114. {
  115. if (_scGasName != null)
  116. return _scGasName.StringValue;
  117. return Display;
  118. }
  119. }
  120. public virtual AITMfcData DeviceData
  121. {
  122. get
  123. {
  124. AITMfcData data = new AITMfcData()
  125. {
  126. UniqueName = _uniqueName,
  127. Type = "MFC",
  128. DeviceName = Name,
  129. DeviceSchematicId = DeviceID,
  130. DisplayName = DisplayName,
  131. FeedBack = SetPoint == 0 ? 0 : FeedBack,
  132. SetPoint = SetPoint,
  133. Scale = Scale,
  134. };
  135. return data;
  136. }
  137. }
  138. private DeviceTimer rampTimer = new DeviceTimer();
  139. protected float rampTarget;
  140. private float rampInitValue;
  141. private int rampTime;
  142. private float _currentWarningRange;
  143. private float _currentAlarmRange;
  144. protected ToleranceChecker _toleranceAlarmChecker = new ToleranceChecker();
  145. protected ToleranceChecker _toleranceWarningChecker = new ToleranceChecker();
  146. protected SCConfigItem _scGasName;
  147. protected SCConfigItem _scEnable;
  148. protected SCConfigItem _scN2Scale;
  149. protected SCConfigItem _scN2MinimumFlow;
  150. protected SCConfigItem _scScaleFactor;
  151. protected SCConfigItem _scAlarmRange;
  152. protected SCConfigItem _scEnableAlarm;
  153. protected SCConfigItem _scAlarmTime;
  154. protected SCConfigItem _scWarningTime;
  155. protected SCConfigItem _scWarningRange;
  156. protected SCConfigItem _scDefaultSetPoint;
  157. protected SCConfigItem _scRegulationFactor;
  158. private SCConfigItem _scFineTuningEnable;
  159. private SCConfigItem _scFineTuningValue;
  160. private R_TRIG _trigOffline = new R_TRIG();
  161. protected double _currentFineTuningValue;
  162. protected string _uniqueName;
  163. private float _percent10Calculate;
  164. private float _percent20Calculate;
  165. private float _percent30Calculate;
  166. private float _percent40Calculate;
  167. private float _percent50Calculate;
  168. private float _percent60Calculate;
  169. private float _percent70Calculate;
  170. private float _percent80Calculate;
  171. private float _percent90Calculate;
  172. private float _percent100Calculate;
  173. private float _verificationCalculate;
  174. private float _verificationSetpoint;
  175. private string _verificationResult = "";
  176. public MfcBase() : base()
  177. {
  178. }
  179. public MfcBase(string module, XmlElement node, string ioModule = "")
  180. {
  181. Unit = node.GetAttribute("unit");
  182. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  183. base.Name = node.GetAttribute("id");
  184. base.Display = node.GetAttribute("display");
  185. base.DeviceID = node.GetAttribute("schematicId");
  186. _scGasName = ParseScNode("scGasName", node, ioModule, $"{Module}.{Name}.GasName");
  187. _scEnable = ParseScNode("scEnable", node);
  188. _scN2Scale = ParseScNode("scN2Scale", node, ioModule, $"{Module}.{Name}.N2Scale");
  189. _scScaleFactor = ParseScNode("scScaleFactor", node, ioModule, $"{Module}.{Name}.ScaleFactor");
  190. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule, $"{Module}.{Name}.AlarmRange");
  191. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{Module}.{Name}.EnableAlarm");
  192. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule, $"{Module}.{Name}.AlarmTime");
  193. _scWarningTime = ParseScNode("scWarningTime", node, ioModule, $"{Module}.{DeviceID}.WarningTime");
  194. _scWarningRange = ParseScNode("scWarningRange", node, ioModule, $"{Module}.{DeviceID}.WarningRange");
  195. _scFineTuningValue = ParseScNode("scFineTuningValue", node, ioModule, $"{Module}.FineTuning.{Name}");
  196. _scFineTuningEnable = ParseScNode("scFineTuningEnable", node, ioModule, $"{Module}.FineTuning.IsEnable");
  197. _scDefaultSetPoint = ParseScNode("scDefaultSetPoint", node);
  198. _scRegulationFactor = ParseScNode("scFlowRegulationFactor", node, ioModule, $"{Module}.{Name}.RegulationFactor");
  199. _uniqueName = $"{Module}.{Name}";
  200. }
  201. public virtual bool Initialize()
  202. {
  203. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  204. DATA.Subscribe($"{Module}.{Name}.FlowFeedback", () => FeedBack);
  205. DATA.Subscribe($"{Module}.{Name}.FlowSetPoint", () => SetPoint);
  206. DATA.Subscribe($"{Module}.{Name}.VerificationResult", () => _verificationResult);
  207. OP.Subscribe($"{Module}.{Name}.SetMfcFlow", (function, args) =>
  208. {
  209. SetMfcFlow(Convert.ToSingle(args[0]));
  210. return true;
  211. });
  212. OP.Subscribe($"{Module}.{Name}.Ramp", (function, args) =>
  213. {
  214. SetMfcFlow(Convert.ToSingle(args[0]));
  215. return true;
  216. });
  217. //for recipe
  218. OP.Subscribe($"{Module}.{Name}.SetFineTuning", (out string reason, int time, object[] param) =>
  219. {
  220. reason = string.Empty;
  221. SetFineTuning(Convert.ToSingle(param[0]));
  222. return true;
  223. });
  224. //for recipe
  225. OP.Subscribe($"{Module}.{Name}.SetTolerance", (out string reason, int time, object[] param) =>
  226. {
  227. reason = string.Empty;
  228. var warning = Convert.ToSingle(param[0]);
  229. var alarm = Convert.ToSingle(param[1]);
  230. SetTolerance((float)warning, (float)alarm);
  231. return true;
  232. });
  233. InitSc();
  234. return true;
  235. }
  236. public virtual void InitSc()
  237. {
  238. _scGasName = SC.GetConfigItem($"{ScBasePath}.{Name}.GasName");
  239. _scEnable = SC.GetConfigItem($"{ScBasePath}.{Name}.Enable");
  240. _scN2Scale = SC.GetConfigItem($"{ScBasePath}.{Name}.N2Scale");
  241. _scScaleFactor = SC.GetConfigItem($"{ScBasePath}.{Name}.ScaleFactor");
  242. _scEnableAlarm = SC.GetConfigItem($"{ScBasePath}.{Name}.EnableAlarm");
  243. _scAlarmRange = SC.GetConfigItem($"{ScBasePath}.{Name}.AlarmRange");
  244. _scAlarmTime = SC.GetConfigItem($"{ScBasePath}.{Name}.AlarmTime");
  245. _scWarningRange = SC.GetConfigItem($"{ScBasePath}.{Name}.WarningRange");
  246. _scWarningTime = SC.GetConfigItem($"{ScBasePath}.{Name}.WarningTime");
  247. }
  248. public virtual void SetTolerance(float warning, float alarm)
  249. {
  250. _currentWarningRange = warning;
  251. _currentAlarmRange = alarm;
  252. _toleranceAlarmChecker.Reset(AlarmTime);
  253. _toleranceWarningChecker.Reset(WarningTime);
  254. }
  255. public virtual void CheckTolerance()
  256. {
  257. if (!EnableAlarm || SetPoint == 0)
  258. return;
  259. _toleranceAlarmChecker.Monitor(FeedBack, (SetPoint * (1 - AlarmRange / 100)), (SetPoint * (1 + AlarmRange / 100)), AlarmTime);
  260. _toleranceWarningChecker.Monitor(FeedBack, (SetPoint * (1 - WarningRange / 100)), (SetPoint * (1 + WarningRange / 100)), WarningTime);
  261. }
  262. public virtual bool CheckToleranceAlarm()
  263. {
  264. if (!EnableAlarm)
  265. return false;
  266. return _toleranceAlarmChecker.Result;
  267. }
  268. public virtual bool CheckToleranceWarning()
  269. {
  270. if (!EnableAlarm)
  271. return false;
  272. return _toleranceWarningChecker.Result;
  273. }
  274. public virtual void SetFineTuning(float fineTuning)
  275. {
  276. _currentFineTuningValue = fineTuning;
  277. }
  278. public virtual void SetMfcFlow(float flow)
  279. {
  280. }
  281. public virtual bool SetMfcFlow(int time, float flow, out string reason)
  282. {
  283. reason = "";
  284. return true;
  285. }
  286. private bool InvokeRamp(string method, object[] args)
  287. {
  288. float target = Convert.ToSingle((string)(args[0].ToString()));
  289. target = Math.Min(target, Scale);
  290. target = Math.Max(target, 0);
  291. int time = 0;
  292. if (args.Length >= 2)
  293. time = Convert.ToInt32((string)(args[1].ToString()));
  294. Ramp(target, time);
  295. EV.PostInfoLog(Module, $"{_uniqueName} ramp to {target}{Unit} in {time} seconds");
  296. return true;
  297. }
  298. public virtual void Monitor()
  299. {
  300. MonitorRamping();
  301. MonitorTolerance();
  302. }
  303. public virtual void Reset()
  304. {
  305. _toleranceAlarmChecker.Reset(AlarmTime);
  306. _toleranceWarningChecker.Reset(WarningTime);
  307. _trigOffline.RST = true;
  308. }
  309. public virtual void Terminate()
  310. {
  311. Ramp(0, 0);
  312. }
  313. public void Ramp(int time)
  314. {
  315. Ramp(0, time);
  316. }
  317. public void Ramp(float target, int time)
  318. {
  319. target = Math.Max(0, target);
  320. target = Math.Min(Scale, target);
  321. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值.零漂问题
  322. rampTime = time;
  323. rampTarget = target;
  324. rampTimer.Start(rampTime);
  325. }
  326. public void StopRamp()
  327. {
  328. Ramp(SetPoint, 0);
  329. }
  330. private void MonitorRamping()
  331. {
  332. if (rampTimer.IsTimeout() || rampTime == 0)
  333. {
  334. SetPoint = rampTarget;
  335. }
  336. else
  337. {
  338. SetPoint = (float)(rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime);
  339. }
  340. }
  341. protected virtual void MonitorTolerance()
  342. {
  343. CheckTolerance();
  344. }
  345. public virtual void SetVerificationResult(float setpoint, float calculateFlow, bool saveResult)
  346. {
  347. //ten points
  348. var delta = Scale / 10;
  349. _verificationResult += $"{setpoint},{calculateFlow};";
  350. if (delta - 0.1 < setpoint && delta + 0.1 > setpoint)
  351. {
  352. _percent10Calculate = calculateFlow;
  353. }
  354. else if (2 * delta - 0.1 < setpoint && 2 * delta + 0.1 > setpoint)
  355. {
  356. _percent20Calculate = calculateFlow;
  357. }
  358. else if (3 * delta - 0.1 < setpoint && 3 * delta + 0.1 > setpoint)
  359. {
  360. _percent30Calculate = calculateFlow;
  361. }
  362. else if (4 * delta - 0.1 < setpoint && 4 * delta + 0.1 > setpoint)
  363. {
  364. _percent40Calculate = calculateFlow;
  365. }
  366. else if (5 * delta - 0.1 < setpoint && 5 * delta + 0.1 > setpoint)
  367. {
  368. _percent50Calculate = calculateFlow;
  369. }
  370. else if (6 * delta - 0.1 < setpoint && 6 * delta + 0.1 > setpoint)
  371. {
  372. _percent60Calculate = calculateFlow;
  373. }
  374. else if (7 * delta - 0.1 < setpoint && 7 * delta + 0.1 > setpoint)
  375. {
  376. _percent70Calculate = calculateFlow;
  377. }
  378. else if (8 * delta - 0.1 < setpoint && 8 * delta + 0.1 > setpoint)
  379. {
  380. _percent80Calculate = calculateFlow;
  381. }
  382. else if (9 * delta - 0.1 < setpoint && 9 * delta + 0.1 > setpoint)
  383. {
  384. _percent90Calculate = calculateFlow;
  385. }
  386. else if (10 * delta - 0.1 < setpoint && 10 * delta + 0.1 > setpoint)
  387. {
  388. _percent100Calculate = calculateFlow;
  389. }
  390. else
  391. {
  392. _verificationCalculate = calculateFlow;
  393. _verificationSetpoint = setpoint;
  394. }
  395. if (saveResult)
  396. {
  397. SaveVerificationData();
  398. }
  399. }
  400. private void SaveVerificationData()
  401. {
  402. var delta = (float)(Scale / 10);
  403. MFCVerificationData data = new MFCVerificationData()
  404. {
  405. Module = Module,
  406. Name = DisplayName,
  407. Percent10Setpoint = delta,
  408. Percent10Calculate = _percent10Calculate,
  409. Percent20Setpoint = delta * 2,
  410. Percent20Calculate = _percent20Calculate,
  411. Percent30Setpoint = delta * 3,
  412. Percent30Calculate = _percent30Calculate,
  413. Percent40Setpoint = delta * 4,
  414. Percent40Calculate = _percent40Calculate,
  415. Percent50Setpoint = delta * 5,
  416. Percent50Calculate = _percent50Calculate,
  417. Percent60Setpoint = delta * 6,
  418. Percent60Calculate = _percent60Calculate,
  419. Percent70Setpoint = delta * 7,
  420. Percent70Calculate = _percent70Calculate,
  421. Percent80Setpoint = delta * 8,
  422. Percent80Calculate = _percent80Calculate,
  423. Percent90Setpoint = delta * 9,
  424. Percent90Calculate = _percent90Calculate,
  425. Percent100Setpoint = delta * 10,
  426. Percent100Calculate = _percent100Calculate,
  427. Setpoint = _verificationSetpoint,
  428. Calculate = _verificationCalculate,
  429. };
  430. MFCVerificationDataRecorder.Add(data);
  431. }
  432. public void ResetVerificationData()
  433. {
  434. _percent10Calculate = 0;
  435. _percent20Calculate = 0;
  436. _percent30Calculate = 0;
  437. _percent40Calculate = 0;
  438. _percent50Calculate = 0;
  439. _percent60Calculate = 0;
  440. _percent70Calculate = 0;
  441. _percent80Calculate = 0;
  442. _percent90Calculate = 0;
  443. _percent100Calculate = 0;
  444. _verificationCalculate = 0;
  445. _verificationSetpoint = 0;
  446. _verificationResult = "";
  447. }
  448. }
  449. }