RelativeStates

model RelativeStates "Definition of relative state variables"
    extends Translational.Interfaces.PartialTwoFlanges;

    parameter StateSelect stateSelect = StateSelect.prefer "Priority to use the relative angle and relative speed as states";
    SI.Position s_rel(start = 0, stateSelect = StateSelect.prefer) "Relative position used as state variable";
    SI.Velocity v_rel(start = 0, stateSelect = StateSelect.prefer) "Relative velocity used as state variable";
    SI.Acceleration a_rel(start = 0) "Relative angular acceleration";
equation
    a_rel = der(v_rel);
    s_rel = flange_b.s - flange_a.s;
    v_rel = der(s_rel);
    flange_a.f = 0;
    flange_b.f = 0;

    annotation (
        Documentation(info = "<html>\n<p>\nUsually, the absolute position and the absolute velocity of\nModelica.Mechanics.Translational.Inertia models are used as state variables.\nIn some circumstances, relative quantities are better suited, e.g.,\nbecause it may be easier to supply initial values.\nIn such cases, model <strong>RelativeStates</strong> allows the definition of state variables\nin the following way:\n</p>\n<ul>\n<li> Connect an instance of this model between two flange connectors.</li>\n<li> The <strong>relative position</strong> and the <strong>relative velocity</strong>\n     between the two connectors are used as <strong>state variables</strong>.</li>\n</ul>\n<p>\nAn example is given in the next figure\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Mechanics/Translational/relativeStates2.png\" alt=\"relativeStates2\">\n</p>\n\n<p>\nHere, the relative position and the relative velocity between\nthe two masses are used as state variables. Additionally, the\nsimulator selects either the absolute position and absolute\nvelocity of model mass1 or of model mass2 as state variables.\n</p>\n\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-100, 0}, 
                        {100, 0}},
                    pattern = LinePattern.Dot,
                    color = {0, 127, 0}), 
                Ellipse(
                    extent = {
                        {-40, 40}, 
                        {40, -40}},
                    lineColor = {52, 219, 218},
                    fillColor = {52, 219, 218},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-40, 40}, 
                        {40, -40}},
                    textString = "S",
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {-150, 90}, 
                        {150, 50}},
                    textString = "%name",
                    lineColor = {0, 0, 255})}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-100, 0}, 
                        {100, 0}},
                    pattern = LinePattern.Dash,
                    color = {0, 127, 0}), 
                Ellipse(
                    extent = {
                        {-40, 40}, 
                        {40, -40}},
                    lineColor = {52, 219, 218},
                    fillColor = {52, 219, 218},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-40, 40}, 
                        {40, -40}},
                    textString = "S",
                    lineColor = {0, 0, 255}), 
                Line(
                    points = {
                        {-100, -10}, 
                        {-100, -80}},
                    color = {160, 160, 164}), 
                Line(
                    points = {
                        {100, -10}, 
                        {100, -80}},
                    color = {160, 160, 164}), 
                Polygon(
                    points = {
                        {80, -65}, 
                        {80, -55}, 
                        {100, -60}, 
                        {80, -65}},
                    lineColor = {160, 160, 164},
                    fillColor = {160, 160, 164},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-100, -60}, 
                        {80, -60}},
                    color = {160, 160, 164}), 
                Text(
                    extent = {
                        {-30, -70}, 
                        {30, -90}},
                    textString = "v_rel",
                    lineColor = {0, 0, 255}), 
                Line(
                    points = {
                        {-76, 80}, 
                        {-5, 80}},
                    color = {128, 128, 128}), 
                Polygon(
                    points = {
                        {14, 80}, 
                        {-6, 85}, 
                        {-6, 75}, 
                        {14, 80}},
                    lineColor = {128, 128, 128},
                    fillColor = {128, 128, 128},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {18, 87}, 
                        {86, 74}},
                    lineColor = {128, 128, 128},
                    textString = "moving direction")}));
end RelativeStates;