PendulumValve.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Venus_RT.Modules;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.Common.Equipment;
  7. using Venus_Core;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.Device;
  11. using Aitex.Core.RT.Log;
  12. using System.Threading.Tasks;
  13. using System.Collections.Concurrent;
  14. using Aitex.Core.Common.DeviceData;
  15. using MECF.Framework.Common.CommonData.DeviceData;
  16. using Aitex.Core.RT.DataCenter;
  17. using Venus_RT.Modules.PMs;
  18. using System.Linq;
  19. namespace Venus_RT.Devices
  20. {
  21. class PendulumValve : IDevice
  22. {
  23. public enum Operation
  24. {
  25. SetPosition,
  26. GetPositionSP,
  27. Hold,
  28. OpenValve,
  29. CloseValve,
  30. SetPressure,
  31. GetPressureSP,
  32. GetAssembly,
  33. GetPosition,
  34. GetPressure,
  35. GetSensor1Data,
  36. GetSensor2Data,
  37. GetPressureCtrlStatus,
  38. GetDeviceStatus,
  39. GetWarnings,
  40. GetSensorOffset,
  41. GetSensor1Offset,
  42. GetSensor2Offset,
  43. GetLearnStatus,
  44. GetLearnPressureLimit,
  45. GetErrorStatus,
  46. GetFatalErrorStatus,
  47. GetThrottleCycleCounter,
  48. GetIsolationCycleCounter,
  49. GetPowerUpCounter,
  50. GetHardwareConfiguration,
  51. GetFirmwareConfiguration,
  52. GetIdentification,
  53. SetAccessMode,
  54. SetInterfaceConfiguration,
  55. SetValveConfiguration,
  56. SetSensorConfiguration,
  57. SetRangeConfiguration,
  58. SetZero,
  59. SetPressureAlignment,
  60. SetLearn,
  61. DownloadLearnData,
  62. UploadLearnData,
  63. SetPIDConfiguration,
  64. GetPIDConfiguration,
  65. SetValveSpeed,
  66. GetValveSpeed,
  67. Reset,
  68. Invalid,
  69. SetP,
  70. SetI,
  71. GetP,
  72. GetI
  73. }
  74. private readonly Dictionary<Operation, string> _noneParaCommandOp = new Dictionary<Operation, string>
  75. {
  76. {Operation.GetPositionSP, "i:38" },
  77. {Operation.Hold, "H:" },
  78. {Operation.CloseValve, "C:" },
  79. {Operation.OpenValve, "O:" },
  80. {Operation.GetPressureSP, "i:38" },
  81. {Operation.GetAssembly, "i:76" },
  82. {Operation.GetPosition, "A:" },
  83. {Operation.GetPressure, "P:" },
  84. {Operation.GetSensor1Data, "i:64" },
  85. {Operation.GetSensor2Data, "i:65" },
  86. {Operation.GetPressureCtrlStatus, "i:36" },
  87. {Operation.GetDeviceStatus, "i:30" },
  88. {Operation.GetWarnings, "i:35" },
  89. {Operation.GetSensorOffset, "i:62" },
  90. {Operation.GetSensor1Offset, "i:60" },
  91. {Operation.GetSensor2Offset, "i:61" },
  92. {Operation.GetLearnStatus, "i:32" },
  93. {Operation.GetLearnPressureLimit, "i:34" },
  94. {Operation.GetErrorStatus, "i:52" },
  95. {Operation.GetFatalErrorStatus, "i:50" },
  96. {Operation.GetThrottleCycleCounter, "i:70" },
  97. {Operation.GetIsolationCycleCounter, "i:71" },
  98. {Operation.GetPowerUpCounter, "i:72" },
  99. {Operation.GetHardwareConfiguration, "i:80" },
  100. {Operation.GetFirmwareConfiguration, "i:82" },
  101. {Operation.GetIdentification, "i:83" },
  102. {Operation.GetP, "i:02B04" },
  103. {Operation.GetI, "i:02B05" },
  104. };
  105. private readonly Dictionary<Operation, string> _singleParaCommandOp = new Dictionary<Operation, string>
  106. {
  107. {Operation.SetPosition, "R:{0:D6}" },
  108. {Operation.SetPressure, "S:{0:D8}" },
  109. {Operation.SetAccessMode, "c:01{0:D2}" },
  110. {Operation.SetInterfaceConfiguration, "s:20{0:D8}" },
  111. {Operation.SetValveConfiguration, "s:04{0:D2}000000" },
  112. {Operation.SetSensorConfiguration, "s:01{0:D8}" },
  113. {Operation.SetRangeConfiguration, "s:21{0:D8}" },
  114. {Operation.SetPressureAlignment, "c:6002{0:D8}" },
  115. {Operation.SetLearn, "L:{0:D8}" },
  116. {Operation.UploadLearnData, "u:{0:D3}" },
  117. {Operation.SetPIDConfiguration, "s:02{0:D8}" },
  118. {Operation.SetValveSpeed, "V:00{0:D4}" },
  119. {Operation.SetP, "s:02B04{0}" },
  120. {Operation.SetI, "s:02B05{0}" }
  121. };
  122. private readonly Dictionary<Operation, string> _twoParaCommandOp = new Dictionary<Operation, string>
  123. {
  124. {Operation.DownloadLearnData, "d:{0:D3}{1:D8}" }
  125. };
  126. private readonly Dictionary<string, string> _deviceError = new Dictionary<string, string>
  127. {
  128. {"000001", "Parity error" },
  129. {"000002", "Input buffer overflow (to many characters) " },
  130. {"000003", "Framing error (data length, number of stop bits)" },
  131. {"000010", "<CR> or <LF> missing" },
  132. {"000011", ": missing" },
  133. {"000012", "Invalid number of characters (between : and [CR][LF])" },
  134. {"000020", "Unknown command" },
  135. {"000021", "Unknown command" },
  136. {"000022", "Invalid value" },
  137. {"000023", "Invalid value" },
  138. {"000030", "Value out of range" },
  139. {"000041", "Command not applicable for hardware configuration" },
  140. {"000060", "ZERO disabled" },
  141. {"000080", "Command not accepted due to local operation" },
  142. {"000082", "Command not accepted due to synchronization, CLOSED or OPEN by digital input, safety mode or fatal error" },
  143. };
  144. public string Module { get; set; }
  145. public string Name { get; set; }
  146. public float Pressure { get; private set; }
  147. public float Position { get; private set; }
  148. public string Status { get; private set; }
  149. public bool IsOpen { get; private set; }
  150. private readonly string EOF = "\r\n";
  151. private readonly int _readInterval = 100;
  152. private readonly int _position_unit = 100;
  153. private readonly float _pressure_ful_range = 500;
  154. private readonly int _foreLinePressureLimit = 750;
  155. private readonly int _chamberPressureLimit = 600;
  156. private readonly int _turboPumpSpeedLimit = 100;
  157. private int _queryFlag = 0;
  158. private readonly AsyncSerialPort _serial;
  159. private Stopwatch _queryWatch = new Stopwatch();
  160. private string _lastAlarmString = string.Empty;
  161. private Operation[] _querys = new Operation[]
  162. {
  163. Operation.GetPressure,
  164. Operation.GetPosition,
  165. Operation.GetDeviceStatus,
  166. Operation.GetP,
  167. Operation.GetI
  168. };
  169. BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
  170. private JetChamber m_JetChamber;
  171. private PressureType m_PressureType;
  172. public float PValue;
  173. public float IValue;
  174. private float m_PressureSetPoint;
  175. private float m_PositionSetPoint;
  176. //private ModuleName _mod;
  177. public AITPendulumValveData DeviceData
  178. {
  179. get
  180. {
  181. AITPendulumValveData deviceData = new AITPendulumValveData
  182. {
  183. DeviceName = Name,
  184. Module = Module,
  185. Pressure = Pressure,
  186. IsOpen = IsOpen || Position > 0,
  187. Position = IsOpen ? Position : 0,
  188. PValue = PValue,
  189. IValue = IValue,
  190. PressureSetPoint= IsOpen ? m_PressureSetPoint : 0,
  191. PositionSetPoint= IsOpen ? m_PositionSetPoint : 0
  192. };
  193. return deviceData;
  194. }
  195. }
  196. public PendulumValve(ModuleName mod,PressureType pressureType)
  197. {
  198. Name = VenusDevice.PendulumValve.ToString();
  199. Module = mod.ToString();
  200. var _PortNum = SC.GetStringValue($"{mod}.PendulumValve.Port");
  201. _serial = new AsyncSerialPort(_PortNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, EOF);
  202. _pressure_ful_range = SC.GetValue<int>($"{mod}.PendulumValve.PressureFullRange");
  203. _foreLinePressureLimit = SC.GetValue<int>($"{mod}.PendulumValve.ForelinePressureLimit");
  204. _chamberPressureLimit = SC.GetValue<int>($"{mod}.PendulumValve.ChamberPressureLimit");
  205. _turboPumpSpeedLimit = SC.GetValue<int>($"{mod}.PendulumValve.TurboPumpSpeedLimit");
  206. IsOpen = false;
  207. Task.Run(() =>
  208. {
  209. foreach (var data in blockingCollection.GetConsumingEnumerable())
  210. {
  211. _serial?.Write(data);
  212. System.Threading.Thread.Sleep(100);
  213. }
  214. });
  215. m_PressureType = pressureType;
  216. m_JetChamber = (JetChamber)SC.GetValue<int>($"{mod}.ChamberType");
  217. }
  218. public bool Initialize()
  219. {
  220. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  221. DATA.Subscribe($"{Module}.{Name}.Position", () => Position);
  222. DATA.Subscribe($"{Module}.{Name}.Pressure", () => Pressure);
  223. DATA.Subscribe($"{Module}.{Name}.PressureSetPoint", () => m_PressureSetPoint);
  224. DATA.Subscribe($"{Module}.{Name}.PositionSetPoint", () => m_PositionSetPoint);
  225. DATA.Subscribe($"{Module}.{Name}.IsOpen", () => IsOpen);
  226. if (!_serial.Open())
  227. {
  228. _noRepeatAlarm("Pendulum Valve 串口无法打开");
  229. return false;
  230. }
  231. _serial.OnDataChanged += OnPortDataChanged;
  232. _serial.OnErrorHappened += OnErrorOccurred;
  233. _queryWatch.Restart();
  234. return true;
  235. }
  236. public void Monitor()
  237. {
  238. if (_queryWatch.ElapsedMilliseconds > _readInterval && _querys.Length>0)
  239. {
  240. SendCommand(_querys[_queryFlag++ % _querys.Length]);
  241. _queryWatch.Restart();
  242. }
  243. ClearDeviceValue();
  244. }
  245. public void Reset()
  246. {
  247. }
  248. public void Terminate()
  249. {
  250. if (IsOpen)
  251. {
  252. TurnValve(false);
  253. }
  254. _serial?.Close();
  255. }
  256. private void OnErrorOccurred(string obj)
  257. {
  258. _noRepeatAlarm($"[{Module}] VAT Pendulum Valve error: [{obj}]");
  259. }
  260. private void OnPortDataChanged(string obj)
  261. {
  262. if (string.IsNullOrEmpty(obj))
  263. {
  264. _noRepeatAlarm("VAT Pendulum Valve receive empty message");
  265. return;
  266. }
  267. try
  268. {
  269. var data = obj.TrimEnd().Split(':');
  270. switch (data[0])
  271. {
  272. case "P":
  273. {
  274. Double pressure;
  275. if (Double.TryParse(data[1], out pressure))
  276. {
  277. Pressure = Convert.ToSingle(pressure) * _pressure_ful_range / 1000000;
  278. //LOG.Write(eEvent.EV_DEVICE_INFO, Module, $" Pressure Raw:{Convert.ToSingle(pressure)} Trans:{Pressure}");
  279. //if ((m_JetChamber == JetChamber.Kepler2200A || m_JetChamber == JetChamber.Kepler2200B))
  280. //{
  281. // //Pressure = Convert.ToSingle(ConvertPressureUnit.ConvertmTorrToPa(pressure * _pressure_ful_range / 1000000));
  282. // Pressure = Convert.ToSingle( pressure * _pressure_ful_range / 1000000);
  283. //}
  284. //else if (m_JetChamber == JetChamber.VenusSE)
  285. //{
  286. // Pressure = Convert.ToSingle(pressure) / 100;
  287. //}
  288. //else if (m_JetChamber == JetChamber.VenusDE)
  289. //{
  290. // Pressure = Convert.ToSingle(pressure);
  291. //}
  292. //else
  293. //{
  294. // Pressure = Convert.ToSingle(pressure) * _pressure_ful_range / 1000000;
  295. //}
  296. }
  297. }
  298. break;
  299. case "A":
  300. {
  301. float position;
  302. if (float.TryParse(data[1], out position))
  303. Position = position / _position_unit;
  304. }
  305. break;
  306. case "i":
  307. {
  308. _tryParseInqueryData(obj);
  309. }
  310. break;
  311. case "C":
  312. IsOpen = false;
  313. break;
  314. case "O":
  315. IsOpen = true;
  316. break;
  317. case "H":
  318. case "R":
  319. case "S":
  320. break;
  321. case "E":
  322. {
  323. _noRepeatAlarm($"[{Module}] VAT Pendulum Valve device error: {_deviceError[data[1]]}");
  324. }
  325. break;
  326. default:
  327. {
  328. _noRepeatAlarm($"VAT Pendulum Valve: unrecognized received data: {obj}");
  329. break;
  330. }
  331. }
  332. }
  333. catch (Exception ex)
  334. {
  335. _noRepeatAlarm($"[{Module}] VAT Pendulum Valve error: [{ex.Message}], Data: {obj}");
  336. }
  337. }
  338. private bool SendCommand(Operation op)
  339. {
  340. if (_noneParaCommandOp.ContainsKey(op))
  341. {
  342. blockingCollection.Add(_noneParaCommandOp[op] + EOF);
  343. return true;
  344. }
  345. else
  346. {
  347. return false;
  348. }
  349. }
  350. private bool SendCommand(Operation op, int data)
  351. {
  352. if (_singleParaCommandOp.ContainsKey(op))
  353. {
  354. var cmd = string.Format(_singleParaCommandOp[op], data) + EOF;
  355. return _sendCmd(cmd);
  356. }
  357. else
  358. {
  359. _noRepeatAlarm("This VAT Pendulum Valve command need 1 data");
  360. return false;
  361. }
  362. }
  363. private bool SendCommand(Operation op, float data)
  364. {
  365. if (_singleParaCommandOp.ContainsKey(op))
  366. {
  367. var cmd = string.Format(_singleParaCommandOp[op], data) + EOF;
  368. return _sendCmd(cmd);
  369. }
  370. else
  371. {
  372. _noRepeatAlarm("This VAT Pendulum Valve command need 1 data");
  373. return false;
  374. }
  375. }
  376. private bool _sendCmd(string cmd)
  377. {
  378. blockingCollection.Add(cmd);
  379. return true;
  380. //return _serial.Write(cmd);
  381. }
  382. private void _noRepeatAlarm(string alarm)
  383. {
  384. if (_lastAlarmString != alarm)
  385. {
  386. _lastAlarmString = alarm;
  387. LOG.Write(eEvent.ERR_PENDULUM_VALVE, ModuleHelper.Converter(Module), alarm);
  388. EV.PostAlarmLog(Module, alarm);
  389. }
  390. }
  391. private bool _tryParseInqueryData(string data)
  392. {
  393. var cmdPrix = data.Substring(0, 4);
  394. if (cmdPrix == "i:02")
  395. {
  396. var pi = data.Substring(5, 2);
  397. if (pi == "04")
  398. {
  399. float s;
  400. float.TryParse(data.Substring(7, data.Length - 9), out s);
  401. PValue = s;
  402. }
  403. else if (pi == "05")
  404. {
  405. float s;
  406. float.TryParse(data.Substring(7, data.Length - 9), out s);
  407. IValue = s;
  408. }
  409. }
  410. else
  411. {
  412. Operation oper = Operation.Invalid;
  413. foreach (var item in _noneParaCommandOp)
  414. {
  415. if (item.Value == cmdPrix)
  416. {
  417. oper = item.Key;
  418. break;
  419. }
  420. }
  421. if (oper == Operation.Invalid)
  422. {
  423. return false;
  424. }
  425. switch (oper)
  426. {
  427. case Operation.GetDeviceStatus:
  428. {
  429. Status = data.Substring(4, 8);
  430. if (Status[1] == 'E')
  431. {
  432. // Fatal Error
  433. _noRepeatAlarm($"Device Status error:{Status}");
  434. }
  435. else if (Status[3] == '1')
  436. {
  437. // Warning Present
  438. _noRepeatAlarm($"Device Warning Present:{Status}");
  439. }
  440. else if (Status[1] == '3')
  441. {
  442. IsOpen = false;
  443. }
  444. else if (Status[1] == '4')
  445. {
  446. IsOpen = true;
  447. }
  448. }
  449. break;
  450. case Operation.GetPressureSP:
  451. case Operation.GetPositionSP:
  452. {
  453. string Pressure = data.Substring(4, 8);
  454. //LOG.Write(eEvent.EV_DEVICE_INFO, Module, $" PV Pressure SetPoint: {Pressure}");
  455. }
  456. break;
  457. default:
  458. break;
  459. }
  460. }
  461. return true;
  462. }
  463. public bool SetPosition(float postion)
  464. {
  465. if (_CheckStatus(true))
  466. {
  467. //TurnValve(true);
  468. m_PositionSetPoint = postion;
  469. float setPosition = (float)Math.Round(postion, 1);
  470. //LOG.Write(eEvent.ERR_PENDULUM_VALVE,Module,$"Set Position : {(int)(setPosition * _position_unit)}");
  471. return SendCommand(Operation.SetPosition, (int)(setPosition * _position_unit));
  472. }
  473. return false;
  474. }
  475. public bool SetPressure(float pressure)
  476. {
  477. if (_CheckStatus(true))
  478. {
  479. //TurnValve(true);
  480. m_PressureSetPoint = pressure;
  481. return SendCommand(Operation.SetPressure, Convert.ToInt32(pressure * 1000000 / _pressure_ful_range));
  482. //if (m_PressureType == PressureType.Pa)
  483. //{
  484. // //var value = Convert.ToInt32(ConvertPressureUnit.ConvertPaTomtorr(pressure) * 1000000 / (_pressure_ful_range));
  485. // return SendCommand(Operation.SetPressure, Convert.ToInt32(ConvertPressureUnit.ConvertPaTomtorr(pressure) * 1000000 / (_pressure_ful_range)));
  486. //}
  487. //else
  488. //{
  489. // return SendCommand(Operation.SetPressure, Convert.ToInt32(pressure * 1000000 / _pressure_ful_range));
  490. //}
  491. }
  492. return false;
  493. }
  494. public bool Hold()
  495. {
  496. return SendCommand(Operation.Hold);
  497. }
  498. public bool TurnValve(bool on)
  499. {
  500. if (on == false)
  501. {
  502. return SendCommand(on ? Operation.OpenValve : Operation.CloseValve);
  503. }
  504. else
  505. {
  506. if (_CheckStatus(on))
  507. {
  508. return SendCommand(on ? Operation.OpenValve : Operation.CloseValve);
  509. }
  510. }
  511. return false;
  512. }
  513. public bool SetPValue(float pValue)
  514. {
  515. return SendCommand(Operation.SetP, pValue);
  516. }
  517. public bool SetIValue(float pValue)
  518. {
  519. return SendCommand(Operation.SetI, pValue);
  520. }
  521. bool _CheckStatus(bool bTurnOn = false)
  522. {
  523. if (Status != null && Status[1] == 'E')
  524. {
  525. _noRepeatAlarm($"PendulumValve is error status, can do turn on/off operation.");
  526. return false;
  527. }
  528. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  529. if (bTurnOn && !_chamber.IsTurboPumpAtSpeed)
  530. {
  531. _noRepeatAlarm($"Turbo Pump not at speed, can not turn on pendulum valve.");
  532. return false;
  533. }
  534. if (_chamber.ForelinePressure > _foreLinePressureLimit)
  535. {
  536. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"Foreline Pressure:{_chamber.ForelinePressure} is higher than {_foreLinePressureLimit}{m_PressureType}, can not turn on pendulum valve.");
  537. return false;
  538. }
  539. if (_chamber.ChamberPressure > _chamberPressureLimit && _chamber.TurboPumpSpeed > _turboPumpSpeedLimit)
  540. {
  541. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"Chamber Pressure:{_chamber.ChamberPressure} is higher than {_chamberPressureLimit}{m_PressureType} and Chamber.TurboPumpSpeed is higher than {_turboPumpSpeedLimit}, can not turn on pendulum valve.");
  542. return false;
  543. }
  544. if (_chamber.IsTurboPumpRunning )
  545. {
  546. if (!_chamber.IsISOOpen && bTurnOn)
  547. {
  548. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"Chamber TurboPumpingvalve is not open, can not turn on pendulum valve.");
  549. return false;
  550. }
  551. }
  552. return true;
  553. }
  554. public bool ReConnect()
  555. {
  556. return _serial.ReConnect();
  557. }
  558. private void ClearDeviceValue()
  559. {
  560. if (_serial.IsConnected==false)
  561. {
  562. IsOpen = false;
  563. Position = 0;
  564. }
  565. }
  566. }
  567. }