home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / h2syn.m < prev    next >
Text File  |  1999-03-05  |  5KB  |  151 lines

  1. # Copyright (C) 1996,1998 A. Scottedward Hodel 
  2. #
  3. # This file is part of Octave. 
  4. #
  5. # Octave is free software; you can redistribute it and/or modify it 
  6. # under the terms of the GNU General Public License as published by the 
  7. # Free Software Foundation; either version 2, or (at your option) any 
  8. # later version. 
  9. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function [K,gain, Kc, Kf, Pc,  Pf] = h2syn(Asys,nu,ny,tol)
  18.   #  [K,gain, Kc, Kf, Pc, Pf] = h2syn(Asys,nu,ny,tol)
  19.   #
  20.   # Design H2 optimal controller per procedure in 
  21.   # Doyle, Glover, Khargonekar, Francis, "State Space Solutions to Standard
  22.   # H2 and Hinf Control Problems", IEEE TAC August 1989
  23.   #
  24.   # Discrete time control per Zhou, Doyle, and Glover, ROBUST AND OPTIMAL
  25.   #   CONTROL, Prentice-Hall, 1996
  26.   #
  27.   # inputs: 
  28.   #   Asys: system data structure.
  29.   #   nu: number of controlled inputs
  30.   #   ny: number of measured outputs
  31.   #   tol: threshhold for 0.  Default: 200*eps
  32.   # outputs: 
  33.   #    K: system controller (system data structure)
  34.   #    gain: optimal closed loop gain (see Kc, Kf warning below)
  35.   #    Kc: full information control (system data structure)
  36.   #    Kf: state estimator (system data structure)
  37.   #       WARNING: incorporation of the is_dgkf nonsingular transformations
  38.   #       Ru and Ry into Kc and Kf has not been tested.  
  39.   #    Pc: ARE solution matrix for regulator subproblem
  40.   #    Pf: ARE solution matrix for filter subproblem
  41.  
  42.   # Updated for System structure December 1996 by John Ingram
  43.  
  44.   if ((nargin < 3) | (nargin > 4))
  45.     usage("[K,gain, Kc, Kf, Pc, Pf] = h2syn(Asys,nu,ny[,tol])");
  46.   elseif(nargin == 3 )
  47.     [chkdgkf,dgs] = is_dgkf(Asys,nu,ny);
  48.   elseif(nargin == 4)
  49.     [chkdgkf,dgs] = is_dgkf(Asys,nu,ny,tol);
  50.   endif
  51.  
  52.   if (!chkdgkf )
  53.     disp("h2syn: system does not meet required assumptions")
  54.     help is_dgkf
  55.     error("h2syn: exit");
  56.   endif
  57.  
  58.   # extract dgs information
  59.               nw = dgs.nw;     nu = dgs.nu;
  60.   A = dgs.A;            Bw = dgs.Bw;    Bu = dgs.Bu;
  61.   Cz = dgs.Cz;          Dzw = dgs.Dzw;  Dzu = dgs.Dzu;    nz = dgs.nz;
  62.   Cy = dgs.Cy;          Dyw = dgs.Dyw;  Dyu = dgs.Dyu;    ny = dgs.ny;
  63.   d22nz = dgs.Dyu_nz;
  64.   dflg = dgs.dflg;
  65.  
  66.   if(norm(Dzw,Inf) > norm([Dzw, Dzu ; Dyw, Dyu],Inf)*1e-12)
  67.     warning("h2syn: Dzw nonzero; feedforward not implemented")
  68.     Dzw
  69.     D = [Dzw, Dzu ; Dyw, Dyu]
  70.   endif
  71.  
  72.   # recover i/o transformations
  73.   Ru = dgs.Ru;         Ry = dgs.Ry;
  74.   [ncstates, ndstates, nout, nin] = sysdimensions(Asys);
  75.   Atsam = sysgettsam(Asys);
  76.   [Ast, Ain, Aout] = sysgetsignals(Asys);
  77.  
  78.   if(dgs.dflg == 0)
  79.     Pc = are(A,Bu*Bu',Cz'*Cz);    # solve control, filtering ARE's
  80.     Pf = are(A',Cy'*Cy,Bw*Bw');
  81.     F2 = -Bu'*Pc;          # calculate feedback gains
  82.     L2 = -Pf*Cy';
  83.  
  84.     AF2 = A + Bu*F2;
  85.     AL2 = A + L2*Cy;
  86.     CzF2 = Cz + (Dzu/Ru)*F2;
  87.     BwL2 = Bw+L2*(Ry\Dyw);
  88.  
  89.   else
  90.     # discrete time solution
  91.     error("h2syn: discrete-time case not yet implemented")
  92.     Pc = dare(A,Bu*Bu',Cz'*Cz);
  93.     Pf = dare(A',Cy'*Cy,Bw*Bw');
  94.   endif
  95.  
  96.   nn = ncstates + ndstates;
  97.   In = eye(nn);
  98.   KA = A + Bu*F2 + L2*Cy;
  99.   Kc1 = ss2sys(AF2,Bw,CzF2,zeros(nz,nw));
  100.   Kf1 = ss2sys(AL2,BwL2,F2,zeros(nu,nw));
  101.  
  102.   g1 = h2norm(Kc1);
  103.   g2 = h2norm(Kf1);
  104.   
  105.   # compute optimal closed loop gain
  106.   gain = sqrt ( g1*g1 + g2*g2 );
  107.  
  108.   if(nargout)
  109.     Kst = strappend(Ast,"_K");
  110.     Kin = strappend(Aout((nout-ny+1):(nout)),"_K");
  111.     Kout = strappend(Ain((nin-nu+1):(nin)),"_K");
  112.  
  113.     # compute systems for return
  114.     K = ss2sys(KA,-L2/Ru,Ry\F2,zeros(nu,ny),Atsam,ncstates,ndstates,Kst,Kin,Kout);
  115.   endif
  116.  
  117.   if (nargout > 2)
  118.     #system full information control state names
  119.     stname2 = strappend(Ast,"_FI");
  120.  
  121.    #system full information control input names
  122.    inname2 = strappend(Ast,"_FI_in");
  123.  
  124.     #system full information control output names
  125.     outname2 = strappend(Aout(1:(nout-ny)),"_FI_out");
  126.  
  127.     nz = rows (Cz);
  128.     nw = columns (Bw);
  129.  
  130.     Kc = ss2sys(AF2, In, CzF2, zeros(nz,nn), Atsam, ...
  131.     ncstates, ndstates, stname2, inname2, outname2);
  132.   endif
  133.  
  134.   if (nargout >3)
  135.     #fix system state estimator state names
  136.     stname3 = strappend(Ast,"_Kf");
  137.  
  138.     #fix system state estimator input names
  139.     inname3 = strappend(Ast,"_Kf_noise");
  140.  
  141.     #fix system state estimator output names
  142.     outname3 = strappend(Ast,"_est");
  143.  
  144.     Kf = ss2sys(AL2, BwL2, In, zeros(nn,nw),Atsam,  ...
  145.       ncstates, ndstates, stname3, inname3,outname3);
  146.   endif
  147.  
  148. endfunction
  149.