% **************************************************************************
% CASCADE-FEEDFORWARD CONTROL SIMULATOR 
% COPYRIGHT (C) 1994, T.E. MARLIN
%
% FILENAME : CFFM3.M
% WRITTEN BY : F.BOUDREAU
% VERSION : 2.0
% LAST UPDATED : NOVEMBER 1994
% DESCRIPTION OF FILE :
%    MENU FOR UPDATING DISTURBANCE MODELS. THIS FILE IS USED FOR BOTH THE
%    PRIMARY AND SECONDARY MODELS; THE VARIABLE 'loopid' INDICATES
%    WHICH MODEL 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 DISTURBANCE TRANSFER FUNCTION EDITING             *\n')
   elseif loopid==2
   fprintf('*            SECONDARY DISTURBANCE TRANSFER FUNCTION EDITING            *\n')
   end
   fprintf('*                                                                       *\n')
   fprintf('*                          Kd%g(T1d%g*s+1)exp(-Dd%g*s)                     *\n',loopid,loopid,loopid)
   fprintf('*                   Gd%g = --------------------------                    *\n',loopid)
   fprintf('*                            (T2d%g*s+1)(T3d%g*s+1)                       *\n',loopid,loopid)
   fprintf('*************************************************************************\n\n')
   fprintf('WHAT PARAMETER WOULD YOU LIKE TO UPDATE ?\n')
   fprintf('                                            Present Values\n')
   fprintf('1) Gain (Kd%g)                                  %-g\n',loopid,Kd(loopid))
   fprintf('2) Lead Time Constant (T1d%g)                   %-g\n',loopid,TAd(loopid))
   fprintf('3) Dead Time (Dd%g)                             %-g\n',loopid,Dd(loopid))   
   fprintf('4) Lag Time Constant (T2d%g)                    %-g\n',loopid,T1d(loopid))
   fprintf('5) Lag Time Constant (T3d%g)                    %-g\n',loopid,T2d(loopid))
   fprintf('6) Size of Step in Disturbance D%g              %-g\n',loopid,Ddelta(loopid))
   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('Kd%g : ',loopid));
      if isempty(temp) ~= 1
         Kd(loopid)=temp;
      end
   elseif m1 == 2
      temp = input(sprintf('T1d%g : ',loopid));
      if isempty(temp) ~= 1
         TAd(loopid) = temp;
      end
   elseif m1 == 3
      temp = input(sprintf('Dd%g : ',loopid));
      if isempty(temp) ~= 1
         if (temp) >= 0
            Dd(loopid) = abs(temp);
         else
            disp('dead time must be non-negative, please reenter')
            pause (4)
         end
      end
   elseif m1 == 4
      temp = input(sprintf('T2d%g : ',loopid));
      if isempty(temp) ~= 1
         if (temp) >= 0
            T1d(loopid) = abs(temp);
         else
            disp('the disturbance must be bounded')
            disp(' ')
            disp('the time constant must be non-negative, please reenter')
            pause (4)
         end
      end
   elseif m1 == 5
      temp = input(sprintf('T3d%g : ',loopid));
      if isempty(temp) ~= 1
         if (temp) >= 0
             T2d(loopid) = abs(temp);
         else
             disp('the disturbance must be bounded')
             disp(' ')
             disp('the time constant must be non-negative, please reenter')
             pause (4)
         end
      end
   elseif m1 == 6
      temp = input(sprintf('Step in D%g : ',loopid));
      if isempty(temp) ~= 1
         Ddelta(loopid) = temp;
      end
   elseif m1 == 7
      break
   end % if
end % while
