글
#ifndef PID_H_ #define PID_H_ #include //Define parameter #define epsilon 0.01 #define dt 0.01 //100ms loop time #define MAX 4 //For Current Saturation #define MIN -4 #define Kp 0.1 #define Kd 0.01 #define Ki 0.005 float PIDcal(float setpoint,float actual_position) { static float pre_error = 0; static float integral = 0; float error; float derivative; float output; //Caculate P,I,D error = setpoint - actual_position; //In case of error too small then stop intergration if(abs(error) > epsilon) { integral = integral + error*dt; } derivative = (error - pre_error)/dt; output = Kp*error + Ki*integral + Kd*derivative; //Saturation Filter if(output > MAX) { output = MAX; } else if(output < MIN) { output = MIN; } //Update error pre_error = error; return output; } #endif /*PID_H_*/
[smartads] [smartads]
The Matlab simulation of the PID algorithm to control the DC motor
and then embedded PID code:
function output = fcn(error) %Declare static value persistent pre_error; if isempty(pre_error) pre_error = 0; end persistent integral; if isempty(integral) integral = 0; end %Constant Value epsilon = 0.01; dt = 0.01; Kp = 5; Kd = 3; Ki = 0.01; if(abs(error) > epsilon) integral = integral + error*dt; end derivative = (error - pre_error)/dt; output = Kp*error + Ki*integral + Kd*derivative; pre_error = error;
And result for the simulation
The complete code can be download here:
http://www.mediafire.com/?kati3avzw8b17d9
source from http://www.embeddedheaven.com/pid-control-algorithm-c-language.htm.
How can get Kp,Ki,Kd??
Ans)
effective PID parameters are a long story.Manually, you select Kp, first and let's Kd, Ki small.You increase Kp until you get expected setting-time and steady-state error, the more Kp smaller steady-state error and the shorter setting-time.However the more Kp also lead to more overshoot in order to reduce overshoot you increase Kd.Of course, you have to trade-off between Kp and Kd, or setting-time and steady-state versus overshoot.
After all, slightly increase Ki ...
RECENT COMMENT