PdiAlgorithm.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Aitex.Core.RT.SCCore;
  2. using Aitex.Core.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MECF.Framework.Common.Algorithm
  9. {
  10. public class PdiAlgorithm : Singleton<PdiAlgorithm>
  11. {
  12. #region 常量
  13. private const double RANGE_PRESSURE = 2;
  14. #endregion
  15. #region 内部变量
  16. /// <summary>
  17. /// 差额系数
  18. /// </summary>
  19. private double _kp;
  20. /// <summary>
  21. /// 积分系数
  22. /// </summary>
  23. private double _ki;
  24. /// <summary>
  25. /// 微分系数
  26. /// </summary>
  27. private double _kd;
  28. /// <summary>
  29. /// 上一次偏差
  30. /// </summary>
  31. private double _lastBias;
  32. /// <summary>
  33. /// 积分值
  34. /// </summary>
  35. private double _integral;
  36. /// <summary>
  37. /// 目标压力
  38. /// </summary>
  39. private double _targetPressure;
  40. /// <summary>
  41. /// 最大偏差
  42. /// </summary>
  43. private double _maxBias = 0;
  44. #endregion
  45. /// <summary>
  46. /// 计算速度
  47. /// </summary>
  48. /// <param name="pressure"></param>
  49. /// <param name="speed"></param>
  50. /// <returns></returns>
  51. public short CalculateSpeed(double kp, double ki, double kd, double targetPressure, double pressure, short speed, double targetLimit,
  52. double downTargetLimit, double minSpeedDelta)
  53. {
  54. _kp = kp;
  55. _ki = ki;
  56. _kd = kd;
  57. _targetPressure = targetPressure;
  58. _maxBias = 0.5 * targetPressure;
  59. double index = 0;
  60. double bias = _targetPressure - pressure;
  61. if (bias >= 0 && bias <= targetLimit)
  62. {
  63. return speed;
  64. }
  65. if (bias >= -downTargetLimit && bias < 0)
  66. {
  67. return speed;
  68. }
  69. _integral += bias;
  70. if (Math.Abs(bias) > _maxBias)
  71. {
  72. index = 0;
  73. }
  74. else if (Math.Abs(bias) < 0.9 * _maxBias)
  75. {
  76. index = 1;
  77. _integral += bias;
  78. }
  79. else
  80. {
  81. index = (_maxBias - Math.Abs(bias)) / (0.9 * _maxBias);
  82. _integral += bias;
  83. }
  84. double pressureBias = _kp * bias + index * _ki * _integral + _kd * (bias - _lastBias);
  85. double range = SC.GetValue<double>("VPWMain.RangePressure");
  86. if (Math.Abs(bias) >= range)
  87. {
  88. if (bias < 0)
  89. {
  90. pressureBias = -minSpeedDelta;
  91. }
  92. else
  93. {
  94. pressureBias = minSpeedDelta;
  95. }
  96. }
  97. _lastBias = bias;
  98. short finalSpeed = (short)Math.Round(speed + pressureBias, 0);
  99. int maxPumpSpeed = SC.GetValue<int>("VPWMain.MaxPumpSpeed");
  100. int minPumpSpeed = SC.GetValue<int>("VPWMain.MinPumpSpeed");
  101. if (finalSpeed >= maxPumpSpeed)
  102. {
  103. finalSpeed = (short)maxPumpSpeed;
  104. }
  105. else if (finalSpeed <= minPumpSpeed)
  106. {
  107. finalSpeed = (short)minPumpSpeed;
  108. }
  109. return finalSpeed;
  110. }
  111. public short CalculateSpeed(double targetPressure, double pressure, short speed, double targetLimit,
  112. double downTargetLimit)
  113. {
  114. //根据现场手动调速得到的3次拟合多项式拟合系统 y=k3x^3+K2x^2+k2x+offset(y=0.8641x^3-131.23x^2+6750.4x-113116) 拟合r2为0.9929
  115. //Pressure Pre-wet Pump Speed
  116. //37.9 1000
  117. //38 1300
  118. //38.6 1600
  119. //38.9 1900
  120. //39.3 2200
  121. //40.6 2500
  122. //41.6 2800
  123. //42.5 3100
  124. //44.3 3400
  125. //45.2 3700
  126. //47.9 4000
  127. //50.4 4300
  128. //51.5 4600
  129. //54.04 4900
  130. //56.13 5200
  131. //58.12 5500
  132. double k3 = 0.8641;
  133. double k2 = -131.23;
  134. double k1 = 6750.4;
  135. double bias = _targetPressure - pressure;
  136. if (bias >= 0 && bias <= targetLimit)
  137. {
  138. return speed;
  139. }
  140. if (bias >= -downTargetLimit && bias < 0)
  141. {
  142. return speed;
  143. }
  144. double speedDelta = k3 * Math.Pow(targetPressure, 3) + k2 * Math.Pow(targetPressure, 2) + k1 * targetPressure -
  145. k3 * Math.Pow(pressure, 3) - k2 * Math.Pow(pressure, 2) - k1 * pressure;
  146. short finalSpeed = (short)Math.Round(speed + speedDelta, 0);
  147. int maxPumpSpeed = SC.GetValue<int>("Prewet.MaxPumpSpeed");
  148. int minPumpSpeed = SC.GetValue<int>("Prewet.MinPumpSpeed");
  149. if (finalSpeed >= maxPumpSpeed)
  150. {
  151. finalSpeed = (short)maxPumpSpeed;
  152. }
  153. else if (finalSpeed <= minPumpSpeed)
  154. {
  155. finalSpeed = (short)minPumpSpeed;
  156. }
  157. return finalSpeed;
  158. }
  159. }
  160. }