InertialDelay

block InertialDelay "Inertial delay with initial parameter"
    import D = Modelica.Electrical.Digital;
    import L = Modelica.Electrical.Digital.Interfaces.Logic;

    extends D.Interfaces.SISO;

    parameter SI.Time delayTime(start = 0) "Minimum time to hold value";
    parameter D.Interfaces.Logic y0 = L.'U' "Initial value of output y";
protected
    D.Interfaces.Logic y_auxiliary(start = y0, fixed = true);
    D.Interfaces.Logic x_old(start = y0, fixed = true);
    discrete SI.Time t_next(start = delayTime, fixed = true);
algorithm
    when 0 < delayTime and change(x) then 
        x_old := x;
        t_next := time + delayTime;
    elsewhen t_next <= time then 
        y_auxiliary := x;
    end when;
    y := if 0 < delayTime then y_auxiliary else x;

    annotation (
        Documentation(
            info = "<html>\n<p>\nProvides the input as output delayed by <em>delayTime</em> if the input holds its value for a longer time than <em>delayTime</em>.\nIf time is less than <em>delayTime</em> the initial value <em>y0</em> holds.\n</p>\n</html>",
            revisions = "<html>\n<dl>\n<dt><em>August 12, 2003</em></dt>\n<dd>by Christoph Clauss revised</dd>\n<dt><em>March 19, 2003</em></dt>\n<dd>by Martin Otter initially modelled.</dd>\n</dl>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Text(
                    extent = {
                        {152, -160}, 
                        {-148, -100}},
                    lineColor = {0, 0, 255},
                    textString = "%name"), 
                Rectangle(
                    extent = {
                        {-50, 100}, 
                        {50, -100}},
                    lineThickness = 0.5,
                    fillColor = {213, 170, 255},
                    fillPattern = FillPattern.Solid), 
                Polygon(points = {
                    {-6, 60}, 
                    {-16, 40}, 
                    {4, 40}, 
                    {-6, 60}}), 
                Line(points = {
                    {10, 60}, 
                    {10, 40}}), 
                Line(points = {
                    {0, 60}, 
                    {20, 60}}), 
                Text(
                    extent = {
                        {-50, -40}, 
                        {50, -20}},
                    textString = "Inertial"), 
                Text(
                    extent = {
                        {-50, -60}, 
                        {50, -40}},
                    textString = "Delay")}));
end InertialDelay;