%PID controller example
%Z-N tuning rule
%Z-N and C-C tuning rules
%step test
%test process one--first order plus delay model
%PI controller design

%PID controller design using pole-assignment technique
num=-0.1;
den=conv([1 -1],[1 1]);
b1=0;
b0=-0.1;
a1=0;    
a0=-1;
%closed-loop performance specification
xi=0.707;
wn=10;
lambda1=10;
Acl_2=[1 2*xi*wn wn^2];  %s2+2xiwns+wn^2
Acl=conv(Acl_2,[1 2*lambda1 lambda1^2]); % put together closed-loop poles. 4th order poly.
[Kc,tauI,tauD,tauf]=pidplace(a1,a0,b1,b0,Acl)
Tsim=3;
deltat=0.001;
sim('PIDController_A');
y1=y;
u1=u;
t1=t;
figure(1)
subplot(211)
%hold on

plot(t,y1,'LineWidth',2,'Color',[.6 0 0])
axis([-0 3 -1 2])
set(gca,'FontSize',20,'FontName','helvetica');
ylabel('Output')
xlabel('Time (sec)')
subplot(212)

plot(t,u1,'LineWidth',2,'Color',[.6 0 0])
axis([-0 3 -450 450])
set(gca,'FontSize',20,'FontName','helvetica');
ylabel('Control')
xlabel('Time (sec)')
%return

sim('PIDController');
%subplot(212)
figure(2)
subplot(211)
plot(t,y,'LineWidth',2,'Color',[.6 0 0])

set(gca,'FontSize',20,'FontName','helvetica');
ylabel('Output')
xlabel('Time (sec)')
axis([-0 3 -1 2])
subplot(212)

plot(t,u,'LineWidth',2,'Color',[.6 0 0])
axis([-0 3 -950 450])
set(gca,'FontSize',20,'FontName','helvetica');
ylabel('Control')
xlabel('Time (sec)')

figure(3)
plot(t,y1,'LineWidth',2,'Color',[.6 0 0])
hold on 
plot(t,y)
set(gca,'FontSize',20,'FontName','helvetica');
legend('1','2')
ylabel('Output')
xlabel('Time (sec)')
axis([-0 3 -1 2])
figure(4)
plot(t,u1,'LineWidth',2,'Color',[.6 0 0])
hold on 
plot(t,u)
set(gca,'FontSize',20,'FontName','helvetica');
legend('1','2')
ylabel('Control')
xlabel('Time (sec)')
axis([-0 3 -950 450])
