% ***************************************************************************
% COPYRIGHT BY DAVE JEWELL AND T.E. MARLIN 1994
% FILNAME : twobytwo.M
% Version 2.0
% LAST UPDATED : December, 1994
% DISCRIPTION OF FILE :
% THIS MAIN MENU IS INVOKED BY TYPING "twobytwo".  IT ALLOWS ALL OTHER MENUS
% TO BE ACCESSED.
% FILES : twobytwo.m    - main program menu
%         data2by2.m    - default data set
%         initial.m     - calculates initail conditions
%         tfpedit.m     - process T.F. editing menu
%         tfdedit.m     - disturbance T.F. editing menu
%         tuning.m      - PID tuning menu
%         runsim.m      - simulation options and invoking
%         name.m        - naming, initial conditions and bounds of variables
%         sim_2by2.m    - main simulation
%         perform.m     - control performance calculations
%         graph.m       - plots graphs
%         dmc_data      - provides initial data
%         dmc_menu      - menu for design of dmc controller
%         dmc_mod       - menu for inputting dmc model parameters
%         dmc_eq        - sets dmc model equal to process model (mismatch=0)
%         dmc_diff      - determines difference model from transfer function
%         dmc_coef      - determines the step weigth coefficients
%         dmc_des       - calculates the dmc controller (Kdmc)
%         dmc_init      - provides initial values for simulation
% ***************************************************************************


clear;
clc; clf; axis ('auto'); 
close all

data2by2;       % LOADS IN SIMULATION DATA

while 1
clc;
disp('*********************************************************************')
disp('*                    2 INPUT / 2 OUTPUT SIMULATOR                   *')
disp('*                    ****************************                   *')
disp('*              McMASTER UNIVERSITY CHEMICAL ENGINEERING             *')
disp('*              ========================================             *')
disp('*                      Written By Dave Jewell                       *')
disp('*********************************************************************')
disp('')
disp('WHAT DO YOU WANT TO DO ?')
disp('')
disp('1) Load In A Previously Saved Case')
disp('2) Edit Plant Transfer Function Model')
disp('3) Edit Disturbance Transfer Function Model')
disp('4) PID Controller Configuration - loop pairing')
disp('5) Tune PID Controllers')
disp('6) Design DMC Controller')
disp('7) Run Simulation, Options And View Plots')
disp('8) Edit Variable Names, Initial Values And Manipulated Variable Bounds')
disp('9) Save The Simulation Data To A File')
disp('10) QUIT TO MATLAB')

mm = input('PLEASE ENTER A SELECTION : ');    
mm = round(mm);                 % ROUNDS OFF SELECTION 

    if (mm<1) | (mm>10)          % VALIDATES SELECTION
	disp('')
	disp('NOT A VALID SELECTION')
	disp('PRESS ENTER TO CONTINUE')
	pause;
    end

if mm == 1
% RETRIEVES PREVIOUSLY SAVED DATA
prevcase=input('Enter the name of the file to be retrieved (ex. a:file.mat) : ','s');
 if (exist(prevcase)>=2) & (isempty(prevcase)~=1)
	disp('                      Loading Data From File');
	eval(['load ',prevcase])        % LOADS IN FILE
	pause(2);
 elseif (isempty(prevcase)~=1)
    disp('The file cannot be found, please try again.')
    pause(2);
 end

elseif mm == 2

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

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

elseif mm == 3

TFD=input('Which DISTURBANCE Transfer Function - (1) Gd1  (2) Gd2 : ');
TFD=round(TFD);

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

elseif mm == 4

PAIRING=input('Which Pairing (1) Gc1->MV1 & Gc2->MV2  (2) Gc1->MV2 & Gc2->MV1 : ');
PAIRING=round(PAIRING);

if isempty(PAIRING)~=1
    if (PAIRING<1) | (PAIRING>2)        % VALIDATES SELECTION
	disp('')
	disp('NOT A VALID SELECTION, MUST BE BETWEEN 1 AND 2')
	disp('PRESS ENTER TO CONTINUE')
	pause;
    elseif PAIRING == 1         % CONFIGURES INPUT/OUTPUT
	config(1,1)=1;config(1,2)=2;
    elseif PAIRING == 2
	config(1,1)=2;config(1,2)=1;
    end
end

elseif mm == 5 

TUNE=input('Which Controller (1) CV1 controlled by Gc1  (2) CV2 controlled by Gc2 : ');
TUNE=round(TUNE);

if isempty(TUNE)~=1
    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;
    else
	TUNING;                 % CALLS CONTROLLER TUNING MENU
    end
end

elseif mm == 6                  %  calls menu for dmc design
DMC_MENU

elseif mm == 7
RUNSIM;                         % CALLS MENU FOR SIMULATION RUNNING

elseif mm == 8                  % INITIAL VALUES, LABELS, BOUNDS  
NAME;

elseif mm == 9
	% SAVE DATA TO FILE
	savecase=input('Please enter the file name (ex. a:file.mat) : ','s'); 
	if isempty(savecase)~=1
	    disp('                          Saving Data To A File')
	    eval(['save ',savecase])            % SAVES DATA
	    pause(2);
	end

elseif mm == 10
return                          % EXITS TO MATLAB ENVIRONMENT

end     % IF
end     % WHILE
