Skip to content
LIBRARY
Examples.Sensors.md

Examples.Sensors

Demonstrate sensors for translational systems.

Replicates the structure of Modelica.Mechanics.Translational.Examples.Sensors.

A sinusoidal force drives a mass. Various sensors measure the system response: force, position, speed, acceleration, and multi-sensor (combined).

Note: MSL uses a one-flange Force source with ForceSensor in series. Here we use the two-flange Force with flange_b connected to a Fixed ground. The ForceSensor is placed between the Force source and the mass.

Usage

TranslationalComponents.Examples.Sensors()

Behavior

julia
using TranslationalComponents #hide
using ModelingToolkit #hide
@named sys = TranslationalComponents.Examples.Sensors() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Demonstrate sensors for translational systems.

Replicates the structure of Modelica.Mechanics.Translational.Examples.Sensors.

A sinusoidal force drives a mass. Various sensors measure the system response:
force, position, speed, acceleration, and multi-sensor (combined).

Note: MSL uses a one-flange Force source with ForceSensor in series. Here we
use the two-flange Force with flange_b connected to a Fixed ground.
The ForceSensor is placed between the Force source and the mass.
"""
example component Sensors
  "Sinusoidal force signal"
  sine_force = BlockComponents.Sources.Sine(amplitude = 10, frequency = 4) {"Dyad": {"placement": {"diagram": {"x1": 20, "x2": 120, "y1": 30, "y2": 130}}}}
  "Force source"
  force = TranslationalComponents.Sources.Force() {
    "Dyad": {"placement": {"diagram": {"x1": 155, "x2": 255, "y1": 140, "y2": 240}}}
  }
  "Ground for force source"
  force_ground = TranslationalComponents.Components.Fixed() {
    "Dyad": {"placement": {"diagram": {"x1": 285, "x2": 385, "y1": 350, "y2": 450}}}
  }
  "Force sensor"
  force_sensor = TranslationalComponents.Sensors.ForceSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 285, "x2": 385, "y1": 140, "y2": 240}}}
  }
  "Multi-sensor (force, position, speed, acceleration, power)"
  multi_sensor = TranslationalComponents.Sensors.MultiSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 415, "x2": 515, "y1": 140, "y2": 240}}}
  }
  "Mass"
  mass = TranslationalComponents.Components.Mass(L = 1, m = 1, s(initial = 0), v(initial = 0)) {
    "Dyad": {"placement": {"diagram": {"x1": 545, "x2": 645, "y1": 140, "y2": 240}}}
  }
  "Position sensor at flange_a"
  position_sensor1 = TranslationalComponents.Sensors.PositionSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 680, "x2": 780, "y1": 20, "y2": 120}}}
  }
  "Speed sensor"
  speed_sensor1 = TranslationalComponents.Sensors.VelocitySensor() {
    "Dyad": {"placement": {"diagram": {"x1": 675, "x2": 775, "y1": 260, "y2": 360}}}
  }
  "Acceleration sensor"
  acc_sensor1 = TranslationalComponents.Sensors.AccelerationSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 675, "x2": 775, "y1": 380, "y2": 480}}}
  }
  "Position sensor at flange_b"
  position_sensor2 = TranslationalComponents.Sensors.PositionSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 675, "x2": 775, "y1": 140, "y2": 240}}}
  }
relations
  connect(sine_force.y, force.f) {"Dyad": {"edges": [{"S": 1, "M": [{"x": 205, "y": 80}], "E": 2}]}}
  connect(force.flange_a, force_ground.flange) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 140, "y": 190}, {"x": 140, "y": 295}, {"x": 335, "y": 295}],
          "E": 2
        }
      ]
    }
  }
  connect(force.flange_b, force_sensor.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(force_sensor.flange_b, multi_sensor.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(multi_sensor.flange_b, mass.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(mass.flange_a, position_sensor1.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 530, "y": 70}], "E": 2}],
      "junctions": [{"x": 530, "y": 190}]
    }
  }
  connect(mass.flange_a, speed_sensor1.flange) {
    "Dyad": {"edges": [{"S": 1, "M": [{"x": 530, "y": 190}, {"x": 530, "y": 310}], "E": 2}]}
  }
  connect(mass.flange_a, acc_sensor1.flange) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 530, "y": 190}, {"x": 530, "y": 310}], "E": -1},
        {"S": -1, "M": [{"x": 660, "y": 430}], "E": 2}
      ],
      "junctions": [{"x": 660, "y": 310}]
    }
  }
  connect(mass.flange_b, position_sensor2.flange) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "atol": {
          "mass.s": 0.001,
          "mass.v": 0.001,
          "speed_sensor1.v": 0.001,
          "acc_sensor1.a": 0.01
        },
        "expect": {
          "final": {"mass.s": 0.3978874, "mass.v": 0, "speed_sensor1.v": 0, "acc_sensor1.a": 0},
          "signals": ["mass.s", "mass.v", "speed_sensor1.v", "acc_sensor1.a"]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Demonstrate sensors for translational systems.

Replicates the structure of Modelica.Mechanics.Translational.Examples.Sensors.

A sinusoidal force drives a mass. Various sensors measure the system response:
force, position, speed, acceleration, and multi-sensor (combined).

Note: MSL uses a one-flange Force source with ForceSensor in series. Here we
use the two-flange Force with flange_b connected to a Fixed ground.
The ForceSensor is placed between the Force source and the mass.
"""
example component Sensors
  "Sinusoidal force signal"
  sine_force = BlockComponents.Sources.Sine(amplitude = 10, frequency = 4) {"Dyad": {"placement": {"diagram": {"x1": 20, "x2": 120, "y1": 30, "y2": 130}}}}
  "Force source"
  force = TranslationalComponents.Sources.Force() {
    "Dyad": {"placement": {"diagram": {"x1": 155, "x2": 255, "y1": 140, "y2": 240}}}
  }
  "Ground for force source"
  force_ground = TranslationalComponents.Components.Fixed() {
    "Dyad": {"placement": {"diagram": {"x1": 285, "x2": 385, "y1": 350, "y2": 450}}}
  }
  "Force sensor"
  force_sensor = TranslationalComponents.Sensors.ForceSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 285, "x2": 385, "y1": 140, "y2": 240}}}
  }
  "Multi-sensor (force, position, speed, acceleration, power)"
  multi_sensor = TranslationalComponents.Sensors.MultiSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 415, "x2": 515, "y1": 140, "y2": 240}}}
  }
  "Mass"
  mass = TranslationalComponents.Components.Mass(L = 1, m = 1, s(initial = 0), v(initial = 0)) {
    "Dyad": {"placement": {"diagram": {"x1": 545, "x2": 645, "y1": 140, "y2": 240}}}
  }
  "Position sensor at flange_a"
  position_sensor1 = TranslationalComponents.Sensors.PositionSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 680, "x2": 780, "y1": 20, "y2": 120}}}
  }
  "Speed sensor"
  speed_sensor1 = TranslationalComponents.Sensors.VelocitySensor() {
    "Dyad": {"placement": {"diagram": {"x1": 675, "x2": 775, "y1": 260, "y2": 360}}}
  }
  "Acceleration sensor"
  acc_sensor1 = TranslationalComponents.Sensors.AccelerationSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 675, "x2": 775, "y1": 380, "y2": 480}}}
  }
  "Position sensor at flange_b"
  position_sensor2 = TranslationalComponents.Sensors.PositionSensor() {
    "Dyad": {"placement": {"diagram": {"x1": 675, "x2": 775, "y1": 140, "y2": 240}}}
  }
relations
  connect(sine_force.y, force.f) {"Dyad": {"edges": [{"S": 1, "M": [{"x": 205, "y": 80}], "E": 2}]}}
  connect(force.flange_a, force_ground.flange) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 140, "y": 190}, {"x": 140, "y": 295}, {"x": 335, "y": 295}],
          "E": 2
        }
      ]
    }
  }
  connect(force.flange_b, force_sensor.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(force_sensor.flange_b, multi_sensor.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(multi_sensor.flange_b, mass.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(mass.flange_a, position_sensor1.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 530, "y": 70}], "E": 2}],
      "junctions": [{"x": 530, "y": 190}]
    }
  }
  connect(mass.flange_a, speed_sensor1.flange) {
    "Dyad": {"edges": [{"S": 1, "M": [{"x": 530, "y": 190}, {"x": 530, "y": 310}], "E": 2}]}
  }
  connect(mass.flange_a, acc_sensor1.flange) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 530, "y": 190}, {"x": 530, "y": 310}], "E": -1},
        {"S": -1, "M": [{"x": 660, "y": 430}], "E": 2}
      ],
      "junctions": [{"x": 660, "y": 310}]
    }
  }
  connect(mass.flange_b, position_sensor2.flange) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "atol": {
          "mass.s": 0.001,
          "mass.v": 0.001,
          "speed_sensor1.v": 0.001,
          "acc_sensor1.a": 0.01
        },
        "expect": {
          "final": {"mass.s": 0.3978874, "mass.v": 0, "speed_sensor1.v": 0, "acc_sensor1.a": 0},
          "signals": ["mass.s", "mass.v", "speed_sensor1.v", "acc_sensor1.a"]
        }
      }
    }
  }
}
end


Test Cases

julia
using TranslationalComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots

snapshotsdir = joinpath(dirname(dirname(pathof(TranslationalComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@named model_case1 = TranslationalComponents.Examples.Sensors()
model_case1 = toggle_namespacing(model_case1, false)

model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 1e+0, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)
<< @setup-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.Sensors_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass.s], width=2, label="Actual value of mass.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass.s")
end
scatter!(plt, [df_case1.t[end]], [0.3978874], label="Final Condition for `mass.s`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.Sensors_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass.v], width=2, label="Actual value of mass.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass.v")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `mass.v`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.speed_sensor1.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.Sensors_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.speed_sensor1.v], width=2, label="Actual value of speed_sensor1.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of speed_sensor1.v")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `speed_sensor1.v`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.acc_sensor1.a])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.Sensors_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.acc_sensor1.a], width=2, label="Actual value of acc_sensor1.a")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of acc_sensor1.a")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `acc_sensor1.a`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
<< @example-block not executed in draft mode >>