% **************************************************************************
% CASCADE-FEEDFORWARD CONTROL SIMULATOR 
% COPYRIGHT (C) 1994, T.E. MARLIN
%
% FILENAME : CFF.M
% WRITTEN BY : F.BOUDREAU
% VERSION : 2.0
% LAST UPDATED : NOVEMBER, 1994
% DESCRIPTION OF FILE :
%    MAIN MENU FOR CASCADE-FEEDFORWARD CONTROL SIMULATOR,
%    AND ENTRY POINT FOR THE PROGRAM.
% ALL FILES IN THIS PACKAGE :
%    FB_FF   .M  - MAIN MENU AND ENTRY POINT
%    CFFDAT  .M  - EXTERNAL DATA FILE
%    CFFGRAPH.M  - PLOTS SIMULATION RESULTS
%    CFFM1   .M  - MENU FOR PROCESS T.F. EDITING
%    CFFM2   .M  - MENU FOR PID TUNING
%    CFFM3   .M  - MENU FOR DISTURBANCE T.F. EDITING
%    CFFMFF  .M  - FEEDFORWARD CONTROLLER MENU
%    CFFMSIM .M  - SIMULATION OPTIONS MENU
%    CFFNAME .M  - MENU FOR VARIABLES NAMES, INITIAL VALUES, BOUNDS
%    CFFSIM  .M  - EXECUTES SIMULATION
%
% NOTES :
% BLOCK DIAGRAM OF PROCESS:
% 
%                                +---+
%               +----------------|Gff|<-----------------*----+--- D1
%               |                +---+                       |
%               |                           D2               |
%               |                           |                |
%               |Cff                        V                V
%               |                         +---+            +---+
% SP1           |                         |Gd2|            |Gd1|
% |             |                         +---+            +---+
% |             |                           |                |
% V E1 +---+ C1 V SP2  E2 +---+ MV +---+ X2 V  CV2  +---+ X1 V  CV1
% O--->|Gc1|--->O--->O--->|Gc2|--->|Gp2|--->O--+--->|Gp1|--->O---+-->
% A    +---+         A    +---+    +---+       |    +---+        |
% |                  |                         |                 |
% |                  +---------------------*---+                 |
% |                                                              |
% +----------------------------------------------------------*---+
%
% MEASUREMENT NOISE OPTION ADDS AUTO-REGRESSIVE NOISE ON D1, Y1, AND
% Y2 AT LOCATIONS MARKED BY ASTERISKS.
% WHEN CASCADE OPTION IS TURNED OFF, THE PRIMARY CONTROLLER OUTPUT PLUS
% THE FEEDFORWARD CONTROL ACTION, I.E. C1 + Cff, IS FED DIRECTLY INTO
% Gp2, AND SECONDARY CONTROLLER SETTINGS ARE IGNORED.
%
%               *** TYPE "FB_FF" TO RUN ! ***
%
% **************************************************************************

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

% EXECUTE ASSIGNMENTS IN EXTERNAL DATA FILE:
cffdat;

loopid = 1;
while 1
   clc;
   disp('*************************************************************************')
   disp('*                  CASCADE-FEEDFORWARD CONTROL SIMULATOR                *')
   disp('*               ******************************************              *')
   disp('*                McMASTER UNIVERSITY CHEMICAL ENGINEERING               *')
   disp('*                ========================================               *')
   disp('*                      Written By Francois Boudreau                     *')
   disp('*************************************************************************')
   disp('')
   disp('WHAT DO YOU WANT TO DO ?')
   disp('')
   disp('1) Load In A Previously Saved Case') 
   disp('2) Edit Plant Transfer Function Models')
   disp('3) Tune PID Controllers')
   disp('4) Edit Disturbance Transfer Function Models')
   disp('5) Tune Feedforward Controller for Primary Disturbance D1')
   disp('6) Edit Variable Names, Initial Values And Bounds')
   disp('7) Run Simulation, Options And Viewing Plots')
   disp('8) Save The Simulation Data To A File')
   disp('9) QUIT TO MATLAB')
   mm = input('PLEASE ENTER A SELECTION : ');
   mm = round(mm);
   if (mm<1 | mm>9)             % 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
      disp('')
      disp('1) Primary        2) Secondary')
      loopid = input('Edit Which Plant Transfer Function Model ? ');
      if isempty(loopid) ~= 1
	 if (loopid>=1 & loopid<=2)
	    cffm1;
	 end
      end
   elseif mm == 3
      disp('')
      disp('1) Primary        2) Secondary')
      loopid = input('Tune Which PID Controller ? ');
      if isempty(loopid) ~= 1
	 if (loopid>=1 & loopid<=2)
	    cffm2;
	 end
      end
   elseif mm == 4
      disp('')
      disp('1) Primary        2) Secondary')
      loopid = input('Edit Which Disturbance Transfer Function Model ? ');
      if isempty(loopid) ~= 1
	 if (loopid>=1 & loopid<=2)
	    cffm3;
	 end
      end
   elseif mm == 5
      cffmff;
   elseif mm == 6
      cffname;      
   elseif mm == 7
      cffmsim;
   elseif mm == 8
      % 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 == 9
      temp = input('QUIT TO MATLAB: ARE YOU SURE (y/n) ? ','s');
      if (temp == 'y' | temp == 'Y')
	 break
      end
   end % if
end % while
