SignTorque

model SignTorque "Constant torque changing sign with speed"
    import Modelica.Constants.pi;

    extends Rotational.Interfaces.PartialTorque;

    parameter Modelica.SIunits.Torque tau_constant "Constant torque (if negative, torque is acting as load)";
    parameter Modelica.Blocks.Types.Regularization reg = Modelica.Blocks.Types.Regularization.Exp "Type of regularization"
        annotation (Evaluate = true);
    parameter Modelica.SIunits.AngularVelocity w0(final min = Modelica.Constants.eps, start = 0.1) "Regularization below w0";
    Modelica.SIunits.AngularVelocity w "Angular velocity of flange with respect to support (= der(phi))";
    Modelica.SIunits.Torque tau "Accelerating torque acting at flange (= -flange.tau)";
equation
    if reg == Modelica.Blocks.Types.Regularization.Exp then 
        tau = tau_constant * (2 / (1 + Modelica.Math.exp(-w / (0.01 * w0))) - 1);
    elseif reg == Modelica.Blocks.Types.Regularization.Sine then 
        tau = tau_constant * smooth(1, if w0 <= abs(w) then sign(w) else Modelica.Math.sin(0.5 * pi * w / w0));
    elseif reg == Modelica.Blocks.Types.Regularization.Linear then 
        tau = tau_constant * (if w0 <= abs(w) then sign(w) else w / w0);
    else 
        tau = tau_constant * (if w0 <= abs(w) then sign(w) else sign(w) * (1 - Modelica.Math.cos(0.5 * pi * w / w0)));
    end if;
    w = der(phi);
    tau = -flange.tau;

    annotation (
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Text(
                    extent = {
                        {-120, -50}, 
                        {120, -20}},
                    textString = "%tau_constant"), 
                Line(
                    points = {
                        {-75, 24}, 
                        {75, 24}},
                    color = {192, 192, 192}), 
                Line(
                    points = {
                        {0, 66}, 
                        {0, -20}},
                    color = {192, 192, 192}), 
                Line(points = {
                    {-74, -12}, 
                    {-8, -12}, 
                    {-6, -10}, 
                    {6, 58}, 
                    {8, 60}, 
                    {48, 60}})}),
        Documentation(info = "<html>\n<p>Model of constant torque which changes sign with direction of rotation.</p>\n<p>Please note:<br>\nPositive torque accelerates in both directions of rotation.<br>\nNegative torque brakes in both directions of rotation.</p>\n<p>Around zero speed regularization avoids numerical problems.</p>\n</html>"));
end SignTorque;