PendulumValve.cs 17 KB

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