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