PendulumValve.cs 20 KB

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