%PI_controller.m
%PI Controller


clc
%echo on

%Plant
fprintf('\nPLANT: G(s)=A/(s^2+s+1)\n')
A=1
numG=[A];
denG=[1 1 1];
zG=roots(numG)
pG=roots(denG)
sysG=tf(numG,denG)
figure(1)
pzmap(sysG)


fprintf('\nPress a key to continue ... \n')
pause

while 1
   Kp=input('\nEnter proportional gain (0 to end): ');
   if Kp==0
      break;
   end
   Ki=input('\nEnter integral gain: ');
   numK=[Kp Ki];
   denK=[1 0];
   sysK=tf(numK,denK)

   %Series connection
   sys_series=series(sysK,sysG);

   %Feeback connection
   sys_feedback=feedback(sys_series,1)

   [Z_feedback,P_feedback,K_feedback]=zpkdata(sys_feedback,'v')
   figure(2)
   pzmap(sys_feedback)

   fprintf('\nPress a key to continue ... \n')
   pause

   tfinal=input('\nEnter simulation time: ');
   [y,t]=step(sys_feedback,tfinal);

   axis_type=input('\nEnter axis type (fixed=1, variable=0): ')
   figure(3)
   plot(t,y,'r-',t,ones(size(t)),'b:')
   title(['STEP RESPONSE - PI CONTROLLER: K_{p}=',num2str(Kp),' K_{I}=',num2str(Ki)])
   if axis_type==1
      axis([0 tfinal 0 2])
   else
      axis([0 tfinal -Inf Inf])
   end
end

%echo off

