RfPowerBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 Aitex.Core.Common.DeviceData;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Device;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.RT.Tolerance;
  14. using Aitex.Core.Util;
  15. using MECF.Framework.Common.CommonData;
  16. using MECF.Framework.Common.DataCenter;
  17. namespace MECF.Framework.Common.Device.Bases
  18. {
  19. public abstract class RfPowerBase : BaseDevice, IDevice
  20. {
  21. public virtual bool IsPowerOn { get; set; }
  22. public virtual bool IsMatchOn { get; set; }
  23. public virtual bool IsError { get; set; }
  24. public virtual bool IsMatchError { get; set; }
  25. public virtual EnumRfPowerWorkMode WorkMode { get; set; }
  26. public virtual EnumRfPowerControlMode ControlMode { get; set; }
  27. public virtual EnumRfPowerRegulationMode RegulationMode { get; set; }
  28. public virtual float ForwardPower { get; set; }
  29. public virtual float ReflectPower { get; set; }
  30. public virtual float PowerSetPoint { get; set; }
  31. public virtual float CLoadSet { get; set; }
  32. public virtual float CTuneSet { get; set; }
  33. public virtual float CLoad { get; set; }
  34. public virtual float CTune { get; set; }
  35. public virtual int VPP { get; set; }
  36. public virtual float Frequency { get; set; }
  37. public virtual float PulsingFrequency { get; set; }
  38. public virtual float PulsingDutyCycle { get; set; }
  39. public virtual float ScalePower { get; set; }
  40. public virtual AITRfPowerData DeviceData { get; set; }
  41. //calibration
  42. //protected SCConfigItem _scEnableCalibration;
  43. //protected SCConfigItem _scCalibrationTable;
  44. protected SCConfigItem _scEnableCalibration;
  45. protected SCConfigItem _scCalibrationTable;
  46. protected SCConfigItem _scRFPhysicalMaxPower;
  47. protected SCConfigItem _scCurrentRFMaxPower;
  48. private List<CalibrationItem> _calibrationTable = new List<CalibrationItem>();
  49. private string _previousSetting;
  50. protected float _recipeAlarmRange;
  51. protected float _recipeWarningRange;
  52. protected int _recipeIgnoreTimeMS;
  53. protected ToleranceChecker _recipeAlarmChecker = new ToleranceChecker();
  54. protected ToleranceChecker _recipeWarningChecker = new ToleranceChecker();
  55. protected DeviceTimer _recipeIgnoreTimer = new DeviceTimer();
  56. protected ToleranceChecker _alarmChecker = new ToleranceChecker();
  57. protected ToleranceChecker _warningChecker = new ToleranceChecker();
  58. protected RfPowerBase( ) : base( )
  59. {
  60. }
  61. protected RfPowerBase(string module, string name) : base(module, name, name, name)
  62. {
  63. }
  64. public virtual bool Initialize()
  65. {
  66. // DATA.Subscribe($"{Module}.{Name}.WorkMode", ()=>WorkMode.ToString());
  67. DATA.Subscribe($"{Module}.{Name}.ControlMode", () => ControlMode.ToString());
  68. DATA.Subscribe($"{Module}.{Name}.RegulationMode", () => RegulationMode.ToString());
  69. DATA.Subscribe($"{Module}.{Name}.ForwardPower", () => ForwardPower);
  70. DATA.Subscribe($"{Module}.{Name}.ReflectPower", () => ReflectPower);
  71. DATA.Subscribe($"{Module}.{Name}.PowerSetPoint", () => PowerSetPoint);
  72. DATA.Subscribe($"{Module}.{Name}.IsPowerOn", () => IsPowerOn);
  73. DATA.Subscribe($"{Module}.{Name}.Frequency", () => Frequency);
  74. DATA.Subscribe($"{Module}.{Name}.PulsingFrequency", () => PulsingFrequency);
  75. DATA.Subscribe($"{Module}.{Name}.PulsingDutyCycle", () => PulsingDutyCycle);
  76. OP.Subscribe($"{Module}.{Name}.SetPowerOn", (function, args) =>
  77. {
  78. return SetPowerOnOff(true, out string reason);
  79. });
  80. OP.Subscribe($"{Module}.{Name}.SetPowerOff", (function, args) =>
  81. {
  82. return SetPowerOnOff(false, out string reason);
  83. });
  84. OP.Subscribe($"{Module}.{Name}.SetPower", (function, args) =>
  85. {
  86. SetPower((float)args[0]);
  87. return true;
  88. });
  89. OP.Subscribe($"{Module}.{Name}.ResetRFStats", (function, args) =>
  90. {
  91. //ResetRFStats();
  92. return true;
  93. });
  94. OP.Subscribe($"{Module}.{Name}.SetRegulationMode", (function, args) =>
  95. {
  96. if (!Enum.TryParse((string) args[0], out EnumRfPowerRegulationMode mode))
  97. {
  98. EV.PostWarningLog(Module, $"Argument {args[0]}not valid");
  99. return false;
  100. }
  101. SetRegulationMode(mode);
  102. return true;
  103. });
  104. OP.Subscribe($"{Module}.{Name}.SetRecipeTolerance", (out string reason, int time, object[] param) =>
  105. {
  106. reason = string.Empty;
  107. _recipeIgnoreTimeMS = Convert.ToInt32(param[0]) * 1000;
  108. _recipeWarningRange = Convert.ToSingle(param[1]);
  109. _recipeAlarmRange = Convert.ToSingle(param[2]);
  110. _recipeAlarmChecker.RST = true;
  111. _recipeWarningChecker.RST = true;
  112. if (_recipeIgnoreTimeMS > 0)
  113. _recipeIgnoreTimer.Start(0);
  114. return true;
  115. });
  116. UpdateCalibrationTable();
  117. return true;
  118. }
  119. public virtual void SetRegulationMode(EnumRfPowerRegulationMode enumRfPowerControlMode)
  120. {
  121. }
  122. public virtual bool SetPowerOnOff(bool isOn, out string reason)
  123. {
  124. reason = string.Empty;
  125. return true;
  126. }
  127. public virtual bool SetMatchingAutoMode(bool isOn, out string reason)
  128. {
  129. reason = string.Empty;
  130. return true;
  131. }
  132. public virtual bool SetMatchPosition(double c1, double c2, out string reason)
  133. {
  134. reason = string.Empty;
  135. return true;
  136. }
  137. public virtual void SetPower(float power)
  138. {
  139. }
  140. public virtual void SetPulseMode(bool on)
  141. {
  142. }
  143. public virtual void SetPulseRateFreq(int nFreq)
  144. {
  145. }
  146. public virtual void SetPulseDutyCycle(int percentage)
  147. {
  148. }
  149. public virtual void Terminate()
  150. {
  151. }
  152. public virtual void ResetRFStats()
  153. {
  154. }
  155. public virtual void Monitor()
  156. {
  157. if (_scCalibrationTable != null)
  158. {
  159. if (string.IsNullOrEmpty(_previousSetting) || _previousSetting != _scCalibrationTable.StringValue)
  160. UpdateCalibrationTable();
  161. }
  162. }
  163. public virtual void Reset()
  164. {
  165. _alarmChecker.RST = true;
  166. _warningChecker.RST = true;
  167. _recipeWarningChecker.RST = true;
  168. _recipeAlarmChecker.RST = true;
  169. }
  170. public virtual void SetCommunicationMode(int mode) { }
  171. protected virtual void UpdateCalibrationTable()
  172. {
  173. if (_scCalibrationTable == null)
  174. return;
  175. if (_previousSetting == _scCalibrationTable.StringValue)
  176. return;
  177. _previousSetting = _scCalibrationTable.StringValue;
  178. if (string.IsNullOrEmpty(_previousSetting))
  179. {
  180. _calibrationTable = new List<CalibrationItem>();
  181. return;
  182. }
  183. var table = new List<Tuple<float, float>>();
  184. string[] items = _previousSetting.Split(';');
  185. for (int i = 0; i < items.Length; i++)
  186. {
  187. string itemValue = items[i];
  188. if (!string.IsNullOrEmpty(itemValue))
  189. {
  190. string[] pairValue = itemValue.Split('#');
  191. if (pairValue.Length == 2)
  192. {
  193. if (float.TryParse(pairValue[0], out float rawData)
  194. && float.TryParse(pairValue[1], out float calibrationData))
  195. {
  196. table.Add(Tuple.Create(rawData, calibrationData));
  197. }
  198. }
  199. }
  200. }
  201. table = table.OrderBy(x => x.Item1).ToList();
  202. var calibrationTable = new List<CalibrationItem>();
  203. for (int i = 0; i < table.Count; i++)
  204. {
  205. if (i == 0 && table[0].Item1 > 0.001)
  206. {
  207. calibrationTable.Add(new CalibrationItem()
  208. {
  209. RawFrom = 0,
  210. CalibrationFrom = 0,
  211. RawTo = table[0].Item1,
  212. CalibrationTo = table[0].Item2,
  213. });
  214. }
  215. if (i == table.Count - 1)
  216. {
  217. float maxValue = (float)ScalePower;
  218. calibrationTable.Add(new CalibrationItem()
  219. {
  220. RawFrom = table[i].Item1,
  221. RawTo = table[i].Item2,
  222. CalibrationFrom = maxValue,
  223. CalibrationTo = maxValue,
  224. });
  225. continue;
  226. }
  227. calibrationTable.Add(new CalibrationItem()
  228. {
  229. RawFrom = table[i].Item1,
  230. CalibrationFrom = table[i].Item2,
  231. RawTo = table[i + 1].Item1,
  232. CalibrationTo = table[i + 1].Item2,
  233. });
  234. }
  235. _calibrationTable = calibrationTable;
  236. }
  237. protected virtual float CalibrationData(float value, bool output)
  238. {
  239. //default enable
  240. if (_scEnableCalibration != null && !_scEnableCalibration.BoolValue)
  241. return value;
  242. if (_scCalibrationTable == null || !_calibrationTable.Any())
  243. return value;
  244. float ret = value;
  245. if (output)
  246. {
  247. //var item = _calibrationTable.FirstOrDefault(x => x.RawFrom <= value && x.RawTo >= value);
  248. //if (item != null && Math.Abs(item.RawTo - item.RawFrom) > 0.01)
  249. //{
  250. // var slope = (item.CalibrationTo - item.CalibrationFrom) / (item.RawTo - item.RawFrom);
  251. // ret = (ret - item.RawFrom) * slope + item.CalibrationFrom;
  252. //}
  253. if (_scRFPhysicalMaxPower != null && _scCurrentRFMaxPower != null && _scRFPhysicalMaxPower.DoubleValue > 0 && _scCurrentRFMaxPower.DoubleValue > 0)
  254. {
  255. ret = (float)(ret * _scRFPhysicalMaxPower.DoubleValue / _scCurrentRFMaxPower.DoubleValue);
  256. }
  257. if (ret >= float.MaxValue || ret >= ScalePower)
  258. ret = ScalePower;
  259. }
  260. else
  261. {
  262. //var item = _calibrationTable.FirstOrDefault(x => x.CalibrationFrom <= value && x.CalibrationTo >= value);
  263. //if(item != null && item.CalibrationTo == 0 && item.CalibrationFrom == 0 && value > 0)
  264. // item = _calibrationTable[_calibrationTable.Count - 1];
  265. //
  266. //if (item != null && Math.Abs(item.CalibrationTo - item.CalibrationFrom) > 0.01)
  267. //{
  268. // var slope = (item.RawTo - item.RawFrom) / (item.CalibrationTo - item.CalibrationFrom);
  269. // ret = (ret - item.CalibrationFrom) * slope + item.RawFrom;
  270. //}
  271. if (ret >= float.MaxValue || ret >= ScalePower)
  272. {
  273. ret = ScalePower;
  274. }
  275. else if (_scRFPhysicalMaxPower != null && _scCurrentRFMaxPower != null && _scRFPhysicalMaxPower.DoubleValue > 0 && _scCurrentRFMaxPower.DoubleValue > 0)
  276. {
  277. ret = (float)(ret * _scCurrentRFMaxPower.DoubleValue / _scRFPhysicalMaxPower.DoubleValue);
  278. }
  279. }
  280. if (ret < 0)
  281. return 0;
  282. if (ret >= float.MaxValue || ret > ScalePower)
  283. ret = value;
  284. return ret;
  285. }
  286. public virtual bool ReConnect()
  287. {
  288. return true;
  289. }
  290. }
  291. }