% **************************************************************************
% CASCADE-FEEDFORWARD CONTROL SIMULATOR
% COPYRIGHT (C), 1994 T.E. MARLIN
%
% FILENAME : CFFM2.M
% WRITTEN BY : F.BOUDREAU
% VERSION : 2.0
% LAST UPDATED : NOVEMBER 1994
% DESCRIPTION OF FILE :
%    MENU FOR PID CONTROLLER TUNING. THIS FILE IS USED FOR BOTH THE
%    PRIMARY AND SECONDARY PID's; THE VARIABLE 'loopid' INDICATES
%    WHICH PID IS BEING EDITED. 1=PRIMARY, 2=SECONDARY.
% ALL FILES IN THIS PACKAGE :
%    CFF     .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
% **************************************************************************

while 1
   clc;   
   fprintf('*************************************************************************\n')
   fprintf('*                                                                       *\n')
   if loopid==1
   fprintf('*                      PRIMARY PID CONTROLLER TUNING                    *\n')
   elseif loopid==2
   fprintf('*                  SECONDARY LOOP PID CONTROLLER TUNING                 *\n')
   end
   fprintf('*                                                                       *\n')
   fprintf('*                                      1                                *\n')
   fprintf('*                     Gc%g = Kc%g(1 + ------- - Td%g*s)                    *\n',loopid,loopid,loopid)
   fprintf('*                                    Ti%g*s                              *\n',loopid)
   fprintf('*************************************************************************\n\n')
   fprintf('WHAT PARAMETER WOULD YOU LIKE TO UPDATE ?\n')
   fprintf('                                            Present Values\n')
   fprintf('1) Controller Gain (Kc%g)                          %-g\n',loopid,KC(loopid))
   fprintf('2) Integral Time (Ti%g)  (=0 for no integral mode) %-g\n',loopid,TI(loopid))
   fprintf('3) Derivative Time (Td%g)                          %-g\n',loopid,TD(loopid))
   if loopid == 1
   fprintf('4) Size of Step to Primary Set-Point SP1          %-g\n',SPdelta)
   fprintf('5) Size of Step to Primary Controller Output      %-g\n',SP_PRC(loopid))
   else
   fprintf('-  Set-Point Cannot Be Altered for Secondary Controller  -\n')
   fprintf('5) Size Of Step to Final Element                  %-g\n',SP_PRC(loopid))
   end
   if PRC == 2
   fprintf('6) Mode for Feedback Controllers                  PRC2\n')   
   elseif PRC == 1
   fprintf('6) Mode for Feedback Controllers                  PRC1\n')
   else
   fprintf('6) Mode for Feedback Controllers                  PIDs ON\n')
   end
   fprintf('7) Quit to main menu\n')
      
   m1 = input('PLEASE ENTER A SELECTION : ');
   if isempty(m1)==1           % RETURN TO MAIN MENU IF <CR> ONLY IS PRESSED
      break
   end
   m1 = round(m1);
   if (m1<1 | m1>7)            % VALIDATES SELECTION
      disp('')
      disp('NOT A VALID SELECTION')
      disp('')
      disp('PRESS ENTER TO CONTINUE')
      pause;
   end

   if m1 == 1
      temp = input(sprintf('Kc%g : ',loopid));
      if isempty(temp) ~= 1
         KC(loopid) = temp;
      end

   elseif m1 == 2
      temp = input(sprintf('Ti%g : ',loopid));
      if isempty(temp) ~= 1
         if (temp) >= 0
            TI(loopid) = temp;
         else
            disp (' the integral time must be non-negative')
            pause (2)
         end
      end

   elseif m1 == 3
      temp = input(sprintf('Td%g : ',loopid));
      if isempty(temp) ~= 1
         if (temp) >= 0.0
             TD(loopid) = temp;
         else
             disp (' the derivative time must be non-negative')
             pause (2)
         end
      end

   elseif m1 == 4
      if loopid == 1
         temp = input('New value for step in SP1 : ');
         if isempty(temp) ~= 1
            SPdelta = temp;
         end
      end

   elseif m1 == 5
      temp = input('Enter The Manipulated Variable Step Size : ');
      if isempty(temp) ~= 1
         SP_PRC(loopid) = temp;
      end

   elseif m1 == 6
      clc;
      disp('SELECT NEW MODE FOR FEEDBACK CONTROLLERS:')
      disp('')
      disp('1) PRC2    (All controllers manual, step to final element)')
      disp('2) PRC1    (Step to primary output. If cascade, secondary in auto)')
      disp('3) PIDs ON (All controllers on)')
      temp = input('PLEASE ENTER A SELECTION : ');
      if isempty(temp) ~= 1
         if (temp>=1 & temp<=3)
            PRC = 3 - temp;
         end
         if (PRC==2 & CASCADE==0)
            disp('')
            disp('NOTE: Mode 1 will be ignored unless the control structure is')
            disp('      changed to CASCADE in the simulation menu.  If it is left as')
            disp('      SINGLE-LOOP, Mode 3 (PIDs ON) will be simulated.') 
            disp('Press Enter to continue.')
            pause;
         end
      end

   elseif m1 == 7
      break
   end % if
end % while
