IoMfc.cs 22 KB

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