New PID library with digital anti-windup and process control

Fork of PID_modified by Chun Feng Huang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PID.h Source File

PID.h

00001 #ifndef PID_H
00002 #define PID_H
00003 //
00004 #include "FILTER_LIB.h"
00005 
00006 class PID{
00007 public:
00008 
00009     // Sampling time
00010     float Ts;
00011     //
00012     bool enable;
00013 
00014     // Flags
00015     bool is_limiting_command;
00016     bool is_limiting_output;
00017     //
00018     bool is_using_integral; // Determine if the integral control is going to be used.
00019     bool is_using_derivative; // Determine if the derivative control is going to be used.
00020     //
00021     bool is_using_outSource_d_error; // Determine whether using the signal for d_error or using numerical derivative to derive d_error from error.
00022     //
00023     bool is_antiWindUp;
00024 
00025     // Parameters
00026     // Feedback gain
00027     float Kp;
00028     float Ki;
00029     float Kd;
00030     //
00031     // float Ka;
00032 
00033     // States
00034     float error;
00035     float d_error;
00036     float error_int;
00037     //
00038     float error_int_increment; // error_int += error_int_increment;
00039 
00040     // Input signal
00041     float command;
00042     float feedbackValue;
00043     // Output signal
00044     float output;
00045     // Error by saturation
00046     float delta_output; // Error by saturation
00047 
00048 
00049     PID(float Kp_in, float Ki_in, float Kd_in,  float Sampletime_in);
00050     // Process controller
00051     void start(); // Run
00052     void pause(); // Stop updating but no reset
00053     void stop(); // Stop and reset
00054     void reset(); // Reset all the states to initial values.
00055     //
00056     void EnableAntiWindUp(float Ka_in);
00057     //
00058     void set_PID_gains(float Kp_in, float Ki_in, float Kd_in); // Setting Kp, Ki, and Kd
00059     void SetInputLimits(float inputLimits_H_in, float inputLimits_L_in);
00060     void SetOutputLimits(float outputLimits_H_in, float outputLimits_L_in);
00061 
00062     // Main function for computing the PID
00063     void set_d_error(float d_error_in); // Insert d_error before iteration.
00064     void iterateOnce(float command_in, float feedbackValue_in);
00065     void iterateOnce_noAntiWindUP(float command_in, float feedbackValue_in);
00066 
00067     // Anti-windup method
00068     // Method 1: Separated operation for anti-windup
00069     void Saturation_output();
00070     void AntiWindUp(float delta); // delta_V = V - V_sat
00071     // Method 2: Single anti-windup operation
00072     void Saturation_AntiWindUp();
00073     //
00074 
00075 private:
00076 
00077     // Derivative
00078     Derivative_appr derivative_error;
00079 
00080     // Saturation
00081     Saturation SAT_command;
00082     Saturation SAT_output;
00083 
00084     // Small over-bound value for numerical stability
00085     float overBound_value; // Small positive value
00086 
00087 };
00088 
00089 #endif /* PID_H*/