PID_v1.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef PID_v1_h
  2. #define PID_v1_h
  3. #define LIBRARY_VERSION 1.2.1
  4. class PID
  5. {
  6. public:
  7. //Constants used in some of the functions below
  8. #define AUTOMATIC 1
  9. #define MANUAL 0
  10. #define DIRECT 0
  11. #define REVERSE 1
  12. #define P_ON_M 0
  13. #define P_ON_E 1
  14. //commonly used functions **************************************************************************
  15. PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
  16. double, double, double, int, int);// Setpoint. Initial tuning parameters are also set here.
  17. // (overload for specifying proportional mode)
  18. PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
  19. double, double, double, int); // Setpoint. Initial tuning parameters are also set here
  20. void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
  21. bool Compute(); // * performs the PID calculation. it should be
  22. // called every time loop() cycles. ON/OFF and
  23. // calculation frequency can be set using SetMode
  24. // SetSampleTime respectively
  25. void SetOutputLimits(double, double); // * clamps the output to a specific range. 0-255 by default, but
  26. // it's likely the user will want to change this depending on
  27. // the application
  28. //available but not commonly used functions ********************************************************
  29. void SetTunings(double, double, // * While most users will set the tunings once in the
  30. double); // constructor, this function gives the user the option
  31. // of changing tunings during runtime for Adaptive control
  32. void SetTunings(double, double, // * overload for specifying proportional mode
  33. double, int);
  34. void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
  35. // means the output will increase when error is positive. REVERSE
  36. // means the opposite. it's very unlikely that this will be needed
  37. // once it is set in the constructor.
  38. void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
  39. // the PID calculation is performed. default is 100
  40. //Display functions ****************************************************************
  41. double GetKp(); // These functions query the pid for interal values.
  42. double GetKi(); // they were created mainly for the pid front-end,
  43. double GetKd(); // where it's important to know what is actually
  44. int GetMode(); // inside the PID.
  45. int GetDirection(); //
  46. private:
  47. void Initialize();
  48. double dispKp; // * we'll hold on to the tuning parameters in user-entered
  49. double dispKi; // format for display purposes
  50. double dispKd; //
  51. double kp; // * (P)roportional Tuning Parameter
  52. double ki; // * (I)ntegral Tuning Parameter
  53. double kd; // * (D)erivative Tuning Parameter
  54. int controllerDirection;
  55. int pOn;
  56. double *myInput; // * Pointers to the Input, Output, and Setpoint variables
  57. double *myOutput; // This creates a hard link between the variables and the
  58. double *mySetpoint; // PID, freeing the user from having to constantly tell us
  59. // what these values are. with pointers we'll just know.
  60. unsigned long lastTime;
  61. double outputSum, lastInput;
  62. unsigned long SampleTime;
  63. double outMin, outMax;
  64. bool inAuto, pOnE;
  65. };
  66. #endif