New PID library with digital anti-windup and process control
Fork of PID_modified by
PID.h@7:6f0e5de35b48, 2017-03-30 (annotated)
- Committer:
- benson516
- Date:
- Thu Mar 30 07:52:35 2017 +0000
- Revision:
- 7:6f0e5de35b48
- Parent:
- 5:016c99bb877f
New PID library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
weisnail | 0:7f9b4ca968ae | 1 | #ifndef PID_H |
weisnail | 0:7f9b4ca968ae | 2 | #define PID_H |
benson516 | 7:6f0e5de35b48 | 3 | // |
benson516 | 7:6f0e5de35b48 | 4 | #include "FILTER_LIB.h" |
weisnail | 0:7f9b4ca968ae | 5 | |
weisnail | 0:7f9b4ca968ae | 6 | class PID{ |
benson516 | 7:6f0e5de35b48 | 7 | public: |
benson516 | 7:6f0e5de35b48 | 8 | |
benson516 | 7:6f0e5de35b48 | 9 | // Sampling time |
benson516 | 7:6f0e5de35b48 | 10 | float Ts; |
benson516 | 7:6f0e5de35b48 | 11 | // |
benson516 | 7:6f0e5de35b48 | 12 | bool enable; |
benson516 | 7:6f0e5de35b48 | 13 | |
benson516 | 7:6f0e5de35b48 | 14 | // Flags |
benson516 | 7:6f0e5de35b48 | 15 | bool is_limiting_command; |
benson516 | 7:6f0e5de35b48 | 16 | bool is_limiting_output; |
benson516 | 7:6f0e5de35b48 | 17 | // |
benson516 | 7:6f0e5de35b48 | 18 | bool is_using_integral; // Determine if the integral control is going to be used. |
benson516 | 7:6f0e5de35b48 | 19 | bool is_using_derivative; // Determine if the derivative control is going to be used. |
benson516 | 7:6f0e5de35b48 | 20 | // |
benson516 | 7:6f0e5de35b48 | 21 | bool is_using_outSource_d_error; // Determine whether using the signal for d_error or using numerical derivative to derive d_error from error. |
benson516 | 7:6f0e5de35b48 | 22 | // |
benson516 | 7:6f0e5de35b48 | 23 | bool is_antiWindUp; |
benson516 | 7:6f0e5de35b48 | 24 | |
benson516 | 7:6f0e5de35b48 | 25 | // Parameters |
benson516 | 7:6f0e5de35b48 | 26 | // Feedback gain |
benson516 | 7:6f0e5de35b48 | 27 | float Kp; |
benson516 | 7:6f0e5de35b48 | 28 | float Ki; |
benson516 | 7:6f0e5de35b48 | 29 | float Kd; |
benson516 | 7:6f0e5de35b48 | 30 | // |
benson516 | 7:6f0e5de35b48 | 31 | // float Ka; |
benson516 | 7:6f0e5de35b48 | 32 | |
benson516 | 7:6f0e5de35b48 | 33 | // States |
benson516 | 7:6f0e5de35b48 | 34 | float error; |
benson516 | 7:6f0e5de35b48 | 35 | float d_error; |
benson516 | 7:6f0e5de35b48 | 36 | float error_int; |
benson516 | 7:6f0e5de35b48 | 37 | // |
benson516 | 7:6f0e5de35b48 | 38 | float error_int_increment; // error_int += error_int_increment; |
weisnail | 0:7f9b4ca968ae | 39 | |
benson516 | 7:6f0e5de35b48 | 40 | // Input signal |
benson516 | 7:6f0e5de35b48 | 41 | float command; |
benson516 | 7:6f0e5de35b48 | 42 | float feedbackValue; |
benson516 | 7:6f0e5de35b48 | 43 | // Output signal |
benson516 | 7:6f0e5de35b48 | 44 | float output; |
benson516 | 7:6f0e5de35b48 | 45 | // Error by saturation |
benson516 | 7:6f0e5de35b48 | 46 | float delta_output; // Error by saturation |
benson516 | 7:6f0e5de35b48 | 47 | |
benson516 | 7:6f0e5de35b48 | 48 | |
benson516 | 7:6f0e5de35b48 | 49 | PID(float Kp_in, float Ki_in, float Kd_in, float Sampletime_in); |
benson516 | 7:6f0e5de35b48 | 50 | // Process controller |
benson516 | 7:6f0e5de35b48 | 51 | void start(); // Run |
benson516 | 7:6f0e5de35b48 | 52 | void pause(); // Stop updating but no reset |
benson516 | 7:6f0e5de35b48 | 53 | void stop(); // Stop and reset |
benson516 | 7:6f0e5de35b48 | 54 | void reset(); // Reset all the states to initial values. |
benson516 | 7:6f0e5de35b48 | 55 | // |
benson516 | 7:6f0e5de35b48 | 56 | void EnableAntiWindUp(float Ka_in); |
benson516 | 7:6f0e5de35b48 | 57 | // |
benson516 | 7:6f0e5de35b48 | 58 | void set_PID_gains(float Kp_in, float Ki_in, float Kd_in); // Setting Kp, Ki, and Kd |
benson516 | 7:6f0e5de35b48 | 59 | void SetInputLimits(float inputLimits_H_in, float inputLimits_L_in); |
benson516 | 7:6f0e5de35b48 | 60 | void SetOutputLimits(float outputLimits_H_in, float outputLimits_L_in); |
weisnail | 0:7f9b4ca968ae | 61 | |
benson516 | 7:6f0e5de35b48 | 62 | // Main function for computing the PID |
benson516 | 7:6f0e5de35b48 | 63 | void set_d_error(float d_error_in); // Insert d_error before iteration. |
benson516 | 7:6f0e5de35b48 | 64 | void iterateOnce(float command_in, float feedbackValue_in); |
benson516 | 7:6f0e5de35b48 | 65 | void iterateOnce_noAntiWindUP(float command_in, float feedbackValue_in); |
benson516 | 7:6f0e5de35b48 | 66 | |
benson516 | 7:6f0e5de35b48 | 67 | // Anti-windup method |
benson516 | 7:6f0e5de35b48 | 68 | // Method 1: Separated operation for anti-windup |
benson516 | 7:6f0e5de35b48 | 69 | void Saturation_output(); |
benson516 | 7:6f0e5de35b48 | 70 | void AntiWindUp(float delta); // delta_V = V - V_sat |
benson516 | 7:6f0e5de35b48 | 71 | // Method 2: Single anti-windup operation |
benson516 | 7:6f0e5de35b48 | 72 | void Saturation_AntiWindUp(); |
benson516 | 7:6f0e5de35b48 | 73 | // |
benson516 | 7:6f0e5de35b48 | 74 | |
benson516 | 7:6f0e5de35b48 | 75 | private: |
benson516 | 7:6f0e5de35b48 | 76 | |
benson516 | 7:6f0e5de35b48 | 77 | // Derivative |
benson516 | 7:6f0e5de35b48 | 78 | Derivative_appr derivative_error; |
benson516 | 7:6f0e5de35b48 | 79 | |
benson516 | 7:6f0e5de35b48 | 80 | // Saturation |
benson516 | 7:6f0e5de35b48 | 81 | Saturation SAT_command; |
benson516 | 7:6f0e5de35b48 | 82 | Saturation SAT_output; |
benson516 | 7:6f0e5de35b48 | 83 | |
benson516 | 7:6f0e5de35b48 | 84 | // Small over-bound value for numerical stability |
benson516 | 7:6f0e5de35b48 | 85 | float overBound_value; // Small positive value |
weisnail | 0:7f9b4ca968ae | 86 | |
weisnail | 0:7f9b4ca968ae | 87 | }; |
weisnail | 0:7f9b4ca968ae | 88 | |
benson516 | 7:6f0e5de35b48 | 89 | #endif /* PID_H*/ |