Sign

block Sign "Output the sign of the input"
    extends Interfaces.SISO;

    parameter Boolean generateEvent = false "Choose whether events shall be generated"
        annotation (Evaluate = true);
equation
    y = if generateEvent then if 0 < u then 1 else if u < 0 then -1 else 0 else if noEvent(0 < u) then 1 else if noEvent(u < 0) then -1 else 0;

    annotation (
        defaultComponentName = "sign1",
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-90, 0}, 
                        {68, 0}},
                    color = {192, 192, 192}), 
                Polygon(
                    points = {
                        {90, 0}, 
                        {68, 8}, 
                        {68, -8}, 
                        {90, 0}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(points = {
                    {-80, -80}, 
                    {0, -80}}), 
                Polygon(
                    points = {
                        {0, 90}, 
                        {-8, 68}, 
                        {8, 68}, 
                        {0, 90}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {0, -80}, 
                        {0, 68}},
                    color = {192, 192, 192}), 
                Text(
                    extent = {
                        {-90, 72}, 
                        {-18, 24}},
                    lineColor = {192, 192, 192},
                    textString = "sign"), 
                Line(points = {
                    {0, 80}, 
                    {80, 80}}), 
                Rectangle(
                    extent = {
                        {-2, 2}, 
                        {2, -4}},
                    fillPattern = FillPattern.Solid)}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-90, 0}, 
                        {68, 0}},
                    color = {192, 192, 192}), 
                Polygon(
                    points = {
                        {90, 0}, 
                        {68, 8}, 
                        {68, -8}, 
                        {90, 0}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(points = {
                    {-80, -80}, 
                    {0, -80}}), 
                Line(points = {
                    {-0.01, 0}, 
                    {0.01, 0}}), 
                Line(points = {
                    {0, 80}, 
                    {80, 80}}), 
                Rectangle(
                    extent = {
                        {-2, 2}, 
                        {2, -4}},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {0, 100}, 
                        {-6, 84}, 
                        {6, 84}, 
                        {0, 100}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {0, -90}, 
                        {0, 84}},
                    color = {192, 192, 192}), 
                Text(
                    extent = {
                        {7, 102}, 
                        {32, 82}},
                    lineColor = {160, 160, 164},
                    textString = "y"), 
                Text(
                    extent = {
                        {70, -6}, 
                        {94, -26}},
                    lineColor = {160, 160, 164},
                    textString = "u"), 
                Text(
                    extent = {
                        {-25, 86}, 
                        {-5, 70}},
                    textString = "1",
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {5, -72}, 
                        {25, -88}},
                    textString = "-1",
                    lineColor = {0, 0, 255})}),
        Documentation(info = "<html>\n<p>\nThis blocks computes the output <strong>y</strong>\nas <strong>sign</strong> of the input <strong>u</strong>:\n</p>\n<pre>\n         1  <strong>if</strong> u &gt; 0\n    y =  0  <strong>if</strong> u == 0\n        -1  <strong>if</strong> u &lt; 0\n</pre>\n<p>\nThe Boolean parameter generateEvent decides whether Events are generated at zero crossing (Modelica specification before 3) or not.\n</p>\n</html>"));
end Sign;