IoMfc.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. using System;
  2. using System.Diagnostics;
  3. using System.Xml;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.RT.SCCore;
  11. using Aitex.Core.RT.Tolerance;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.Common.DBCore;
  14. using Venus_RT.Modules;
  15. using Aitex.Core.RT.Log;
  16. namespace Venus_RT.Devices.IODevices
  17. {
  18. //will be upgrade later
  19. public class MfcBase1 : BaseDevice, IDevice
  20. {
  21. public virtual double SetPoint { get; set; }
  22. public virtual double FeedBack { get; set; }
  23. public virtual bool IsOutOfTolerance { get; }
  24. public virtual double Scale { set; get; }
  25. public virtual string DisplayName { set; get; }
  26. private float _percent10Calculate;
  27. private float _percent20Calculate;
  28. private float _percent30Calculate;
  29. private float _percent40Calculate;
  30. private float _percent50Calculate;
  31. private float _percent60Calculate;
  32. private float _percent70Calculate;
  33. private float _percent80Calculate;
  34. private float _percent90Calculate;
  35. private float _percent100Calculate;
  36. private float _verificationCalculate;
  37. private float _verificationSetpoint;
  38. private string _verificationResult = "";
  39. public MfcBase1()
  40. {
  41. }
  42. public virtual bool Initialize()
  43. {
  44. DATA.Subscribe($"{Module}.{Name}.VerificationResult", () => _verificationResult);
  45. DATA.Subscribe($"{Module}.{Name}.FeedBack", () => FeedBack);
  46. DATA.Subscribe($"{Module}.{Name}.SetPoint", () => SetPoint);
  47. return true;
  48. }
  49. public virtual void Monitor()
  50. {
  51. }
  52. public virtual void Reset()
  53. {
  54. }
  55. public virtual void Terminate()
  56. {
  57. }
  58. public virtual void Ramp(int time)
  59. {
  60. }
  61. public virtual void Ramp(double target, int time)
  62. {
  63. }
  64. public virtual void StopRamp()
  65. {
  66. }
  67. public virtual void SetVerificationResult(float setpoint, float calculateFlow, bool saveResult, double time, double deviation, bool result)
  68. {
  69. //ten points
  70. var delta = Scale / 10;
  71. _verificationResult += $"{setpoint},{calculateFlow},{time},{deviation},{result};";
  72. if (delta - 0.1 < setpoint && delta + 0.1 > setpoint)
  73. {
  74. _percent10Calculate = calculateFlow;
  75. }
  76. else if (2 * delta - 0.1 < setpoint && 2 * delta + 0.1 > setpoint)
  77. {
  78. _percent20Calculate = calculateFlow;
  79. }
  80. else if (3 * delta - 0.1 < setpoint && 3 * delta + 0.1 > setpoint)
  81. {
  82. _percent30Calculate = calculateFlow;
  83. }
  84. else if (4 * delta - 0.1 < setpoint && 4 * delta + 0.1 > setpoint)
  85. {
  86. _percent40Calculate = calculateFlow;
  87. }
  88. else if (5 * delta - 0.1 < setpoint && 5 * delta + 0.1 > setpoint)
  89. {
  90. _percent50Calculate = calculateFlow;
  91. }
  92. else if (6 * delta - 0.1 < setpoint && 6 * delta + 0.1 > setpoint)
  93. {
  94. _percent60Calculate = calculateFlow;
  95. }
  96. else if (7 * delta - 0.1 < setpoint && 7 * delta + 0.1 > setpoint)
  97. {
  98. _percent70Calculate = calculateFlow;
  99. }
  100. else if (8 * delta - 0.1 < setpoint && 8 * delta + 0.1 > setpoint)
  101. {
  102. _percent80Calculate = calculateFlow;
  103. }
  104. else if (9 * delta - 0.1 < setpoint && 9 * delta + 0.1 > setpoint)
  105. {
  106. _percent90Calculate = calculateFlow;
  107. }
  108. else if (10 * delta - 0.1 < setpoint && 10 * delta + 0.1 > setpoint)
  109. {
  110. _percent100Calculate = calculateFlow;
  111. }
  112. else
  113. {
  114. _verificationCalculate = calculateFlow;
  115. _verificationSetpoint = setpoint;
  116. }
  117. if (saveResult)
  118. {
  119. SaveVerificationData();
  120. }
  121. }
  122. private void SaveVerificationData()
  123. {
  124. var delta = (float)(Scale / 10);
  125. MFCVerificationData data = new MFCVerificationData()
  126. {
  127. Module = Module,
  128. Name = DisplayName,
  129. Percent10Setpoint = delta,
  130. Percent10Calculate = _percent10Calculate,
  131. Percent20Setpoint = delta * 2,
  132. Percent20Calculate = _percent20Calculate,
  133. Percent30Setpoint = delta * 3,
  134. Percent30Calculate = _percent30Calculate,
  135. Percent40Setpoint = delta * 4,
  136. Percent40Calculate = _percent40Calculate,
  137. Percent50Setpoint = delta * 5,
  138. Percent50Calculate = _percent50Calculate,
  139. Percent60Setpoint = delta * 6,
  140. Percent60Calculate = _percent60Calculate,
  141. Percent70Setpoint = delta * 7,
  142. Percent70Calculate = _percent70Calculate,
  143. Percent80Setpoint = delta * 8,
  144. Percent80Calculate = _percent80Calculate,
  145. Percent90Setpoint = delta * 9,
  146. Percent90Calculate = _percent90Calculate,
  147. Percent100Setpoint = delta * 10,
  148. Percent100Calculate = _percent100Calculate,
  149. Setpoint = _verificationSetpoint,
  150. Calculate = _verificationCalculate,
  151. };
  152. MFCVerificationDataRecorder.Add(data);
  153. }
  154. public void ResetVerificationData()
  155. {
  156. _percent10Calculate = 0;
  157. _percent20Calculate = 0;
  158. _percent30Calculate = 0;
  159. _percent40Calculate = 0;
  160. _percent50Calculate = 0;
  161. _percent60Calculate = 0;
  162. _percent70Calculate = 0;
  163. _percent80Calculate = 0;
  164. _percent90Calculate = 0;
  165. _percent100Calculate = 0;
  166. _verificationCalculate = 0;
  167. _verificationSetpoint = 0;
  168. _verificationResult = "";
  169. }
  170. }
  171. public class IoMfc : MfcBase1
  172. {
  173. public string Unit
  174. {
  175. get; set;
  176. }
  177. [Subscription(AITMfcDataPropertyName.Scale)]
  178. public override double Scale
  179. {
  180. get
  181. {
  182. if (_scN2Scale == null || _scScaleFactor == null)
  183. return 0;
  184. return _scN2Scale.IntValue * _scScaleFactor.DoubleValue;
  185. }
  186. }
  187. [Subscription(AITMfcDataPropertyName.SetPoint)]
  188. public override double SetPoint
  189. {
  190. get
  191. {
  192. if (_aoFlow != null)
  193. {
  194. byte[] high = BitConverter.GetBytes(_aoFlow.Buffer[_aoFlow.Index]);
  195. byte[] low = BitConverter.GetBytes(_aoFlow.Buffer[_aoFlow.Index + 1]);
  196. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  197. return flow * Scale / RtInstance.ANALOG_TRANS_RANGE;
  198. }
  199. return 0;
  200. }
  201. set
  202. {
  203. if (_aoFlow != null)
  204. {
  205. byte[] flow = BitConverter.GetBytes((float)(value * RtInstance.ANALOG_TRANS_RANGE / Scale));
  206. _aoFlow.Buffer[_aoFlow.Index] = BitConverter.ToInt16(flow, 0);
  207. _aoFlow.Buffer[_aoFlow.Index + 1] = BitConverter.ToInt16(flow, 2);
  208. }
  209. }
  210. }
  211. [Subscription(AITMfcDataPropertyName.DefaultSetPoint)]
  212. public double DefaultSetPoint
  213. {
  214. get
  215. {
  216. if (_scDefaultSetPoint != null)
  217. return _scDefaultSetPoint.IntValue;
  218. return 0;
  219. }
  220. }
  221. [Subscription(AITMfcDataPropertyName.FeedBack)]
  222. public override double FeedBack
  223. {
  224. get
  225. {
  226. if (_aiFlow != null)
  227. {
  228. byte[] high = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index]);
  229. byte[] low = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index + 1]);
  230. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  231. return (_scRegulationFactor != null && _scRegulationFactor.IntValue > 0) ? flow * Scale / RtInstance.ANALOG_TRANS_RANGE / _scRegulationFactor.IntValue
  232. : flow * Scale / RtInstance.ANALOG_TRANS_RANGE;
  233. }
  234. return 0;
  235. }
  236. }
  237. [Subscription(AITMfcDataPropertyName.IsOutOfTolerance)]
  238. public override bool IsOutOfTolerance
  239. {
  240. get
  241. {
  242. return _toleranceChecker.Result;
  243. }
  244. }
  245. [Subscription(AITMfcDataPropertyName.IsEnableAlarm)]
  246. public bool EnableAlarm
  247. {
  248. get
  249. {
  250. if (_scEnableAlarm != null)
  251. return _scEnableAlarm.BoolValue;
  252. return false;
  253. }
  254. }
  255. [Subscription(AITMfcDataPropertyName.AlarmRange)]
  256. public double AlarmRange
  257. {
  258. get
  259. {
  260. if (_scAlarmRange != null)
  261. return _scAlarmRange.IntValue;
  262. return 0;
  263. }
  264. }
  265. [Subscription(AITMfcDataPropertyName.AlarmTime)]
  266. public double AlarmTime
  267. {
  268. get
  269. {
  270. if (_scAlarmTime != null)
  271. return _scAlarmTime.IntValue;
  272. return 0;
  273. }
  274. }
  275. [Subscription(AITMfcDataPropertyName.PressureAlarm)]
  276. public bool PressureAlarm
  277. {
  278. get { return _diPressureAlarm != null ? _diPressureAlarm.Value : true; }
  279. }
  280. [Subscription(AITMfcDataPropertyName.MfcAlarm)]
  281. public bool MfcAlarm
  282. {
  283. get
  284. {
  285. return _bMfcAlarm;
  286. }
  287. }
  288. [Subscription(AITMfcDataPropertyName.IsEnable)]
  289. public bool Enable
  290. {
  291. get
  292. {
  293. if (_scEnable != null)
  294. return _scEnable.BoolValue;
  295. return false;
  296. }
  297. }
  298. [Subscription(AITMfcDataPropertyName.IsOffline)]
  299. public bool IsOffline
  300. {
  301. get
  302. {
  303. if (_diOffline != null)
  304. return _diOffline.Value;
  305. return false;
  306. }
  307. }
  308. public override string DisplayName
  309. {
  310. get
  311. {
  312. if (_scGasName != null)
  313. return _scGasName.StringValue;
  314. return Display;
  315. }
  316. }
  317. private DeviceTimer rampTimer = new DeviceTimer();
  318. private double rampTarget;
  319. private double rampInitValue;
  320. private int rampTime;
  321. private bool _bMfcAlarm = false;
  322. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  323. private AIAccessor _aiFlow;
  324. private AOAccessor _aoFlow;
  325. private AOAccessor _aoRange;
  326. private DIAccessor _diOffline;
  327. private DIAccessor _diPressureAlarm;
  328. private SCConfigItem _scGasName;
  329. private SCConfigItem _scEnable;
  330. private SCConfigItem _scN2Scale;
  331. private SCConfigItem _scScaleFactor;
  332. private SCConfigItem _scAlarmRange;
  333. private SCConfigItem _scEnableAlarm;
  334. private SCConfigItem _scAlarmTime;
  335. private SCConfigItem _scDefaultSetPoint;
  336. private SCConfigItem _scRegulationFactor;
  337. private R_TRIG _trigOffline = new R_TRIG();
  338. private R_TRIG _trigPressureAlarm = new R_TRIG();
  339. private string _uniqueName;
  340. private string GasFlowOutOfTolerance = "GasFlowOutOfTolerance";
  341. public IoMfc(string module, XmlElement node, string ioModule = "")
  342. {
  343. Unit = node.GetAttribute("unit");
  344. base.Module = module;
  345. base.Name = node.GetAttribute("id");
  346. base.Display = node.GetAttribute("display");
  347. base.DeviceID = node.GetAttribute("schematicId");
  348. _aoRange = ParseAoNode("aoRange", node, ioModule);
  349. _diOffline = ParseDiNode("diOffline", node, ioModule);
  350. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  351. _aoFlow = ParseAoNode("aoFlow", node, ioModule);
  352. _diPressureAlarm = ParseDiNode("diPressureAlarm", node, ioModule);
  353. _scGasName = SC.GetConfigItem($"{Module}.{Name}.GasName");
  354. _scEnable = SC.GetConfigItem($"{Module}.{Name}.Enable");
  355. _scN2Scale = SC.GetConfigItem($"{Module}.{Name}.MfcN2Scale");
  356. _scScaleFactor = SC.GetConfigItem($"{Module}.{Name}.MfcScaleFactor");
  357. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmRange");
  358. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.MfcEnableAlarm");
  359. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmTime");
  360. _scDefaultSetPoint = SC.GetConfigItem($"{Module}.{Name}.DefaultSetPoint");
  361. _scRegulationFactor = SC.GetConfigItem($"{module}.{Name}.FlowRegulationFactor");
  362. _uniqueName = $"{Module}.{Name}";
  363. #if DEBUG
  364. Debug.Assert(!string.IsNullOrWhiteSpace(_scGasName.StringValue));
  365. Debug.Assert(null != _scN2Scale);
  366. Debug.Assert(null != _aoFlow);
  367. Debug.Assert(null != _aiFlow);
  368. #endif
  369. }
  370. public override bool Initialize()
  371. {
  372. EV.Subscribe(new EventItem("Event", GasFlowOutOfTolerance, "Gas Flow Out Of Tolerance", EventLevel.Alarm, EventType.HostNotification));
  373. DATA.Subscribe($"{Module}.{Name}", () =>
  374. {
  375. AITMfcData data = new AITMfcData
  376. {
  377. Type = "MFC",
  378. UniqueName = _uniqueName,
  379. DeviceName = Name,
  380. DeviceSchematicId = DeviceID,
  381. DisplayName = DisplayName,
  382. FeedBack = FeedBack,
  383. SetPoint = SetPoint,
  384. Scale = Scale,
  385. IsOffline = IsOffline,
  386. };
  387. return data;
  388. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  389. OP.Subscribe($"{Module}.{Name}.{AITMfcOperation.Ramp}", (name, args) =>
  390. {
  391. double target = (double)args[0];
  392. target = Math.Min(target, Scale);
  393. target = Math.Max(target, 0);
  394. Ramp(target, 0);
  395. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Ramp to {target}{Unit}");
  396. return true;
  397. });
  398. DEVICE.Register($"{Module}.{Name}.{AITMfcOperation.Ramp}", (out string reason, int time, object[] param) =>
  399. {
  400. double target = Convert.ToDouble((string)param[0]);
  401. target = Math.Min(target, Scale);
  402. target = Math.Max(target, 0);
  403. Ramp(target, time);
  404. reason = $"{Display} ramp to {target}{Unit}";
  405. return true;
  406. });
  407. //@AAA use recipe
  408. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  409. {
  410. double target = Convert.ToDouble((string)param[0]);
  411. target = Math.Min(target, Scale);
  412. target = Math.Max(target, 0);
  413. Ramp(target, time);
  414. reason = $"{Display} ramp to {target}{Unit}";
  415. return true;
  416. });
  417. return base.Initialize();
  418. }
  419. public override void Monitor()
  420. {
  421. if (Enable)
  422. {
  423. Ramping();
  424. CheckTolerance();
  425. if (_aoRange != null)
  426. _aoRange.Value = (short)Scale;
  427. _trigOffline.CLK = IsOffline;
  428. if (_trigOffline.Q)
  429. {
  430. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, string.Format("{0} is offline", DisplayName));
  431. _bMfcAlarm = true;
  432. }
  433. _trigPressureAlarm.CLK = PressureAlarm == false;
  434. if (_trigPressureAlarm.Q)
  435. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}, {DisplayName} Pressure Alarm");
  436. if (PressureAlarm)
  437. {
  438. _trigPressureAlarm.RST = true;
  439. }
  440. }
  441. }
  442. public override void Reset()
  443. {
  444. _bMfcAlarm = false;
  445. _toleranceChecker.Reset(AlarmTime);
  446. _trigPressureAlarm.RST = true;
  447. _trigOffline.RST = true;
  448. }
  449. public override void Terminate()
  450. {
  451. Ramp(DefaultSetPoint, 0);
  452. }
  453. public override void Ramp(int time)
  454. {
  455. Ramp(DefaultSetPoint, time);
  456. }
  457. public override void Ramp(double target, int time)
  458. {
  459. target = Math.Max(0, target);
  460. target = Math.Min(Scale, target);
  461. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值。零漂问题
  462. rampTime = time;
  463. rampTarget = target;
  464. rampTimer.Start(rampTime);
  465. }
  466. public override void StopRamp()
  467. {
  468. Ramp(SetPoint, 0);
  469. }
  470. private void Ramping()
  471. {
  472. if (rampTimer.IsTimeout() || rampTime == 0)
  473. {
  474. SetPoint = rampTarget;
  475. }
  476. else
  477. {
  478. SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;
  479. }
  480. }
  481. private void CheckTolerance()
  482. {
  483. if (!EnableAlarm)
  484. return;
  485. // 流率检查
  486. _toleranceChecker.Monitor(FeedBack, SetPoint - Math.Abs(AlarmRange), SetPoint + Math.Abs(AlarmRange), AlarmTime);
  487. if (_toleranceChecker.Trig)
  488. {
  489. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, Display + $" 越界 in {AlarmTime:0} seconds");
  490. EV.Notify(GasFlowOutOfTolerance);
  491. }
  492. }
  493. }
  494. }