RfPowerBase.cs 11 KB

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