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