FirstOrderHold

block FirstOrderHold "First order hold of a sampled-data system"
    extends Modelica.Blocks.Interfaces.DiscreteSISO;

protected
    Modelica.SIunits.Time tSample;
    Real uSample;
    Real pre_uSample;
    Real c;
initial equation
    pre(c) = 0;
    pre(tSample) = time;
    pre(uSample) = u;
    pre(pre_uSample) = u;
equation
    when sampleTrigger then 
        tSample = time;
        uSample = u;
        pre_uSample = pre(uSample);
        c = if firstTrigger then 0 else (uSample - pre_uSample) / samplePeriod;
    end when;
    y = pre_uSample + pre(c) * (time - tSample);

    annotation (
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-79, -41}, 
                        {-59, -33}, 
                        {-40, 1}, 
                        {-20, 9}, 
                        {0, 63}, 
                        {21, 20}, 
                        {41, 10}, 
                        {60, 20}},
                    color = {0, 0, 127}), 
                Line(
                    points = {
                        {60, 20}, 
                        {81, 10}},
                    color = {0, 0, 127})}),
        Documentation(info = "<html>\n<p>\nThe output signal is the extrapolation through the\nvalues of the last two sampled input signals.\n</p>\n</html>"));
end FirstOrderHold;