IoRf.cs 19 KB

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