Skip to content

improve anti-windup #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/common/pid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,31 @@ float PIDController::operator() (float error){
// u_p = P *e(k)
float proportional = P * error;
// Tustin transform of the integral part
// u_ik = u_ik_1 + I*Ts/2*(ek + ek_1)
float integral = integral_prev + I*Ts*0.5*(error + error_prev);
// antiwindup - limit the output
integral = _constrain(integral, -limit, limit);
// u_ik = u_ik_1 + I*Ts*e(k)
float integral = integral_prev + I*Ts*error;
// Discrete derivation
// u_dk = D(ek - ek_1)/Ts
float derivative = D*(error - error_prev)/Ts;

// sum all the components
float output = proportional + integral + derivative;
// antiwindup - limit the output variable
output = _constrain(output, -limit, limit);


// limit the acceleration by ramping the output
float output_rate = (output - output_prev)/Ts;
if (output_rate > output_ramp)
output = output_prev + output_ramp*Ts;
else if (output_rate < -output_ramp)
output = output_prev - output_ramp*Ts;

// limit abs value of output and antiwindup for integrator
if (output > limit) {
output = limit;
integral = output - proportional;
} else if (output < -limit) {
output = -limit;
integral = output - proportional;
}

// saving for the next pass
integral_prev = integral;
output_prev = output;
Expand Down