SecondOrder

block SecondOrder "Second order transfer function block (= 2 poles)"
    import Modelica.Blocks.Types.Init;

    parameter Real k(unit = "1") = 1 "Gain";
    parameter Real w(start = 1) "Angular frequency";
    parameter Real D(start = 1) "Damping";
    parameter Modelica.Blocks.Types.Init initType = Modelica.Blocks.Types.Init.NoInit "Type of initialization (1: no init, 2: steady state, 3/4: initial output)"
        annotation (
            Evaluate = true,
            Dialog(group = "Initialization"));
    parameter Real y_start = 0 "Initial or guess value of output (= state)"
        annotation (Dialog(group = "Initialization"));
    parameter Real yd_start = 0 "Initial or guess value of derivative of output (= state)"
        annotation (Dialog(group = "Initialization"));

    extends Interfaces.SISO(y(start = y_start));

    output Real yd(start = yd_start) "Derivative of y";
initial equation
    if initType == Init.SteadyState then 
        der(y) = 0;
        der(yd) = 0;
    elseif initType == Init.InitialState or initType == Init.InitialOutput then 
        y = y_start;
        yd = yd_start;
    end if;
equation
    der(y) = yd;
    der(yd) = w * (w * (k * u - y) - 2 * D * yd);

    annotation (
        Documentation(info = "<html>\n<p>\nThis blocks defines the transfer function between the input u and\nthe output y as <em>second order</em> system:\n</p>\n<pre>\n                             k\n     y = ---------------------------------------- * u\n            ( s / w )^2 + 2*D*( s / w ) + 1\n</pre>\n<p>\nIf you would like to be able to change easily between different\ntransfer functions (FirstOrder, SecondOrder, ... ) by changing\nparameters, use the general model class <strong>TransferFunction</strong>\ninstead and model a second order SISO system with parameters<br>\nb = {k}, a = {1/w^2, 2*D/w, 1}.\n</p>\n<pre>\nExample:\n\n   parameter: k =  0.3,  w = 0.5,  D = 0.4\n   results in:\n                  0.3\n      y = ------------------- * u\n          4.0 s^2 + 1.6 s + 1\n</pre>\n\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-80, 78}, 
                        {-80, -90}},
                    color = {192, 192, 192}), 
                Polygon(
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {-80, 90}, 
                        {-88, 68}, 
                        {-72, 68}, 
                        {-80, 90}}), 
                Line(
                    points = {
                        {-90, -80}, 
                        {82, -80}},
                    color = {192, 192, 192}), 
                Polygon(
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {90, -80}, 
                        {68, -72}, 
                        {68, -88}, 
                        {90, -80}}), 
                Line(
                    origin = {-1.939, -1.816},
                    points = {
                        {81.939, 36.056}, 
                        {65.362, 36.056}, 
                        {14.39, -26.199}, 
                        {-29.966, 113.485}, 
                        {-65.374, -61.217}, 
                        {-78.061, -78.184}},
                    color = {0, 0, 127},
                    smooth = Smooth.Bezier), 
                Text(
                    lineColor = {192, 192, 192},
                    extent = {
                        {0, -70}, 
                        {60, -10}},
                    textString = "PT2"), 
                Text(
                    extent = {
                        {-150, -150}, 
                        {150, -110}},
                    textString = "w=%w")}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Rectangle(
                    extent = {
                        {-60, 60}, 
                        {60, -60}},
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {-60, 60}, 
                        {60, 14}},
                    textString = "k"), 
                Text(
                    extent = {
                        {-60, 8}, 
                        {-32, -20}},
                    textString = "s"), 
                Line(
                    points = {
                        {-100, 0}, 
                        {-60, 0}},
                    color = {0, 0, 255}), 
                Line(
                    points = {
                        {60, 0}, 
                        {100, 0}},
                    color = {0, 0, 255}), 
                Line(points = {
                    {-50, 14}, 
                    {50, 14}}), 
                Line(points = {
                    {-54, -20}, 
                    {-38, -20}}), 
                Text(
                    extent = {
                        {-52, -26}, 
                        {-36, -48}},
                    textString = "w"), 
                Line(points = {
                    {-50, 2}, 
                    {-56, -8}, 
                    {-56, -28}, 
                    {-52, -46}}), 
                Line(points = {
                    {-40, 2}, 
                    {-34, -10}, 
                    {-34, -30}, 
                    {-38, -46}}), 
                Text(
                    extent = {
                        {-34, 8}, 
                        {-22, -10}},
                    textString = "2"), 
                Text(
                    extent = {
                        {-34, -6}, 
                        {6, -36}},
                    textString = "+2D"), 
                Text(
                    extent = {
                        {2, 8}, 
                        {30, -20}},
                    textString = "s"), 
                Line(points = {
                    {8, -20}, 
                    {24, -20}}), 
                Text(
                    extent = {
                        {10, -26}, 
                        {26, -48}},
                    textString = "w"), 
                Line(points = {
                    {12, 2}, 
                    {6, -8}, 
                    {6, -28}, 
                    {10, -46}}), 
                Line(points = {
                    {22, 2}, 
                    {28, -10}, 
                    {28, -30}, 
                    {24, -46}}), 
                Text(
                    extent = {
                        {30, 2}, 
                        {58, -42}},
                    textString = "+1")}));
end SecondOrder;