Skip to content

Correct unit delay #4651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Modelica/Blocks/Discrete.mo
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,17 @@ values of the last two sampled input signals.
parameter Real y_start=0 "Initial value of output signal";
extends Interfaces.DiscreteSISO;

protected
Real u_internal;
equation
when sampleTrigger then
y = pre(u);
y = pre(u_internal);
u_internal = u;
end when;

initial equation
y = y_start;
y = y_start;
u_internal = y_start;
annotation (
Documentation(info="<html>
<p>
Expand All @@ -135,6 +139,8 @@ previous sample instant. Before the second sample instant,
the output y is identical to parameter yStart.
</p>

</html>", revisions="<html>
<p>May 23, 2025 - updated to work without requiring a sampled input, but work the same if the input was sampled.</p>
</html>"), Icon(
coordinateSystem(preserveAspectRatio=true,
extent={{-100.0,-100.0},{100.0,100.0}}),
Expand Down
5 changes: 5 additions & 0 deletions ModelicaTest/Blocks.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,8 @@ This shows the improvements in the numerics when balance=true is set.
annotation (Placement(transformation(extent={{0,40},{20,60}})));
Modelica.Blocks.Discrete.TriggeredMax triggeredMax
annotation (Placement(transformation(extent={{60,70},{80,90}})));
Modelica.Blocks.Discrete.UnitDelay unitDelay1(samplePeriod=samplePeriod)
annotation (Placement(transformation(extent={{0,0},{20,20}})));
equation
connect(sine.y, sampler.u)
annotation (Line(points={{-59,80},{-42,80}}, color={0,0,127}));
Expand All @@ -1864,6 +1866,9 @@ This shows the improvements in the numerics when balance=true is set.
{-52,80},{-52,96},{54,96},{54,80},{58,80}}, color={0,0,127}));
connect(booleanPulse.y, triggeredMax.trigger)
annotation (Line(points={{21,50},{70,50},{70,68.2}}, color={255,0,255}));
connect(sampler.y, unitDelay1.u) annotation (Line(points={{-19,80},{-12,80},
{-12,10},{-2,10}}, color={0,0,127}));
assert(abs(unitDelay.y-unitDelay1.y)<1e-3, "Should be the same");
annotation (experiment(StopTime=1.1));
end Discrete;

Expand Down