% FILENAME: RESPS.M
% DATE: MARCH 22, 1994
% VERSION: 2.0
% REVISED : January 1995
% COPYRIGHT 1994-5 by T. Marlin and R. Cipolla   
% This M-file enables the user to define and execute cases for a response
% surface study for PID controller tuning.  The plot may also be called
% from this menu
% ********************************************************************

while 1
  clc
  disp('*************************************************************')
  disp('*                RESPONSE SURFACE PARAMATERS                *')
  disp('*                                                           *')
  disp('*************************************************************')
  disp('')
  disp('SELECT THE APPROPRIATE MENU ITEM(s)')
  disp('')
  disp('MODIFY...')
  disp('                                                  PRESENT STATUS')
  disp('1) Input Range for Tuning Constants   ')
  disp('2) Specify Contour Levels ' )
  if chg == 'd'
    disp('3) Select input pertubation                       Disturbance')
  else
    disp('3) Select input pertubation                       Set Point')
  end
  disp('4) Construct Contour Plot  ')
  disp('5) View Contour Plot       ')
  disp('6) Save Contour Plot(s) to META-file')
  disp('==> Press Enter to return to Main Menu ')
        
  res = input('Enter the desired selection:   ');
  
  if isempty(res), break, end   % go back to main menu if ENTER is pressed
      
  res = round(res);
  if (res<1) | (res>6)
      disp('')
      disp('Not a valid selection')
      disp('')
      disp('Press ENTER to continue')
      pause;
  end    
q=0;  
while q==0  
  if res == 1
      clc
      disp('')
      disp([' The Tuning Parameters are: Kc =  ',num2str(kc),' '])     
      disp(['                            Ti =  ',num2str(ti),' '])     
      disp(['                            Td =  ',num2str(td),' '])     
      disp('')
      junk = input('Enter minimum value for Kc (controller gain):  ') ;
      if junk > kc
        disp([' Error: Enter value smaller than ',num2str(kc),' '])
        disp('')
        disp(' Press any key to continue .... ')
        pause
        junk = c_1;
        break
      end
      if isempty(junk)
          junk=c_1; 
      end
      c_1=junk; ct_count=0;
      disp('')
      
      junk = input('Enter maximum value for Kc (controller gain):  ') ;
      if junk < kc
        disp([' Error: Enter value larger than ',num2str(kc),' '])
        disp('')
        disp(' Press any key to continue .... ')
        pause
        junk=c_2;
        break
      end
      if isempty(junk)
          junk=c_2; 
      end
      c_2=junk;
      disp('')
      
      junk = input('Enter minimum value for Ti (integral time):  ') ;
      if junk > ti
        disp([' Error: Enter value smaller than ',num2str(ti),' '])
        disp('')
        disp(' Press any key to continue .... ')
        pause
        junk=b_1;
        break
      end
      if isempty(junk)
          junk=b_1; 
      end
      b_1=junk;
      disp('')
      
      junk = input('Enter maximum value for Ti (integral time):  ') ;
      if junk < ti
        disp([' Error: Enter value larger than ',num2str(ti),' '])
        disp('')
        disp(' Press any key to continue .... ')
        pause
        junk=b_2;
        break
      end
      if isempty(junk)
          junk=b_2; 
      end
      b_2=junk;
      disp('')
       
      junkkc = input(' Enter the number of intervals for Kc: '); 
      if ver_sion == 0
        if junkkc > 32
         disp([' Error: Exceeded matrix limitations, no. intervals unchanged at  ',num2str(int_kc)])
         disp('')
         disp(' Press any key to continue .... ')
         junkkc = int_kc;
         pause
         end
      end
      if isempty(junkkc)
          junkkc = int_kc;  %= (c_2-c_1)/c_i; 
      end
      disp('')
      int_kc=junkkc;
      c_i=(c_2-c_1)/int_kc;   
      
      junkti = input(' Enter the number of intervals for Ti: '); 
      if ver_sion == 0
        if junkti > 32
         disp([' Error: Exceeded matrix limitations, no. intervals unchanged at ', num2str(int_ti)])
         disp('')
         disp(' Press any key to continue .... ')
         junkti=int_ti;
         pause
         end
      end
      if isempty(junkti)
          junkti = int_ti;  % = (b_2-b_1)/b_i; 
      end
      int_ti = junkti;
      b_i=(b_2-b_1)/int_ti;  q=1; 
 
 
 elseif res == 2
       ct_count=1;
       disp('')
       junk= input(' Enter the number of levels for the Contour Plot: ');
       if isempty(junk)
           junk=numlev;
       end
       
       if junk == 1
         disp('')
         disp(' You must specify at least 2 levels !! ')
         disp('')
         disp(' Press any key to continue ....')
         pause
         junk=numlev;
         break
       end

       if junk > 7
         disp ('Too many levels, value reset to 7')
         disp ('press enter to continue')
         junk = 7;
         pause;
       end
       
       if junk ~= numlev
           le_vel=zeros(1,junk);  
       end 
       numlev=junk;  
       
       clc
       disp('')
       for u=1:1:numlev
         disp('')
         junk=input(' Enter the level as a % of nominal (eg. 1.2 for 120% of nominal):  ') ;
         clc
         if isempty(junk)
             junk=1.0;   % same as optimum
         end
         le_vel(u)=junk;
       end
       break
 elseif res == 3
       disp('')
       disp(' For a set point change, type :  s')
       disp(' For a disturbance, type :  d')
       disp('')
       junkchg=input(' Enter a letter :  ','s');
       disp('')
       disp(' note : Simulation Parameters can be specified in Sub-menu 6 ')
       disp(' Press Enter to continue .... ')
       pause
       if isempty(junkchg)
           junkchg= chg;
       end
       if (junkchg~= 'd') & (junkchg~='s')
           disp (' illegal entry, please select menu item again')
           disp (' press enter to continue')
           junkchg = chg;
           pause
       end
       chg = junkchg;
      
      if chg == 'd'
        change=1; betaa=tdist; 
      else
        change=2; betaa=tset;
      end
      q=1; ct_count=0;
  elseif res == 4
     clc
     disp('')
     disp(' Creating plot will require plenty of time !!! ')
     disp(' Note: Any optimization or simulation(s) results not SAVED will be unrecoverable.')
     junk1=input(' Construct Contour Plot ...[y/n]','s');
     if junk1 == 'n'
        break
     else
        c_t = 1; ct_count = 0;
        con_tour  
        op_ts=0;
        disp('')
        disp(' Contour plot complete.')
        disp('')
        disp(' To view plot select menu item 5 ')
        pause(5)
        break
     end   
     if isempty(junk1), break;  end 
       
  elseif res == 5 | res == 6
     
     if res == 5
       save_graph=0;
     else
        save_graph = 0;
        disp (' Saving graph not possible with this version of Software Laboratory')
        disp ('Will proceed to display contour plot')
        disp (' Press enter to continue')
        pause
       %save_graph=1;
     end  
     
     if c_t == 0  
      disp(' ')
      disp(' No contour plot data available, run menu item 4.') 
      disp(' ')
      disp(' Press Enter to continue .....')
      pause
      break
     else 
      disp(' ')
      disp(' ')
      disp('0) Objective Function')
      disp('1) IAE')
      disp('2) ISE')
      disp('3) ITAE')
      
      if save_graph == 0 
        junk=input('Enter a number (0 - 3) to choose Contours you wish to see: '); 
      else
        junk=input('Enter a number (0 - 3) to choose Contour Plot you wish to save: '); 
      end
      if isempty(junk)
         junk=o_func;
      end
      o_func=junk;
      con_plot
     end
  else
      break
  end 
end  
end 
