RfPowerBase.cs 12 KB

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