Skip to content
RelativeSensorsTest.md

RelativeSensorsTest

A test rig for sensors measuring relative translational motion between two independently forced masses.

This test component sets up a dynamic system to verify the behavior of relative motion sensors. It features two 1kg masses, body1 and body2, initially at rest and starting from the same position. body1 is subjected to a constant force F_1 = 1\,\text{N} and body2 is subjected to a constant force F_2 = 10\,\text{N}. Both forces are applied with respect to a fixed inertial frame. Consequently, body1 experiences a constant acceleration math a_1 = F_1/m_1 = 1\,\text{m/s}^2 and body2 experiences a constant acceleration math a_2 = F_2/m_2 = 10\,\text{m/s}^2. Three types of sensors measure the relative motion, defined as the motion of body2 with respect to body1:

  • Relative position: math s_{rel}(t) = s_2(t) - s_1(t)

  • Relative speed: math v_{rel}(t) = v_2(t) - v_1(t) = \frac{d}{dt}s_{rel}(t)

  • Relative acceleration: math a_{rel}(t) = a_2(t) - a_1(t) = \frac{d}{dt}v_{rel}(t)

Usage

RelativeSensorsTest()

Behavior

[constant1.y(t)=force1.f(t)constant2.y(t)=force2.f(t)connect(fixed+flange,force1+flangea,force2+flangea)connect(force1+flangeb,body1+flangea)connect(force2+flangeb,body2+flangea)connect(body1+flangeb,relativespeed_sensor+flange_a,relativeposition_sensor+flange_a,relativeacceleration_sensor+flange_a)connect(body2+flangeb,relativespeed_sensor+flange_b,relativeposition_sensor+flange_b,relativeacceleration_sensor+flange_b)body1.flange_a.s(t)=12body1.L+body1.s(t)body1.flange_b.s(t)=12body1.L+body1.s(t)body1.v(t)=dbody1.s(t)dtbody1.a(t)=dbody1.v(t)dt(body1.a(t)+body1.gsin(body1.theta))body1.m=body1.flange_b.f(t)+body1.flange_a.f(t)body2.flange_a.s(t)=12body2.L+body2.s(t)body2.flange_b.s(t)=12body2.L+body2.s(t)body2.v(t)=dbody2.s(t)dtbody2.a(t)=dbody2.v(t)dt(body2.a(t)+body2.gsin(body2.theta))body2.m=body2.flange_a.f(t)+body2.flange_b.f(t)force1.flange_a.f(t)=force1.f(t)force1.flange_b.f(t)=force1.f(t)force2.flange_a.f(t)=force2.f(t)force2.flange_b.f(t)=force2.f(t)fixed.flange.s(t)=fixed.s0constant1.y(t)=constant1.kconstant2.y(t)=constant2.k0=relative_speed_sensor.flange_a.f(t)+relative_speed_sensor.flange_b.f(t)relative_speed_sensor.s_rel(t)=relative_speed_sensor.flange_b.s(t)relative_speed_sensor.flange_a.s(t)relative_speed_sensor.v_rel(t)=drelative_speed_sensor.s_rel(t)dt0=relative_speed_sensor.flange_a.f(t)0=relative_position_sensor.flange_b.f(t)+relative_position_sensor.flange_a.f(t)relative_position_sensor.s_rel(t)=relative_position_sensor.flange_b.s(t)relative_position_sensor.flange_a.s(t)0=relative_position_sensor.flange_a.f(t)0=relative_acceleration_sensor.flange_b.f(t)+relative_acceleration_sensor.flange_a.f(t)relative_acceleration_sensor.s_rel(t)=relative_acceleration_sensor.flange_b.s(t)relative_acceleration_sensor.flange_a.s(t)relative_acceleration_sensor.v_rel(t)=drelative_acceleration_sensor.s_rel(t)dtrelative_acceleration_sensor.a_rel(t)=drelative_acceleration_sensor.v_rel(t)dt0=relative_acceleration_sensor.flange_a.f(t)]

Source

dyad
# A test rig for sensors measuring relative translational motion between two independently forced masses.
#
# This test component sets up a dynamic system to verify the behavior of relative motion sensors. It features two 1kg masses, `body1` and `body2`, initially at rest and starting from the same position.
# `body1` is subjected to a constant force `F_1 = 1\,\text{N}` and `body2` is subjected to a constant force `F_2 = 10\,\text{N}`. Both forces are applied with respect to a fixed inertial frame.
# Consequently, `body1` experiences a constant acceleration ```math a_1 = F_1/m_1 = 1\,\text{m/s}^2 ``` and `body2` experiences a constant acceleration ```math a_2 = F_2/m_2 = 10\,\text{m/s}^2 ```.
# Three types of sensors measure the relative motion, defined as the motion of `body2` with respect to `body1`:
# - Relative position: ```math s_{rel}(t) = s_2(t) - s_1(t) ```
# - Relative speed: ```math v_{rel}(t) = v_2(t) - v_1(t) = \frac{d}{dt}s_{rel}(t) ```
# - Relative acceleration: ```math a_{rel}(t) = a_2(t) - a_1(t) = \frac{d}{dt}v_{rel}(t) ```
test component RelativeSensorsTest
  # First translational mass (1kg) in the test setup.
  body1 = Mass(m=1, L=0)
  # Second translational mass (1kg) in the test setup.
  body2 = Mass(m=1, L=0)
  # Force source applying force to body1.
  force1 = Force()
  # Force source applying force to body2.
  force2 = Force()
  # Fixed inertial reference frame for the applied forces.
  fixed = Fixed()
  # Constant signal (1.0, typically Force [N]) for force1.
  constant1 = BlockComponents.Constant(k=1)
  # Constant signal (10.0, typically Force [N]) for force2.
  constant2 = BlockComponents.Constant(k=10)
  # Sensor measuring the relative speed between body1 and body2 (v_body2 - v_body1).
  relative_speed_sensor = RelativeSpeedSensor()
  # Sensor measuring the relative position between body1 and body2 (s_body2 - s_body1).
  relative_position_sensor = RelativePositionSensor()
  # Sensor measuring the relative acceleration between body1 and body2 (a_body2 - a_body1).
  relative_acceleration_sensor = RelativeAccelerationSensor()
relations
  connect(constant1.y, force1.f)
  connect(constant2.y, force2.f)
  connect(fixed.flange, force1.flange_a, force2.flange_a)
  connect(force1.flange_b, body1.flange_a)
  connect(force2.flange_b, body2.flange_a)
  connect(body1.flange_b, relative_speed_sensor.flange_a, relative_position_sensor.flange_a, relative_acceleration_sensor.flange_a)
  connect(body2.flange_b, relative_speed_sensor.flange_b, relative_position_sensor.flange_b, relative_acceleration_sensor.flange_b)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "initial": {"body1.s": 0, "body2.s": 0},
        "expect": {
          "final": {
            "relative_position_sensor.s_rel": 112.5,
            "relative_speed_sensor.v_rel": 44.99999999,
            "relative_acceleration_sensor.a_rel": 9
          }
        }
      }
    }
  }
}
end
Flattened Source
dyad
# A test rig for sensors measuring relative translational motion between two independently forced masses.
#
# This test component sets up a dynamic system to verify the behavior of relative motion sensors. It features two 1kg masses, `body1` and `body2`, initially at rest and starting from the same position.
# `body1` is subjected to a constant force `F_1 = 1\,\text{N}` and `body2` is subjected to a constant force `F_2 = 10\,\text{N}`. Both forces are applied with respect to a fixed inertial frame.
# Consequently, `body1` experiences a constant acceleration ```math a_1 = F_1/m_1 = 1\,\text{m/s}^2 ``` and `body2` experiences a constant acceleration ```math a_2 = F_2/m_2 = 10\,\text{m/s}^2 ```.
# Three types of sensors measure the relative motion, defined as the motion of `body2` with respect to `body1`:
# - Relative position: ```math s_{rel}(t) = s_2(t) - s_1(t) ```
# - Relative speed: ```math v_{rel}(t) = v_2(t) - v_1(t) = \frac{d}{dt}s_{rel}(t) ```
# - Relative acceleration: ```math a_{rel}(t) = a_2(t) - a_1(t) = \frac{d}{dt}v_{rel}(t) ```
test component RelativeSensorsTest
  # First translational mass (1kg) in the test setup.
  body1 = Mass(m=1, L=0)
  # Second translational mass (1kg) in the test setup.
  body2 = Mass(m=1, L=0)
  # Force source applying force to body1.
  force1 = Force()
  # Force source applying force to body2.
  force2 = Force()
  # Fixed inertial reference frame for the applied forces.
  fixed = Fixed()
  # Constant signal (1.0, typically Force [N]) for force1.
  constant1 = BlockComponents.Constant(k=1)
  # Constant signal (10.0, typically Force [N]) for force2.
  constant2 = BlockComponents.Constant(k=10)
  # Sensor measuring the relative speed between body1 and body2 (v_body2 - v_body1).
  relative_speed_sensor = RelativeSpeedSensor()
  # Sensor measuring the relative position between body1 and body2 (s_body2 - s_body1).
  relative_position_sensor = RelativePositionSensor()
  # Sensor measuring the relative acceleration between body1 and body2 (a_body2 - a_body1).
  relative_acceleration_sensor = RelativeAccelerationSensor()
relations
  connect(constant1.y, force1.f)
  connect(constant2.y, force2.f)
  connect(fixed.flange, force1.flange_a, force2.flange_a)
  connect(force1.flange_b, body1.flange_a)
  connect(force2.flange_b, body2.flange_a)
  connect(body1.flange_b, relative_speed_sensor.flange_a, relative_position_sensor.flange_a, relative_acceleration_sensor.flange_a)
  connect(body2.flange_b, relative_speed_sensor.flange_b, relative_position_sensor.flange_b, relative_acceleration_sensor.flange_b)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "initial": {"body1.s": 0, "body2.s": 0},
        "expect": {
          "final": {
            "relative_position_sensor.s_rel": 112.5,
            "relative_speed_sensor.v_rel": 44.99999999,
            "relative_acceleration_sensor.a_rel": 9
          }
        }
      }
    }
  }
}
end


Test Cases

This is setup code, that must be run before each test case.

julia
using TranslationalComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(TranslationalComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/TranslationalComponents/khJb7/test/snapshots"

Test Case case1

julia
@mtkbuild model_case1 = RelativeSensorsTest()
u0_case1 = [model_case1.body1.s => 0, model_case1.body2.s => 0]
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 5))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 7-element Vector{Float64}:
 0.0
 9.999999999999999e-5
 0.0010999999999999998
 0.011099999999999997
 0.11109999999999996
 1.1110999999999995
 5.0
u: 7-element Vector{Vector{Float64}}:
 [-0.0, 0.0, -0.0, 0.0]
 [9.999999999999996e-5, 4.9999999999999695e-9, 0.0009999999999999998, 5.0000000000000004e-8]
 [0.0010999999999999996, 6.049999999999966e-7, 0.010999999999999996, 6.049999999999997e-6]
 [0.011099999999999994, 6.160499999999969e-5, 0.11099999999999995, 0.0006160500000000001]
 [0.11109999999999993, 0.00617160499999997, 1.1109999999999993, 0.061716049999999974]
 [1.1110999999999993, 0.6172716049999964, 11.110999999999994, 6.1727160499999965]
 [4.999999999999999, 12.499999999999947, 49.99999999999999, 125.00000000000007]