IoMfc.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. byte[] _buffer_1 = new byte[4];
  215. byte[] _buffer_2 = new byte[4];
  216. public string Unit
  217. {
  218. get; set;
  219. }
  220. [Subscription(AITMfcDataPropertyName.Scale)]
  221. public override double Scale
  222. {
  223. get
  224. {
  225. if (_scN2Scale == null || _scScaleFactor == null)
  226. return 0;
  227. return _scN2Scale.IntValue * _scScaleFactor.DoubleValue;
  228. }
  229. }
  230. [Subscription(AITMfcDataPropertyName.SetPoint)]
  231. public override double SetPoint
  232. {
  233. get
  234. {
  235. if (_aoFlow != null)
  236. {
  237. byte[] high = BitConverter.GetBytes(_aoFlow.Buffer[_aoFlow.Index]);
  238. byte[] low = BitConverter.GetBytes(_aoFlow.Buffer[_aoFlow.Index + 1]);
  239. _buffer_1[0] = high[0];
  240. _buffer_1[1] = high[1];
  241. _buffer_1[2] = low[0];
  242. _buffer_1[3] = low[1];
  243. float flow = BitConverter.ToSingle(_buffer_1, 0);
  244. return flow * Scale / RtInstance.ANALOG_TRANS_RANGE;
  245. }
  246. return 0;
  247. }
  248. set
  249. {
  250. if (_aoFlow != null)
  251. {
  252. byte[] flow = BitConverter.GetBytes((float)(value * RtInstance.ANALOG_TRANS_RANGE / Scale));
  253. _aoFlow.Buffer[_aoFlow.Index] = BitConverter.ToInt16(flow, 0);
  254. _aoFlow.Buffer[_aoFlow.Index + 1] = BitConverter.ToInt16(flow, 2);
  255. }
  256. }
  257. }
  258. [Subscription(AITMfcDataPropertyName.DefaultSetPoint)]
  259. public double DefaultSetPoint
  260. {
  261. get
  262. {
  263. if (_scDefaultSetPoint != null)
  264. return _scDefaultSetPoint.IntValue;
  265. return 0;
  266. }
  267. }
  268. [Subscription(AITMfcDataPropertyName.FeedBack)]
  269. public override double FeedBack
  270. {
  271. get
  272. {
  273. if (_aiFlow != null)
  274. {
  275. byte[] high = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index]);
  276. byte[] low = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index + 1]);
  277. _buffer_2[0] = high[0];
  278. _buffer_2[1] = high[1];
  279. _buffer_2[2] = low[0];
  280. _buffer_2[3] = low[1];
  281. float flow = BitConverter.ToSingle(_buffer_2, 0);
  282. return (_scRegulationFactor != null && _scRegulationFactor.IntValue > 0) ? flow * Scale / RtInstance.ANALOG_TRANS_RANGE / _scRegulationFactor.IntValue
  283. : flow * Scale / RtInstance.ANALOG_TRANS_RANGE;
  284. }
  285. return 0;
  286. }
  287. }
  288. [Subscription(AITMfcDataPropertyName.IsOutOfTolerance)]
  289. public override bool IsOutOfTolerance
  290. {
  291. get
  292. {
  293. return _toleranceChecker.Result;
  294. }
  295. }
  296. [Subscription(AITMfcDataPropertyName.IsEnableAlarm)]
  297. public bool EnableAlarm
  298. {
  299. get
  300. {
  301. if (_scEnableAlarm != null)
  302. return _scEnableAlarm.BoolValue;
  303. return false;
  304. }
  305. }
  306. [Subscription(AITMfcDataPropertyName.AlarmRange)]
  307. public double AlarmRange
  308. {
  309. get
  310. {
  311. if (_scAlarmRange != null)
  312. return _scAlarmRange.IntValue;
  313. return 0;
  314. }
  315. }
  316. [Subscription(AITMfcDataPropertyName.AlarmTime)]
  317. public double AlarmTime
  318. {
  319. get
  320. {
  321. if (_scAlarmTime != null)
  322. return _scAlarmTime.IntValue;
  323. return 0;
  324. }
  325. }
  326. [Subscription(AITMfcDataPropertyName.PressureAlarm)]
  327. public bool PressureAlarm
  328. {
  329. get { return _diPressureAlarm != null ? _diPressureAlarm.Value : true; }
  330. }
  331. [Subscription(AITMfcDataPropertyName.MfcAlarm)]
  332. public bool MfcAlarm
  333. {
  334. get
  335. {
  336. return _bMfcAlarm;
  337. }
  338. }
  339. [Subscription(AITMfcDataPropertyName.IsEnable)]
  340. public bool Enable
  341. {
  342. get
  343. {
  344. if (_scEnable != null)
  345. return _scEnable.BoolValue;
  346. return false;
  347. }
  348. }
  349. [Subscription(AITMfcDataPropertyName.IsOffline)]
  350. public bool IsOffline
  351. {
  352. get
  353. {
  354. if (_diOffline != null)
  355. return _diOffline.Value;
  356. return false;
  357. }
  358. }
  359. public override string DisplayName
  360. {
  361. get
  362. {
  363. if (_scGasName != null)
  364. return _scGasName.StringValue;
  365. return Display;
  366. }
  367. }
  368. private DeviceTimer rampTimer = new DeviceTimer();
  369. private double rampTarget;
  370. private double rampInitValue;
  371. private int rampTime;
  372. private bool _bMfcAlarm = false;
  373. private bool _bRamping = false;
  374. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  375. private AIAccessor _aiFlow;
  376. private AOAccessor _aoFlow;
  377. private AOAccessor _aoRange;
  378. private DIAccessor _diOffline;
  379. private DIAccessor _diPressureAlarm;
  380. private SCConfigItem _scGasName;
  381. private SCConfigItem _scEnable;
  382. private SCConfigItem _scN2Scale;
  383. private SCConfigItem _scScaleFactor;
  384. private SCConfigItem _scAlarmRange;
  385. private SCConfigItem _scEnableAlarm;
  386. private SCConfigItem _scAlarmTime;
  387. private SCConfigItem _scDefaultSetPoint;
  388. private SCConfigItem _scRegulationFactor;
  389. private R_TRIG _trigOffline = new R_TRIG();
  390. private R_TRIG _trigPressureAlarm = new R_TRIG();
  391. private string _uniqueName;
  392. private string GasFlowOutOfTolerance = "GasFlowOutOfTolerance";
  393. public IoMfc(string module, XmlElement node, string ioModule = "")
  394. {
  395. Unit = node.GetAttribute("unit");
  396. base.Module = module;
  397. base.Name = node.GetAttribute("id");
  398. base.Display = node.GetAttribute("display");
  399. base.DeviceID = node.GetAttribute("schematicId");
  400. _aoRange = ParseAoNode("aoRange", node, ioModule);
  401. _diOffline = ParseDiNode("diOffline", node, ioModule);
  402. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  403. _aoFlow = ParseAoNode("aoFlow", node, ioModule);
  404. _diPressureAlarm = ParseDiNode("diPressureAlarm", node, ioModule);
  405. _scGasName = SC.GetConfigItem($"{Module}.{Name}.GasName");
  406. _scEnable = SC.GetConfigItem($"{Module}.{Name}.Enable");
  407. _scN2Scale = SC.GetConfigItem($"{Module}.{Name}.MfcN2Scale");
  408. _scScaleFactor = SC.GetConfigItem($"{Module}.{Name}.MfcScaleFactor");
  409. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmRange");
  410. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.MfcEnableAlarm");
  411. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmTime");
  412. _scDefaultSetPoint = SC.GetConfigItem($"{Module}.{Name}.DefaultSetPoint");
  413. _scRegulationFactor = SC.GetConfigItem($"{module}.{Name}.FlowRegulationFactor");
  414. _uniqueName = $"{Module}.{Name}";
  415. #if DEBUG
  416. //Debug.Assert(!string.IsNullOrWhiteSpace(_scGasName.StringValue));
  417. //Debug.Assert(null != _scN2Scale);
  418. //Debug.Assert(null != _aoFlow);
  419. //Debug.Assert(null != _aiFlow);
  420. #endif
  421. }
  422. public override bool Initialize()
  423. {
  424. EV.Subscribe(new EventItem("Event", GasFlowOutOfTolerance, "Gas Flow Out Of Tolerance", EventLevel.Alarm, EventType.HostNotification));
  425. DATA.Subscribe($"{Module}.{Name}", () =>
  426. {
  427. AITMfcData data = new AITMfcData
  428. {
  429. Type = "MFC",
  430. UniqueName = _uniqueName,
  431. DeviceName = Name,
  432. DeviceSchematicId = DeviceID,
  433. DisplayName = DisplayName,
  434. FeedBack = FeedBack,
  435. SetPoint = SetPoint,
  436. Scale = Scale,
  437. IsOffline = IsOffline,
  438. IsWarning =PressureAlarm
  439. };
  440. return data;
  441. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  442. OP.Subscribe($"{Module}.{Name}.{AITMfcOperation.Ramp}", (name, args) =>
  443. {
  444. double target = (double)args[0];
  445. target = Math.Min(target, Scale);
  446. target = Math.Max(target, 0);
  447. Ramp(target, 0);
  448. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Ramp to {target}{Unit}");
  449. return true;
  450. });
  451. DEVICE.Register($"{Module}.{Name}.{AITMfcOperation.Ramp}", (out string reason, int time, object[] param) =>
  452. {
  453. double target = Convert.ToDouble((string)param[0]);
  454. target = Math.Min(target, Scale);
  455. target = Math.Max(target, 0);
  456. Ramp(target, time);
  457. reason = $"{Display} ramp to {target}{Unit}";
  458. return true;
  459. });
  460. OP.Subscribe($"{Module}.{Name}.SetPoint", (cmd, args) => {
  461. SetPoint = Convert.ToDouble( args[0]);
  462. return true;
  463. });
  464. //@AAA use recipe
  465. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  466. {
  467. double target = Convert.ToDouble((string)param[0]);
  468. target = Math.Min(target, Scale);
  469. target = Math.Max(target, 0);
  470. Ramp(target, time);
  471. reason = $"{Display} ramp to {target}{Unit}";
  472. return true;
  473. });
  474. return base.Initialize();
  475. }
  476. public override void Monitor()
  477. {
  478. if (Enable)
  479. {
  480. Ramping();
  481. CheckTolerance();
  482. if (_aoRange != null)
  483. _aoRange.Value = (short)Scale;
  484. _trigOffline.CLK = IsOffline;
  485. if (_trigOffline.Q)
  486. {
  487. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, string.Format("{0} is offline", DisplayName));
  488. _bMfcAlarm = true;
  489. }
  490. _trigPressureAlarm.CLK = PressureAlarm == false;
  491. if (_trigPressureAlarm.Q)
  492. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}, {DisplayName} Pressure Alarm");
  493. if (PressureAlarm)
  494. {
  495. _trigPressureAlarm.RST = true;
  496. }
  497. }
  498. }
  499. public override void Reset()
  500. {
  501. _bMfcAlarm = false;
  502. _toleranceChecker.Reset(AlarmTime);
  503. _trigPressureAlarm.RST = true;
  504. _trigOffline.RST = true;
  505. }
  506. public override void Terminate()
  507. {
  508. _bRamping = false;
  509. Ramp(DefaultSetPoint, 0);
  510. }
  511. public override void Ramp(int time)
  512. {
  513. _bRamping = true;
  514. Ramp(DefaultSetPoint, time);
  515. }
  516. public override void Ramp(double target, int time)
  517. {
  518. _bRamping = true;
  519. target = Math.Max(0, target);
  520. target = Math.Min(Scale, target);
  521. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值。零漂问题
  522. rampTime = time;
  523. rampTarget = target;
  524. rampTimer.Start(rampTime);
  525. }
  526. public override void StopRamp()
  527. {
  528. _bRamping = false;
  529. Ramp(SetPoint, 0);
  530. }
  531. private void Ramping()
  532. {
  533. if (_bRamping)
  534. {
  535. if (rampTimer.IsTimeout() || rampTime == 0)
  536. {
  537. SetPoint = rampTarget;
  538. _bRamping = false;
  539. }
  540. else
  541. {
  542. SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;
  543. }
  544. }
  545. }
  546. private void CheckTolerance()
  547. {
  548. if (!EnableAlarm)
  549. return;
  550. // 流率检查
  551. _toleranceChecker.Monitor(FeedBack, SetPoint - Math.Abs(AlarmRange), SetPoint + Math.Abs(AlarmRange), AlarmTime);
  552. if (_toleranceChecker.Trig)
  553. {
  554. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, Display + $" 越界 in {AlarmTime:0} seconds");
  555. EV.Notify(GasFlowOutOfTolerance);
  556. }
  557. }
  558. }
  559. }