IoMfc.cs 25 KB

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