PendulumValve.cs 15 KB

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