RfPowerBase.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  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.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.RT.Tolerance;
  15. using Aitex.Core.Util;
  16. using MECF.Framework.Common.CommonData;
  17. namespace MECF.Framework.Common.Device.Bases
  18. {
  19. public abstract class RfPowerBase : BaseDevice, IDevice
  20. {
  21. public virtual bool IsConnected => true;
  22. public virtual bool IsPowerOn { get; set; }
  23. public virtual bool IsError { get; set; }
  24. public virtual bool IsHighFrequentQueryMode { get; set; }
  25. public virtual EnumRfPowerClockMode ClockMode { get; set; }
  26. public virtual EnumRfPowerWorkMode WorkMode { get; set; }
  27. public virtual EnumRfPowerRegulationMode RegulationMode { get; set; }
  28. public virtual EnumRfPowerControlMode ControlMode { get; set; }
  29. public virtual EnumRfPowerPulsingMode PulsingMode { get; set; }
  30. public virtual float ForwardPower { get; set; }
  31. public virtual float ReflectPower { get; set; }
  32. public virtual float LoadPower { get; set; }
  33. public virtual float PowerSetPoint { get; set; }
  34. public virtual float Frequency { get; set; }
  35. public virtual float PulsingFrequency { get; set; }
  36. public virtual float PulsingDutyCycle { get; set; }
  37. public virtual bool IsInterlockOk { get; set; }
  38. public virtual AITRfPowerData DeviceData { get; set; }
  39. private float _currentWarningRange;
  40. private float _currentAlarmRange;
  41. protected SCConfigItem _scEnableAlarm;
  42. protected SCConfigItem _scAlarmTime;
  43. protected SCConfigItem _scAlarmRange;
  44. protected SCConfigItem _scWarningTime;
  45. protected SCConfigItem _scWarningRange;
  46. protected SCConfigItem _scReflectedPowerMonitorTime;
  47. protected SCConfigItem _scRecipeIgnoreTime;
  48. protected SCConfigItem _scPowerScale;
  49. protected ToleranceChecker _toleranceAlarmChecker = new ToleranceChecker();
  50. protected ToleranceChecker _toleranceWarningChecker = new ToleranceChecker();
  51. protected ToleranceChecker _prThresholdChecker = new ToleranceChecker();
  52. protected double _currentFineTuningValue;
  53. protected SCConfigItem _scFineTuningEnable;
  54. protected SCConfigItem _scFineTuningValue;
  55. protected float _PrThreshold;
  56. protected DeviceTimer _recipeIgnoreTimer = new DeviceTimer();
  57. protected DeviceTimer _rampTimer = new DeviceTimer();
  58. protected float _rampTarget;
  59. protected float _rampInitValue;
  60. protected int _rampTime;//unit ms
  61. //calibration
  62. protected SCConfigItem _scEnableCalibration;
  63. protected SCConfigItem _scCalibrationTable;
  64. private List<CalibrationItem> _calibrationTable = new List<CalibrationItem>();
  65. private string _previousSetting;
  66. public virtual bool EnableAlarm
  67. {
  68. get
  69. {
  70. if (_scEnableAlarm != null)
  71. return _scEnableAlarm.BoolValue;
  72. return false;
  73. }
  74. }
  75. public virtual double AlarmTime
  76. {
  77. get
  78. {
  79. if (_scAlarmTime != null)
  80. return _scAlarmTime.DoubleValue;
  81. return 0;
  82. }
  83. }
  84. public virtual double AlarmRange
  85. {
  86. get
  87. {
  88. if (_currentAlarmRange > 0)
  89. return _currentAlarmRange;
  90. if (_scAlarmRange != null)
  91. return _scAlarmRange.DoubleValue;
  92. return 0;
  93. }
  94. }
  95. public virtual double WarningTime
  96. {
  97. get
  98. {
  99. if (_scWarningTime != null)
  100. return _scWarningTime.DoubleValue;
  101. return 0;
  102. }
  103. }
  104. public virtual double WarningRange
  105. {
  106. get
  107. {
  108. if (_currentWarningRange > 0)
  109. return _currentWarningRange;
  110. if (_scWarningRange != null)
  111. return _scWarningRange.DoubleValue;
  112. return 0;
  113. }
  114. }
  115. //unit second
  116. public virtual double PrThresholdMonitorTime
  117. {
  118. get
  119. {
  120. if (_scReflectedPowerMonitorTime != null)
  121. return _scReflectedPowerMonitorTime.DoubleValue;
  122. return 0;
  123. }
  124. }
  125. //unit second
  126. public virtual double RecipeIgnoreTime
  127. {
  128. get
  129. {
  130. if (_scRecipeIgnoreTime != null)
  131. return _scRecipeIgnoreTime.DoubleValue;
  132. return 0;
  133. }
  134. }
  135. public virtual double FineTuningValue
  136. {
  137. get
  138. {
  139. if (_scFineTuningEnable == null || !_scFineTuningEnable.BoolValue)
  140. return 1;
  141. if (_currentFineTuningValue != 0)
  142. return 1 + _currentFineTuningValue / 100;
  143. return _scFineTuningValue != null ? 1 + _scFineTuningValue.DoubleValue / 100 : 1;
  144. }
  145. }
  146. public virtual int PowerRange
  147. {
  148. get
  149. {
  150. if (_scPowerScale == null)
  151. return 1000;
  152. return _scPowerScale.IntValue;
  153. }
  154. }
  155. protected RfPowerBase(string module, string name) : base(module, name, name, name)
  156. {
  157. _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.EnableAlarm");
  158. _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.AlarmTime");
  159. _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.AlarmRange");
  160. _scWarningTime = SC.GetConfigItem($"{Module}.{Name}.WarningTime");
  161. _scWarningRange = SC.GetConfigItem($"{Module}.{Name}.WarningRange");
  162. _scFineTuningValue = SC.GetConfigItem($"{Module}.FineTuning.{Name}");
  163. _scFineTuningEnable = SC.GetConfigItem($"{Module}.FineTuning.IsEnable");
  164. }
  165. protected RfPowerBase() : base()
  166. {
  167. }
  168. protected RfPowerBase(string module, string name, XmlElement node = null, string ioModule = "") : base(module, name, name, name)
  169. {
  170. if (node != null)
  171. {
  172. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{Module}.{Name}.EnableAlarm");
  173. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule, $"{Module}.{Name}.AlarmTime");
  174. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule, $"{Module}.{Name}.AlarmRange");
  175. _scWarningTime = ParseScNode("scWarningTime", node, ioModule, $"{Module}.{Name}.WarningTime");
  176. _scWarningRange = ParseScNode("scWarningRange", node, ioModule, $"{Module}.{Name}.WarningRange");
  177. _scRecipeIgnoreTime = ParseScNode("scRecipeIgnoreTime", node, ioModule, $"{Module}.{Name}.RecipeIgnoreTime");
  178. _scReflectedPowerMonitorTime = ParseScNode("scReflectedPowerMonitorTime", node, ioModule, $"{Module}.{Name}.ReflectedPowerMonitorTime");
  179. _scEnableCalibration = ParseScNode("scCalibrationTable", node, ioModule, $"{Module}.{Name}.EnableCalibration");
  180. _scCalibrationTable = ParseScNode("scCalibrationTable", node, ioModule, $"{Module}.{Name}.CalibrationTable");
  181. _scFineTuningValue = ParseScNode("scFineTuningValue", node, ioModule, $"{Module}.FineTuning.{Name}");
  182. _scFineTuningEnable = ParseScNode("scFineTuningEnable", node, ioModule, $"{Module}.FineTuning.IsEnable");
  183. }
  184. }
  185. public virtual bool Initialize()
  186. {
  187. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  188. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode.ToString());
  189. DATA.Subscribe($"{Module}.{Name}.RegulationMode", () => RegulationMode.ToString());
  190. DATA.Subscribe($"{Module}.{Name}.ControlMode", () => ControlMode.ToString());
  191. DATA.Subscribe($"{Module}.{Name}.ForwardPower", () => ForwardPower);
  192. DATA.Subscribe($"{Module}.{Name}.ReflectPower", () => ReflectPower);
  193. DATA.Subscribe($"{Module}.{Name}.LoadPower", () => LoadPower);
  194. DATA.Subscribe($"{Module}.{Name}.PowerSetPoint", () => PowerSetPoint);
  195. DATA.Subscribe($"{Module}.{Name}.Frequency", () => Frequency);
  196. DATA.Subscribe($"{Module}.{Name}.PulsingFrequency", () => PulsingFrequency);
  197. DATA.Subscribe($"{Module}.{Name}.PulsingDutyCycle", () => PulsingDutyCycle);
  198. DATA.Subscribe($"{Module}.{Name}.IsPowerOn", () => IsPowerOn);
  199. OP.Subscribe($"{Module}.{Name}.SetPowerOn", (function, args) =>
  200. {
  201. if (!SetPowerOnOff(true, out string reason))
  202. {
  203. EV.PostWarningLog(Module, $"{Module} {Name} RF on failed, for {reason}");
  204. return false;
  205. }
  206. return true;
  207. });
  208. OP.Subscribe($"{Module}.{Name}.SetPowerOff", (function, args) =>
  209. {
  210. if (!SetPowerOnOff(false, out string reason))
  211. {
  212. EV.PostWarningLog(Module, $"{Module} {Name} RF off failed, for {reason}");
  213. return false;
  214. }
  215. return true;
  216. });
  217. OP.Subscribe($"{Module}.{Name}.SetPower", (function, args) =>
  218. {
  219. SetPower((float)args[0]);
  220. return true;
  221. });
  222. OP.Subscribe($"{Module}.{Name}.SetRegulationMode", (function, args) =>
  223. {
  224. if (!Enum.TryParse((string)args[0], out EnumRfPowerRegulationMode mode))
  225. {
  226. EV.PostWarningLog(Module, $"Argument {args[0]}not valid");
  227. return false;
  228. }
  229. SetRegulationMode(mode);
  230. return true;
  231. });
  232. //for recipe
  233. OP.Subscribe($"{Module}.SetRFParameters", (out string reason, int time, object[] param) =>
  234. {
  235. reason = string.Empty;
  236. SetRFParameters(param);
  237. return true;
  238. });
  239. //for recipe
  240. OP.Subscribe($"{Module}.{Name}.SetTolerance", (out string reason, int time, object[] param) =>
  241. {
  242. reason = string.Empty;
  243. var warning = Convert.ToSingle(param[0]);
  244. var alarm = Convert.ToSingle(param[1]);
  245. SetTolerance((float)warning, (float)alarm);
  246. return true;
  247. });
  248. //for recipe
  249. OP.Subscribe($"{Module}.{Name}.SetFineTuning", (out string reason, int time, object[] param) =>
  250. {
  251. reason = string.Empty;
  252. SetFineTuning(Convert.ToSingle(param[0]));
  253. return true;
  254. });
  255. InitSc();
  256. UpdateCalibrationTable();
  257. return true;
  258. }
  259. public virtual void InitSc()
  260. {
  261. }
  262. public virtual void SetFineTuning(float fineTuning)
  263. {
  264. _currentFineTuningValue = fineTuning;
  265. }
  266. public virtual void SetTolerance(float warning, float alarm)
  267. {
  268. _currentWarningRange = warning;
  269. _currentAlarmRange = alarm;
  270. _toleranceAlarmChecker.Reset(AlarmTime);
  271. _toleranceWarningChecker.Reset(WarningTime);
  272. if (RecipeIgnoreTime > 0)
  273. _recipeIgnoreTimer.Start(0);
  274. }
  275. public virtual void CheckTolerance()
  276. {
  277. if (!EnableAlarm || PowerSetPoint == 0 || (RecipeIgnoreTime > 0 && _recipeIgnoreTimer.GetElapseTime() < RecipeIgnoreTime * 1000))
  278. return;
  279. _toleranceAlarmChecker.Monitor(ForwardPower, (PowerSetPoint * (1 - AlarmRange / 100)), (PowerSetPoint * (1 + AlarmRange / 100)), AlarmTime);
  280. _toleranceWarningChecker.Monitor(ForwardPower, (PowerSetPoint * (1 - WarningRange / 100)), (PowerSetPoint * (1 + WarningRange / 100)), WarningTime);
  281. if (_PrThreshold > 0)
  282. _prThresholdChecker.Monitor(ReflectPower, 0, _PrThreshold, PrThresholdMonitorTime);
  283. }
  284. public virtual bool CheckToleranceAlarm()
  285. {
  286. if (!EnableAlarm)
  287. return false;
  288. return _toleranceAlarmChecker.Result;
  289. }
  290. public virtual bool CheckToleranceWarning()
  291. {
  292. if (!EnableAlarm)
  293. return false;
  294. return _toleranceWarningChecker.Result;
  295. }
  296. public virtual bool CheckPrThreshold()
  297. {
  298. if (!EnableAlarm)
  299. return false;
  300. return _prThresholdChecker.Result;
  301. }
  302. public virtual void SetRegulationMode(EnumRfPowerRegulationMode enumRfPowerControlMode)
  303. {
  304. }
  305. public virtual void SetWorkMode(EnumRfPowerWorkMode enumRfPowerWorkMode)
  306. {
  307. }
  308. public virtual void SetClockMode(EnumRfPowerClockMode clockMode)
  309. {
  310. }
  311. public virtual void SetControlMode(EnumRfPowerControlMode enumRfPowerControlMode)
  312. {
  313. }
  314. public virtual void SetPulsingMode(EnumRfPowerPulsingMode enumRfPowerPulsingMode)
  315. {
  316. }
  317. public virtual void SetRemoteMode(bool isRemote)
  318. {
  319. }
  320. public virtual bool SetPowerOnOff(bool isOn, out string reason)
  321. {
  322. reason = string.Empty;
  323. return true;
  324. }
  325. public virtual void SetPower(float power)
  326. {
  327. }
  328. public virtual void SetPower(float power, float rampTime, bool isCalibration = false)
  329. {
  330. }
  331. public virtual void SetFreq(float freq)
  332. {
  333. }
  334. public virtual void SetPulsingFreq(float pulsingFreq)
  335. {
  336. }
  337. public virtual void SetPulsingDutyCycle(int dutyCycle)
  338. {
  339. }
  340. public virtual void SetRFParameters(object[] param)
  341. {
  342. }
  343. public virtual void SetPrThreshold(float threshold)
  344. {
  345. _PrThreshold = threshold;
  346. _prThresholdChecker.Reset(PrThresholdMonitorTime);
  347. }
  348. public virtual void SetRampTime(float rampTime)
  349. {
  350. }
  351. public virtual void Terminate()
  352. {
  353. }
  354. public virtual void Monitor()
  355. {
  356. CheckTolerance();
  357. if (_scCalibrationTable != null)
  358. {
  359. if (string.IsNullOrEmpty(_previousSetting) || _previousSetting != _scCalibrationTable.StringValue)
  360. UpdateCalibrationTable();
  361. }
  362. }
  363. public virtual void Reset()
  364. {
  365. }
  366. protected virtual void UpdateCalibrationTable()
  367. {
  368. if (_scCalibrationTable == null)
  369. return;
  370. if (_previousSetting == _scCalibrationTable.StringValue)
  371. return;
  372. _previousSetting = _scCalibrationTable.StringValue;
  373. if (string.IsNullOrEmpty(_previousSetting))
  374. {
  375. _calibrationTable = new List<CalibrationItem>();
  376. return;
  377. }
  378. var table = new List<Tuple<float, float>>();
  379. string[] items = _previousSetting.Split(';');
  380. for (int i = 0; i < items.Length; i++)
  381. {
  382. string itemValue = items[i];
  383. if (!string.IsNullOrEmpty(itemValue))
  384. {
  385. string[] pairValue = itemValue.Split('#');
  386. if (pairValue.Length == 2)
  387. {
  388. if (float.TryParse(pairValue[0], out float rawData)
  389. && float.TryParse(pairValue[1], out float calibrationData))
  390. {
  391. table.Add(Tuple.Create(rawData, calibrationData));
  392. }
  393. }
  394. }
  395. }
  396. table = table.OrderBy(x => x.Item1).ToList();
  397. var calibrationTable = new List<CalibrationItem>();
  398. for (int i = 0; i < table.Count; i++)
  399. {
  400. if (i == 0 && table[0].Item1 > 0.001)
  401. {
  402. calibrationTable.Add(new CalibrationItem()
  403. {
  404. RawFrom = 0,
  405. CalibrationFrom = 0,
  406. RawTo = table[0].Item1,
  407. CalibrationTo = table[0].Item2,
  408. });
  409. }
  410. if (i == table.Count - 1)
  411. {
  412. float maxValue = (float)PowerRange;
  413. calibrationTable.Add(new CalibrationItem()
  414. {
  415. RawFrom = table[i].Item1,
  416. RawTo = table[i].Item2,
  417. CalibrationFrom = maxValue,
  418. CalibrationTo = maxValue,
  419. });
  420. continue;
  421. }
  422. calibrationTable.Add(new CalibrationItem()
  423. {
  424. RawFrom = table[i].Item1,
  425. CalibrationFrom = table[i].Item2,
  426. RawTo = table[i + 1].Item1,
  427. CalibrationTo = table[i + 1].Item2,
  428. });
  429. }
  430. _calibrationTable = calibrationTable;
  431. }
  432. protected virtual float CalibrationData(float value, bool output)
  433. {
  434. //default enable
  435. if (_scEnableCalibration != null && !_scEnableCalibration.BoolValue)
  436. return value;
  437. if (_scCalibrationTable == null || !_calibrationTable.Any())
  438. return value;
  439. float ret = value;
  440. if (output)
  441. {
  442. var item = _calibrationTable.FirstOrDefault(x => x.RawFrom <= value && x.RawTo >= value);
  443. if (item != null && Math.Abs(item.RawTo - item.RawFrom) > 0.01)
  444. {
  445. var slope = (item.CalibrationTo - item.CalibrationFrom) / (item.RawTo - item.RawFrom);
  446. ret = (ret - item.RawFrom) * slope + item.CalibrationFrom;
  447. }
  448. }
  449. else
  450. {
  451. var item = _calibrationTable.FirstOrDefault(x => x.CalibrationFrom <= value && x.CalibrationTo >= value);
  452. if (item != null && Math.Abs(item.CalibrationTo - item.CalibrationFrom) > 0.01)
  453. {
  454. var slope = (item.RawTo - item.RawFrom) / (item.CalibrationTo - item.CalibrationFrom);
  455. ret = (ret - item.CalibrationFrom) * slope + item.RawFrom;
  456. }
  457. }
  458. if (ret < 0)
  459. return 0;
  460. if (ret >= float.MaxValue || ret > PowerRange)
  461. ret = value;
  462. return ret;
  463. }
  464. }
  465. }