%Simulation of PI control of an AFM in tapping mode

aminit;
global k ki T

tmax=10;dt=1e-3;t=0:dt:10;
T=0.2513;
kc = 21.67;Tc = 0.4798;
%Ziegler-Nichols tuning
kzn=0.4*kc;Tizn=0.8*Tc;kizn=kzn/Tizn;
k=kzn;ki=kizn;
[t,x,yzn]=sim('afmtap',tmax);

%Modified tuning
ktk=0.16*kc;kappa=1/kc;
Titk=Tc/(1+kappa);kitk=ktk/Titk;
k=ktk;ki=kitk;
[t,x,ytk]=sim('afmtap',tmax);

%Plot data
figure(1); clf; subplot(421);
plot(t,yzn(:,1),'b--',t,ytk(:,1),'b-','Linewidth',AM_data_linewidth);
ylabel('y');
amaxis([0 10 0 1.5])

lgh = legend('Ziegler-Nichols', 'Modified ZN', 'Location', 'southeast');
legend(lgh, 'boxoff');

subplot(423);
plot(t,yzn(:,2),'b--',t,ytk(:,2),'b-','Linewidth',AM_data_linewidth);
ylabel('u');xlabel('t');
amaxis([0 10 0 15]);


amprint('afmtap-step.eps');
