Rev 2277 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2262 | linlun | 1 | #ifndef PID_v1_h |
| 2 | #define PID_v1_h |
||
| 3 | |||
| 4 | typedef float FloatType; |
||
| 5 | //typedef double floatType; |
||
| 6 | #include <stdbool.h> |
||
| 7 | #include <stdio.h> |
||
| 8 | |||
| 9 | //Constants used in some of the functions below |
||
| 10 | |||
| 11 | typedef enum |
||
| 12 | { |
||
| 13 | PID_Mode_Automatic = 1, |
||
| 14 | PID_Mode_Manual = 0 |
||
| 15 | } PidModeType; |
||
| 16 | |||
| 17 | typedef enum |
||
| 18 | { |
||
| 19 | PID_Direction_Direct = 0, |
||
| 20 | PID_Direction_Reverse = 1 |
||
| 21 | } PidDirectionType; |
||
| 22 | |||
| 23 | static const FloatType MAX_KD_MIN = 0.01; // Greater than 0 to avoid division by 0 in calculation of Kd term |
||
| 24 | |||
| 25 | typedef struct{ |
||
| 26 | FloatType dispKp; // * we'll hold on to the tuning parameters in user-entered |
||
| 27 | FloatType dispKi; // format for display purposes |
||
| 28 | FloatType dispKd; // |
||
| 29 | |||
| 30 | FloatType kp; // * (P)roportional Tuning Parameter |
||
| 31 | FloatType ki; // * (I)ntegral Tuning Parameter |
||
| 32 | FloatType kd; // * (D)erivative Tuning Parameter |
||
| 33 | |||
| 34 | FloatType PTerm; // * (P)roportional term contributing to the output |
||
| 35 | FloatType ITerm; // * (I)ntegral term contributing to the output |
||
| 36 | FloatType DTerm; // * (D)erivative term contributing to the output |
||
| 37 | |||
| 38 | FloatType MaxKd; // * Maximum derivative gain. |
||
| 39 | // set to 10-20 to avoid applying derivation |
||
| 40 | // to high frequency measurement noise. |
||
| 41 | PidModeType mode; |
||
| 42 | PidDirectionType controllerDirection; |
||
| 43 | |||
| 44 | FloatType* myInput; // * Pointers to the Input, Output, and Setpoint variables |
||
| 45 | FloatType* myOutput; // This creates a hard link between the variables and the |
||
| 46 | FloatType* mySetpoint; // PID, freeing the user from having to constantly tell us |
||
| 47 | // what these values are. with pointers we'll just know. |
||
| 48 | |||
| 49 | uint32_t lastTime; |
||
| 50 | FloatType lastInput; |
||
| 51 | |||
| 52 | uint32_t SampleTime; |
||
| 53 | FloatType outMin, outMax; |
||
| 54 | }PidType; |
||
| 55 | |||
| 56 | // commonly used functions ************************************************************************** |
||
| 57 | |||
| 58 | // constructor. links the PID to the Input, Output, and |
||
| 59 | // Setpoint. Initial tuning parameters are also set here |
||
| 60 | void PID_init(PidType* pid, |
||
| 61 | FloatType* Input, |
||
| 62 | FloatType* Output, |
||
| 63 | FloatType* Setpoint, |
||
| 64 | FloatType kp, |
||
| 65 | FloatType ki, |
||
| 66 | FloatType kd, |
||
| 67 | PidDirectionType controllerDirection); |
||
| 68 | |||
| 69 | // sets PID to either Manual (0) or Auto (non-0) |
||
| 70 | void PID_SetMode(PidType* pid, PidModeType mode); |
||
| 71 | |||
| 72 | // performs the PID calculation. it should be |
||
| 73 | // called every time loop() cycles. ON/OFF and |
||
| 74 | // calculation frequency can be set using SetMode |
||
| 75 | // SetSampleTime respectively |
||
| 76 | bool PID_Compute(PidType* pid); |
||
| 77 | |||
| 78 | // clamps the output to a specific range. 0-255 by default, but |
||
| 79 | // it's likely the user will want to change this depending on |
||
| 80 | // the application |
||
| 81 | void PID_SetOutputLimits(PidType* pid, FloatType min, FloatType max); |
||
| 82 | // available but not commonly used functions ******************************************************** |
||
| 83 | |||
| 84 | // While most users will set the tunings once in the |
||
| 85 | // constructor, this function gives the user the option |
||
| 86 | // of changing tunings during runtime for Adaptive control |
||
| 87 | void PID_SetTunings(PidType* pid, FloatType kp, FloatType ki, FloatType kd); |
||
| 88 | |||
| 89 | // Sets the Direction, or "Action" of the controller. DIRECT |
||
| 90 | // means the output will increase when error is positive. REVERSE |
||
| 91 | // means the opposite. it's very unlikely that this will be needed |
||
| 92 | // once it is set in the constructor. |
||
| 93 | void PID_SetControllerDirection(PidType* pid, PidDirectionType Direction); |
||
| 94 | |||
| 95 | // sets the frequency, in Milliseconds, with which |
||
| 96 | // the PID calculation is performed. default is 100 |
||
| 97 | void PID_SetSampleTime(PidType* pid, uint32_t newSampleTime); |
||
| 98 | |||
| 99 | // set the maximum derivative gain. default is 10. |
||
| 100 | void PID_SetMaxKd(PidType* pid, FloatType newMaxKd); |
||
| 101 | |||
| 102 | //Display functions **************************************************************** |
||
| 103 | // These functions query the pid for interal values. |
||
| 104 | // they were created mainly for the pid front-end, |
||
| 105 | // where it's important to know what is actually |
||
| 106 | // inside the PID. |
||
| 107 | FloatType PID_GetKp(PidType* pid); |
||
| 108 | FloatType PID_GetKi(PidType* pid); |
||
| 109 | FloatType PID_GetKd(PidType* pid); |
||
| 2277 | linlun | 110 | FloatType PID_GetMin(PidType* pid); |
| 111 | FloatType PID_GetMax(PidType* pid); |
||
| 2283 | linlun | 112 | uint32_t PID_GetSampleTime(PidType* pid); |
| 2262 | linlun | 113 | PidModeType PID_GetMode(PidType* pid); |
| 114 | PidDirectionType PID_GetDirection(PidType* pid); |
||
| 115 | FloatType PID_GetPTerm(PidType* pid); |
||
| 116 | FloatType PID_GetITerm(PidType* pid); |
||
| 117 | FloatType PID_GetDTerm(PidType* pid); |
||
| 118 | |||
| 119 | #endif |
||
| 120 | |||
| 121 |