SawTooth

block SawTooth "Generate saw tooth signal"
    parameter Real amplitude = 1 "Amplitude of saw tooth"
        annotation (Dialog(groupImage = "modelica://Modelica/Resources/Images/Blocks/Sources/SawTooth.png"));
    parameter SIunits.Time period(final min = Modelica.Constants.small, start = 1) "Time for one period";
    parameter Integer nperiod = -1 "Number of periods (< 0 means infinite number of periods)";

    extends Interfaces.SignalSource;

protected
    SIunits.Time T_start(final start = startTime) "Start time of current period";
    Integer count "Period count";
initial algorithm
    count := integer((time - startTime) / period);
    T_start := startTime + count * period;
equation
    when pre(count) < integer((time - startTime) / period) then 
        count = pre(count) + 1;
        T_start = time;
    end when;
    y = offset + (if time < startTime or nperiod == 0 or 0 < nperiod and nperiod <= count then 0 else amplitude * (time - T_start) / period);

    annotation (
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-80, 68}, 
                        {-80, -80}},
                    color = {192, 192, 192}), 
                Polygon(
                    points = {
                        {-80, 90}, 
                        {-88, 68}, 
                        {-72, 68}, 
                        {-80, 90}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-90, -70}, 
                        {82, -70}},
                    color = {192, 192, 192}), 
                Polygon(
                    points = {
                        {90, -70}, 
                        {68, -62}, 
                        {68, -78}, 
                        {90, -70}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(points = {
                    {-80, -70}, 
                    {-60, -70}, 
                    {0, 40}, 
                    {0, -70}, 
                    {60, 41}, 
                    {60, -70}}), 
                Text(
                    extent = {
                        {-147, -152}, 
                        {153, -112}},
                    textString = "period=%period")}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Polygon(
                    points = {
                        {-80, 90}, 
                        {-86, 68}, 
                        {-74, 68}, 
                        {-80, 90}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-80, 68}, 
                        {-80, -80}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-90, -70}, 
                        {82, -70}},
                    color = {95, 95, 95}), 
                Polygon(
                    points = {
                        {90, -70}, 
                        {68, -65}, 
                        {68, -75}, 
                        {90, -70}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {-34, -20}, 
                        {-37, -33}, 
                        {-31, -33}, 
                        {-34, -20}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-34, -20}, 
                        {-34, -70}},
                    color = {95, 95, 95}), 
                Polygon(
                    points = {
                        {-34, -70}, 
                        {-37, -57}, 
                        {-31, -57}, 
                        {-34, -70}, 
                        {-34, -70}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-65, -39}, 
                        {-29, -47}},
                    textString = "offset"), 
                Text(
                    extent = {
                        {-29, -72}, 
                        {13, -80}},
                    textString = "startTime"), 
                Text(
                    extent = {
                        {-82, 92}, 
                        {-43, 76}},
                    textString = "y"), 
                Text(
                    extent = {
                        {67, -78}, 
                        {88, -87}},
                    textString = "time"), 
                Line(
                    points = {
                        {-10, -20}, 
                        {-10, -70}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-10, 88}, 
                        {-10, -20}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {30, 88}, 
                        {30, 59}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-10, 83}, 
                        {30, 83}},
                    color = {95, 95, 95}), 
                Text(
                    extent = {
                        {-12, 94}, 
                        {34, 85}},
                    textString = "period"), 
                Line(
                    points = {
                        {-44, 60}, 
                        {30, 60}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-34, 47}, 
                        {-34, -20}},
                    color = {95, 95, 95}), 
                Text(
                    extent = {
                        {-73, 25}, 
                        {-36, 16}},
                    textString = "amplitude"), 
                Polygon(
                    points = {
                        {-34, 60}, 
                        {-37, 47}, 
                        {-31, 47}, 
                        {-34, 60}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {-34, -20}, 
                        {-37, -7}, 
                        {-31, -7}, 
                        {-34, -20}, 
                        {-34, -20}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {-10, 83}, 
                        {-1, 85}, 
                        {-1, 81}, 
                        {-10, 83}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {30, 83}, 
                        {22, 85}, 
                        {22, 81}, 
                        {30, 83}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-80, -20}, 
                        {-10, -20}, 
                        {30, 60}, 
                        {30, -20}, 
                        {72, 60}, 
                        {72, -20}},
                    color = {0, 0, 255},
                    thickness = 0.5)}),
        Documentation(info = "<html>\n<p>\nThe Real output y is a saw tooth signal:\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Blocks/Sources/SawTooth.png\"\n     alt=\"SawTooth.png\">\n</p>\n</html>"));
end SawTooth;