%  ***********  GENERAL SINGLE LEVEL CONTROL  **************
%  **  CAN BE USED FOR EXAMPLES 18.2 TO 18.4  *************
%  **  AND FIGURES 18-6A-6C                   *************
%  COPYRIGHT McMASTER UNIVERSITY
%  WRITTEN BY T. MARLIN
%  FILENEME: LEVEL.M
%  LAST UPDATED: DECEMBER 1994
%  VERSION 2.0
%  DESCRIPTION
%  this is the main program for the system of M-files 
%  used to  input data and simulate a single, non-self-
%  regulatory level with linear of non-linear PI 
%  feedback control
%  ********************************************************

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

   lvlinit ; %  call program with initial data

%  ********************************************************
%   THE MAIN MENU ENABLES THE USER TO SELECT THE OPTIONS
%  ********************************************************

while 1
  clc
  disp('*************************************************************')
  disp('*     SIMULATION OF NON-SELF-REGULATING LEVEL SYSTEM        *')
  disp('*                                                           *')
  disp('*************************************************************')
  disp('')
  disp('SELECT THE APPROPRIATE MENU ITEM(s)')
  disp('')
  disp('                               PRESENT VALUES')
  disp ('1) Level process parameters')
  disp ('2) Controller parameters')
  fprintf('3) Total time of simulation    %4.2f\n',tend)
  fprintf('4) Simulation delta time       %4.3f\n',deltat)
  disp ('5) Execute the simulation')
  disp ('6) Display plots')
  disp('7) Return to MATLAB')
  disp('')
        
  userin = input('Enter the desired selection:   ');
  
  userin = round(userin);
  if (userin<1) | (userin>7)
      disp('')
      disp('Not a valid selection')
      disp('')
      disp('Press ENTER to continue')
      pause;
  end    

% ********************************************************  
  if userin == 1

      propara  % call menu for process parameters
% ********************************************************
  elseif userin == 2

        pipara  % call menu for controller parameters

% *********************************************************
  elseif userin == 3
      junk = input('Enter value for total time:  ') ;
      if isempty(junk) 
          junk=tend;
      elseif junk <= 0
          junk=tend;
          disp('positive value required, please reenter');
          pause (2)
      end

      if junk/deltat > 7000
        junk = 7000*deltat;
        disp('')
        disp(' Total time steps > 7000, total time decreased')
        disp('')
        disp(' Press any key to continue');
        pause
      end

      tend=junk; 
  
% **********************************************************
  elseif userin == 4
      junk = input('Enter value for integration time step:  ') ;
      if isempty(junk)
          junk= deltat;
      elseif junk <= 0
          junk=deltat;
          disp('positive value required, please reenter');
          pause (2)
      end

      if tend/junk > 7000
        junk = tend/7000;
        disp('')
        disp(' Total time steps > 7000, step size increased ')
        disp('')
        disp(' Press any key to continue');
        pause
      end

      deltat=junk; 
% **********************************************************  
  elseif userin == 5

       lvlsim         % call M-file to execute the simulation
       result = 1;
% **********************************************************  
  elseif userin == 6

       if result > 0.5
           lvlplot % call M-file to plot results
       end
       if result < 0.5
           disp ('')
           disp ( 'must execute simulation before plotting')
           disp ('')
           disp (' strike any key to continue')
           pause
       end
% ***********************************************************
  elseif userin ==7
       break
  end 
  
end 
