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
full_equations(sys) #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)
  "Force source"
  force = TranslationalComponents.Sources.Force()
  "Ground for force source"
  force_ground = TranslationalComponents.Components.Fixed()
  "Force sensor"
  force_sensor = TranslationalComponents.Sensors.ForceSensor()
  "Multi-sensor (force, position, speed, acceleration, power)"
  multi_sensor = TranslationalComponents.Sensors.MultiSensor()
  "Mass"
  mass = TranslationalComponents.Components.Mass(L = 1, m = 1, s = initial 0, v = initial 0)
  "Position sensor at flange_a"
  position_sensor1 = TranslationalComponents.Sensors.PositionSensor()
  "Speed sensor"
  speed_sensor1 = TranslationalComponents.Sensors.SpeedSensor()
  "Acceleration sensor"
  acc_sensor1 = TranslationalComponents.Sensors.AccelerationSensor()
  "Position sensor at flange_b"
  position_sensor2 = TranslationalComponents.Sensors.PositionSensor()
relations
  connect(sine_force.y, force.f)
  connect(force.flange_a, force_ground.flange)
  connect(force.flange_b, force_sensor.flange_a)
  connect(force_sensor.flange_b, multi_sensor.flange_a)
  connect(multi_sensor.flange_b, mass.flange_a)
  connect(mass.flange_a, position_sensor1.flange)
  connect(mass.flange_a, speed_sensor1.flange)
  connect(mass.flange_a, acc_sensor1.flange)
  connect(mass.flange_b, position_sensor2.flange)
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)
  "Force source"
  force = TranslationalComponents.Sources.Force()
  "Ground for force source"
  force_ground = TranslationalComponents.Components.Fixed()
  "Force sensor"
  force_sensor = TranslationalComponents.Sensors.ForceSensor()
  "Multi-sensor (force, position, speed, acceleration, power)"
  multi_sensor = TranslationalComponents.Sensors.MultiSensor()
  "Mass"
  mass = TranslationalComponents.Components.Mass(L = 1, m = 1, s = initial 0, v = initial 0)
  "Position sensor at flange_a"
  position_sensor1 = TranslationalComponents.Sensors.PositionSensor()
  "Speed sensor"
  speed_sensor1 = TranslationalComponents.Sensors.SpeedSensor()
  "Acceleration sensor"
  acc_sensor1 = TranslationalComponents.Sensors.AccelerationSensor()
  "Position sensor at flange_b"
  position_sensor2 = TranslationalComponents.Sensors.PositionSensor()
relations
  connect(sine_force.y, force.f)
  connect(force.flange_a, force_ground.flange)
  connect(force.flange_b, force_sensor.flange_a)
  connect(force_sensor.flange_b, multi_sensor.flange_a)
  connect(multi_sensor.flange_b, mass.flange_a)
  connect(mass.flange_a, position_sensor1.flange)
  connect(mass.flange_a, speed_sensor1.flange)
  connect(mass.flange_a, acc_sensor1.flange)
  connect(mass.flange_b, position_sensor2.flange)
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 >>