IoMicrowave.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.IOCore;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.RT.OperationCenter;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.RT.Tolerance;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.Event;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Xml;
  18. namespace Aitex.Core.RT.Device.Unit
  19. {
  20. public class IoMicrowave : BaseDevice, IDevice
  21. {
  22. public float PowerRange
  23. {
  24. get
  25. {
  26. return _scPowerRange == null ? 99999 : (float)_scPowerRange.IntValue;
  27. }
  28. }
  29. public float ScaleFrequency
  30. {
  31. get
  32. {
  33. return 1000;
  34. }
  35. }
  36. public string UnitPower
  37. {
  38. get
  39. {
  40. return "W";
  41. }
  42. }
  43. public bool IsRfOn //True:on
  44. {
  45. get
  46. {
  47. if (_diStatus != null)
  48. return _diStatus.Value;
  49. return _doPowerOn.Value;
  50. }
  51. }
  52. public float PowerSetPoint
  53. {
  54. get
  55. {
  56. float tuned = _isFloatAioType ? GetTuneValue(_aoPower.FloatValue, false) : GetTuneValue(_aoPower.Value, false);
  57. float calibrated = CalibrationPower(tuned, false);
  58. if (calibrated < 0)
  59. calibrated = 0;
  60. return calibrated;
  61. }
  62. set
  63. {
  64. float tuned = GetTuneValue(value, true);
  65. float calibrated = CalibrationPower(tuned, true);
  66. if (calibrated < 0)
  67. calibrated = 0;
  68. if (_isFloatAioType)
  69. _aoPower.FloatValue = calibrated;
  70. else
  71. _aoPower.Value = (short)calibrated;
  72. }
  73. }
  74. public float ImpulseSetPoint
  75. {
  76. get
  77. {
  78. if (_aoImpulseTime == null)
  79. return 0;
  80. return _isFloatAioType ? _aoImpulseTime.FloatValue : _aoImpulseTime.Value;
  81. }
  82. set
  83. {
  84. if (_aoImpulseTime == null)
  85. return;
  86. if (_isFloatAioType)
  87. _aoImpulseTime.FloatValue = value;
  88. else
  89. _aoImpulseTime.Value = (short)value;
  90. }
  91. }
  92. public float PauseTimeSetPoint
  93. {
  94. get
  95. {
  96. if (_aoPauseTime == null)
  97. return 0;
  98. return _isFloatAioType ? _aoPauseTime.FloatValue : _aoPauseTime.Value;
  99. }
  100. set
  101. {
  102. if (_aoPauseTime == null)
  103. return;
  104. if (_isFloatAioType)
  105. _aoPauseTime.FloatValue = value;
  106. else
  107. _aoPauseTime.Value = (short)value;
  108. }
  109. }
  110. public float RFForwardPower
  111. {
  112. get
  113. {
  114. if (_aiForwardPower == null)
  115. return 0;
  116. float tuned = _isFloatAioType
  117. ? GetTuneValue(_aiForwardPower.FloatValue, false)
  118. : GetTuneValue(_aiForwardPower.Value, false);
  119. float calibrated = CalibrationPower(tuned, false);
  120. if (calibrated < 0)
  121. calibrated = 0;
  122. return calibrated;
  123. }
  124. }
  125. public float RFReflectPower
  126. {
  127. get
  128. {
  129. return _aiReflectedPower == null ? 0 : (_isFloatAioType ? GetTuneValue(_aiReflectedPower.FloatValue, false) : GetTuneValue(_aiReflectedPower.Value, false));
  130. }
  131. }
  132. public bool RFInterlock
  133. {
  134. get
  135. {
  136. return _diIntlk == null || _diIntlk.Value;
  137. }
  138. }
  139. public bool MainPowerOnSetPoint
  140. {
  141. get
  142. {
  143. if (_doPowerOn != null)
  144. return _doPowerOn.Value;
  145. return false;
  146. }
  147. set
  148. {
  149. if (_doPowerOn != null)
  150. {
  151. if (!_doPowerOn.SetValue(value, out string reason))
  152. {
  153. LOG.Write(reason);
  154. }
  155. }
  156. }
  157. }
  158. public bool HeatOnSetPoint
  159. {
  160. get
  161. {
  162. if (_doHeaterOn != null)
  163. return _doHeaterOn.Value;
  164. return false;
  165. }
  166. set
  167. {
  168. if (_doHeaterOn != null)
  169. {
  170. if (!_doHeaterOn.SetValue(value, out string reason))
  171. {
  172. LOG.Write(reason);
  173. }
  174. }
  175. }
  176. }
  177. public bool HeatOnComplete
  178. {
  179. get
  180. {
  181. if (_diHeatComplete != null)
  182. return _diHeatComplete.Value;
  183. return false;
  184. }
  185. }
  186. private DateTime _powerOnStartTime;
  187. private TimeSpan _powerOnElapsedTime;
  188. public string PowerOnTime
  189. {
  190. get
  191. {
  192. if (IsRfOn)
  193. _powerOnElapsedTime = DateTime.Now - _powerOnStartTime;
  194. return string.Format("{0}:{1}:{2}", ((int)_powerOnElapsedTime.TotalHours).ToString("00"),
  195. _powerOnElapsedTime.Minutes.ToString("00"), (_powerOnElapsedTime.Seconds > 0 ? (_powerOnElapsedTime.Seconds + 1) : 0).ToString("00"));
  196. }
  197. }
  198. public int ErrorCode
  199. {
  200. get
  201. {
  202. return _isFloatAioType ? (int)_aiErrorCode.FloatValue : _aiErrorCode.Value;
  203. }
  204. }
  205. private Dictionary<int, string> _errorCodeMap = new Dictionary<int, string>()
  206. {
  207. {0, "No Error"},
  208. {128, "Communication error"},
  209. {129, "Software interlock error"},
  210. {130, "Hardware interlock error"},
  211. {131, "Cooling error power supply and magnetron head"},
  212. {134, "Anode current error"},
  213. {135, "Anode voltage error"},
  214. {137, "Low-power error"},
  215. {138, "ARC fault"},
  216. {255, "Service error or CANbus error"},
  217. };
  218. public string ErrorCodeString
  219. {
  220. get
  221. {
  222. if (_errorCodeMap.ContainsKey(ErrorCode))
  223. return _errorCodeMap[ErrorCode];
  224. return "";
  225. }
  226. }
  227. public bool IsAlarm
  228. {
  229. get
  230. {
  231. return _diAlarm.Value || _diGroundFault.Value || _diOverVoltageCurrent.Value || _diCoolingError.Value ||
  232. _diInterlockFault.Value;
  233. }
  234. }
  235. public bool ResetErrorSetPoint
  236. {
  237. get
  238. {
  239. return _doResetError != null && _doResetError.Value;
  240. }
  241. set
  242. {
  243. if (_doResetError != null)
  244. _doResetError.Value = value;
  245. }
  246. }
  247. public string AlarmText
  248. {
  249. get
  250. {
  251. if (!IsAlarm)
  252. return "";
  253. string result = "";
  254. if (_diGroundFault.Value) result += "Ground Fault;";
  255. if (_diOverVoltageCurrent.Value) result += "Over Voltage Current;";
  256. if (_diCoolingError.Value) result += "Cooling Error;";
  257. if (_diInterlockFault.Value) result += "Interlock Fault;";
  258. if (ErrorCode > 0)
  259. result += ErrorCodeString;
  260. return result;
  261. }
  262. }
  263. private AITRfData DeviceData
  264. {
  265. get
  266. {
  267. AITRfData data = new AITRfData()
  268. {
  269. Module = Module,
  270. DeviceName = Name,
  271. DeviceSchematicId = DeviceID,
  272. DisplayName = Display,
  273. ForwardPower = RFForwardPower,
  274. ReflectPower = RFReflectPower,
  275. IsInterlockOk = RFInterlock,
  276. IsRfOn = IsRfOn,
  277. IsRfAlarm = IsAlarm,
  278. PowerSetPoint = PowerSetPoint,
  279. ScalePower = PowerRange,
  280. UnitPower = UnitPower,
  281. PowerOnElapsedTime = PowerOnTime,
  282. EnablePulsing = false,
  283. EnableVoltageCurrent = false,
  284. IsToleranceError = !AlarmPowerToleranceAlarm.IsAcknowledged || !AlarmReflectPowerToleranceAlarm.IsAcknowledged,
  285. IsToleranceWarning = !AlarmPowerToleranceWarning.IsAcknowledged || !AlarmReflectPowerToleranceWarning.IsAcknowledged,
  286. WorkMode = (int)RfMode.ContinuousWaveMode,
  287. };
  288. data.AttrValue["HeatOnSetPoint"] = HeatOnSetPoint;
  289. data.AttrValue["HeatOnComplete"] = HeatOnComplete;
  290. return data;
  291. }
  292. }
  293. private DIAccessor _diStatus = null;
  294. private DIAccessor _diIntlk = null;
  295. private DIAccessor _diAlarm;
  296. private DIAccessor _diHeatComplete;
  297. private DIAccessor _diControlVoltageOn;
  298. private DIAccessor _diGroundFault;
  299. private DIAccessor _diOverVoltageCurrent;
  300. private DIAccessor _diCoolingError;
  301. private DIAccessor _diInterlockFault;
  302. private DOAccessor _doPowerOn = null;
  303. private DOAccessor _doOn = null;
  304. private DOAccessor _doHeaterOn = null;
  305. private DOAccessor _doResetError = null;
  306. private AIAccessor _aiReflectedPower = null;
  307. private AIAccessor _aiForwardPower = null;
  308. private AIAccessor _aiErrorCode = null;
  309. private AOAccessor _aoPower = null;
  310. private AOAccessor _aoImpulseTime = null;
  311. private AOAccessor _aoPauseTime = null;
  312. //SC
  313. private SCConfigItem _scEnablePowerAlarm;
  314. private SCConfigItem _scPowerAlarmRange = null;
  315. private SCConfigItem _scPowerAlarmTime = null;
  316. private SCConfigItem _scPowerWarningRange = null;
  317. private SCConfigItem _scPowerWarningTime = null;
  318. private SCConfigItem _scEnableReflectPowerAlarm;
  319. private SCConfigItem _scReflectPowerAlarmRange = null;
  320. private SCConfigItem _scReflectPowerAlarmTime = null;
  321. private SCConfigItem _scReflectPowerWarningRange = null;
  322. private SCConfigItem _scReflectPowerWarningTime = null;
  323. private SCConfigItem _scPowerRange;
  324. private SCConfigItem _scCoefficient;
  325. private SCConfigItem _scPowerOnTimeout;
  326. private SCConfigItem _scImpulseTime;
  327. private SCConfigItem _scPauseTime;
  328. private SCConfigItem _scPowerTuneEnable;
  329. private SCConfigItem _scPowerTuneIsPercent;
  330. private SCConfigItem _scPowerTuneTable;
  331. private SCConfigItem[] _scPowerCalibrationTable;
  332. private SCConfigItem _scPowerCalibrationEnable;
  333. private F_TRIG _trigInterlock = new F_TRIG();
  334. private R_TRIG _trigOn = new R_TRIG();
  335. private R_TRIG _trigError = new R_TRIG();
  336. readonly DeviceTimer _powerOnTimer = new DeviceTimer();
  337. private const string RFHighReflect = "RFHighReflect";
  338. private const string RFHardwareInterlock = "RFHardwareInterlock";
  339. private const string RFOutOfTolerance = "RFOutOfTolerance";
  340. public AlarmEventItem AlarmPowerOnFailed { get; set; }
  341. public AlarmEventItem AlarmPowerToleranceWarning { get; set; }
  342. public AlarmEventItem AlarmPowerToleranceAlarm { get; set; }
  343. public AlarmEventItem AlarmReflectPowerToleranceWarning { get; set; }
  344. public AlarmEventItem AlarmReflectPowerToleranceAlarm { get; set; }
  345. public AlarmEventItem AlarmDeviceAlarm { get; set; }
  346. private ToleranceChecker _checkerPowerWarning;
  347. private ToleranceChecker _checkerPowerAlarm;
  348. private ToleranceChecker _checkerReflectPowerWarning;
  349. private ToleranceChecker _checkerReflectPowerAlarm;
  350. private bool _isFloatAioType = false;
  351. private Stopwatch _timerResetError = new Stopwatch();
  352. private SCConfigItem _scResetErrorTimeout;
  353. private List<RfTuneItem> _tuneTable;
  354. //calibration
  355. private List<CalbrationParas> _calibrationFormla;
  356. private List<RFCalibrationItem> _rfCalibrationDatas = new List<RFCalibrationItem>();
  357. private const int TenPoints = 10;
  358. public IoMicrowave(string module, XmlElement node, string ioModule = "")
  359. {
  360. var attrModule = node.GetAttribute("module");
  361. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  362. base.Name = node.GetAttribute("id");
  363. base.Display = node.GetAttribute("display");
  364. base.DeviceID = node.GetAttribute("schematicId");
  365. string scBasePath = node.GetAttribute("scBasePath");
  366. if (string.IsNullOrEmpty(scBasePath))
  367. scBasePath = $"{Module}.{Name}";
  368. else
  369. {
  370. scBasePath = scBasePath.Replace("{module}", Module);
  371. }
  372. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");
  373. _diStatus = ParseDiNode("diOn", node, ioModule);
  374. _diIntlk = ParseDiNode("diInterlock", node, ioModule);
  375. _diAlarm = ParseDiNode("diAlarm", node, ioModule);
  376. _diHeatComplete = ParseDiNode("diHeatComplete", node, ioModule);
  377. _diControlVoltageOn = ParseDiNode("diControlVoltageOn", node, ioModule);
  378. _diGroundFault = ParseDiNode("diGroundFault", node, ioModule);
  379. _diOverVoltageCurrent = ParseDiNode("diOverVoltageCurrent", node, ioModule);
  380. _diCoolingError = ParseDiNode("diCoolingError", node, ioModule);
  381. _diInterlockFault = ParseDiNode("diInterlockFault", node, ioModule);
  382. _doPowerOn = ParseDoNode("doPowerOn", node, ioModule);
  383. _doOn = ParseDoNode("doOn", node, ioModule);
  384. _doHeaterOn = ParseDoNode("doHeaterOn", node, ioModule);
  385. _doResetError = ParseDoNode("doReset", node, ioModule);
  386. _aiReflectedPower = ParseAiNode("aiReflectPower", node, ioModule);
  387. _aiForwardPower = ParseAiNode("aiForwardPower", node, ioModule);
  388. _aiErrorCode = ParseAiNode("aiErrorCode", node, ioModule);
  389. _aoPower = ParseAoNode("aoPower", node, ioModule);
  390. _aoImpulseTime = ParseAoNode("aoImpulseTime", node, ioModule);
  391. _aoPauseTime = ParseAoNode("aoPauseTime", node, ioModule);
  392. _scEnablePowerAlarm = ParseScNode("scEnablePowerAlarm", node, ioModule, $"{scBasePath}.{Name}.EnablePowerAlarm");
  393. _scPowerAlarmRange = ParseScNode("scPowerAlarmRange", node, ioModule, $"{scBasePath}.{Name}.PowerAlarmRange");
  394. _scPowerAlarmTime = ParseScNode("scPowerAlarmTime", node, ioModule, $"{scBasePath}.{Name}.PowerAlarmTime");
  395. _scPowerWarningRange = ParseScNode("scPowerWarningRange", node, ioModule, $"{scBasePath}.{Name}.PowerWarningRange");
  396. _scPowerWarningTime = ParseScNode("scPowerWarningTime", node, ioModule, $"{scBasePath}.{Name}.PowerWarningTime");
  397. _scEnableReflectPowerAlarm = ParseScNode("scEnableReflectPowerAlarm", node, ioModule, $"{scBasePath}.{Name}.EnableReflectPowerAlarm");
  398. _scReflectPowerAlarmRange = ParseScNode("scReflectPowerAlarmRange", node, ioModule, $"{scBasePath}.{Name}.ReflectPowerAlarmRange");
  399. _scReflectPowerAlarmTime = ParseScNode("scReflectPowerAlarmTime", node, ioModule, $"{scBasePath}.{Name}.ReflectPowerAlarmTime");
  400. _scReflectPowerWarningRange = ParseScNode("scReflectPowerWarningRange", node, ioModule, $"{scBasePath}.{Name}.ReflectPowerWarningRange");
  401. _scReflectPowerWarningTime = ParseScNode("scReflectPowerWarningTime", node, ioModule, $"{scBasePath}.{Name}.ReflectPowerWarningTime");
  402. _scPowerRange = ParseScNode("scPowerRange", node, ioModule, $"{scBasePath}.{Name}.PowerRange");
  403. _scCoefficient = ParseScNode("scPowerCoefficient", node, ioModule, $"{scBasePath}.{Name}.PowerCoefficient");
  404. _scPowerOnTimeout = ParseScNode("scPowerOnTimeout", node, ioModule, $"{scBasePath}.{Name}.PowerOnTimeout");
  405. _scResetErrorTimeout = ParseScNode("scResultErrorTimeout", node, ioModule, $"{scBasePath}.{Name}.ResetErrorTimeout");
  406. _scImpulseTime = ParseScNode("scImpulseTime", node, ioModule, $"{scBasePath}.{Name}.ImpulseTime");
  407. _scPauseTime = ParseScNode("scPauseTime", node, ioModule, $"{scBasePath}.{Name}.PauseTime");
  408. _scPowerTuneEnable = SC.GetConfigItem($"{scBasePath}.{Name}.PowerTuneEnable");
  409. _scPowerTuneIsPercent = SC.GetConfigItem($"{scBasePath}.{Name}.PowerTuneIsPercent");
  410. _scPowerTuneTable = SC.GetConfigItem($"{scBasePath}.{Name}.PowerTuneTable");
  411. _scPowerCalibrationTable = new SCConfigItem[10];
  412. for (int i = 0; i < _scPowerCalibrationTable.Length; i++)
  413. {
  414. _scPowerCalibrationTable[i] = SC.GetConfigItem($"{scBasePath}.{Name}.Rate{i + 1}MeterPower");
  415. }
  416. _scPowerCalibrationEnable = SC.GetConfigItem($"{scBasePath}.{Name}.PowerCalibrationEnable");
  417. }
  418. public bool Initialize()
  419. {
  420. UpdateTuneTable();
  421. InitializeCalalibration();
  422. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  423. DATA.Subscribe($"{Module}.{Name}.PowerSetPoint", () => PowerSetPoint);
  424. DATA.Subscribe($"{Module}.{Name}.ForwardPower", () => RFForwardPower);
  425. DATA.Subscribe($"{Module}.{Name}.ReflectPower", () => RFReflectPower);
  426. DATA.Subscribe($"{Module}.{Name}.IsRfOn", () => _diStatus.Value);
  427. DATA.Subscribe($"{Module}.{Name}.IsInterlockOK", () => RFInterlock);
  428. DATA.Subscribe($"{Module}.{Name}.RfOnSetPoint", () => _doOn.Value);
  429. DATA.Subscribe($"{Module}.{Name}.IsAlarm", () => _diAlarm.Value);
  430. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPowerOnOff}", SetPowerOnOff);
  431. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetPower}", SetPower);
  432. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetHeatOnOff}", SetHeatOnOff);
  433. AlarmPowerToleranceWarning = SubscribeAlarm($"{Module}.{Name}.PowerToleranceWarning", "", ResetPowerWarningChecker, EventLevel.Warning);
  434. AlarmPowerToleranceAlarm = SubscribeAlarm($"{Module}.{Name}.PowerToleranceAlarm", "", ResetPowerAlarmChecker);
  435. AlarmReflectPowerToleranceWarning = SubscribeAlarm($"{Module}.{Name}.ReflectPowerToleranceWarning", "", ResetReflectPowerWarningChecker, EventLevel.Warning);
  436. AlarmReflectPowerToleranceAlarm = SubscribeAlarm($"{Module}.{Name}.ReflectPowerToleranceAlarm", "", ResetReflectPowerAlarmChecker);
  437. AlarmPowerOnFailed = SubscribeAlarm($"{Module}.{Name}.PowerOnFailed", $"Failed turn {Display} on", null);
  438. AlarmDeviceAlarm = SubscribeAlarm($"{Module}.{Name}.DeviceAlarm", $"{Display} alarmed", ResetDeviceAlarmChecker);
  439. _checkerPowerWarning = new ToleranceChecker(_scPowerAlarmTime.IntValue);
  440. _checkerPowerAlarm = new ToleranceChecker(_scReflectPowerAlarmTime.IntValue);
  441. _checkerReflectPowerWarning = new ToleranceChecker(_scPowerAlarmTime.IntValue);
  442. _checkerReflectPowerAlarm = new ToleranceChecker(_scReflectPowerAlarmTime.IntValue);
  443. return true;
  444. }
  445. private bool SetPowerOnOff(out string reason, int time, object[] param)
  446. {
  447. return SetPowerOnOff(Convert.ToBoolean((string)param[0]), out reason);
  448. }
  449. public bool SetHeatOnOff(bool isOn, out string reason)
  450. {
  451. HeatOnSetPoint = isOn;
  452. reason = string.Empty;
  453. return true;
  454. }
  455. private bool SetHeatOnOff(out string reason, int time, object[] param)
  456. {
  457. HeatOnSetPoint = Convert.ToBoolean((string)param[0]);
  458. reason = string.Empty;
  459. return true;
  460. }
  461. public bool SetMainPowerOnOff(bool isOn, out string reason)
  462. {
  463. MainPowerOnSetPoint = isOn;
  464. reason = string.Empty;
  465. return true;
  466. }
  467. private bool SetPower(out string reason, int time, object[] param)
  468. {
  469. reason = string.Empty;
  470. float power = (float)Convert.ToDouble((string)param[0]);
  471. if (power < 0 || power > PowerRange)
  472. {
  473. reason = $"{Name} setpoint value {power} is out of power range, should between 0 and {PowerRange}";
  474. EV.PostWarningLog(Module, reason);
  475. return false;
  476. }
  477. PowerSetPoint = power;
  478. reason = $"{Name} set power to {power}";
  479. EV.PostInfoLog(Module, reason);
  480. return true;
  481. }
  482. public void Stop()
  483. {
  484. string reason = String.Empty;
  485. _aoPower.Value = 0;
  486. }
  487. public void Terminate()
  488. {
  489. }
  490. public void Monitor()
  491. {
  492. try
  493. {
  494. if (!IsRfOn)
  495. {
  496. if (_scImpulseTime != null)
  497. ImpulseSetPoint = _scImpulseTime.IntValue;
  498. if (_scPauseTime != null)
  499. PauseTimeSetPoint = _scPauseTime.IntValue;
  500. }
  501. if (_powerOnTimer.IsTimeout() && !_diStatus.Value)
  502. {
  503. AlarmPowerOnFailed.Description =
  504. $"Can not turn {Display} power on in {(int)(_powerOnTimer.GetElapseTime() / 1000)} seconds";
  505. AlarmPowerOnFailed.Set();
  506. SetPowerOnOff(false, out string _);
  507. _powerOnTimer.Stop();
  508. }
  509. _trigOn.CLK = IsRfOn;
  510. if (_trigOn.Q)
  511. {
  512. _powerOnStartTime = DateTime.Now;
  513. _checkerPowerAlarm.Reset(_scPowerAlarmTime.IntValue);
  514. _checkerPowerWarning.Reset(_scPowerWarningTime.IntValue);
  515. _checkerReflectPowerAlarm.Reset(_scReflectPowerAlarmTime.IntValue);
  516. _checkerReflectPowerWarning.Reset(_scReflectPowerWarningTime.IntValue);
  517. }
  518. if (_trigOn.M)
  519. {
  520. _checkerPowerWarning.Monitor(RFForwardPower, PowerSetPoint - _scPowerWarningRange.IntValue, PowerSetPoint + _scPowerWarningRange.IntValue, _scPowerWarningTime.IntValue);
  521. if (_checkerPowerWarning.Trig)
  522. {
  523. AlarmPowerToleranceWarning.Description = $"{Display} Forward power {RFForwardPower} out of range[{(PowerSetPoint - _scPowerWarningRange.IntValue)},{(PowerSetPoint + _scPowerWarningRange.IntValue)}] in {_scPowerWarningTime.IntValue} seconds";
  524. AlarmPowerToleranceWarning.Set();
  525. }
  526. _checkerPowerAlarm.Monitor(RFForwardPower, PowerSetPoint - _scPowerAlarmRange.IntValue, PowerSetPoint + _scPowerAlarmRange.IntValue, _scPowerAlarmTime.IntValue);
  527. if (_checkerPowerAlarm.Trig)
  528. {
  529. AlarmPowerToleranceAlarm.Description = $"{Display} Forward power {RFForwardPower} out of range[{(PowerSetPoint - _scPowerAlarmRange.IntValue)},{(PowerSetPoint + _scPowerAlarmRange.IntValue)}] in {_scPowerAlarmTime.IntValue} seconds";
  530. AlarmPowerToleranceAlarm.Set();
  531. SetPowerOnOff(false, out _);
  532. }
  533. _checkerReflectPowerWarning.Monitor(RFReflectPower, 0, _scReflectPowerWarningRange.IntValue, _scReflectPowerWarningTime.IntValue);
  534. if (_checkerReflectPowerWarning.Trig)
  535. {
  536. AlarmReflectPowerToleranceWarning.Description = $"{Display} reflect power {RFReflectPower} out of range[0,{_scReflectPowerWarningRange.IntValue}] in {_scReflectPowerWarningTime.IntValue} seconds";
  537. AlarmReflectPowerToleranceWarning.Set();
  538. }
  539. _checkerReflectPowerAlarm.Monitor(RFReflectPower, 0, _scReflectPowerAlarmRange.IntValue, _scReflectPowerAlarmTime.IntValue);
  540. if (_checkerReflectPowerAlarm.Trig)
  541. {
  542. AlarmReflectPowerToleranceAlarm.Description = $"{Display} reflect power {RFReflectPower} out of range[0,{_scReflectPowerAlarmRange.IntValue}] in {_scReflectPowerAlarmTime.IntValue} seconds";
  543. AlarmReflectPowerToleranceAlarm.Set();
  544. SetPowerOnOff(false, out _);
  545. }
  546. }
  547. _trigError.CLK = IsAlarm;
  548. if (_trigError.Q)
  549. {
  550. AlarmDeviceAlarm.Set(AlarmText);
  551. SetPowerOnOff(false, out string _);
  552. }
  553. if (ResetErrorSetPoint)
  554. {
  555. if (!IsAlarm)
  556. {
  557. AlarmDeviceAlarm.Reset();
  558. ResetErrorSetPoint = false;
  559. _timerResetError.Stop();
  560. }
  561. if (IsAlarm && _timerResetError.IsRunning &&
  562. _timerResetError.ElapsedMilliseconds > _scResetErrorTimeout.IntValue * 1000)
  563. {
  564. _timerResetError.Stop();
  565. EV.PostWarningLog(Module, $"Can not reset {Display} error in {_scResetErrorTimeout.IntValue} seconds");
  566. ResetErrorSetPoint = false;
  567. }
  568. }
  569. }
  570. catch (Exception ex)
  571. {
  572. LOG.Write(ex);
  573. }
  574. }
  575. public void Reset()
  576. {
  577. _trigInterlock.RST = true;
  578. _trigError.RST = true;
  579. _trigOn.RST = true;
  580. AlarmDeviceAlarm.Reset();
  581. AlarmPowerOnFailed.Reset();
  582. AlarmPowerToleranceAlarm.Reset();
  583. AlarmPowerToleranceWarning.Reset();
  584. AlarmReflectPowerToleranceAlarm.Reset();
  585. AlarmReflectPowerToleranceWarning.Reset();
  586. }
  587. public bool ResetDeviceAlarmChecker()
  588. {
  589. if (IsAlarm)
  590. {
  591. if (!ResetErrorSetPoint)
  592. {
  593. _timerResetError.Restart();
  594. ResetErrorSetPoint = true;
  595. }
  596. }
  597. return !IsAlarm;
  598. }
  599. public bool ResetPowerWarningChecker()
  600. {
  601. _checkerPowerWarning.Reset(_scPowerWarningTime.IntValue);
  602. return true;
  603. }
  604. public bool ResetPowerAlarmChecker()
  605. {
  606. _checkerPowerAlarm.Reset(_scPowerAlarmTime.IntValue);
  607. return true;
  608. }
  609. public bool ResetReflectPowerWarningChecker()
  610. {
  611. _checkerReflectPowerWarning.Reset(_scReflectPowerWarningTime.IntValue);
  612. return true;
  613. }
  614. public bool ResetReflectPowerAlarmChecker()
  615. {
  616. _checkerReflectPowerAlarm.Reset(_scReflectPowerAlarmTime.IntValue);
  617. return true;
  618. }
  619. public bool SetPowerOnOff(bool isOn, out string reason)
  620. {
  621. if (isOn)
  622. {
  623. reason = string.Empty;
  624. if (!_diIntlk.Value)
  625. {
  626. reason = $"{Name} interlock is not satisfied, can not be on";
  627. EV.PostWarningLog(Module, reason);
  628. return false;
  629. }
  630. if (!HeatOnComplete)
  631. {
  632. reason = $"{Name} heat on not complete";
  633. EV.PostWarningLog(Module, reason);
  634. return false;
  635. }
  636. }
  637. bool result = true;
  638. if (!isOn)
  639. {
  640. PowerSetPoint = 0;
  641. }
  642. else
  643. {
  644. if (PowerSetPoint < 0.01)
  645. {
  646. Task.Run(() =>
  647. {
  648. Thread.Sleep(200);
  649. result = _doOn.SetValue(isOn, out string reason1);
  650. if (!result)
  651. {
  652. EV.PostWarningLog(Module, reason1);
  653. }
  654. if (result)
  655. {
  656. string info = $"Set {Display} power " + (isOn ? "On" : "Off");
  657. if (isOn)
  658. _powerOnTimer.Start(Math.Min(Math.Max(2, _scPowerOnTimeout.IntValue), 30) * 1000);
  659. else
  660. _powerOnTimer.Stop();
  661. EV.PostInfoLog(Module, info);
  662. }
  663. });
  664. reason = string.Empty;
  665. return result;
  666. }
  667. }
  668. result = _doOn.SetValue(isOn, out reason);
  669. if (result)
  670. {
  671. reason = $"Set {Display} power " + (isOn ? "On" : "Off");
  672. if (isOn)
  673. _powerOnTimer.Start(Math.Min(Math.Max(2, _scPowerOnTimeout.IntValue), 30) * 1000);
  674. else
  675. _powerOnTimer.Stop();
  676. EV.PostInfoLog(Module, reason);
  677. }
  678. return result;
  679. }
  680. public void UpdateTuneTable()
  681. {
  682. _tuneTable = new List<RfTuneItem>();
  683. // 100#200#5#-6;
  684. string scValue = _scPowerTuneTable.StringValue;
  685. if (string.IsNullOrEmpty(scValue))
  686. return;
  687. string[] items = scValue.Split(';');
  688. for (int i = 0; i < items.Length; i++)
  689. {
  690. string itemValue = items[i];
  691. if (!string.IsNullOrEmpty(itemValue))
  692. {
  693. string[] pairValue = itemValue.Split('#');
  694. if (pairValue.Length == 4)
  695. {
  696. if (int.TryParse(pairValue[0], out int rangeItem1)
  697. && int.TryParse(pairValue[1], out int rangeItem2)
  698. && int.TryParse(pairValue[2], out int setItem1)
  699. && int.TryParse(pairValue[3], out int setItem2))
  700. {
  701. _tuneTable.Add(new RfTuneItem()
  702. {
  703. Percent = setItem1,
  704. RangeFrom = rangeItem1,
  705. RangeTo = rangeItem2,
  706. Value = setItem2,
  707. });
  708. }
  709. }
  710. }
  711. }
  712. }
  713. public float GetTuneValue(float raw, bool isOutput)
  714. {
  715. if (_scPowerTuneEnable == null || !_scPowerTuneEnable.BoolValue)
  716. return raw;
  717. float tuned = raw;
  718. foreach (var rfTuneItem in _tuneTable)
  719. {
  720. if (raw >= rfTuneItem.RangeFrom && raw <= rfTuneItem.RangeTo)
  721. {
  722. if (isOutput)
  723. {
  724. double percent = rfTuneItem.Percent / 100.0;
  725. tuned = (float)(_scPowerTuneIsPercent.BoolValue ? (raw + raw * percent) : (raw + rfTuneItem.Value));
  726. }
  727. else
  728. {
  729. double percent = rfTuneItem.Percent / 100.0;
  730. percent = 1 + percent;
  731. if (Math.Abs(percent) < 0.01)
  732. percent = 1;
  733. tuned = (float)(_scPowerTuneIsPercent.BoolValue ? (raw / percent) : (raw - rfTuneItem.Value));
  734. }
  735. }
  736. }
  737. if (tuned < 0)
  738. {
  739. tuned = 0;
  740. }
  741. if (tuned > PowerRange)
  742. {
  743. tuned = PowerRange;
  744. }
  745. return tuned;
  746. }
  747. public void InitializeCalalibration()
  748. {
  749. var records = new List<RFCalibrationItem>();
  750. for (int i = 0; i < 10; i++)
  751. {
  752. records.Add(new RFCalibrationItem()
  753. {
  754. Setpoint = (float)(PowerRange / 10 * (i + 1)),
  755. Meter = (float)_scPowerCalibrationTable[i].DoubleValue,
  756. });
  757. }
  758. _calibrationFormla = new List<CalbrationParas>();
  759. for (int i = 0; i < records.Count; i++)
  760. {
  761. if (i + 1 >= records.Count)
  762. break;
  763. var para = Caculate(records[i], records[i + 1]);
  764. _calibrationFormla.Add(para);
  765. }
  766. var para0 = new CalbrationParas()
  767. {
  768. upperLimit = (float)(PowerRange / 10),
  769. lowerLimit = 0,
  770. paraA = _calibrationFormla[0].paraA,
  771. paraB = _calibrationFormla[0].paraB,
  772. };
  773. _calibrationFormla.Add(para0);
  774. _calibrationFormla = _calibrationFormla.OrderBy(x => x.lowerLimit).ToList();
  775. }
  776. public void UpdateCalibrationInfo(List<float> meterList)
  777. {
  778. for (int i = 0; i < meterList.Count; i++)
  779. {
  780. _scPowerCalibrationTable[i].DoubleValue = meterList[i];
  781. }
  782. InitializeCalalibration();
  783. }
  784. private CalbrationParas Caculate(RFCalibrationItem record1, RFCalibrationItem record2)
  785. {
  786. var para = new CalbrationParas();
  787. para.paraA = (record1.Setpoint - record2.Setpoint) / (record1.Meter - record2.Meter);
  788. para.paraB = record1.Setpoint - record1.Meter * para.paraA;
  789. para.upperLimit = record1.Setpoint > record2.Setpoint ? record1.Setpoint : record2.Setpoint;
  790. para.lowerLimit = record1.Setpoint < record2.Setpoint ? record1.Setpoint : record2.Setpoint;
  791. return para;
  792. }
  793. private float CalibrationPower(float power, bool output)
  794. {
  795. //default enable
  796. if (_scPowerCalibrationEnable != null && !_scPowerCalibrationEnable.BoolValue)
  797. return power;
  798. float ret = power;
  799. if (output)
  800. {
  801. if (_calibrationFormla != null && _calibrationFormla.Any())
  802. {
  803. var para = _calibrationFormla.FirstOrDefault(x => x.lowerLimit <= power && x.upperLimit >= power);
  804. ret = ret * para.paraA + para.paraB;
  805. }
  806. return ret;
  807. }
  808. if (_calibrationFormla != null && _calibrationFormla.Any())
  809. {
  810. var para = _calibrationFormla.FirstOrDefault(x => x.lowerLimit <= power && x.upperLimit >= power);
  811. ret = (ret - para.paraB) / para.paraA;
  812. }
  813. return ret < 0 || ret > float.MaxValue ? 0 : ret;
  814. }
  815. public void SaveRFCalibrationData()
  816. {
  817. if (_rfCalibrationDatas.Count != TenPoints)
  818. {
  819. EV.PostInfoLog(Module, $"Save Rf calibration data failed, for some points have not been calibrated.");
  820. return;
  821. }
  822. _rfCalibrationDatas = _rfCalibrationDatas.OrderBy(x => x.Setpoint).ToList();
  823. //RFCalibrationData data = new RFCalibrationData()
  824. //{
  825. // Module = Module,
  826. // Name = Name,
  827. // Rate1Setpoint = _rfCalibrationDatas[0].Setpoint,
  828. // Rate1Feedback = _rfCalibrationDatas[0].Feedback,
  829. // Rate1Meter = _rfCalibrationDatas[0].Meter,
  830. // Rate2Setpoint = _rfCalibrationDatas[1].Setpoint,
  831. // Rate2Feedback = _rfCalibrationDatas[1].Feedback,
  832. // Rate2Meter = _rfCalibrationDatas[1].Meter,
  833. // Rate3Setpoint = _rfCalibrationDatas[2].Setpoint,
  834. // Rate3Feedback = _rfCalibrationDatas[2].Feedback,
  835. // Rate3Meter = _rfCalibrationDatas[2].Meter,
  836. // Rate4Setpoint = _rfCalibrationDatas[3].Setpoint,
  837. // Rate4Feedback = _rfCalibrationDatas[3].Feedback,
  838. // Rate4Meter = _rfCalibrationDatas[3].Meter,
  839. // Rate5Setpoint = _rfCalibrationDatas[4].Setpoint,
  840. // Rate5Feedback = _rfCalibrationDatas[4].Feedback,
  841. // Rate5Meter = _rfCalibrationDatas[4].Meter,
  842. // Rate6Setpoint = _rfCalibrationDatas[5].Setpoint,
  843. // Rate6Feedback = _rfCalibrationDatas[5].Feedback,
  844. // Rate6Meter = _rfCalibrationDatas[5].Meter,
  845. // Rate7Setpoint = _rfCalibrationDatas[6].Setpoint,
  846. // Rate7Feedback = _rfCalibrationDatas[6].Feedback,
  847. // Rate7Meter = _rfCalibrationDatas[6].Meter,
  848. // Rate8Setpoint = _rfCalibrationDatas[7].Setpoint,
  849. // Rate8Feedback = _rfCalibrationDatas[7].Feedback,
  850. // Rate8Meter = _rfCalibrationDatas[7].Meter,
  851. // Rate9Setpoint = _rfCalibrationDatas[8].Setpoint,
  852. // Rate9Feedback = _rfCalibrationDatas[8].Feedback,
  853. // Rate9Meter = _rfCalibrationDatas[8].Meter,
  854. // Rate10Setpoint = _rfCalibrationDatas[9].Setpoint,
  855. // Rate10Feedback = _rfCalibrationDatas[9].Feedback,
  856. // Rate10Meter = _rfCalibrationDatas[9].Meter,
  857. //};
  858. //RFCalibrationDataRecorder.Add(data);
  859. for (int i = 0; i < _rfCalibrationDatas.Count; i++)
  860. {
  861. SC.SetItemValue($"{Module}.RF.Rate{i + 1}MeterPower", (double)_rfCalibrationDatas[i].Meter);
  862. }
  863. InitializeCalalibration();
  864. EV.PostInfoLog(Module, $"Save RF calibration data successed.");
  865. }
  866. public void SaveRFCalibrationPoint(float setpoint, float meterValue)
  867. {
  868. if (_rfCalibrationDatas.Any(x => x.Setpoint == setpoint))
  869. {
  870. _rfCalibrationDatas.RemoveAll(x => x.Setpoint == setpoint);
  871. }
  872. var data = new RFCalibrationItem()
  873. {
  874. Setpoint = setpoint,
  875. Meter = meterValue,
  876. Feedback = RFForwardPower,
  877. };
  878. _rfCalibrationDatas.Add(data);
  879. }
  880. private struct CalbrationParas
  881. {
  882. //y = a*x + b, x is setpoint
  883. public float paraA;
  884. public float paraB;
  885. public float upperLimit;
  886. public float lowerLimit;
  887. }
  888. private class RFCalibrationItem
  889. {
  890. public float Setpoint { get; set; }
  891. public float Feedback { get; set; }
  892. public float Meter { get; set; }
  893. }
  894. }
  895. }