Chapter 4
Chapter 4
We can define a PID controller in MATLAB using the transfer function directly:
Kp = 1;
Ki = 1;
Kd = 1;
s = tf('s');
C = Kp + Ki/s + Kd*s
s = tf('s');
P_cruise = 1/(m*s + b);
Kp = 100;
C = pid(Kp);
T = feedback(C*P_cruise,1)
t = 0:0.1:20;
step(r*T,t)
axis([0 20 0 10])
You can increase the proportional gain, K p, to reduce the rise time and the steady-
state error. Change the existing m-file so that K p equals 5000 and rerun it in the
MATLAB command window
Kp = 5000;
C = pid(Kp);
T = feedback(C*P_cruise,1);
step(r*T,t)
axis([0 20 0 10])
PI control
The closed-loop transfer function of this cruise control system with a PI controller
K p s+ K i
T ( s )= 2
m s + ( b+ K p ) s+ K i
T = feedback(C*P_cruise,1);
step(r*T,t)
When K p equals 800 and K p equals 40
Kp = 800;
Ki = 40;
C = pid(Kp,Ki);
T = feedback(C*P_cruise,1);
step(r*T,t)
PID control
2
K p s+ K i + K d s
T ( s )= 2
(m+ K d )s + ( b+ K p ) s+ K i
T = feedback(C*P_cruise,1);
Proportional control
employing a proportional controller with a gain of 100, that is, C(s) = 100
Kp = 100;
C = pid(Kp);
sys_cl = feedback(C*P_motor,1)
t = 0:0.01:5;
step(sys_cl,t)
grid
title('Step Response with Proportional Control')
PID control
Kp = 75;
Ki = 1;
Kd = 1;
C = pid(Kp,Ki,Kd);
sys_cl = feedback(C*P_motor,1);
step(sys_cl,[0:1:200])
title('PID Control with Small Ki and Small Kd')
Tuning the gains
This process can be sped up by increasing the value of . Go back to your m-file
and change to 200 as in the following
Kp = 100;
Ki = 200;
Kd = 1;
C = pid(Kp,Ki,Kd);
sys_cl = feedback(C*P_motor,1);
step(sys_cl)
grid
title('PID Control with Large Ki and Small Kd')
Stepinfo:
As we had hoped, the increased Kd reduced the resulting overshoot. Now we know
that if we use a PID controller with Kp = 100, Ki = 200, and Kd = 10, all of our
design requirements will be satisfied.
J = 0.01;
b = 0.1;
K = 0.01;
R = 1;
L = 0.5;
s = tf('s');
P = K/(s*((J*s+b)*(L*s+R)+K^2))
Proportional control
Let's first try using a proportional controller with gain ranging from 1 to 21.
Kp = 1;
for i = 1:3
C(:,:,i) = pid(Kp);
Kp = Kp + 10;
end
sys_cl = feedback(C*P_motor,1);
sys_cl = feedback(C*P_motor,1);
t = 0:0.001:0.2;
step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t)
ylabel('Position, \theta (radians)')
title('Response to a Step Reference with Different Values of K_p')
legend('Kp = 1', 'Kp = 11', 'Kp = 21')
dist_cl = feedback(P_motor,C);
step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t)
ylabel('Position, \theta (radians)')
title('Response to a Step Disturbance with Different Values of K_p')
legend('Kp = 1', 'Kp = 11','Kp = 21')
PI control
We will set = 21 and test integral gains ranging from 100 to 500.
Kp = 21;
Ki = 100;
for i = 1:5
C(:,:,i) = pid(Kp,Ki);
Ki = Ki + 200;
end
sys_cl = feedback(C*P_motor,1);
t = 0:0.001:0.4;
step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t)
ylabel('Position, \theta (radians)')
title('Response to a Step Reference with K_p = 21 and Different Values of
K_i')
legend('Ki = 100', 'Ki = 300', 'Ki = 500')
step disturbance response.
dist_cl = feedback(P_motor,C);
step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t)
ylabel('Position, \theta (radians)')
title('Response to a Step Disturbance with K_p = 21 and Different Values of
K_i')
legend('Ki = 100', 'Ki = 300', 'Ki = 500')
PID control
Adding a derivative term to the controller means that we now have all three terms
of the PID controller. We will investigate derivative gains ranging from 0.05 to
0.25.
Kp = 21;
Ki = 500;
Kd = 0.05;
for i = 1:3
C(:,:,i) = pid(Kp,Ki,Kd);
Kd = Kd + 0.1;
end
sys_cl = feedback(C*P_motor,1);
t = 0:0.001:0.1;
step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t)
ylabel('Position, \theta (radians)')
title('Step Response with K_p = 21, K_i = 500 and Different Values of K_d')
legend('Kd = 0.05', 'Kd = 0.15', 'Kd = 0.25')
see what happened to the step disturbance response
dist_cl = feedback(P_motor,C);
t = 0:0.001:0.2;
step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t)
ylabel('Position, \theta (radians)')
title('Step Response with K_p = 21, K_i = 500 and Different values of K_d')
legend('Kd = 0.05', 'Kd = 0.15', 'Kd = 0.25')