InertialDelaySensitive

model InertialDelaySensitive "Provide the input as output if it holds its value for a specific amount of time"
    import D = Modelica.Electrical.Digital;
    import L = Modelica.Electrical.Digital.Interfaces.Logic;

    extends D.Interfaces.SISO(x(start = L.'U', fixed = true), y(start = y0, fixed = true));

    parameter SI.Time tLH(start = 0) "rise inertial delay";
    parameter SI.Time tHL(start = 0) "fall inertial delay";
    parameter D.Interfaces.Logic y0 = L.'U' "initial value of output";
protected
    constant Integer delayTable[L,L] = Modelica.Electrical.Digital.Tables.DelayTable "specification of delay according to signal change";
    SI.Time delayTime;
    D.Interfaces.Logic y_auxiliary(start = y0, fixed = true);
    D.Interfaces.Logic y_old(start = y0, fixed = true);
    Integer lh;
    discrete SI.Time t_next;
algorithm
    when {initial(), (0 < tLH or 0 < tHL) and change(x) and not initial()} then 
        y_old := if initial() or pre(y) == L.'U' then y0 else pre(y);
        lh := delayTable[y_old,x];
        delayTime := if 0 < lh then tLH else if lh < 0 then tHL else 0;
        t_next := time + delayTime;
        if lh == 0 or abs(delayTime) < Modelica.Constants.small then 
            y_auxiliary := x;
        end if;
    elsewhen t_next <= time then 
        y_auxiliary := x;
    end when;
    y := if 0 < tLH or 0 < tHL then y_auxiliary else x;

    annotation (
        Documentation(
            info = "<html>\n<p>\nProvides the input as output delayed by <em>Tdel</em> if the input holds its value for a longer time than <em>Tdel</em>.\nIf the time is less than <em>Tdel</em> the initial value <em>y0</em> holds.<br>\nThe delay <em>Tdel</em> depends on the values of the signal change. To calculate <em>Tdel</em>, the DelayTable specified in\nDigital.Tables is used. If the corresponding value is 1, then <em>tLH</em> is used, if it is -1, then <em>tHL</em>\nis used, if it is zero, the input is not delayed.\n</p>\n</html>",
            revisions = "<html>\n<ul>\n<li><em>January 24, 2013</em> Initial value for y set to y0\n       by Kristin Majetta and Christoph Clauss<br>\n       </li>\n<li><em>September 8, 2009</em> pre(y) and x are used to select <em>tHL</em> or <em>tLH</em>\n       by Ulrich Donath<br>\n       </li>\n<li><em>January 13, 2005</em> improved when-conditions and declaration of delayTable\n       by Dynasim<br>\n       </li>\n<li><em>September 15, 2004</em> color changed, names changed\n       by Christoph Clauss<br>\n       </li>\n<li><em>May 12, 2004</em> test <em>if Tdel=0</em> replaced\n       by Christoph Clauss<br>\n       </li>\n<li><em>February 5, 2004</em> handling of <em>tHL=0</em> or <em>tLH=0</em> revised\n       by Christoph Clauss<br>\n       </li>\n<li><em>October 12, 2003</em>\n       by Christoph Clauss<br>\n       initially modelled</li>\n</ul>\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), 
                Text(
                    extent = {
                        {-50, -40}, 
                        {50, -20}},
                    textString = "Inertial"), 
                Text(
                    extent = {
                        {-50, -60}, 
                        {50, -40}},
                    textString = "Delay"), 
                Text(
                    extent = {
                        {-50, -80}, 
                        {50, -60}},
                    textString = "sensitive"), 
                Polygon(points = {
                    {-6, 60}, 
                    {-16, 40}, 
                    {4, 40}, 
                    {-6, 60}}), 
                Line(points = {
                    {10, 60}, 
                    {10, 40}}), 
                Line(points = {
                    {0, 60}, 
                    {20, 60}})}));
end InertialDelaySensitive;