IoRf.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. private string RFHighReflect = "RFHighReflect";
  179. private string RFOutOfTolerance = "RFOutOfTolerance";
  180. public IoRf(string module, XmlElement node, string ioModule = "")
  181. {
  182. base.Module = module;
  183. base.Name = node.GetAttribute("id");
  184. base.Display = node.GetAttribute("display");
  185. base.DeviceID = node.GetAttribute("schematicId");
  186. _diStatus = ParseDiNode("diOnOffFeedback", node, ioModule);
  187. _diOffline = ParseDiNode("diOffline", node, ioModule);
  188. _diIntlk = ParseDiNode("diInterlock", node, ioModule);
  189. _diOverTemp = ParseDiNode("diOverTemp", node, ioModule);
  190. _diIArc = ParseDiNode("diIArc", node, ioModule);
  191. _diVArc = ParseDiNode("diVArc", node, ioModule);
  192. _diBreakState = ParseDiNode("diBreakerState", node, ioModule);
  193. _diHighReflectPower = ParseDiNode("diHighReflectPower", node, ioModule);
  194. _diMatchCommWithGenerator = ParseDiNode("diMatchCommWithGenerator", node, ioModule);
  195. _doPowerOn = ParseDoNode("doOnOff", node, ioModule);
  196. _aiReflectedPower = ParseAiNode("aiReflectPower", node, ioModule);
  197. _aiForwardPower = ParseAiNode("aiForwardPower", node, ioModule);
  198. _aiVoltage = ParseAiNode("aiVoltage", node, ioModule);
  199. _aiCurrent = ParseAiNode("aiCurrent", node, ioModule);
  200. _aoPower = ParseAoNode("aoPower", node, ioModule);
  201. //_aoWorkMode = ParseAoNode("aoWorkMode", node);
  202. _aoPulsingFrency = ParseAoNode("aoFrequency", node, ioModule);
  203. _aoPulsingDutyCycle = ParseAoNode("aoDuty", node, ioModule);
  204. _aoCoefficient = ParseAoNode("aoCoefficient", node, ioModule);
  205. _scPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmRange");
  206. _scPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.PowerAlarmTime");
  207. _scReflectPowerAlarmRange = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmRange");
  208. _scReflectPowerAlarmTime = SC.GetValue<double>($"{Module}.{Name}.ReflectPowerAlarmTime");
  209. _scPowerRange = SC.GetValue<double>($"{Module}.{Name}.PowerRange");
  210. _scCoefficient = SC.GetValue<double>($"{Module}.{Name}.Coefficient");
  211. _scEnablePulsingFunction = SC.GetValue<bool>($"{Module}.{Name}.EnablePulsingFunction");
  212. _scEnableReflectPower = SC.GetValue<bool>($"{Module}.{Name}.EnableReflectPower");
  213. _scEnableVoltageCurrent = SC.GetValue<bool>($"{Module}.{Name}.EnableVoltageCurrent");
  214. _scRegulationFactor = SC.GetValue<double>($"{Module}.{Name}.PowerRegulationFactor");
  215. Debug.Assert(null != _doPowerOn && null != _aoPower);
  216. }
  217. public override bool Initialize()
  218. {
  219. base.Initialize();
  220. EV.Subscribe(new EventItem("Event", RFHighReflect, "RF High Reflect", EventLevel.Alarm, EventType.HostNotification));
  221. EV.Subscribe(new EventItem("Event", RFOutOfTolerance, "RF Out Of Tolerance", EventLevel.Alarm, EventType.HostNotification));
  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 * RtInstance.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 * RtInstance.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. EV.Notify(RFOutOfTolerance);
  360. SetPowerOnOff(false, out reason);
  361. }
  362. _checkerReflectPower.Monitor(RFReflectPower, double.MinValue, _scReflectPowerAlarmRange, _scReflectPowerAlarmTime);
  363. if (_checkerReflectPower.Trig)
  364. {
  365. EV.PostMessage(Module, EventEnum.ToleranceAlarm, Module, Display,
  366. $"Reflect power {RFReflectPower:0} out of range[0,{_scReflectPowerAlarmRange:0}] in {_scReflectPowerAlarmTime:0} seconds");
  367. EV.Notify(RFHighReflect);
  368. SetPowerOnOff(false, out _);
  369. }
  370. }
  371. _trigOffline.CLK = IsOffline;
  372. if (_trigOffline.Q)
  373. {
  374. EV.PostMessage(Module, EventEnum.DefaultAlarm, "The RF generator is offline");
  375. }
  376. _trigOverTemp.CLK = IsOverTemp;
  377. if (_trigOverTemp.Q)
  378. {
  379. EV.PostAlarmLog(Module, $"{Name} over temperature");
  380. }
  381. if (_diHighReflectPower != null)
  382. {
  383. _trigHighReflectPower.CLK = _diHighReflectPower.Value;
  384. if (_trigOffline.Q)
  385. {
  386. EV.Notify(RFHighReflect);
  387. EV.PostMessage(Module, EventEnum.DefaultAlarm, "RF trig high reflect power");
  388. }
  389. }
  390. if (_diMatchCommWithGenerator != null)
  391. {
  392. _trigMatchCommWithGenerator.CLK = !_diMatchCommWithGenerator.Value;
  393. if (_trigMatchCommWithGenerator.Q)
  394. {
  395. EV.PostMessage(Module, EventEnum.DefaultAlarm, "RF trig match not communicate with generator");
  396. }
  397. }
  398. }
  399. catch (Exception ex)
  400. {
  401. LOG.Write(ex);
  402. }
  403. }
  404. public override void Reset()
  405. {
  406. _interlockTrig.RST = true;
  407. _trigOffline.RST = true;
  408. _trigOverTemp.RST = true;
  409. _trigMatchCommWithGenerator.RST = true;
  410. _trigHighReflectPower.RST = true;
  411. }
  412. public void SetRfMode(RfMode mode)
  413. {
  414. throw new NotImplementedException();
  415. }
  416. }
  417. }