RfPowerBase.cs 10 KB

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