%PID_TUNING_PFC2
%this  is a sort of joke to approach old users with a procedure 
%understandable by any beginner in control
%We have the simplest first order process : gain/time constant
%Cautious,  approach not to perturb old PID experts% 
%How to tune a PI controller in front of a simple first order process
% every operator in industry has his own method ( the best of course !)
% here the target is to propose a new  (again !!) procedure where,
% the gain of the process is estimated with the time constant
% The beauty of PFC (if you know the process of course) is that the only tuning parameter
%, understood by anybody , is the TRBF ( Closed LOOP Desired Time Response CLTR in English!)
% you dial it and the process response is an exponential reference trajectory going with no overshoot
% to the set point
% The funny trick is that, in this unique case, an elementary PFC is like a PI controller !
% with the advantage that there is no rule for tuning  (among the 2000  since S Nichols 1942):
% you ,just,dial; the TRBF and you do get it
% a little difference : PFC takes into account limits on the actuator !
% and also time delays
% now , the beginner is at ease , and then we  can face real problems  
%------------
%here we tune a PI controller  thru a PFC controller since there is in that unique case
% a primitive correspondance : funny !

clear;
close
tech=1;% sampling period
tf=400;w=1:1:tf;
%process definition to be controlled by  PI
%K=input(    ' K = ');% gain process
K=1;tau=20;a=exp(-tech/tau); b=1-a;
%tau=input(' tau =  ');a=exp(-tech/tau); b=1-a;
%disp(' TRBF= Desired Closed Loop Time Response ')
%TRBF=input(' TRBF =  ');%desired closed loop time response, only tuning desired factor
TRBF=40;lh=1-exp(-tech*3/TRBF);%same tuning
%computation of PI parameters tuning-----------------IMPORTANT PId=PFC G=lh/(K*b);%gain ------------------------------------------------------------------------HERE THE TRICK
Tintg= K*b*tau/lh;%integral--------------------------------------------------------------SMART
%PID parameters
G= 0.01*lh/(K*b);
%-------------------------------------------------------------------
disp(['Gain of PID      = ',num2str(G)])
disp(['Integral of PID  = ', num2str(Tintg)])
%test of PID
cons=100;%set point
u=zeros(1, tf);sp=u;e=u;mv=u;intg=u;
%-------------
%PID with correct Gain and Tintg
Kp=K;%Kp could be different from K for testing robustness
pert=u;r=100;
for ii=2+r: 1: tf
   if ii>150; pert(ii)=10;end
   sp(ii)= sp(ii-1)*a+ b*Kp*(e(ii-1-r)+pert(ii));%process, %basic elementary PI controller
   er =cons -sp(ii);%error
   intg(ii)=intg(ii-1) + er;%integral
   e(ii)= er*G+ (1/Tintg)*intg(ii);% controller P I 
end
figure (1)
plot( w, e, 'k',w,sp, 'r',w,pert,'b')
grid;zoom
title ('A simple PID LOOP TUNER   e k / sp r /')
%so you just feed into the PLC : the gain of the 1rst order  process and its time constant
% and you select a desired closed loop time response TRBF WITHOUT CV OVERSHOOT 
%because you go thru a PFC design implemented as a PID !
% extension with time delay is feasible
%
%-----------------------------------------------------------------PFC delay now !
disp( '---------------')
disp( 'Same conditions with PFC ?!---------------')
%PFC: process with time delay
%TRBF=input(' TRBF = ')%desired  closed loop time response 
TRBF=40;%exponential reference trajectory 
delay= 100;
tf=400;cons=100;
sm=zeros(1,tf);e=sm;sp=sm;
lh=1-exp(-tech*3/TRBF);
%emax=input( ' emax=   ')
Kp=1;
for ii=2+delay: 1: tf;
   sp(ii)= sp(ii-1)*a+ b*Kp*e(ii-1-delay);%process
   sm(ii)= sm(ii-1)*a+ b*Kp*e(ii-1);%model
   spred =sp(ii)+sm(ii)-sm(ii-delay);
   d= ( cons-spred)*lh+sm(ii)*b;
   e(ii)=d/(Kp*b);
   %if e(ii)> emax; e(ii)=emax;end
end
figure (2)
plot( w, e, 'k',w,sp, 'r')
grid;zoom
axis  ([ delay  tf 0 150])
text( 120, 120, 'MV')
text( 222, 75, 'CV')
title ( 'PFC delay  ')
%  both cases : you fix a TRBF desired closed loop time response and you do get it!
% in the PFC case the responses with or without an immense delay are identical
