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