% ***************************************************************************
% COPYRIGHT BY DAVE JEWELL AND T.E. MARLIN 1993
% FILNAME : TUNING.M
% Version 2.0
% LAST UPDATED : DECEMBER 1994
% DISCRIPTION OF FILE :
% THIS M-FILE IS ACCESSED FROM "twobytwo.m" ALLOWS THE USER TO ENTER THE 
% PARAMETERS FOR PID CONTROLLER TUNING
% ***************************************************************************

clc;
while 1
clc;
if TFP == 1
i=1;j=1;
elseif TFP == 2
i=1;j=2;
elseif TFP == 3
i=2;j=1;
elseif TFP == 4
i=2;j=2;
end
disp('*********************************************************************')
disp('*                                                                   *')
disp('*                 PROCESS TRANSFER FUNCTION EDITING                 *')
disp('*                                                                   *')
disp('*                        CV(s)    Kp(T1s+1)exp(-Ds)                 *')
disp('*                Gp(s) = ----- = -------------------                *')
disp('*                        MV(s)     (T2s+1)(T3s+1)                   *')
disp('*********************************************************************')

disp('WHAT PARAMETER WOULD YOU LIKE TO UPDATE ?')
disp('                                          Present Values')
fprintf('1) Process Gain (Kp)                           %-g\n',Kp(i,j))
fprintf('2) Lead Time Constant (T1)                     %-g\n',tauA(i,j))
fprintf('3) Dead Time (D)                               %-g\n',DT(i,j))
fprintf('4) Lag Time Constant (T2)                      %-g\n',tau1(i,j))
fprintf('5) Lag Time Constant (T3)                      %-g\n',tau2(i,j))
fprintf('6) Edit Another Transfer Function              Gp%-g%-g\n',i,j)
disp('7) Previous Menu')
mm = input('PLEASE ENTER A SELECTION : ');
if isempty(mm)==1
break
end
mm = round(mm);                          % round off selection
if (mm<1) | (mm>6)
	disp('')                       % selection validation
	disp('NOT A VALID SELECTION')
	disp('')
	disp('PRESS ENTER TO CONTINUE')
	pause;
end

% A SERIES OF PROMPTS FOR CONTROLLER TUNING 
if mm == 1
TEMP = input('Kp : ');
    if isempty(TEMP)~=1
	Kp(i,j)=TEMP;
    end
	
elseif mm == 2
TEMP = input('T1 : ');
    if isempty(TEMP)~=1
	tauA(i,j)=TEMP;
    end


elseif mm == 3
TEMP = input('D : ');
    if isempty(TEMP)~=1
	xxhold=DT(i,j) ; DT(i,j)=TEMP;
	if DT(i,j)<0
	  disp('Dead Time CANNOT be less than 0, Press enter to continue')
	  DT(i,j)=xxhold;
	  pause;
	end
    end

elseif mm == 4
TEMP = input('T2 : ');
    if isempty(TEMP)~=1
	xxhold=tau1(i,j) ; tau1(i,j)=TEMP;
	if tau1(i,j)<0
	  disp('Time Constant CANNOT be less than 0, Press enter to continue')
	  tau1(i,j)=xxhold;
	  pause;
	end
    end


elseif mm == 5
TEMP = input('T3 : ');
    if isempty(TEMP)~=1
	xxhold=tau2(i,j) ;  tau2(i,j)=TEMP;
	if tau2(i,j)<0
	  disp('Time Constant CANNOT be less than 0, Press enter to continue')
	  tau2(i,j)=xxhold;
	  pause;
	end
    end

elseif mm == 6
TEMP=input('Which PLANT Transfer Function - (1) Gp11 (2) Gp12 (3) Gp21 (4) Gp22 : ');
TEMP=round(TEMP);

if isempty(TEMP)~=1
    if (TEMP<1) | (TEMP>4)      % VALIDATES SELECTION
	disp('')
	disp('NOT A VALID SELECTION, MUST BE BETWEEN 1 AND 4')
	disp('PRESS ENTER TO CONTINUE')
	pause;
    else
	TFP=TEMP;
    end
end

elseif mm == 7
return
end     % if
end     % while
