IoMfc.cs 21 KB

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