root/trunk/Modelica/Blocks/Logical.mo

Revision 1144, 42.9 kB (checked in by otter, 5 months ago)

Fixed #88 (In the diagram layer of Modelica.Blocks.Logical.Hysteresis the
horizontal axis is wrong: time sould be replaced by u)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1within Modelica.Blocks;
2package Logical "Library of components with Boolean input and output signals"
3    extends Modelica.Icons.Library;
4
5  annotation(Documentation(info="<html>
6<p>
7This package provides blocks with Boolean input and output signals
8to describe logical networks. A typical example for a logical
9network built with package Logical is shown in the next figure:
10</p>
11<p align=\"center\">
12<img src=\"../Images/Blocks/LogicalNetwork1.png\">
13</p>
14
15<p>
16The actual value of Boolean input and/or output signals is displayed
17in the respective block icon as \"circle\", where \"white\" color means
18value <b>false</b> and \"green\" color means value <b>true</b>. These
19values are visualized in a diagram animation.
20</p>
21</html>"));
22
23  model And "Logical 'and': y = u1 and u2"
24    extends Blocks.Interfaces.partialBooleanSI2SO;
25    annotation (defaultComponentName="and1",
26           Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},
27              {100,100}}), graphics={Text(
28            extent={{-90,40},{90,-40}},
29            lineColor={0,0,0},
30            textString="and")}),
31                              Diagram(coordinateSystem(preserveAspectRatio=
32              false, extent={{-100,-100},{100,100}}),
33                                      graphics),
34      Documentation(info="<html>
35<p>
36The output is <b>true</b> if all inputs are <b>true</b>, otherwise
37the output is <b>false</b>.
38</p>
39</html>"));
40  equation
41    y = u1 and u2;
42  end And;
43
44  model Or "Logical 'or': y = u1 or u2"
45    extends Blocks.Interfaces.partialBooleanSI2SO;
46    annotation (defaultComponentName="or1",
47           Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},
48              {100,100}}), graphics={Text(
49            extent={{-90,40},{90,-40}},
50            lineColor={0,0,0},
51            textString="or")}),
52                              Diagram(coordinateSystem(preserveAspectRatio=
53              false, extent={{-100,-100},{100,100}}),
54                                      graphics),
55      Documentation(info="<html>
56<p>
57The output is <b>true</b> if at least one input is <b>true</b>, otherwise
58the output is <b>false</b>.
59</p>
60</html>"));
61  equation
62    y = u1 or u2;
63  end Or;
64
65  model Xor "Logical 'xor': y = u1 xor u2"
66    extends Blocks.Interfaces.partialBooleanSI2SO;
67    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
68              -100},{100,100}}), graphics={Text(
69            extent={{-90,40},{90,-40}},
70            lineColor={0,0,0},
71            textString="xor")}),
72                              Diagram(coordinateSystem(preserveAspectRatio=
73              false, extent={{-100,-100},{100,100}}),
74                                      graphics),
75      Documentation(info="<html>
76<p>
77The output is <b>true</b> if exactly one input is <b>true</b>, otherwise
78the output is <b>false</b>.
79</p>
80</html>"));
81  equation
82    y =not  ( (u1 and u2) or (not u1 and not u2));
83  end Xor;
84
85  model Nor "Logical 'nor': y = not (u1 or u2)"
86    extends Blocks.Interfaces.partialBooleanSI2SO;
87    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
88              -100},{100,100}}), graphics={Text(
89            extent={{-90,40},{90,-40}},
90            lineColor={0,0,0},
91            textString="nor")}),
92                              Diagram(coordinateSystem(preserveAspectRatio=
93              false, extent={{-100,-100},{100,100}}),
94                                      graphics),
95      Documentation(info="<html>
96<p>
97The output is <b>true</b> if none of the inputs is <b>true</b>, otherwise
98the output is <b>false</b>.
99</p>
100</html>"));
101  equation
102    y =not  ( u1 or u2);
103  end Nor;
104
105  model Nand "Logical 'nand': y = not (u1 and u2)"
106    extends Blocks.Interfaces.partialBooleanSI2SO;
107    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
108              -100},{100,100}}), graphics={Text(
109            extent={{-90,40},{90,-40}},
110            lineColor={0,0,0},
111            textString="nand")}),
112                              Diagram(coordinateSystem(preserveAspectRatio=
113              false, extent={{-100,-100},{100,100}}),
114                                      graphics),
115      Documentation(info="<html>
116<p>
117The output is <b>true</b> if at least one input is <b>false</b>, otherwise
118the output is <b>false</b>.
119</p>
120</html>"));
121  equation
122    y =not  ( u1 and u2);
123  end Nand;
124
125  model Not "Logical 'not': y = not u"
126    extends Blocks.Interfaces.partialBooleanSISO;
127
128    annotation (defaultComponentName="not1",
129         Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{
130              100,100}}), graphics={Text(
131            extent={{-90,40},{90,-40}},
132            lineColor={0,0,0},
133            textString="not")}),
134                            Diagram(coordinateSystem(preserveAspectRatio=true,
135            extent={{-100,-100},{100,100}}),
136                                    graphics),
137      Documentation(info="<html>
138<p>
139The output is <b>true</b> if the input is <b>false</b>, otherwise
140the output is <b>false</b>.
141</p>
142</html>"));
143  equation
144    y =not  u;
145  end Not;
146
147  model Pre
148    "Breaks algebraic loops by an infinitesimal small time delay (y = pre(u): event iteration continues until u = pre(u))"
149
150    parameter Boolean pre_u_start = false
151      "Start value of pre(u) at initial time";
152    extends Blocks.Interfaces.partialBooleanSISO;
153
154    annotation (defaultComponentName="pre1",
155         Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{
156              100,100}}), graphics={Text(
157            extent={{-90,40},{90,-40}},
158            lineColor={0,0,0},
159            textString="pre")}),
160                            Diagram(coordinateSystem(preserveAspectRatio=true,
161            extent={{-100,-100},{100,100}}),
162                                    graphics),
163      Documentation(info="<html>
164<p>
165This block delays the Boolean input by an infinitesimal small time delay and
166therefore breaks algebraic loops. In a network of logical blocks, in every
167\"closed connection loop\" at least one logical block must have a delay,
168since algebraic systems of Boolean equations are not solveable.
169</p>
170
171<p>
172The \"Pre\" block returns the value of the \"input\" signal from the
173last \"event iteration\". The \"event iteration\" stops, once both
174values are identical (u = pre(u)).
175</p>
176</html>"));
177  initial equation
178    pre(u) = pre_u_start;
179  equation
180    y = pre(u);
181  end Pre;
182
183  model Edge "Output y is true, if the input u has a rising edge (y = edge(u))"
184
185    parameter Boolean pre_u_start = false
186      "Start value of pre(u) at initial time";
187    extends Blocks.Interfaces.partialBooleanSISO;
188
189    annotation (defaultComponentName="edge1",
190      Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100,
191              100}}), graphics={Text(
192            extent={{-90,40},{90,-40}},
193            lineColor={0,0,0},
194            textString="edge")}),
195                            Diagram(coordinateSystem(preserveAspectRatio=true,
196            extent={{-100,-100},{100,100}}),
197                                    graphics),
198      Documentation(info="<html>
199<p>
200The output is <b>true</b> if the Boolean input has a rising edge
201from <b>false</b> to <b>true</b>, otherwise
202the output is <b>false</b>.
203</p>
204</html>"));
205  initial equation
206    pre(u) = pre_u_start;
207  equation
208    y = edge(u);
209  end Edge;
210
211  model FallingEdge
212    "Output y is true, if the input u has a falling edge (y = edge(not u))"
213
214    parameter Boolean pre_u_start = false
215      "Start value of pre(u) at initial time";
216    extends Blocks.Interfaces.partialBooleanSISO;
217
218    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
219              -100},{100,100}}), graphics={Text(
220            extent={{-90,40},{90,-40}},
221            lineColor={0,0,0},
222            textString="falling")}),
223                            Diagram(coordinateSystem(preserveAspectRatio=true,
224            extent={{-100,-100},{100,100}}),
225                                    graphics),
226      Documentation(info="<html>
227<p>
228The output is <b>true</b> if the Boolean input has a falling edge
229from <b>true</b> to <b>false</b>, otherwise
230the output is <b>false</b>.
231</p>
232</html>"));
233  protected
234   Boolean not_u=not u;
235  initial equation
236    pre(not_u) =not  pre_u_start;
237  equation
238    y = edge(not_u);
239  end FallingEdge;
240
241  model Change
242    "Output y is true, if the input u has a rising or falling edge (y = change(u))"
243
244    parameter Boolean pre_u_start = false
245      "Start value of pre(u) at initial time";
246    extends Blocks.Interfaces.partialBooleanSISO;
247
248    annotation (defaultComponentName="change1",
249      Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100,
250              100}}), graphics={Text(
251            extent={{-90,40},{90,-40}},
252            lineColor={0,0,0},
253            textString="change")}),
254                            Diagram(coordinateSystem(preserveAspectRatio=true,
255            extent={{-100,-100},{100,100}}),
256                                    graphics),
257      Documentation(info="<html>
258<p>
259The output is <b>true</b> if the Boolean input has either a rising edge
260from <b>false</b> to <b>true</b> or a falling edge from
261<b>true</b> to <b>false</b>, otherwise
262the output is <b>false</b>.
263</p>
264</html>"));
265  initial equation
266    pre(u) = pre_u_start;
267  equation
268    y = change(u);
269  end Change;
270
271  block GreaterThreshold
272    "Output y is true, if input u is greater than threshold"
273    extends Blocks.Interfaces.partialBooleanThresholdComparison;
274    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
275              -100},{100,100}}), graphics={Text(
276            extent={{-90,-40},{60,40}},
277            lineColor={0,0,0},
278            textString=">")}),
279                        Diagram(coordinateSystem(preserveAspectRatio=true,
280            extent={{-100,-100},{100,100}}),
281                                graphics),
282      Documentation(info="<html>
283<p>
284The output is <b>true</b> if the Real input is greater than
285parameter <b>threshold</b>, otherwise
286the output is <b>false</b>.
287</p>
288</html>"));
289  equation
290    y = u > threshold;
291  end GreaterThreshold;
292
293  block GreaterEqualThreshold
294    "Output y is true, if input u is greater or equal than threshold"
295
296    extends Blocks.Interfaces.partialBooleanThresholdComparison;
297    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
298              -100},{100,100}}), graphics={Text(
299            extent={{-90,-40},{60,40}},
300            lineColor={0,0,0},
301            textString=">=")}),
302                            Diagram(coordinateSystem(preserveAspectRatio=true,
303            extent={{-100,-100},{100,100}}),
304                                    graphics),
305      Documentation(info="<html>
306<p>
307The output is <b>true</b> if the Real input is greater than or equal to
308parameter <b>threshold</b>, otherwise
309the output is <b>false</b>.
310</p>
311</html>"));
312  equation
313    y = u >= threshold;
314  end GreaterEqualThreshold;
315
316  block LessThreshold "Output y is true, if input u is less than threshold"
317
318    extends Blocks.Interfaces.partialBooleanThresholdComparison;
319    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
320              -100},{100,100}}), graphics={Text(
321            extent={{-90,-40},{60,40}},
322            lineColor={0,0,0},
323            textString="<")}),
324                            Diagram(coordinateSystem(preserveAspectRatio=true,
325            extent={{-100,-100},{100,100}}),
326                                    graphics),
327      Documentation(info="<html>
328<p>
329The output is <b>true</b> if the Real input is less than
330parameter <b>threshold</b>, otherwise
331the output is <b>false</b>.
332</p>
333</html>"));
334  equation
335    y = u < threshold;
336  end LessThreshold;
337
338  block LessEqualThreshold
339    "Output y is true, if input u is less or equal than threshold"
340    extends Blocks.Interfaces.partialBooleanThresholdComparison;
341    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
342              -100},{100,100}}), graphics={Text(
343            extent={{-90,-40},{60,40}},
344            lineColor={0,0,0},
345            textString="<=")}),
346                            Diagram(coordinateSystem(preserveAspectRatio=true,
347            extent={{-100,-100},{100,100}}),
348                                    graphics),
349      Documentation(info="<html>
350<p>
351The output is <b>true</b> if the Real input is less than or equal to
352parameter <b>threshold</b>, otherwise
353the output is <b>false</b>.
354</p>
355</html>"));
356  equation
357    y = u <= threshold;
358  end LessEqualThreshold;
359
360  block Greater "Output y is true, if input u1 is greater as input u2"
361    extends Blocks.Interfaces.partialBooleanComparison;
362
363    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
364              -100},{100,100}}), graphics={
365          Ellipse(extent={{32,10},{52,-10}}, lineColor={0,0,255}),
366          Line(points={{-100,-80},{42,-80},{42,0}}, color={0,0,255}),
367          Text(
368            extent={{-80,-60},{20,60}},
369            lineColor={0,0,0},
370            textString=">")}),
371        Documentation(info="<html>
372<p>
373The output is <b>true</b> if Real input u1 is greater than
374Real input u2, otherwise the output is <b>false</b>.
375</p>
376</html>"));
377
378  equation
379    y = u1 > u2;
380  end Greater;
381
382  block GreaterEqual
383    "Output y is true, if input u1 is greater or equal as input u2"
384    extends Blocks.Interfaces.partialBooleanComparison;
385
386    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
387              -100},{100,100}}), graphics={
388          Ellipse(extent={{32,10},{52,-10}}, lineColor={0,0,255}),
389          Line(points={{-100,-80},{42,-80},{42,0}}, color={0,0,255}),
390          Text(
391            extent={{-80,-60},{20,60}},
392            lineColor={0,0,0},
393            textString=">=")}),
394        Documentation(info="<html>
395<p>
396The output is <b>true</b> if Real input u1 is greater than or equal to
397Real input u2, otherwise the output is <b>false</b>.
398</p>
399</html>"));
400
401  equation
402    y = u1 >= u2;
403  end GreaterEqual;
404
405  block Less "Output y is true, if input u1 is less as input u2"
406    extends Blocks.Interfaces.partialBooleanComparison;
407
408    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
409              -100},{100,100}}), graphics={
410          Ellipse(extent={{32,10},{52,-10}}, lineColor={0,0,255}),
411          Line(points={{-100,-80},{42,-80},{42,0}}, color={0,0,255}),
412          Text(
413            extent={{-80,-60},{20,60}},
414            lineColor={0,0,0},
415            textString="<")}),
416        Documentation(info="<html>
417<p>
418The output is <b>true</b> if Real input u1 is less than
419Real input u2, otherwise the output is <b>false</b>.
420</p>
421</html>"));
422
423  equation
424    y = u1 < u2;
425  end Less;
426
427  block LessEqual "Output y is true, if input u1 is less or equal as input u2"
428    extends Blocks.Interfaces.partialBooleanComparison;
429
430    annotation (Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,
431              -100},{100,100}}), graphics={
432          Ellipse(extent={{32,10},{52,-10}}, lineColor={0,0,255}),
433          Line(points={{-100,-80},{42,-80},{42,0}}, color={0,0,255}),
434          Text(
435            extent={{-80,-60},{20,60}},
436            lineColor={0,0,0},
437            textString="<=")}),
438        Documentation(info="<html>
439<p>
440The output is <b>true</b> if Real input u1 is less than or equal to
441Real input u2, otherwise the output is <b>false</b>.
442</p>
443</html>"));
444
445  equation
446    y = u1 <= u2;
447  end LessEqual;
448
449  block ZeroCrossing "Trigger zero crossing of input u"
450    extends Blocks.Interfaces.partialBooleanSO;
451    Blocks.Interfaces.RealInput u annotation (Placement(transformation(extent={
452              {-140,-20},{-100,20}}, rotation=0)));
453    Blocks.Interfaces.BooleanInput enable
454      "Zero input crossing is triggered if the enable input signal is true" 
455      annotation (Placement(transformation(
456          origin={0,-120},
457          extent={{-20,-20},{20,20}},
458          rotation=90)));
459
460    annotation (Documentation(info="<HTML>
461<p>
462The output \"y\" is <b>true</b> at the
463time instant when the input \"u\" becomes
464zero, provided the input \"enable\" is
465<b>true</b>. At all other time instants, the output \"y\" is <b>false</b>.
466If the input \"u\" is zero at a time instant when the \"enable\"
467input changes its value, then the output y is <b>false</b>.
468</p>
469<p>
470Note, that in the plot window of a Modelica simulator, the output of
471this block is usually identically to <b>false</b>, because the output
472may only be <b>true</b> at an event instant, but not during
473continuous integration. In order to check that this component is
474actually working as expected, one should connect its output to, e.g.,
475component <i>ModelicaAdditions.Blocks.Discrete.TriggeredSampler</i>.
476</p>
477</HTML>"), Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},
478              {100,100}}), graphics={
479          Line(points={{-78,68},{-78,-80}}, color={192,192,192}),
480          Polygon(
481            points={{-78,90},{-86,68},{-70,68},{-78,90}},
482            lineColor={192,192,192},
483            fillColor={192,192,192},
484            fillPattern=FillPattern.Solid),
485          Line(points={{-88,0},{70,0}}, color={192,192,192}),
486          Line(points={{-78,0},{-73.2,32.3},{-70,50.3},{-66.7,64.5},{-63.5,74.2},
487                {-60.3,79.3},{-57.1,79.6},{-53.9,75.3},{-50.7,67.1},{-46.6,52.2},
488                {-41,25.8},{-33,-13.9},{-28.2,-33.7},{-24.1,-45.9},{-20.1,-53.2},
489                {-16.1,-55.3},{-12.1,-52.5},{-8.1,-45.3},{-3.23,-32.1},{10.44,
490                13.7},{15.3,26.4},{20.1,34.8},{24.1,38},{28.9,37.2},{33.8,31.8},
491                {40.2,19.4},{53.1,-10.5},{59.5,-21.2},{65.1,-25.9},{70.7,-25.9},
492                {77.2,-20.5},{82,-13.8}}, color={192,192,192}),
493          Polygon(
494            points={{92,0},{70,8},{70,-8},{92,0}},
495            lineColor={192,192,192},
496            fillColor={192,192,192},
497            fillPattern=FillPattern.Solid),
498          Line(points={{-36,-59},{-36,81}}, color={255,0,255}),
499          Line(points={{6,-59},{6,81}}, color={255,0,255}),
500          Line(points={{49,-59},{49,81}}, color={255,0,255}),
501          Line(points={{-78,0},{70,0}}, color={255,0,255})}));
502  protected
503    Boolean disable=not   enable;
504    Boolean u_pos;
505  initial equation
506    pre(u_pos)  = false;
507    pre(enable) = false;
508    pre(disable) =not  pre(enable);
509  equation
510    u_pos = enable and u >= 0;
511    y = change(u_pos) and not edge(enable) and not edge(disable);
512  end ZeroCrossing;
513
514  block LogicalSwitch "Logical Switch"
515    extends Blocks.Interfaces.partialBooleanSI3SO;
516
517    annotation (
518      Documentation(info="<html>
519<p>The LogicalSwitch switches, depending on the
520Boolean u2 connector (the middle connector),
521between the two possible input signals
522u1 (upper connector) and u3 (lower connector).</p>
523<p>If u2 is true, connector y is set equal to
524u1, else it is set equal to u2.</p>
525</html>
526"),   Icon(coordinateSystem(
527          preserveAspectRatio=true,
528          extent={{-100,-100},{100,100}},
529          grid={2,2}), graphics={
530          Line(
531            points={{12,0},{100,0}},
532            color={255,0,255},
533            pattern=LinePattern.Solid,
534            thickness=0.25,
535            arrow={Arrow.None,Arrow.None}),
536          Line(
537            points={{-100,0},{-40,0}},
538            color={255,0,255},
539            pattern=LinePattern.Solid,
540            thickness=0.25,
541            arrow={Arrow.None,Arrow.None}),
542          Line(
543            points={{-100,-80},{-40,-80},{-40,-80}},
544            color={255,0,255},
545            pattern=LinePattern.Solid,
546            thickness=0.25,
547            arrow={Arrow.None,Arrow.None}),
548          Line(points={{-40,12},{-40,-10}}, color={255,0,255}),
549          Line(points={{-100,80},{-40,80}}, color={255,0,255}),
550          Line(
551            points={{-40,80},{8,2}},
552            color={255,0,127},
553            thickness=1),
554          Ellipse(
555            extent={{2,8},{18,-6}},
556            fillColor={0,0,0},
557            fillPattern=FillPattern.Solid,
558            lineColor={0,0,255})}),
559      Diagram(coordinateSystem(
560          preserveAspectRatio=true,
561          extent={{-100,-100},{100,100}},
562          grid={2,2}), graphics));
563
564  equation
565    y = if u2 then u1 else u3;
566  end LogicalSwitch;
567
568  block Switch "Switch between two Real signals"
569    extends Blocks.Interfaces.partialBooleanBlockIcon;
570    Blocks.Interfaces.RealInput u1 "Connector of first Real input signal" 
571                                   annotation (Placement(transformation(extent=
572              {{-140,60},{-100,100}}, rotation=0)));
573    Blocks.Interfaces.BooleanInput u2 "Connector of Boolean input signal" 
574                                      annotation (Placement(transformation(
575            extent={{-140,-20},{-100,20}}, rotation=0)));
576    Blocks.Interfaces.RealInput u3 "Connector of second Real input signal" 
577                                   annotation (Placement(transformation(extent=
578              {{-140,-100},{-100,-60}}, rotation=0)));
579    Blocks.Interfaces.RealOutput y "Connector of Real output signal" 
580                                   annotation (Placement(transformation(extent=
581              {{100,-10},{120,10}}, rotation=0)));
582
583    annotation (defaultComponentName="switch1",
584      Documentation(info="<html>
585<p>The Logical.Switch switches, depending on the
586logical connector u2 (the middle connector)
587between the two possible input signals
588u1 (upper connector) and u3 (lower connector).</p>
589<p>If u2 is <b>true</b>, the output signal y is set equal to
590