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