% ***************************************************************************
% COPYRIGHT BY DAVE JEWELL AND T.E. MARLIN 1994
% 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;
disp('*********************************************************************')
disp('*                                                                   *')
disp('*              DISTURBANCE TRANSFER FUNCTION EDITING                *')
disp('*                                                                   *')
disp('*                      CV(s)    Kd(Td1s+1)exp(-Dds)                 *')
disp('*              Gd(s) = ----- = ---------------------                *')
disp('*                       D(s)     (Td2s+1)(Td3s+1)                   *')
disp('*********************************************************************')

disp('WHAT PARAMETER WOULD YOU LIKE TO UPDATE ?')
disp('                                          Present Values')
fprintf('1) Disturbance Gain (Kd)                        %-g\n',Kd(TFD))
fprintf('2) Lead Time Constant (Td1)                     %-g\n',DtauA(TFD))
fprintf('3) Dead Time (Dd)                               %-g\n',DDT(TFD))
fprintf('4) Lag Time Constant (Td2)                      %-g\n',Dtau1(TFD))
fprintf('5) Lag Time Constant (Td3)                      %-g\n',Dtau2(TFD))
fprintf('6) Disturbance Step Size                        %-g\n',MAGSTEP)
fprintf('7) Edit Another Transfer Function               Gd%-g\n',TFD)
disp('8) Previous Menu')
mm = input('PLEASE ENTER A SELECTION : ');
if isempty(mm)==1
break
end
mm = round(mm);                          % round off selection
if (mm<1) | (mm>8)
	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('Kd : ');
    if isempty(TEMP)~=1
	Kd(TFD)=TEMP;
    end

elseif mm == 2
TEMP = input('Td1 : ');
    if isempty(TEMP)~=1
	DtauA(TFD)=TEMP;
    end

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

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

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

elseif mm == 6
TEMP = input('Magnitude : ');
    if isempty(TEMP)~=1
	MAGSTEP=TEMP;
    end

elseif mm == 7
TEMP=input('Which DISTURBANCE Transfer Function - (1) Gd1  (2) Gd2 : ');
TEMP=round(TEMP);

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

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