Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BalanceCar by
PID_v2.h@0:a4d8f5b3c546, 2016-06-04 (annotated)
- Committer:
- lixianyu
- Date:
- Sat Jun 04 03:16:52 2016 +0000
- Revision:
- 0:a4d8f5b3c546
Pass compile!!
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| lixianyu | 0:a4d8f5b3c546 | 1 | #ifndef PID_v2_h |
| lixianyu | 0:a4d8f5b3c546 | 2 | #define PID_v2_h |
| lixianyu | 0:a4d8f5b3c546 | 3 | #define LIBRARY_VERSION 1.1.2 |
| lixianyu | 0:a4d8f5b3c546 | 4 | |
| lixianyu | 0:a4d8f5b3c546 | 5 | class PID |
| lixianyu | 0:a4d8f5b3c546 | 6 | { |
| lixianyu | 0:a4d8f5b3c546 | 7 | |
| lixianyu | 0:a4d8f5b3c546 | 8 | |
| lixianyu | 0:a4d8f5b3c546 | 9 | public: |
| lixianyu | 0:a4d8f5b3c546 | 10 | |
| lixianyu | 0:a4d8f5b3c546 | 11 | //Constants used in some of the functions below |
| lixianyu | 0:a4d8f5b3c546 | 12 | #define AUTOMATIC 1 |
| lixianyu | 0:a4d8f5b3c546 | 13 | #define MANUAL 0 |
| lixianyu | 0:a4d8f5b3c546 | 14 | #define DIRECT 0 |
| lixianyu | 0:a4d8f5b3c546 | 15 | #define REVERSE 1 |
| lixianyu | 0:a4d8f5b3c546 | 16 | |
| lixianyu | 0:a4d8f5b3c546 | 17 | //commonly used functions ************************************************************************** |
| lixianyu | 0:a4d8f5b3c546 | 18 | PID(double, double, double, int); // Setpoint. Initial tuning parameters are also set here |
| lixianyu | 0:a4d8f5b3c546 | 19 | |
| lixianyu | 0:a4d8f5b3c546 | 20 | void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0) |
| lixianyu | 0:a4d8f5b3c546 | 21 | |
| lixianyu | 0:a4d8f5b3c546 | 22 | float Compute(float, float); // * performs the PID calculation. it should be |
| lixianyu | 0:a4d8f5b3c546 | 23 | // called every time loop() cycles. ON/OFF and |
| lixianyu | 0:a4d8f5b3c546 | 24 | // calculation frequency can be set using SetMode |
| lixianyu | 0:a4d8f5b3c546 | 25 | // SetSampleTime respectively |
| lixianyu | 0:a4d8f5b3c546 | 26 | |
| lixianyu | 0:a4d8f5b3c546 | 27 | void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but |
| lixianyu | 0:a4d8f5b3c546 | 28 | //it's likely the user will want to change this depending on |
| lixianyu | 0:a4d8f5b3c546 | 29 | //the application |
| lixianyu | 0:a4d8f5b3c546 | 30 | |
| lixianyu | 0:a4d8f5b3c546 | 31 | |
| lixianyu | 0:a4d8f5b3c546 | 32 | void SetITermLimits(double, double); |
| lixianyu | 0:a4d8f5b3c546 | 33 | //available but not commonly used functions ******************************************************** |
| lixianyu | 0:a4d8f5b3c546 | 34 | void SetTunings(double, double, // * While most users will set the tunings once in the |
| lixianyu | 0:a4d8f5b3c546 | 35 | double); // constructor, this function gives the user the option |
| lixianyu | 0:a4d8f5b3c546 | 36 | // of changing tunings during runtime for Adaptive control |
| lixianyu | 0:a4d8f5b3c546 | 37 | void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT |
| lixianyu | 0:a4d8f5b3c546 | 38 | // means the output will increase when error is positive. REVERSE |
| lixianyu | 0:a4d8f5b3c546 | 39 | // means the opposite. it's very unlikely that this will be needed |
| lixianyu | 0:a4d8f5b3c546 | 40 | // once it is set in the constructor. |
| lixianyu | 0:a4d8f5b3c546 | 41 | void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which |
| lixianyu | 0:a4d8f5b3c546 | 42 | // the PID calculation is performed. default is 100 |
| lixianyu | 0:a4d8f5b3c546 | 43 | |
| lixianyu | 0:a4d8f5b3c546 | 44 | |
| lixianyu | 0:a4d8f5b3c546 | 45 | |
| lixianyu | 0:a4d8f5b3c546 | 46 | //Display functions **************************************************************** |
| lixianyu | 0:a4d8f5b3c546 | 47 | double GetKp(); // These functions query the pid for interal values. |
| lixianyu | 0:a4d8f5b3c546 | 48 | double GetKi(); // they were created mainly for the pid front-end, |
| lixianyu | 0:a4d8f5b3c546 | 49 | double GetKd(); // where it's important to know what is actually |
| lixianyu | 0:a4d8f5b3c546 | 50 | int GetMode(); // inside the PID. |
| lixianyu | 0:a4d8f5b3c546 | 51 | int GetDirection(); // |
| lixianyu | 0:a4d8f5b3c546 | 52 | |
| lixianyu | 0:a4d8f5b3c546 | 53 | private: |
| lixianyu | 0:a4d8f5b3c546 | 54 | void Initialize(); |
| lixianyu | 0:a4d8f5b3c546 | 55 | |
| lixianyu | 0:a4d8f5b3c546 | 56 | double dispKp; // * we'll hold on to the tuning parameters in user-entered |
| lixianyu | 0:a4d8f5b3c546 | 57 | double dispKi; // format for display purposes |
| lixianyu | 0:a4d8f5b3c546 | 58 | double dispKd; // |
| lixianyu | 0:a4d8f5b3c546 | 59 | |
| lixianyu | 0:a4d8f5b3c546 | 60 | double kp; // * (P)roportional Tuning Parameter |
| lixianyu | 0:a4d8f5b3c546 | 61 | double ki; // * (I)ntegral Tuning Parameter |
| lixianyu | 0:a4d8f5b3c546 | 62 | double kd; // * (D)erivative Tuning Parameter |
| lixianyu | 0:a4d8f5b3c546 | 63 | |
| lixianyu | 0:a4d8f5b3c546 | 64 | int controllerDirection; |
| lixianyu | 0:a4d8f5b3c546 | 65 | |
| lixianyu | 0:a4d8f5b3c546 | 66 | float *myInput; // * Pointers to the Input, Output, and Setpoint variables |
| lixianyu | 0:a4d8f5b3c546 | 67 | float *myOutput; // This creates a hard link between the variables and the |
| lixianyu | 0:a4d8f5b3c546 | 68 | float *mySetpoint; // PID, freeing the user from having to constantly tell us |
| lixianyu | 0:a4d8f5b3c546 | 69 | // what these values are. with pointers we'll just know. |
| lixianyu | 0:a4d8f5b3c546 | 70 | |
| lixianyu | 0:a4d8f5b3c546 | 71 | unsigned long lastTime; |
| lixianyu | 0:a4d8f5b3c546 | 72 | double ITerm, lastInput; |
| lixianyu | 0:a4d8f5b3c546 | 73 | |
| lixianyu | 0:a4d8f5b3c546 | 74 | unsigned long SampleTime; |
| lixianyu | 0:a4d8f5b3c546 | 75 | double outMin, outMax; |
| lixianyu | 0:a4d8f5b3c546 | 76 | double errorMin, errorMax; |
| lixianyu | 0:a4d8f5b3c546 | 77 | bool inAuto; |
| lixianyu | 0:a4d8f5b3c546 | 78 | }; |
| lixianyu | 0:a4d8f5b3c546 | 79 | #endif |
| lixianyu | 0:a4d8f5b3c546 | 80 | |
| lixianyu | 0:a4d8f5b3c546 | 81 |
