Guest User

Untitled

a guest
Jul 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. public class PIDController
  2. {
  3. public enum PIDMode
  4. {
  5. Manual,
  6. Auto,
  7. }
  8.  
  9. public enum PIDAction
  10. {
  11. Indirect,
  12. Direct,
  13. }
  14.  
  15. public PIDMode Mode { get; set; }
  16. public PIDAction Action { get; set; }
  17.  
  18. public double Proportional { get; set; }
  19. public double Integral { get; set; }
  20. public double Derivative { get; set; }
  21.  
  22. public double Minimum { get; set; }
  23. public double Maximum { get; set; }
  24.  
  25. public double DeltaMinimum { get; set; }
  26. public double DeltaMaximum { get; set; }
  27.  
  28. private double _ProportionalTerm;
  29. private double _Integrator;
  30. private double _Derivator;
  31.  
  32. public double Setpoint { get; set; }
  33.  
  34. private double _Feedback;
  35. public double Feedback
  36. {
  37. get
  38. {
  39. return _Feedback;
  40. }
  41. }
  42.  
  43. public void Calculate(double Input, long Time)
  44. {
  45. double output;
  46.  
  47. // Compute the error value
  48. double Error = Setpoint - Input;
  49.  
  50. if (Mode == PIDMode.Auto)
  51. {
  52. if (Action == PIDAction.Direct)
  53. Error = 0 - Error;
  54.  
  55. // Compute the proportional component
  56. _ProportionalTerm = 1000.0f * (Error - _Derivator) / (double)Time;
  57.  
  58. // Compute the integrator component, clamped to min/max delta movement
  59. _Integrator += (float)Error * (float)Time / 1000.0f;
  60. if (_Integrator < DeltaMinimum)
  61. _Integrator = DeltaMinimum;
  62. if (_Integrator > DeltaMaximum)
  63. _Integrator = DeltaMaximum;
  64.  
  65. // Add the proportional component
  66. output = (Proportional * Error);
  67.  
  68. // Add the integral component
  69. output += Integral * _Integrator;
  70.  
  71. // Add the derivative component
  72. output += Derivative * _ProportionalTerm;
  73.  
  74. // Clamp output to min/max
  75. if (output < Minimum)
  76. output = Minimum;
  77. if (output > Maximum)
  78. output = Maximum;
  79. }
  80. else
  81. {
  82. // Manual mode, move directly to the Setpoint
  83. output = Setpoint;
  84. }
  85.  
  86. // Store values
  87. _Derivator = Error;
  88.  
  89. // Returns the result
  90. _Feedback = output;
  91. }
Add Comment
Please, Sign In to add comment