*************************************************************** * Arduino PID Library - Version 1.1.1 * by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com * * This Library is licensed under a GPLv3 License ***************************************************************
PID.h@0:f6efcf8146b1, 2017-06-02 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Fri Jun 02 19:37:02 2017 +0000
- Revision:
- 0:f6efcf8146b1
Testprogram for PID lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 0:f6efcf8146b1 | 1 | #ifndef PID_h |
gert_lauritsen | 0:f6efcf8146b1 | 2 | #define PID_h |
gert_lauritsen | 0:f6efcf8146b1 | 3 | #define LIBRARY_VERSION 1.1.1 |
gert_lauritsen | 0:f6efcf8146b1 | 4 | #include "mbed.h" |
gert_lauritsen | 0:f6efcf8146b1 | 5 | class PID |
gert_lauritsen | 0:f6efcf8146b1 | 6 | { |
gert_lauritsen | 0:f6efcf8146b1 | 7 | |
gert_lauritsen | 0:f6efcf8146b1 | 8 | |
gert_lauritsen | 0:f6efcf8146b1 | 9 | public: |
gert_lauritsen | 0:f6efcf8146b1 | 10 | |
gert_lauritsen | 0:f6efcf8146b1 | 11 | //Constants used in some of the functions below |
gert_lauritsen | 0:f6efcf8146b1 | 12 | #define AUTOMATIC 1 |
gert_lauritsen | 0:f6efcf8146b1 | 13 | #define MANUAL 0 |
gert_lauritsen | 0:f6efcf8146b1 | 14 | #define DIRECT 0 |
gert_lauritsen | 0:f6efcf8146b1 | 15 | #define REVERSE 1 |
gert_lauritsen | 0:f6efcf8146b1 | 16 | |
gert_lauritsen | 0:f6efcf8146b1 | 17 | //commonly used functions ************************************************************************** |
gert_lauritsen | 0:f6efcf8146b1 | 18 | PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and |
gert_lauritsen | 0:f6efcf8146b1 | 19 | double, double, double, int); // Setpoint. Initial tuning parameters are also set here |
gert_lauritsen | 0:f6efcf8146b1 | 20 | |
gert_lauritsen | 0:f6efcf8146b1 | 21 | void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0) |
gert_lauritsen | 0:f6efcf8146b1 | 22 | |
gert_lauritsen | 0:f6efcf8146b1 | 23 | bool Compute(); // * performs the PID calculation. it should be |
gert_lauritsen | 0:f6efcf8146b1 | 24 | // called every time loop() cycles. ON/OFF and |
gert_lauritsen | 0:f6efcf8146b1 | 25 | // calculation frequency can be set using SetMode |
gert_lauritsen | 0:f6efcf8146b1 | 26 | // SetSampleTime respectively |
gert_lauritsen | 0:f6efcf8146b1 | 27 | |
gert_lauritsen | 0:f6efcf8146b1 | 28 | void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but |
gert_lauritsen | 0:f6efcf8146b1 | 29 | //it's likely the user will want to change this depending on |
gert_lauritsen | 0:f6efcf8146b1 | 30 | //the application |
gert_lauritsen | 0:f6efcf8146b1 | 31 | |
gert_lauritsen | 0:f6efcf8146b1 | 32 | |
gert_lauritsen | 0:f6efcf8146b1 | 33 | |
gert_lauritsen | 0:f6efcf8146b1 | 34 | //available but not commonly used functions ******************************************************** |
gert_lauritsen | 0:f6efcf8146b1 | 35 | void SetTunings(double, double, // * While most users will set the tunings once in the |
gert_lauritsen | 0:f6efcf8146b1 | 36 | double); // constructor, this function gives the user the option |
gert_lauritsen | 0:f6efcf8146b1 | 37 | // of changing tunings during runtime for Adaptive control |
gert_lauritsen | 0:f6efcf8146b1 | 38 | void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT |
gert_lauritsen | 0:f6efcf8146b1 | 39 | // means the output will increase when error is positive. REVERSE |
gert_lauritsen | 0:f6efcf8146b1 | 40 | // means the opposite. it's very unlikely that this will be needed |
gert_lauritsen | 0:f6efcf8146b1 | 41 | // once it is set in the constructor. |
gert_lauritsen | 0:f6efcf8146b1 | 42 | void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which |
gert_lauritsen | 0:f6efcf8146b1 | 43 | // the PID calculation is performed. default is 100 |
gert_lauritsen | 0:f6efcf8146b1 | 44 | |
gert_lauritsen | 0:f6efcf8146b1 | 45 | |
gert_lauritsen | 0:f6efcf8146b1 | 46 | |
gert_lauritsen | 0:f6efcf8146b1 | 47 | //Display functions **************************************************************** |
gert_lauritsen | 0:f6efcf8146b1 | 48 | double GetKp(); // These functions query the pid for interal values. |
gert_lauritsen | 0:f6efcf8146b1 | 49 | double GetKi(); // they were created mainly for the pid front-end, |
gert_lauritsen | 0:f6efcf8146b1 | 50 | double GetKd(); // where it's important to know what is actually |
gert_lauritsen | 0:f6efcf8146b1 | 51 | int GetMode(); // inside the PID. |
gert_lauritsen | 0:f6efcf8146b1 | 52 | int GetDirection(); // |
gert_lauritsen | 0:f6efcf8146b1 | 53 | |
gert_lauritsen | 0:f6efcf8146b1 | 54 | private: |
gert_lauritsen | 0:f6efcf8146b1 | 55 | void Initialize(); |
gert_lauritsen | 0:f6efcf8146b1 | 56 | Ticker Timer; |
gert_lauritsen | 0:f6efcf8146b1 | 57 | unsigned long millis; |
gert_lauritsen | 0:f6efcf8146b1 | 58 | double dispKp; // * we'll hold on to the tuning parameters in user-entered |
gert_lauritsen | 0:f6efcf8146b1 | 59 | double dispKi; // format for display purposes |
gert_lauritsen | 0:f6efcf8146b1 | 60 | double dispKd; // |
gert_lauritsen | 0:f6efcf8146b1 | 61 | |
gert_lauritsen | 0:f6efcf8146b1 | 62 | double kp; // * (P)roportional Tuning Parameter |
gert_lauritsen | 0:f6efcf8146b1 | 63 | double ki; // * (I)ntegral Tuning Parameter |
gert_lauritsen | 0:f6efcf8146b1 | 64 | double kd; // * (D)erivative Tuning Parameter |
gert_lauritsen | 0:f6efcf8146b1 | 65 | |
gert_lauritsen | 0:f6efcf8146b1 | 66 | int controllerDirection; |
gert_lauritsen | 0:f6efcf8146b1 | 67 | |
gert_lauritsen | 0:f6efcf8146b1 | 68 | double *myInput; // * Pointers to the Input, Output, and Setpoint variables |
gert_lauritsen | 0:f6efcf8146b1 | 69 | double *myOutput; // This creates a hard link between the variables and the |
gert_lauritsen | 0:f6efcf8146b1 | 70 | double *mySetpoint; // PID, freeing the user from having to constantly tell us |
gert_lauritsen | 0:f6efcf8146b1 | 71 | // what these values are. with pointers we'll just know. |
gert_lauritsen | 0:f6efcf8146b1 | 72 | |
gert_lauritsen | 0:f6efcf8146b1 | 73 | unsigned long lastTime; |
gert_lauritsen | 0:f6efcf8146b1 | 74 | double ITerm, lastInput; |
gert_lauritsen | 0:f6efcf8146b1 | 75 | |
gert_lauritsen | 0:f6efcf8146b1 | 76 | unsigned long SampleTime; |
gert_lauritsen | 0:f6efcf8146b1 | 77 | double outMin, outMax; |
gert_lauritsen | 0:f6efcf8146b1 | 78 | bool inAuto; |
gert_lauritsen | 0:f6efcf8146b1 | 79 | void GeneralTimer(); |
gert_lauritsen | 0:f6efcf8146b1 | 80 | }; |
gert_lauritsen | 0:f6efcf8146b1 | 81 | #endif |
gert_lauritsen | 0:f6efcf8146b1 | 82 |