IoRf.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using System;
  2. using System.Diagnostics;
  3. using System.Xml;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.RT.Tolerance;
  13. using Aitex.Core.Util;
  14. using MECF.Framework.Common.Device.Bases;
  15. using SorterRT.Modules;
  16. namespace VirgoRT.Devices
  17. {
  18. public class IoRf : RfPowerBase
  19. {
  20. //IO
  21. public bool IsOffline => _diOffline != null && _diOffline.RawData;
  22. public override float ScalePower => (float)_scPowerRange;
  23. public float ScaleFrequency => 1000;
  24. public float ScaleDuty => 100;
  25. public string UnitPower => "w";
  26. public string UnitFrequency => "Hz";
  27. public string UnitDuty => "%";
  28. [Subscription(AITRfProperty.IsOverTemp)]
  29. public bool IsOverTemp => _diOverTemp != null && _diOverTemp.Value;
  30. [Subscription(AITRfProperty.RFEnable)]
  31. public override bool IsPowerOn //True:on
  32. {
  33. get
  34. {
  35. if (_diStatus != null)
  36. return _diStatus.Value;
  37. return _doPowerOn.Value;
  38. }
  39. }
  40. [Subscription(AITRfProperty.RFSetPoint)]
  41. public override float PowerSetPoint
  42. {
  43. get
  44. {
  45. if (_aoPower != null)
  46. {
  47. byte[] high = BitConverter.GetBytes(_aoPower.Buffer[_aoPower.Index]);
  48. byte[] low = BitConverter.GetBytes(_aoPower.Buffer[_aoPower.Index + 1]);
  49. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  50. return flow * ScalePower / RtInstance.ANALOG_TRANS_RANGE;
  51. //return _aoPower.Value* ScalePower / RtInstance.ANALOG_TRANS_RANGE;
  52. }
  53. return 0;
  54. }
  55. set
  56. {
  57. if (_aoPower != null)
  58. {
  59. byte[] flow = BitConverter.GetBytes((float)(value * RtInstance.ANALOG_TRANS_RANGE / ScalePower));
  60. _aoPower.Buffer[_aoPower.Index] = BitConverter.ToInt16(flow, 0);
  61. _aoPower.Buffer[_aoPower.Index + 1] = BitConverter.ToInt16(flow, 2);
  62. //_aoPower.Value = (short)(value * RtInstance.ANALOG_TRANS_RANGE / ScalePower);
  63. }
  64. }
  65. }
  66. [Subscription(AITRfProperty.RFDuty)]
  67. public float DutySetPoint => _aoPulsingDutyCycle?.Value ?? 0;
  68. [Subscription(AITRfProperty.RFFrequency)]
  69. public float FrequencySetPoint => _aoPulsingFrency?.Value ?? 0;
  70. [Subscription(AITRfProperty.RFForwardPower)]
  71. public float RFForwardPower
  72. {
  73. get
  74. {
  75. if (_aiForwardPower == null)
  76. return 0;
  77. byte[] high = BitConverter.GetBytes(_aiForwardPower.Buffer[_aiForwardPower.Index]);
  78. byte[] low = BitConverter.GetBytes(_aiForwardPower.Buffer[_aiForwardPower.Index + 1]);
  79. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  80. return (_scRegulationFactor > 0) ? (float)(flow * ScalePower / RtInstance.ANALOG_TRANS_RANGE / _scRegulationFactor)
  81. : flow * ScalePower / RtInstance.ANALOG_TRANS_RANGE;
  82. }
  83. }
  84. [Subscription(AITRfProperty.RFReflectPower)]
  85. public float RFReflectPower => _aiReflectedPower?.Value ?? 0;
  86. [Subscription(AITRfProperty.RFInterlock)]
  87. public bool RFInterlock => _diIntlk == null || _diIntlk.Value;
  88. public new AITRfData DeviceData
  89. {
  90. get
  91. {
  92. return new AITRfData()
  93. {
  94. Module = Module,
  95. DeviceName = Name,
  96. DeviceSchematicId = DeviceID,
  97. DisplayName = Display,
  98. ForwardPower = RFForwardPower,
  99. ReflectPower = RFReflectPower,
  100. IsInterlockOk = RFInterlock,
  101. IsRfOn = IsPowerOn,
  102. PowerSetPoint = PowerSetPoint,
  103. FrequencySetPoint = FrequencySetPoint,
  104. DutySetPoint = DutySetPoint,
  105. ScalePower = ScalePower,
  106. ScaleDuty = ScaleDuty,
  107. ScaleFrequency = ScaleFrequency,
  108. UnitDuty = UnitDuty,
  109. UnitFrequency = UnitFrequency,
  110. UnitPower = UnitPower,
  111. WorkMode = (int)RfMode.ContinuousWaveMode,
  112. PowerOnElapsedTime = PowerOnTime,
  113. EnablePulsing = EnablePulsing,
  114. EnableReflectPower = EnableReflectPower,
  115. EnableVoltageCurrent = EnableVoltageCurrent,
  116. Voltage = Voltage,
  117. Current = Current,
  118. };
  119. }
  120. }
  121. [Subscription(AITRfProperty.Voltage)]
  122. public float Voltage => _aiVoltage?.Value ?? 0;
  123. [Subscription(AITRfProperty.Current)]
  124. public float Current => _aiCurrent?.Value ?? 0;
  125. private DateTime _powerOnStartTime;
  126. private TimeSpan _powerOnElapsedTime;
  127. [Subscription("PowerOnTime")]
  128. public string PowerOnTime
  129. {
  130. get
  131. {
  132. if (IsPowerOn)
  133. _powerOnElapsedTime = DateTime.Now - _powerOnStartTime;
  134. return
  135. $"{(int)_powerOnElapsedTime.TotalHours:00}:{_powerOnElapsedTime.Minutes:00}:{(_powerOnElapsedTime.Seconds > 0 ? (_powerOnElapsedTime.Seconds + 1) : 0):00}";
  136. }
  137. }
  138. public bool EnablePulsing => _scEnablePulsingFunction;
  139. public bool EnableReflectPower => _scEnableReflectPower;
  140. public bool EnableVoltageCurrent => _scEnableVoltageCurrent;
  141. private readonly DIAccessor _diStatus;
  142. private readonly DIAccessor _diIntlk;
  143. private readonly DIAccessor _diOffline;
  144. private readonly DIAccessor _diOverTemp;
  145. private DIAccessor _diIArc;
  146. private DIAccessor _diVArc;
  147. private DIAccessor _diBreakState;
  148. private readonly DIAccessor _diHighReflectPower;
  149. private readonly DIAccessor _diMatchCommWithGenerator;
  150. private readonly DOAccessor _doPowerOn;
  151. private readonly AIAccessor _aiReflectedPower;
  152. private readonly AIAccessor _aiForwardPower;
  153. private readonly AIAccessor _aiVoltage;
  154. private readonly AIAccessor _aiCurrent;
  155. private readonly AOAccessor _aoPower;
  156. //private AOAccessor _aoWorkMode;
  157. private readonly AOAccessor _aoPulsingFrency;
  158. private readonly AOAccessor _aoPulsingDutyCycle;
  159. private readonly AOAccessor _aoCoefficient;
  160. private readonly double _scPowerAlarmRange;
  161. private readonly double _scPowerAlarmTime;
  162. private readonly double _scReflectPowerAlarmRange;
  163. private readonly double _scReflectPowerAlarmTime;
  164. private readonly double _scPowerRange;
  165. private readonly double _scCoefficient;
  166. private readonly bool _scEnablePulsingFunction;
  167. private readonly bool _scEnableReflectPower;
  168. private readonly bool _scEnableVoltageCurrent;
  169. private readonly double _scRegulationFactor;
  170. private readonly F_TRIG _interlockTrig = new F_TRIG();
  171. private readonly R_TRIG _rfOnTrigger = new R_TRIG();
  172. private readonly R_TRIG _trigOffline = new R_TRIG();
  173. private readonly R_TRIG _trigOverTemp = new R_TRIG();
  174. private readonly R_TRIG _trigHighReflectPower = new R_TRIG();
  175. private readonly R_TRIG _trigMatchCommWithGenerator = new R_TRIG();
  176. private ToleranceChecker _checkerPower;
  177. private ToleranceChecker _checkerReflectPower;
  178. public IoRf(string module, XmlElement node, string ioModule = "")
  179. {
  180. base.Module = module;
  181. base.Name = node.GetAttribute("id");
  182. base.Display = node.GetAttribute("display");
  183. base.DeviceID = node.GetAttribute("schematicId");
  184. _diStatus = ParseDiNode("diOnOffFeedback", node, ioModule);
  185. _diOffline = ParseDiNode("diOffline", node, ioModule);
  186. _diIntlk = ParseDiNode("diInterlock", node, ioModule);
  187. _diOverTemp = ParseDiNode("diOverTemp", node, ioModule);
  188. _diIArc = ParseDiNode("diIArc", node, ioModule);
  189. _diVArc = ParseDiNode("diVArc", node, ioModule);
  190. _diBreakState = ParseDiNode("diBreakerState", node, ioModule);
  191. _diHighReflectPower = ParseDiNode("diHighReflectPower", node, ioModule);
  192. _diMatchCommWithGenerator = ParseDiNode("diMatchCommWithGenerator", node, ioModule);
  193. _doPowerOn = ParseDoNode("doOnOff", node, ioModule);
  194. _aiReflectedPower = ParseAiNode("aiReflectPower", node, ioModule);
  195. _aiForwardPower = ParseAiNode("aiForwardPower", node, ioModule);
  196. _aiVoltage = ParseAiNode("aiVoltage", node, ioModule);
  197. _aiCurrent = ParseAiNode("aiCurrent", node, ioModule);
  198. _aoPower = ParseAoNode("aoPower", node, ioModule);
  199. //_aoWorkMode = ParseAoNode("aoWorkMode", node);
  200. _aoPulsingFrency = ParseAoNode("aoFrequency", node, ioModule);
  201. _aoPulsingDutyCycle = ParseAoNode("aoDuty", node, ioModule);
  202. _aoCoefficient = ParseAoNode("aoCoefficient", node, ioModule);
  203. _scPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmRange");
  204. _scPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmTime");
  205. _scReflectPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmRange");
  206. _scReflectPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmTime");
  207. _scPowerRange = SC.GetValue<double>($"{Module}.{Name}.PowerRange");
  208. _scCoefficient = SC.GetValue<double>($"{Module}.{Name}.Coefficient");
  209. _scEnablePulsingFunction = SC.GetValue<bool>($"{Module}.{Name}.EnablePulsingFunction");
  210. _scEnableReflectPower = SC.GetValue<bool>($"{Module}.{Name}.EnableReflectPower");
  211. _scEnableVoltageCurrent = SC.GetValue<bool>($"{Module}.{Name}.EnableVoltageCurrent");
  212. _scRegulationFactor = SC.GetValue<double>($"{Module}.{Name}.PowerRegulationFactor");
  213. Debug.Assert(null != _doPowerOn && null != _aoPower);
  214. }
  215. public override bool Initialize()
  216. {
  217. base.Initialize();
  218. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  219. DEVICE.Register($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", SetPowerOnOff);
  220. DEVICE.Register($"{Module}.{Name}.{AITRfOperation.SetContinuousPower}", SetContinuousPower);
  221. DEVICE.Register($"{Module}.{Name}.{AITRfOperation.SetPower}", SetPower);
  222. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", (out string reason, int time, object[] param) =>
  223. {
  224. SetPowerOnOff(Convert.ToBoolean((string)param[0]), out reason);
  225. return true;
  226. });
  227. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetContinuousPower}", (out string reason, int time, object[] param) =>
  228. {
  229. SetContinuousPower(out reason, 0, param);
  230. return true;
  231. });
  232. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPower}", (out string reason, int time, object[] param) =>
  233. {
  234. SetPower(out reason, 0, param);
  235. return true;
  236. });
  237. OP.Subscribe($"{Module}.Match.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  238. {
  239. reason = "";
  240. return true;
  241. });
  242. OP.Subscribe($"{Module}.Match.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  243. {
  244. reason = "";
  245. return true;
  246. });
  247. OP.Subscribe($"{Module}.Match.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  248. {
  249. reason = "";
  250. return true;
  251. });
  252. OP.Subscribe($"{Module}.Match.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  253. {
  254. reason = "";
  255. return true;
  256. });
  257. return true;
  258. }
  259. public override void SetPower(float val)
  260. {
  261. this.SetPower(out _, 0, new object []{ val });
  262. }
  263. public override bool SetPowerOnOff(bool isOn, out string reason)
  264. {
  265. if (!_diIntlk.Value)
  266. {
  267. reason = "RF interlock is not satisfied,can not be on";
  268. EV.PostAlarmLog($"{Module}.{Name}", reason);
  269. return false;
  270. }
  271. if (!isOn)
  272. {
  273. PowerSetPoint = 0;
  274. }
  275. return _doPowerOn.SetValue(isOn, out reason);
  276. }
  277. private bool? SetPowerOnOff(out string reason, int time, object[] param)
  278. {
  279. bool isOn = Convert.ToBoolean((string)param[0]);
  280. if (!_diIntlk.Value)
  281. {
  282. reason = "RF interlock is not satisfied,can not power on";
  283. EV.PostAlarmLog($"{Module}.{Name}", reason);
  284. return false;
  285. }
  286. bool result = _doPowerOn.SetValue(isOn, out reason);
  287. if (result) reason = string.Format("Set RF power " + (isOn ? "On" : "Off"));
  288. return result;
  289. }
  290. public bool? SetContinuousPower(out string reason, int time, object[] param)
  291. {
  292. float power = (float)Convert.ToDouble((string)param[0]);
  293. power = Math.Max(0, power);
  294. power = Math.Min(ScalePower, power);
  295. byte[] flow = BitConverter.GetBytes((float)(power * RtInstance.ANALOG_TRANS_RANGE / ScalePower));
  296. _aoPower.Buffer[_aoPower.Index] = BitConverter.ToInt16(flow, 0);
  297. _aoPower.Buffer[_aoPower.Index + 1] = BitConverter.ToInt16(flow, 2);
  298. //_aoPower.Value = (short)(power * RtInstance.ANALOG_TRANS_RANGE / ScalePower);
  299. reason = $"RF set Power {power}";
  300. return true;
  301. }
  302. private bool? SetPower(out string reason, int time, object[] param)
  303. {
  304. reason = string.Empty;
  305. float power = (float)Convert.ToDouble(param[0]);
  306. power = Math.Max(0, power);
  307. power = Math.Min(ScalePower, power);
  308. byte[] flow = BitConverter.GetBytes((float)(power * RtInstance.ANALOG_TRANS_RANGE / ScalePower));
  309. _aoPower.Buffer[_aoPower.Index] = BitConverter.ToInt16(flow, 0);
  310. _aoPower.Buffer[_aoPower.Index + 1] = BitConverter.ToInt16(flow, 2);
  311. //_aoPower.Value = (short)(power * RtInstance.ANALOG_TRANS_RANGE / ScalePower);
  312. reason = $"RF set Power:{power}";
  313. return true;
  314. }
  315. public void Stop()
  316. {
  317. string reason = String.Empty;
  318. _aoPower.Value = 0;
  319. }
  320. public override void Terminate()
  321. {
  322. }
  323. public override void Monitor()
  324. {
  325. try
  326. {
  327. if (_aoCoefficient != null)
  328. {
  329. _aoCoefficient.Value = (short)_scCoefficient;
  330. }
  331. if (_checkerPower == null)
  332. _checkerPower = new ToleranceChecker(_scPowerAlarmTime);
  333. if (_checkerReflectPower == null)
  334. _checkerReflectPower = new ToleranceChecker(_scReflectPowerAlarmTime);
  335. _interlockTrig.CLK = _diIntlk.Value;
  336. if (_interlockTrig.Q)
  337. {
  338. //EV.PostMessage(Module, EventEnum.RFInterlockFailed);
  339. }
  340. _rfOnTrigger.CLK = IsPowerOn;
  341. if (_rfOnTrigger.Q)
  342. {
  343. _powerOnStartTime = DateTime.Now;
  344. _checkerPower.Reset(_scPowerAlarmTime);
  345. _checkerReflectPower.Reset(_scReflectPowerAlarmTime);
  346. }
  347. if (_rfOnTrigger.M)
  348. {
  349. _checkerPower.Monitor(RFForwardPower, PowerSetPoint - _scPowerAlarmRange, PowerSetPoint + _scPowerAlarmRange, _scPowerAlarmTime);
  350. if (_checkerPower.Trig)
  351. {
  352. string reason;
  353. EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
  354. $"Forward power {RFForwardPower:0} out of range[{(PowerSetPoint - _scPowerAlarmRange):0},{(PowerSetPoint + _scPowerAlarmRange):0}] in {_scPowerAlarmTime:0} seconds");
  355. SetPowerOnOff(false, out reason);
  356. }
  357. _checkerReflectPower.Monitor(RFReflectPower, double.MinValue, _scReflectPowerAlarmRange, _scReflectPowerAlarmTime);
  358. if (_checkerReflectPower.Trig)
  359. {
  360. EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
  361. $"Reflect power {RFReflectPower:0} out of range[0,{_scReflectPowerAlarmRange:0}] in {_scReflectPowerAlarmTime:0} seconds");
  362. SetPowerOnOff(false, out _);
  363. }
  364. }
  365. _trigOffline.CLK = IsOffline;
  366. if (_trigOffline.Q)
  367. {
  368. EV.PostMessage(Module, EventEnum.DefaultAlarm, "The RF generator is offline");
  369. }
  370. _trigOverTemp.CLK = IsOverTemp;
  371. if (_trigOverTemp.Q)
  372. {
  373. EV.PostAlarmLog(Module, $"{Name} over temperature");
  374. }
  375. if (_diHighReflectPower != null)
  376. {
  377. _trigHighReflectPower.CLK = _diHighReflectPower.Value;
  378. if (_trigOffline.Q)
  379. {
  380. EV.PostMessage(Module, EventEnum.DefaultAlarm, "RF trig high reflect power");
  381. }
  382. }
  383. if (_diMatchCommWithGenerator != null)
  384. {
  385. _trigMatchCommWithGenerator.CLK = !_diMatchCommWithGenerator.Value;
  386. if (_trigMatchCommWithGenerator.Q)
  387. {
  388. EV.PostMessage(Module, EventEnum.DefaultAlarm, "RF trig match not communicate with generator");
  389. }
  390. }
  391. }
  392. catch (Exception ex)
  393. {
  394. LOG.Write(ex);
  395. throw ex;
  396. }
  397. }
  398. public override void Reset()
  399. {
  400. _interlockTrig.RST = true;
  401. _trigOffline.RST = true;
  402. _trigOverTemp.RST = true;
  403. _trigMatchCommWithGenerator.RST = true;
  404. _trigHighReflectPower.RST = true;
  405. }
  406. public void SetRfMode(RfMode mode)
  407. {
  408. throw new NotImplementedException();
  409. }
  410. }
  411. }