IoMfc.cs 18 KB

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