PendulumValve.cs 15 KB

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