PdiAlgorithm.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /// <summary>
  14. /// 差额系数
  15. /// </summary>
  16. private double _kp;
  17. /// <summary>
  18. /// 积分系数
  19. /// </summary>
  20. private double _ki;
  21. /// <summary>
  22. /// 微分系数
  23. /// </summary>
  24. private double _kd;
  25. /// <summary>
  26. /// 上一次偏差
  27. /// </summary>
  28. private double _lastBias;
  29. /// <summary>
  30. /// 积分值
  31. /// </summary>
  32. private double _integral;
  33. /// <summary>
  34. /// 目标压力
  35. /// </summary>
  36. private double _targetPressure;
  37. /// <summary>
  38. /// 最大偏差
  39. /// </summary>
  40. private double _maxBias = 0;
  41. #endregion
  42. /// <summary>
  43. /// 计算速度
  44. /// </summary>
  45. /// <param name="pressure"></param>
  46. /// <param name="speed"></param>
  47. /// <returns></returns>
  48. public short CalculateSpeed(double kp,double ki,double kd,double targetPressure,double pressure,short speed,double targetLimit)
  49. {
  50. _kp = kp;
  51. _ki = ki;
  52. _kd = kd;
  53. _targetPressure = targetPressure;
  54. _maxBias = 0.5 * targetPressure;
  55. double index = 0;
  56. double bias=_targetPressure - pressure;
  57. if(Math.Abs(bias)<=targetLimit)
  58. {
  59. return speed;
  60. }
  61. _integral += bias;
  62. if(Math.Abs(bias)>_maxBias)
  63. {
  64. index = 0;
  65. }
  66. else if(Math.Abs(bias)<0.9*_maxBias)
  67. {
  68. index = 1;
  69. _integral += bias;
  70. }
  71. else
  72. {
  73. index=(_maxBias-Math.Abs(bias))/(0.9*_maxBias);
  74. _integral += bias;
  75. }
  76. double pressureBias=_kp*bias+index*_ki*_integral+_kd*(bias-_lastBias);
  77. //根据伯努利一般形态P+1/2V^2=C
  78. double speedBias =Math.Round(Math.Sqrt(2 * pressureBias),2);
  79. _lastBias=bias;
  80. short finalSpeed= (short)Math.Round(speed+pressureBias,0);
  81. int maxPumpSpeed = SC.GetValue<int>("Prewet.MaxPumpSpeed");
  82. int minPumpSpeed = SC.GetValue<int>("Prewet.MinPumpSpeed");
  83. if (finalSpeed >= maxPumpSpeed)
  84. {
  85. finalSpeed = (short)maxPumpSpeed;
  86. }
  87. else if(finalSpeed<=minPumpSpeed)
  88. {
  89. finalSpeed = (short)minPumpSpeed;
  90. }
  91. return finalSpeed;
  92. }
  93. }
  94. }