% ***************************************************************************
% 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('*                       PID CONTROLLER TUNING                       *')
disp('*                                                                   *')
disp('*                         MV(s)           1                         *')
disp('*                 Gc(s) = ----- = Kc(1 + --- + Tds)                 *')
disp('*                          E(s)          Tis                        *')
disp('*********************************************************************')

disp('WHAT PARAMETER WOULD YOU LIKE TO UPDATE ?')
disp('                                          Present Values')
fprintf('1) Controller Gain (Kc)                        %-g\n',config(2,TUNE))
fprintf('2) Integral Time (Ti)                          %-g\n',config(3,TUNE))
fprintf('3) Derivative Time (Td)                        %-g\n',config(4,TUNE))
fprintf('4) Contoller Set Point                         %-g\n',config(5,TUNE))
fprintf('5) Size Of Step For Process Reaction Curve     %-g\n',config(7,TUNE))
fprintf('6) Tune Another Controller                     Gc%-g\n',TUNE)
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>7)
    disp('')                       % selection validation
    disp('NOT A VALID SELECTION')
    disp('PRESS ENTER TO CONTINUE')
    pause;
end

% A SERIES OF PROMPTS FOR CONTROLLER TUNING 
if mm == 1
TEMP=input('Kc : ');
if isempty(TEMP) ~= 1
	config(2,TUNE)=TEMP;
end

elseif mm == 2
TEMP=input('Ti : ');

if isempty(TEMP) ~= 1
	xxhold=config(3,TUNE);  config(3,TUNE)=TEMP;
	  if config(3,TUNE)<=0
	  disp('Integral Time MUST be greater than 0, Press enter to continue')
	  config(3,TUNE)=xxhold;
	  pause;
	  end
end

elseif mm == 3
TEMP=input('Td : ');
if isempty(TEMP) ~= 1
       xxhold=config(4,TUNE);  config(4,TUNE)=TEMP;
	  if config(4,TUNE)<0
	  disp('Derivative Time MUST not be negative, Press enter to continue')
	  config(4,TUNE)=xxhold;
	  pause;
	  end
end
	
elseif mm == 4
TEMP=input('Please Enter The Set Point : ');
    if isempty(TEMP) ~= 1
	config(5,TUNE)=TEMP;
    end

config(6,TUNE)=tSTEP;
TEMP=input('Time The Set Point Change Occurs : ');
    if isempty(TEMP) ~= 1
	xxhold=config(6,TUNE);  config(6,TUNE)=TEMP;
	  if config(6,TUNE)<0
	  disp('Time MUST not be negative, 10% of total time will be used')
	  disp('Press enter to continue')
	  config(6,TUNE)=0.10*tend;
	  pause;
	  end
    
    end
elseif mm == 5
    TEMP=input('Enter the step to the controller output : ');
if isempty(TEMP) ~= 1
	config(7,TUNE)=TEMP;
    disp('');
    disp('To Activate The Process Reaction Curve Mode You Must Set The Controller')
    disp('Gain (Kc) To Zero. The Step Will Occur At 10% Of Total Time.')
    disp('Press Enter To Continue')
    pause;
end

elseif mm == 6
TUNE=input('Which Controller (1) Gc1  (2) Gc2 : ');
TUNE=round(TUNE);

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

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