PendulumValve.cs 16 KB

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