IoMfc.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 bool _bRamping = false;
  323. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  324. private AIAccessor _aiFlow;
  325. private AOAccessor _aoFlow;
  326. private AOAccessor _aoRange;
  327. private DIAccessor _diOffline;
  328. private DIAccessor _diPressureAlarm;
  329. private SCConfigItem _scGasName;
  330. private SCConfigItem _scEnable;
  331. private SCConfigItem _scN2Scale;
  332. private SCConfigItem _scScaleFactor;
  333. private SCConfigItem _scAlarmRange;
  334. private SCConfigItem _scEnableAlarm;
  335. private SCConfigItem _scAlarmTime;
  336. private SCConfigItem _scDefaultSetPoint;
  337. private SCConfigItem _scRegulationFactor;
  338. private R_TRIG _trigOffline = new R_TRIG();
  339. private R_TRIG _trigPressureAlarm = new R_TRIG();
  340. private string _uniqueName;
  341. private string GasFlowOutOfTolerance = "GasFlowOutOfTolerance";
  342. public IoMfc(string module, XmlElement node, string ioModule = "")
  343. {
  344. Unit = node.GetAttribute("unit");
  345. base.Module = module;
  346. base.Name = node.GetAttribute("id");
  347. base.Display = node.GetAttribute("display");
  348. base.DeviceID = node.GetAttribute("schematicId");
  349. _aoRange = ParseAoNode("aoRange", node, ioModule);
  350. _diOffline = ParseDiNode("diOffline", node, ioModule);
  351. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  352. _aoFlow = ParseAoNode("aoFlow", node, ioModule);
  353. _diPressureAlarm = ParseDiNode("diPressureAlarm", node, ioModule);
  354. _scGasName = SC.GetConfigItem($"{Module}.{Name}.GasName");
  355. _scEnable = SC.GetConfigItem($"{Module}.{Name}.Enable");
  356. _scN2Scale = SC.GetConfigItem($"{Module}.{Name}.MfcN2Scale");
  357. _scScaleFactor = SC.GetConfigItem($"{Module}.{Name}.MfcScaleFactor");
  358. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmRange");
  359. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.MfcEnableAlarm");
  360. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmTime");
  361. _scDefaultSetPoint = SC.GetConfigItem($"{Module}.{Name}.DefaultSetPoint");
  362. _scRegulationFactor = SC.GetConfigItem($"{module}.{Name}.FlowRegulationFactor");
  363. _uniqueName = $"{Module}.{Name}";
  364. #if DEBUG
  365. //Debug.Assert(!string.IsNullOrWhiteSpace(_scGasName.StringValue));
  366. //Debug.Assert(null != _scN2Scale);
  367. //Debug.Assert(null != _aoFlow);
  368. //Debug.Assert(null != _aiFlow);
  369. #endif
  370. }
  371. public override bool Initialize()
  372. {
  373. EV.Subscribe(new EventItem("Event", GasFlowOutOfTolerance, "Gas Flow Out Of Tolerance", EventLevel.Alarm, EventType.HostNotification));
  374. DATA.Subscribe($"{Module}.{Name}", () =>
  375. {
  376. AITMfcData data = new AITMfcData
  377. {
  378. Type = "MFC",
  379. UniqueName = _uniqueName,
  380. DeviceName = Name,
  381. DeviceSchematicId = DeviceID,
  382. DisplayName = DisplayName,
  383. FeedBack = FeedBack,
  384. SetPoint = SetPoint,
  385. Scale = Scale,
  386. IsOffline = IsOffline,
  387. IsWarning =PressureAlarm
  388. };
  389. return data;
  390. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  391. OP.Subscribe($"{Module}.{Name}.{AITMfcOperation.Ramp}", (name, args) =>
  392. {
  393. double target = (double)args[0];
  394. target = Math.Min(target, Scale);
  395. target = Math.Max(target, 0);
  396. Ramp(target, 0);
  397. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Ramp to {target}{Unit}");
  398. return true;
  399. });
  400. DEVICE.Register($"{Module}.{Name}.{AITMfcOperation.Ramp}", (out string reason, int time, object[] param) =>
  401. {
  402. double target = Convert.ToDouble((string)param[0]);
  403. target = Math.Min(target, Scale);
  404. target = Math.Max(target, 0);
  405. Ramp(target, time);
  406. reason = $"{Display} ramp to {target}{Unit}";
  407. return true;
  408. });
  409. OP.Subscribe($"{Module}.{Name}.SetPoint", (cmd, args) => {
  410. SetPoint = Convert.ToDouble( args[0]);
  411. return true;
  412. });
  413. //@AAA use recipe
  414. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  415. {
  416. double target = Convert.ToDouble((string)param[0]);
  417. target = Math.Min(target, Scale);
  418. target = Math.Max(target, 0);
  419. Ramp(target, time);
  420. reason = $"{Display} ramp to {target}{Unit}";
  421. return true;
  422. });
  423. return base.Initialize();
  424. }
  425. public override void Monitor()
  426. {
  427. if (Enable)
  428. {
  429. Ramping();
  430. CheckTolerance();
  431. if (_aoRange != null)
  432. _aoRange.Value = (short)Scale;
  433. _trigOffline.CLK = IsOffline;
  434. if (_trigOffline.Q)
  435. {
  436. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, string.Format("{0} is offline", DisplayName));
  437. _bMfcAlarm = true;
  438. }
  439. _trigPressureAlarm.CLK = PressureAlarm == false;
  440. if (_trigPressureAlarm.Q)
  441. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"{Name}, {DisplayName} Pressure Alarm");
  442. if (PressureAlarm)
  443. {
  444. _trigPressureAlarm.RST = true;
  445. }
  446. }
  447. }
  448. public override void Reset()
  449. {
  450. _bMfcAlarm = false;
  451. _toleranceChecker.Reset(AlarmTime);
  452. _trigPressureAlarm.RST = true;
  453. _trigOffline.RST = true;
  454. }
  455. public override void Terminate()
  456. {
  457. _bRamping = false;
  458. Ramp(DefaultSetPoint, 0);
  459. }
  460. public override void Ramp(int time)
  461. {
  462. _bRamping = true;
  463. Ramp(DefaultSetPoint, time);
  464. }
  465. public override void Ramp(double target, int time)
  466. {
  467. _bRamping = true;
  468. target = Math.Max(0, target);
  469. target = Math.Min(Scale, target);
  470. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值。零漂问题
  471. rampTime = time;
  472. rampTarget = target;
  473. rampTimer.Start(rampTime);
  474. }
  475. public override void StopRamp()
  476. {
  477. _bRamping = false;
  478. Ramp(SetPoint, 0);
  479. }
  480. private void Ramping()
  481. {
  482. if (_bRamping)
  483. {
  484. if (rampTimer.IsTimeout() || rampTime == 0)
  485. {
  486. SetPoint = rampTarget;
  487. _bRamping = false;
  488. }
  489. else
  490. {
  491. SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;
  492. }
  493. }
  494. }
  495. private void CheckTolerance()
  496. {
  497. if (!EnableAlarm)
  498. return;
  499. // 流率检查
  500. _toleranceChecker.Monitor(FeedBack, SetPoint - Math.Abs(AlarmRange), SetPoint + Math.Abs(AlarmRange), AlarmTime);
  501. if (_toleranceChecker.Trig)
  502. {
  503. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, Display + $" 越界 in {AlarmTime:0} seconds");
  504. EV.Notify(GasFlowOutOfTolerance);
  505. }
  506. }
  507. }
  508. }