IoMfc.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 SorterRT.Modules;
  15. namespace VirgoRT.Devices.IODevices
  16. {
  17. //will be upgrade later
  18. public class MfcBase1 : BaseDevice, IDevice
  19. {
  20. public virtual double SetPoint { get; set; }
  21. public virtual double FeedBack { get; set; }
  22. public virtual bool IsOutOfTolerance { get; }
  23. public virtual double Scale { set; get; }
  24. public virtual string DisplayName { set; get; }
  25. private float _percent10Calculate;
  26. private float _percent20Calculate;
  27. private float _percent30Calculate;
  28. private float _percent40Calculate;
  29. private float _percent50Calculate;
  30. private float _percent60Calculate;
  31. private float _percent70Calculate;
  32. private float _percent80Calculate;
  33. private float _percent90Calculate;
  34. private float _percent100Calculate;
  35. private float _verificationCalculate;
  36. private float _verificationSetpoint;
  37. private string _verificationResult = "";
  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. return flow * Scale / RtInstance.ANALOG_TRANS_RANGE;
  195. }
  196. return 0;
  197. }
  198. set
  199. {
  200. if (_aoFlow != null)
  201. {
  202. byte[] flow = BitConverter.GetBytes((float)(value * RtInstance.ANALOG_TRANS_RANGE / Scale));
  203. _aoFlow.Buffer[_aoFlow.Index] = BitConverter.ToInt16(flow, 0);
  204. _aoFlow.Buffer[_aoFlow.Index + 1] = BitConverter.ToInt16(flow, 2);
  205. }
  206. }
  207. }
  208. [Subscription(AITMfcDataPropertyName.DefaultSetPoint)]
  209. public double DefaultSetPoint
  210. {
  211. get
  212. {
  213. if (_scDefaultSetPoint != null)
  214. return _scDefaultSetPoint.IntValue;
  215. return 0;
  216. }
  217. }
  218. [Subscription(AITMfcDataPropertyName.FeedBack)]
  219. public override double FeedBack
  220. {
  221. get
  222. {
  223. if (_aiFlow != null)
  224. {
  225. byte[] high = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index]);
  226. byte[] low = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index + 1]);
  227. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  228. return (_scRegulationFactor != null && _scRegulationFactor.IntValue > 0) ? flow * Scale / RtInstance.ANALOG_TRANS_RANGE / _scRegulationFactor.IntValue
  229. : flow * Scale / RtInstance.ANALOG_TRANS_RANGE;
  230. }
  231. return 0;
  232. }
  233. }
  234. [Subscription(AITMfcDataPropertyName.IsOutOfTolerance)]
  235. public override bool IsOutOfTolerance
  236. {
  237. get
  238. {
  239. return _toleranceChecker.Result;
  240. }
  241. }
  242. [Subscription(AITMfcDataPropertyName.IsEnableAlarm)]
  243. public bool EnableAlarm
  244. {
  245. get
  246. {
  247. if (_scEnableAlarm != null)
  248. return _scEnableAlarm.BoolValue;
  249. return false;
  250. }
  251. }
  252. [Subscription(AITMfcDataPropertyName.AlarmRange)]
  253. public double AlarmRange
  254. {
  255. get
  256. {
  257. if (_scAlarmRange != null)
  258. return _scAlarmRange.IntValue;
  259. return 0;
  260. }
  261. }
  262. [Subscription(AITMfcDataPropertyName.AlarmTime)]
  263. public double AlarmTime
  264. {
  265. get
  266. {
  267. if (_scAlarmTime != null)
  268. return _scAlarmTime.IntValue;
  269. return 0;
  270. }
  271. }
  272. [Subscription(AITMfcDataPropertyName.PressureAlarm)]
  273. public bool PressureAlarm
  274. {
  275. get { return _diPressureAlarm != null ? _diPressureAlarm.Value : true; }
  276. }
  277. [Subscription(AITMfcDataPropertyName.MfcAlarm)]
  278. public bool MfcAlarm
  279. {
  280. get
  281. {
  282. return _bMfcAlarm;
  283. }
  284. }
  285. [Subscription(AITMfcDataPropertyName.IsEnable)]
  286. public bool Enable
  287. {
  288. get
  289. {
  290. if (_scEnable != null)
  291. return _scEnable.BoolValue;
  292. return false;
  293. }
  294. }
  295. [Subscription(AITMfcDataPropertyName.IsOffline)]
  296. public bool IsOffline
  297. {
  298. get
  299. {
  300. if (_diOffline != null)
  301. return _diOffline.Value;
  302. return false;
  303. }
  304. }
  305. public override string DisplayName
  306. {
  307. get
  308. {
  309. if (_scGasName != null)
  310. return _scGasName.StringValue;
  311. return Display;
  312. }
  313. }
  314. private DeviceTimer rampTimer = new DeviceTimer();
  315. private double rampTarget;
  316. private double rampInitValue;
  317. private int rampTime;
  318. private bool _bMfcAlarm = false;
  319. private ToleranceChecker _toleranceChecker = new ToleranceChecker();
  320. private AIAccessor _aiFlow;
  321. private AOAccessor _aoFlow;
  322. private AOAccessor _aoRange;
  323. private DIAccessor _diOffline;
  324. private DIAccessor _diPressureAlarm;
  325. private SCConfigItem _scGasName;
  326. private SCConfigItem _scEnable;
  327. private SCConfigItem _scN2Scale;
  328. private SCConfigItem _scScaleFactor;
  329. private SCConfigItem _scAlarmRange;
  330. private SCConfigItem _scEnableAlarm;
  331. private SCConfigItem _scAlarmTime;
  332. private SCConfigItem _scDefaultSetPoint;
  333. private SCConfigItem _scRegulationFactor;
  334. private R_TRIG _trigOffline = new R_TRIG();
  335. private R_TRIG _trigPressureAlarm = new R_TRIG();
  336. private string _uniqueName;
  337. private string GasFlowOutOfTolerance = "GasFlowOutOfTolerance";
  338. public IoMfc(string module, XmlElement node, string ioModule = "")
  339. {
  340. Unit = node.GetAttribute("unit");
  341. base.Module = module;
  342. base.Name = node.GetAttribute("id");
  343. base.Display = node.GetAttribute("display");
  344. base.DeviceID = node.GetAttribute("schematicId");
  345. _aoRange = ParseAoNode("aoRange", node, ioModule);
  346. _diOffline = ParseDiNode("diOffline", node, ioModule);
  347. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  348. _aoFlow = ParseAoNode("aoFlow", node, ioModule);
  349. _diPressureAlarm = ParseDiNode("diPressureAlarm", node, ioModule);
  350. _scGasName = SC.GetConfigItem($"{Module}.{Name}.GasName");
  351. _scEnable = SC.GetConfigItem($"{Module}.{Name}.Enable");
  352. _scN2Scale = SC.GetConfigItem($"{Module}.{Name}.MfcN2Scale");
  353. _scScaleFactor = SC.GetConfigItem($"{Module}.{Name}.MfcScaleFactor");
  354. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmRange");
  355. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.MfcEnableAlarm");
  356. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmTime");
  357. _scDefaultSetPoint = SC.GetConfigItem($"{Module}.{Name}.DefaultSetPoint");
  358. _scRegulationFactor = SC.GetConfigItem($"{module}.{Name}.FlowRegulationFactor");
  359. _uniqueName = $"{Module}.{Name}";
  360. #if DEBUG
  361. Debug.Assert(!string.IsNullOrWhiteSpace(_scGasName.StringValue));
  362. Debug.Assert(null != _scN2Scale);
  363. Debug.Assert(null != _aoFlow);
  364. Debug.Assert(null != _aiFlow);
  365. #endif
  366. }
  367. public override bool Initialize()
  368. {
  369. EV.Subscribe(new EventItem("Event", GasFlowOutOfTolerance, "Gas Flow Out Of Tolerance", EventLevel.Alarm, EventType.HostNotification));
  370. DATA.Subscribe($"{Module}.{Name}", () =>
  371. {
  372. AITMfcData data = new AITMfcData
  373. {
  374. Type = "MFC",
  375. UniqueName = _uniqueName,
  376. DeviceName = Name,
  377. DeviceSchematicId = DeviceID,
  378. DisplayName = DisplayName,
  379. FeedBack = FeedBack,
  380. SetPoint = SetPoint,
  381. Scale = Scale,
  382. IsOffline = IsOffline,
  383. };
  384. return data;
  385. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  386. OP.Subscribe($"{Module}.{Name}.{AITMfcOperation.Ramp}", (name, args) =>
  387. {
  388. double target = (double)args[0];
  389. target = Math.Min(target, Scale);
  390. target = Math.Max(target, 0);
  391. Ramp(target, 0);
  392. EV.PostInfoLog(Module, $"Ramp to {target}{Unit}");
  393. return true;
  394. });
  395. DEVICE.Register($"{Module}.{Name}.{AITMfcOperation.Ramp}", (out string reason, int time, object[] param) =>
  396. {
  397. double target = Convert.ToDouble((string)param[0]);
  398. target = Math.Min(target, Scale);
  399. target = Math.Max(target, 0);
  400. Ramp(target, time);
  401. reason = $"{Display} ramp to {target}{Unit}";
  402. return true;
  403. });
  404. //@AAA use recipe
  405. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  406. {
  407. double target = Convert.ToDouble((string)param[0]);
  408. target = Math.Min(target, Scale);
  409. target = Math.Max(target, 0);
  410. Ramp(target, time);
  411. reason = $"{Display} ramp to {target}{Unit}";
  412. return true;
  413. });
  414. return base.Initialize();
  415. }
  416. public override void Monitor()
  417. {
  418. if (Enable)
  419. {
  420. Ramping();
  421. CheckTolerance();
  422. if (_aoRange != null)
  423. _aoRange.Value = (short)Scale;
  424. _trigOffline.CLK = IsOffline;
  425. if (_trigOffline.Q)
  426. {
  427. EV.PostAlarmLog(Module, string.Format("{0} is offline", DisplayName));
  428. _bMfcAlarm = true;
  429. }
  430. _trigPressureAlarm.CLK = PressureAlarm == false;
  431. if (_trigPressureAlarm.Q)
  432. EV.PostAlarmLog(Module, $"{Name}, {DisplayName} Pressure Alarm");
  433. if (PressureAlarm)
  434. {
  435. _trigPressureAlarm.RST = true;
  436. }
  437. }
  438. }
  439. public override void Reset()
  440. {
  441. _bMfcAlarm = false;
  442. _toleranceChecker.Reset(AlarmTime);
  443. _trigPressureAlarm.RST = true;
  444. _trigOffline.RST = true;
  445. }
  446. public override void Terminate()
  447. {
  448. Ramp(DefaultSetPoint, 0);
  449. }
  450. public override void Ramp(int time)
  451. {
  452. Ramp(DefaultSetPoint, time);
  453. }
  454. public override void Ramp(double target, int time)
  455. {
  456. target = Math.Max(0, target);
  457. target = Math.Min(Scale, target);
  458. rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值。零漂问题
  459. rampTime = time;
  460. rampTarget = target;
  461. rampTimer.Start(rampTime);
  462. }
  463. public override void StopRamp()
  464. {
  465. Ramp(SetPoint, 0);
  466. }
  467. private void Ramping()
  468. {
  469. if (rampTimer.IsTimeout() || rampTime == 0)
  470. {
  471. SetPoint = rampTarget;
  472. }
  473. else
  474. {
  475. SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;
  476. }
  477. }
  478. private void CheckTolerance()
  479. {
  480. if (!EnableAlarm)
  481. return;
  482. // 流率检查
  483. _toleranceChecker.Monitor(FeedBack, SetPoint - Math.Abs(AlarmRange), SetPoint + Math.Abs(AlarmRange), AlarmTime);
  484. if (_toleranceChecker.Trig)
  485. {
  486. EV.PostAlarmLog(Module, Display + $" 越界 in {AlarmTime:0} seconds");
  487. EV.Notify(GasFlowOutOfTolerance);
  488. }
  489. }
  490. }
  491. }