root/branches/maintenance/2.2.1/Modelica/Blocks/Discrete.mo

Revision 463, 17.0 kB (checked in by hubertus, 22 months ago)

Created maintenance version from Dynasims version with minimal changes (no checksum, 1 graphics, a few improved documentation places, structurallyIncomplete annotations in Visualizers)

Line 
1package Discrete "Discrete input/output blocks with fixed sample period" 
2 
3  extends Modelica.Icons.Library;
4 
5  annotation(preferedView="info", Documentation(info="<html>
6<p>
7This package contains <b>discrete control blocks</b>
8with <b>fixed sample period</b>.
9Every component of this package is structured in the following way:
10</p>
11<ol>
12<li> A component has <b>continuous real</b> input and output signals.</li>
13<li> The <b>input</b> signals are <b>sampled</b> by the given sample period
14     defined via parameter <b>samplePeriod</b>.
15     The first sample instant is defined by parameter <b>startTime</b>.
16<li> The <b>output</b> signals are computed from the sampled input signals.
17</ol>
18<p>
19A <b>sampled data system</b> may consist of components of package <b>Discrete</b>
20and of every other purely <b>algebraic</b> input/output block, such
21as the components of packages <b>Modelica.Blocks.Math</b>,
22<b>Modelica.Blocks.Nonlinear</b> or <b>Modelica.Blocks.Sources</b>.
23</p>
24
25</HTML>
26", revisions="<html>
27<ul>
28<li><i>October 21, 2002</i>
29       by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
30       New components TriggeredSampler and TriggeredMax added.</li>
31<li><i>June 18, 2000</i>
32       by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
33       Realized based on a corresponding library of Dieter Moormann and
34       Hilding Elmqvist.</li>
35</ul>
36</html>"));
37 
38  block Sampler "Ideal sampling of continuous signals" 
39    extends Interfaces.DiscreteSISO;
40   
41    annotation (
42      Icon(
43        Ellipse(extent=[-25, -10; -45, 10], style(color=3, fillColor=7)),
44        Ellipse(extent=[45, -10; 25, 10], style(color=73, fillColor=7)),
45        Line(points=[-100, 0; -45, 0], style(color=3)),
46        Line(points=[45, 0; 100, 0], style(color=73)),
47        Line(points=[-35, 0; 30, 35], style(color=3))),
48      Diagram(
49        Ellipse(extent=[-25, -10; -45, 10], style(color=3, fillColor=7)),
50        Ellipse(extent=[45, -10; 25, 10], style(color=73, fillColor=7)),
51        Line(points=[-100, 0; -45, 0], style(color=3)),
52        Line(points=[45, 0; 100, 0], style(color=73)),
53        Line(points=[-35, 0; 30, 35], style(color=3))),
54      Documentation(info="<HTML>
55<p>
56Samples the continues input signal with a sampling rate defined
57via parameter <b>samplePeriod</b>.
58</p>
59</HTML>
60"));
61  equation 
62    when {sampleTrigger, initial()} then
63      y = u;
64    end when;
65  end Sampler;
66 
67  block ZeroOrderHold "Zero order hold of a sampled-data system" 
68    extends Interfaces.DiscreteSISO;
69  protected 
70    Real ySample;
71    annotation (
72      Coordsys(
73        extent=[-100, -100; 100, 100],
74        grid=[2, 2],
75        component=[20, 20]),
76      Window(
77        x=0.3,
78        y=0.07,
79        width=0.63,
80        height=0.68),
81      Icon(Line(points=[-78, -42; -52, -42; -52, 0; -26, 0; -26, 24; -6, 24; -6,
82                64; 18, 64; 18, 20; 38, 20; 38, 0; 44, 0; 44, 0; 62, 0])),
83      Documentation(info="<HTML>
84<p>
85The output is identical to the sampled input signal at sample
86time instants and holds the output at the value of the last
87sample instant during the sample points.
88</p>
89</HTML>
90"));
91  equation 
92    when {sampleTrigger, initial()} then
93      ySample = u;
94    end when;
95    /* Define y=ySample with an infinitesimal delay to break potential
96       algebraic loops if both the continuous and the discrete part have
97       direct feedthrough
98    */
99    y = pre(ySample);
100  end ZeroOrderHold;
101 
102  block FirstOrderHold "First order hold of a sampled-data system" 
103    extends Interfaces.DiscreteSISO;
104  protected 
105    Real ySample;
106    Real tSample;
107    Real c;
108    annotation (
109      Coordsys(
110        extent=[-100, -100; 100, 100],
111        grid=[1, 1],
112        component=[20, 20]),
113      Window(
114        x=0.21,
115        y=0.08,
116        width=0.75,
117        height=0.76),
118      Icon(Line(points=[-79, -41; -59, -33; -40, 1; -20, 9; 0, 63; 21, 20; 41,
119              10; 60, 20]), Line(points=[60, 19; 81, 10])),
120      Documentation(info="<HTML>
121<p>
122The output signal is the extrapolation through the
123values of the last two sampled input signals.
124</p>
125</HTML>
126"));
127  equation 
128    when sampleTrigger then
129      ySample = u;
130      tSample = time;
131      c = if firstTrigger then 0 else (ySample - pre(ySample))/samplePeriod;
132    end when;
133    /* Use pre(ySample) and pre(c) to break potential algebraic loops by an
134       infinitesimal delay if both the continuous and the discrete part
135       have direct feedthrough.
136    */
137    y = pre(ySample) + pre(c)*(time - tSample);
138  end FirstOrderHold;
139 
140  block UnitDelay "Unit Delay Block" 
141    parameter Real y_start=0 "Initial value of output signal";
142    extends Interfaces.DiscreteSISO;
143   
144    annotation (
145      Coordsys(
146        extent=[-100, -100; 100, 100],
147        grid=[2, 2],
148        component=[20, 20]),
149      Window(
150        x=0.24,
151        y=0.09,
152        width=0.6,
153        height=0.6),
154      Documentation(info="<html>
155<p>
156This block describes a unit delay:
157</p>
158<pre>
159          1
160     y = --- * u
161          z
162</pre>
163<p>
164that is, the output signal y is the input signal u of the
165previous sample instant. Before the second sample instant,
166the output y is identical to parameter yStart.
167</p>
168
169</HTML>
170"),   Icon(
171        Line(points=[-30, 0; 30, 0]),
172        Text(extent=[-90, 10; 90, 90], string="1"),
173        Text(extent=[-90, -10; 90, -90], string="z")),
174      Diagram(
175        Rectangle(extent=[-60, 60; 60, -60], style(color=73)),
176        Text(extent=[-160, 10; -140, -10], string="u"),
177        Text(extent=[115, 10; 135, -10], string="y"),
178        Line(points=[-100, 0; -60, 0], style(color=73)),
179        Line(points=[60, 0; 100, 0], style(color=73)),
180        Line(points=[40, 0; -40, 0], style(color=0)),
181        Text(
182          extent=[-55, 55; 55, 5],
183          string="1",
184          style(color=0)),
185        Text(
186          extent=[-55, -5; 55, -55],
187          string="z",
188          style(color=0))));
189  equation 
190    when sampleTrigger then
191      y = pre(u);
192    end when;
193   
194  initial equation 
195      y = y_start;
196  end UnitDelay;
197 
198  block TransferFunction "Discrete Transfer Function block" 
199    parameter Real b[:]={1} "Numerator coefficients of transfer function.";
200    parameter Real a[:]={1,1} "Denominator coefficients of transfer function.";
201    extends Interfaces.DiscreteSISO;
202    output Real x[size(a, 1) - 1] 
203      "State of transfer function from controller canonical form";
204  protected 
205    parameter Integer nb=size(b, 1) "Size of Numerator of transfer function";
206    parameter Integer na=size(a, 1) "Size of Denominator of transfer function";
207    Real x1;
208    Real xext[size(a, 1)];
209    annotation (
210      Coordsys(
211        extent=[-100, -100; 100, 100],
212        grid=[2, 2],
213        component=[20, 20]),
214      Window(
215        x=0.25,
216        y=0.08,
217        width=0.65,
218        height=0.69),
219      Documentation(info="<html>
220<p>The <b>discrete transfer function</b> block defines the
221transfer function between the input signal u and the output
222signal y. The numerator has the order nb-1, the denominator
223has the order na-1.</p>
224<pre>          b(1)*z^(nb-1) + b(2)*z^(nb-2) + ... + b(nb)
225   y(z) = -------------------------------------------- * u(z)
226          a(1)*z^(na-1) + a(2)*z^(na-2) + ... + a(na)
227</pre>
228<p>State variables <b>x</b> are defined according to
229<b>controller canonical</b> form. Initial values of the
230states can be set as start values of <b>x</b>.<p>
231<p>Example:</p>
232<pre>     Blocks.Discrete.TransferFunction g(b = {2,4}, a = {1,3});
233</pre>
234<p>results in the following transfer function:</p>
235<pre>        2*z + 4
236   y = --------- * u
237         z + 3
238</pre>
239
240</HTML>
241", revisions="<html>
242<p><b>Release Notes:</b></p>
243<ul>
244<li><i>November 15, 2000</i>
245    by <a href=\"http://www.dynasim.se\">Hans Olsson</a>:<br>
246    Converted to when-semantics of Modelica 1.4 with special
247    care to avoid unnecessary algebraic loops.</li>
248<li><i>June 18, 2000</i>
249    by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
250    Realized based on a corresponding model of Dieter Moormann
251    and Hilding Elmqvist.</li>
252</ul>
253</html>"),
254      Icon(
255        Line(points=[82, 0; -84, 0], style(color=73)),
256        Text(
257          extent=[-92, 92; 86, 12],
258          string="b(z)",
259          style(color=73)),
260        Text(
261          extent=[-90, -12; 90, -90],
262          string="a(z)",
263          style(color=73))),
264      Diagram(
265        Rectangle(extent=[-60, 60; 60, -60], style(fillPattern=0)),
266        Line(points=[40, 0; -44, 0], style(color=0, thickness=2)),
267        Text(
268          extent=[-54, 54; 54, 4],
269          string="b(z)",
270          style(color=0)),
271        Text(
272          extent=[-54, -6; 56, -56],
273          string="a(z)",
274          style(color=0)),
275        Line(points=[-100, 0; -60, 0]),
276        Line(points=[60, 0; 100, 0])));
277  equation 
278    when sampleTrigger then
279      /* State variables x are defined according to
280       controller canonical form. */
281      x1 = (u - a[2:size(a, 1)]*pre(x))/a[1];
282      xext = vector([x1; pre(x)]);
283      x = xext[1:size(x, 1)];
284      y = vector([zeros(na - nb, 1); b])*xext;
285    end when;
286    /* This is a non-sampled equation and above there are two separate
287       when-clauses. This breaks feeback loops without direct terms,
288       since in that case y will be independent of x1 (and only dependent
289       on pre(x)).
290    */
291    /* Corresponding (simpler) version using when-semantics of Modelica 1.3:
292   equation
293     when sampleTrigger then
294      [x; xn] = [x1; pre(x)];
295      [u] = transpose([a])*[x1; pre(x)];
296      [y] = transpose([zeros(na - nb, 1); b])*[x1; pre(x)];
297     end when;
298*/
299  end TransferFunction;
300 
301  block StateSpace "Discrete State Space block" 
302    parameter Real A[:, size(A, 1)]=[1, 0; 0, 1] 
303      "Matrix A of state space model";
304    parameter Real B[size(A, 1), :]=[1; 1] "Matrix B of state space model";
305    parameter Real C[:, size(A, 1)]=[1, 1] "Matrix C of state space model";
306    parameter Real D[size(C, 1), size(B, 2)]=zeros(size(C, 1), size(B, 2)) 
307      "Matrix D of state space model";
308   
309    extends Interfaces.DiscreteMIMO(final nin=size(B, 2), final nout=size(C, 1));
310    output Real x[size(A, 1)] "State vector";
311   
312    annotation (
313      Coordsys(
314        extent=[-100, -100; 100, 100],
315        grid=[2, 2],
316        component=[20, 20]),
317      Window(
318        x=0.25,
319        y=0.18,
320        width=0.6,
321        height=0.65),
322      Documentation(info="<html>
323<p>
324The <b>discrete state space</b> block defines the relation
325between the input u=inPort.signal and the output
326y=outPort.signal in state space form:
327</p>
328<pre>
329    x = A * pre(x) + B * u
330    y = C * pre(x) + D * u
331</pre>
332<p>
333where pre(x) is the value of the discrete state x at
334the previous sample time instant.
335The input is a vector of length nu, the output is a vector
336of length ny and nx is the number of states. Accordingly
337</p>
338<pre>
339        A has the dimension: A(nx,nx),
340        B has the dimension: B(nx,nu),
341        C has the dimension: C(ny,nx),
342        D has the dimension: D(ny,nu)
343</pre>
344<p>
345Example:
346</p>
347<pre>
348     parameter: A = [0.12, 2;3, 1.5]
349     parameter: B = [2, 7;3, 1]
350     parameter: C = [0.1, 2]
351     parameter: D = zeros(ny,nu)
352
353results in the following equations:
354  [x[1]]   [0.12  2.00] [pre(x[1])]   [2.0  7.0] [u[1]]
355  [    ] = [          ]*[         ] + [        ]*[    ]
356  [x[2]]   [3.00  1.50] [pre(x[2])]   [0.1  2.0] [u[2]]
357                             [pre(x[1])]            [u[1]]
358       y[1]   = [0.1  2.0] * [         ] + [0  0] * [    ]
359                             [pre(x[2])]            [u[2]]
360</pre>
361</HTML>
362"),   Icon(
363        Text(extent=[-90, 15; -15, 90], string="A"),
364        Text(extent=[15, 15; 90, 90], string="B"),
365        Text(extent=[-52, 28; 54, -20], string="z"),
366        Text(extent=[-90, -15; -15, -90], string="C"),
367        Text(extent=[15, -15; 90, -90], string="D")),
368      Diagram(
369        Rectangle(extent=[-60, 60; 60, -60], style(fillPattern=0)),
370        Text(
371          extent=[-54, 50; 52, -10],
372          string="zx=Ax+Bu",
373          style(
374            color=0,
375            gradient=0,
376            fillColor=10,
377            fillPattern=0)),
378        Text(
379          extent=[-56, 14; 54, -50],
380          string="  y=Cx+Du",
381          style(
382            color=0,
383            fillColor=8,
384            fillPattern=1)),
385        Line(points=[-102, 0; -60, 0]),
386        Line(points=[60, 0; 100, 0])));
387  equation 
388    when sampleTrigger then
389      x = A*pre(x) + B*u;
390      y = C*pre(x) + D*u;
391    end when;
392  end StateSpace;
393 
394  block TriggeredSampler "Triggered sampling of continuous signals" 
395    extends Interfaces.DiscreteBlockIcon;
396    replaceable type SignalType = Real "type of input and output signal";
397    parameter SignalType y_start=0 "initial value of output signal";
398   
399    annotation (
400      Icon(
401        Ellipse(extent=[-25, -10; -45, 10], style(color=3, fillColor=7)),
402        Ellipse(extent=[45, -10; 25, 10], style(color=73, fillColor=7)),
403        Line(points=[-100, 0; -45, 0], style(color=3)),
404        Line(points=[45, 0; 100, 0], style(color=73)),
405        Line(points=[0, -100; 0, -26], style(color=5)),
406        Line(points=[-35, 0; 28, -48], style(color=3))),
407      Diagram(
408        Ellipse(extent=[-25, -10; -45, 10], style(color=3, fillColor=7)),
409        Ellipse(extent=[45, -10; 25, 10], style(color=73, fillColor=7)),
410        Line(points=[-100, 0; -45, 0], style(color=3)),
411        Line(points=[45, 0; 100, 0], style(color=73)),
412        Line(points=[-35, 0; 28, -48], style(color=3)),
413        Line(points=[0, -100; 0, -26], style(color=5))),
414      Documentation(info="<HTML>
415<p>
416Samples the continuous input signal whenever the trigger input
417signal is rising (i.e., trigger changes from <b>false</b> to
418<b>true</b>) and provides the sampled input signal as output.
419Before the first sampling, the output signal is equal to
420the initial value defined via parameter <b>y0</b>.
421</p>
422</HTML>
423"));
424    Modelica.Blocks.Interfaces.RealInput u(redeclare type SignalType = 
425          SignalType) "Connector with an input signal of type SignalType" 
426                                                          annotation (extent=[-
427          140, -20; -100, 20]);
428    Modelica.Blocks.Interfaces.RealOutput y(redeclare type SignalType = 
429          SignalType) "Connector with an output signal of type SignalType" 
430                                                           annotation (extent=[
431          100, -10; 120, 10]);
432    Modelica.Blocks.Interfaces.BooleanInput trigger annotation (
433        extent=[-20, -138; 20, -98], rotation=90);
434  equation 
435    when trigger then
436      y = u;
437    end when;
438  initial equation 
439    y = y_start;
440  end TriggeredSampler;
441 
442  block TriggeredMax
443    "Compute maximum, absolute value of continuous signal at trigger instants" 
444   
445    extends Interfaces.DiscreteBlockIcon;
446    replaceable type SignalType = Real "type of input and output signal";
447    annotation (
448      Icon(
449        Ellipse(extent=[-25, -10; -45, 10], style(color=3, fillColor=7)),
450        Ellipse(extent=[45, -10; 25, 10], style(color=73, fillColor=7)),
451        Line(points=[-100, 0; -45, 0], style(color=3)),
452        Line(points=[45, 0; 100, 0], style(color=73)),
453        Line(points=[0, -100; 0, -26], style(color=5)),
454        Line(points=[-35, 0; 28, -48], style(color=3)),
455        Text(
456          extent=[-86, 82; 82, 24],
457          string="max",
458          style(color=0))),
459      Diagram(
460        Ellipse(extent=[-25, -10; -45, 10], style(color=3, fillColor=7)),
461        Ellipse(extent=[45, -10; 25, 10], style(color=73, fillColor=7)),
462        Line(points=[-100, 0; -45, 0], style(color=3)),
463        Line(points=[45, 0; 100, 0], style(color=73)),
464        Line(points=[-35, 0; 28, -48], style(color=3)),
465        Line(points=[0, -100; 0, -26], style(color=5))),
466      Documentation(info="<HTML>
467<p>
468Samples the continuous input signal whenever the trigger input
469signal is rising (i.e., trigger changes from <b>false</b> to
470<b>true</b>). The maximum, absolute value of the input signal
471at the sampling point is provided as output signal.
472</p>
473</HTML>
474"));
475    Modelica.Blocks.Interfaces.RealInput u(redeclare type SignalType = 
476          SignalType) "Connector with an input signal of type SignalType" 
477                                                          annotation (extent=[-
478          140, -20; -100, 20]);
479    Modelica.Blocks.Interfaces.RealOutput y(redeclare type SignalType = 
480          SignalType) "Connector with an output signal of type SignalType" 
481                                                           annotation (extent=[
482          100, -10; 120, 10]);
483    Modelica.Blocks.Interfaces.BooleanInput trigger annotation (
484        extent=[-20, -138; 20, -98], rotation=90);
485  equation 
486    when trigger then
487       y = max(pre(y), abs(u));
488    end when;
489  initial equation 
490    y = 0;
491  end TriggeredMax;
492end Discrete;
Note: See TracBrowser for help on using the browser.