File: sysconnect.m

package info (click to toggle)
octave-control 1.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,628 kB
  • ctags: 160
  • sloc: makefile: 64; sh: 4
file content (295 lines) | stat: -rw-r--r-- 8,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
## Copyright (C) 1996, 1998, 2000, 2002, 2004, 2005, 2006, 2007
##               Auburn University.  All rights reserved.
##
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{clsys} =} sysconnect (@var{sys}, @var{out_idx}, @var{in_idx}, @var{order}, @var{tol})
## Close the loop from specified outputs to respective specified inputs
##
## @strong{Inputs}
## @table @var
## @item   sys
## System data structure.
## @item   out_idx
## @itemx  in_idx
## Names or indices of signals to connect (see @code{sysidx}).
## The output specified by @math{out_idx(ii)} is connected to the input
## specified by @math{in_idx(ii)}.
## @item   order
## logical flag (default = 0)
## @table @code
## @item        0
## Leave inputs and outputs in their original order.
## @item        1
## Permute inputs and outputs to the order shown in the diagram below.
## @end table
## @item     tol
## Tolerance for singularities in algebraic loops, default: 200@code{eps}.
## @end table
##
## @strong{Outputs}
## @table @var
## @item clsys
## Resulting closed loop system.
## @end table
##
## @strong{Method}
##
## @code{sysconnect} internally permutes selected inputs, outputs as shown
## below, closes the loop, and then permutes inputs and outputs back to their
## original order
## @example
## @group
##                  --------------------
##  u_1       ----->|                  |----> y_1
##                  |        sys       |
##          old u_2 |                  |
## u_2* ---->(+)--->|                  |----->y_2
## (in_idx)   ^     --------------------    | (out_idx)
##            |                             |
##            -------------------------------
## @end group
## @end example
## The input that has the summing junction added to it has an * added to
## the end  of the input name.
## @end deftypefn

## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu>
## Created: August 1995
## modified by John Ingram July 1996

function sys = sysconnect (sys, output_list, input_list, order, tol)

  if (nargin < 3 || nargin > 5)
    print_usage ();
  endif

  ## check order
  if (nargin < 4)
    order = 0;
  elseif (order != 0 && order != 1)
    error ("sysconnect: order must be either 0 or 1")
  endif

  if (isa (sys.a, "single") || isa (sys.b, "single") || isa (sys.c, "single") ||
      isa (sys.d, "single"))
    myeps = eps ("single");
  else
    myeps = eps;
  endif

  if (nargin < 5)
    tol = 200*myeps;
  elseif (! is_sample (tol))
    error ("sysconnect: tol must be a positive scalar");
  elseif (tol > 1e2*sqrt(myeps))
    warning ("sysconnect: tol set to large value=%g, eps=%g", tol, myeps);
  endif

  ## convert signal names to indices
  if (is_signal_list (input_list) || ischar (input_list))
    input_list = sysidx (sys, "in", input_list);
  endif
  if (is_signal_list (output_list) || ischar (output_list))
    output_list = sysidx (sys, "out", output_list);
  endif

  ## verify sizes,format of input, output lists
  if (min (size (output_list)) * min (size (input_list)) != 1)
    error ("output_list and input_list must be vectors");
  else
    lo = length (output_list);
    li = length (input_list);
    if (lo != li)
      error ("output_list and input_list must be of the same length")
    endif

    if (is_duplicate_entry (output_list) || is_duplicate_entry (input_list))
      error ("duplicate entry in input_list and/or output_list");
    endif
  endif

  [nc, nz, mm, pp] = sysdimensions (sys);
  nn = nc+nz;

  if (! isstruct (sys))
    error ("sys must be in structured system form")
  elseif (pp < lo)
    error ("length(output_list)=%d, sys has only %d system outputs", lo, pp);
  elseif (mm < li)
    error ("length(input_list)=%d, sys has only %d system inputs", li, mm);
  endif

  ## check that there are enough inputs/outputs in the system for the lists
  if (max (input_list) > mm)
    error ("max(input_list) exceeds the number of inputs");
  elseif (max (output_list) > pp)
    error ("max(output_list) exceeds the number of outputs");
  endif

  output_list = reshape (output_list, 1, length (output_list));

  ## make sure we're in state space form
  sys = sysupdate (sys, "ss");

  ## permute rows and columns of B,C,D matrices into pseudo-dgkf form...
  all_inputs = sysreorder (mm, input_list);
  all_outputs = sysreorder (pp, output_list);

  [aa, bb, cc, dd] = sys2ss (sys);
  bb = bb(:,all_inputs);
  cc = cc(all_outputs,:);
  dd = dd(all_outputs,all_inputs);

  yd = sysgetsignals (sys, "yd");
  yd = yd(all_outputs);

  ## m1, p1 = number of inputs, outputs that are not being connected
  m1 = mm-li;
  p1 = pp-li;

  ## m2, p2: 1st column, row of B, C that is being connected
  m2 = m1+1;
  p2 = p1+1;

  ## partition system into a DGKF-like form; the loop is closed around
  ## B2, C2
  if (m1 > 0)
    B1 = bb(:,1:m1);
    D21= dd(p2:pp,1:m1);
  endif
  B2 = bb(:,m2:mm);
  if (p1 > 0)
    C1 = cc(1:p1,:);
    D12= dd(1:p1,m2:mm);
  endif
  C2 = cc(p2:pp,:);
  if (m1*p1 > 0)
    D11 = dd(1:p1,1:m1);
  endif
  D22 = dd(p2:pp,m2:mm);

  if (norm (D22))
    warning ("sysconnect: possible algebraic loop, D22 non-zero");
    D22i = eye (size (D22)) - D22;
    C2h = D22i\C2;
    if (m1 > 0)
      D21h = D22i\D21;
    endif
    D22h = D22i\D22;
  else
    C2h = C2;
    if (m1 > 0)
      D21h = D21;
    endif
    D22h = D22;

  endif

  ## check cont state -> disc output -> cont state
  dyi = find (yd(p2:pp));

  ## disp("sysconnect: dyi=")
  ## dyi
  ## nc
  ## disp("/sysconnect");

  if ((nc > 0) & find (dyi > 0))
    B2con = B2(1:nc,dyi);       # connection to cont states
    C2hd = C2h(dyi,1:nc);       # cont states -> outputs
  else
    B2con = C2hd = [];
  endif

  if (max (size (B2con)) & max (size (C2hd)))
    if (norm (B2con*C2hd))
      warning ("sysconnect: cont-state -> disc output -> cont state derivative");
      warning ("connection made; resulting system may not be meaningful");
    endif
  endif

  Ac = aa+B2*C2h;
  if (m1 > 0)
    B1c = B1 + B2*D21h;
  endif
  B2c = B2*(eye(size(D22h)) + D22h);
  if (p1*m1 > 0)
    D11c = D11 + D12*D21h;
  endif
  if (p1 > 0)
    C1c  = C1+D12*C2h;
    D12c = D12*(eye(size(D22h))+D22h);
  endif

  ## construct system data structure
  if (m1 > 0)
   Bc = [B1c, B2c];
  else
   Bc = B2c;
  endif

  if (p1 > 0)
    Cc = [C1c;C2h];
  else
    Cc = C2h;
  endif

  if (m1*p1 > 0)
    Dc = [D11c,D12c; D21h,D22h];
  elseif (m1 > 0)
    Dc = [D21h, D22h];
  elseif (p1 > 0)
    Dc = [D12c; D22h];
  else
    Dc = D22h;
  endif

  ## permute rows and columns of Bc, Cc, Dc back into original order
  Im = eye (mm, mm);
  Pi = Im(:,all_inputs);
  back_inputs = Pi*[1:mm]';

  Ip = eye (pp, pp);
  Po = Ip(:,all_outputs);
  back_outputs = Po*[1:pp]';

  Bc = Bc(:,back_inputs);
  Cc = Cc(back_outputs,:);
  Dc = Dc(back_outputs,back_inputs);
  yd = yd(back_outputs);

  ## rebuild system
  Ts = sysgettsam (sys);
  [stnam, innam, outnam] = sysgetsignals (sys);
  sys = ss (Ac, Bc, Cc, Dc, Ts, nc, nz, stnam, innam, outnam, find (yd));

  ## update connected input names
  for ii = 1:length(input_list)
    idx = input_list(ii);
    tmpval = sysgetsignals (sys, "in", idx);
    strval = sprintf ("%s*", tmpval{1} );
    sys = syssetsignals (sys, "in", strval, idx);
  endfor

  ## maintain original system type if it was SISO
  if (strcmp (sysgettype (sys), "tf"))
    sysupdate (sys, "tf");
  elseif (strcmp (sysgettype (sys),"zp"))
    sysupdate (sys, "zp");
  endif

endfunction