%  Figure 4.16      Feedback Control of Dynamic Systems, 8e
%                   Franklin, Powell, Emami
% Figure 4.16   PID control of motor speed
% Example 4.6
% 
clf; 

K=.0670; L1=0.1; J1=0.0113; R=0.45; b=0.0280;

kp=3; ki= 15; kd=0.3;

np=K;
dp=[L1*J1 R*J1+b*L1 R*b+K*K];

dclp=[L1*J1 R*J1+b*L1 R*b+K*K+K*kp];
nclp=K*kp;
nclpw=[L1 R];

dclpi=[L1*J1 R*J1+b*L1 R*b+K*K+K*kp K*ki];
nclpi=[K*kp K*ki];
nclpiw=[L1 R 0];

dclpid=[  L1*J1 R*J1+b*L1+K*kd R*b+K*K+K*kp K*ki];
nclpid=[K*kd K*kp K*ki];
nclpidw=[L1 R 0];

sysp=tf(nclp,dclp);
syspw=tf(nclpw,dclp);

syspi=tf(nclpi,dclpi);
syspiw=tf(nclpiw,dclpi);

syspid=tf(nclpid,dclpid);
syspidw=tf(nclpidw,dclpid);

figure(1)

t=0:0.01:6;
[y1w,t]=step(syspw,t);
[y2w,t]=step(syspiw,t);
[y3w,t]=step(syspidw,t);

plot(t,y1w,t,y2w,t,y3w,'LineWidth',2);
xlabel('Time (msec)');
ylabel('\it{y(t)}');
title('Fig. 4.16(a): Response of P, PI, and PID control to a disturbance step')
text(0.3,5,'P')
text(2,1,'PI')
text(0.5,-1','PID')
grid on;

figure(2)

[y1,t]=step(sysp,t);
[y2,t]=step(syspi,t);
[y3,t]=step(syspid,t);

plot(t,y1,t,y2,t,y3,'LineWidth',2);
xlabel('Time (msec)');
ylabel('\it {y(t)}');
title('Fig. 4.16(b): Response of P, PI, and PID control to a reference step')
text(0.4,0.7,'P')
text(2,1.2,'PI')
text(0.5,1,'PID')
grid on;