SensorsTest
Test environment for verifying absolute position, speed, and acceleration sensors monitoring a mass driven by a constant force.
This test component simulates a one-dimensional translational mechanical system to validate the behavior of absolute sensors. A mass (body
) is subjected to a constant force (force
, driven by constant1
). One side of the force element is connected to a fixed point (ground
), and the other side acts on the mass. The sensors (position_sensor
, speed_sensor
, acceleration_sensor
) are then attached to this mass to measure its absolute position, speed, and acceleration, respectively. The metadata
section includes a test case that checks if the sensor outputs match these expected dynamics.
Usage
SensorsTest()
Behavior
Source
# Test environment for verifying absolute position, speed, and acceleration sensors monitoring a mass driven by a constant force.
#
# This test component simulates a one-dimensional translational mechanical system to validate the behavior of absolute sensors.
# A mass (`body`) is subjected to a constant force (`force`, driven by `constant1`). One side of the force element is connected to
# a fixed point (`ground`), and the other side acts on the mass. The sensors (`position_sensor`, `speed_sensor`, `acceleration_sensor`)
# are then attached to this mass to measure its absolute position, speed, and acceleration, respectively.
# The `metadata` section includes a test case that checks if the sensor outputs match these expected dynamics.
test component SensorsTest
# Represents the translational mass being sensed.
body = Mass(m=1, L=0)
# Represents the force applied to the mass.
force = Force()
# Provides a constant signal input (k=1) to the force component.
constant1 = BlockComponents.Constant(k=1)
# Represents the fixed mechanical ground.
ground = Fixed()
# Sensor to measure the absolute speed of the connected flange.
speed_sensor = SpeedSensor()
# Sensor to measure the absolute position of the connected flange.
position_sensor = PositionSensor()
# Sensor to measure the absolute acceleration of the connected flange.
acceleration_sensor = AccelerationSensor()
relations
connect(constant1.y, force.f)
connect(force.flange_b, body.flange_a)
connect(force.flange_a, ground.flange)
connect(body.flange_b, speed_sensor.flange, position_sensor.flange, acceleration_sensor.flange)
metadata {
"Dyad": {
"tests": {
"case1": {
"stop": 5,
"initial": {"body.s": 0},
"expect": {
"final": {
"body.s": 12.49999999,
"body.v": 4.99999999,
"body.a": 1,
"position_sensor.s": 12.49999999,
"speed_sensor.v": 4.99999999,
"acceleration_sensor.a": 1
}
}
}
}
}
}
end
Flattened Source
# Test environment for verifying absolute position, speed, and acceleration sensors monitoring a mass driven by a constant force.
#
# This test component simulates a one-dimensional translational mechanical system to validate the behavior of absolute sensors.
# A mass (`body`) is subjected to a constant force (`force`, driven by `constant1`). One side of the force element is connected to
# a fixed point (`ground`), and the other side acts on the mass. The sensors (`position_sensor`, `speed_sensor`, `acceleration_sensor`)
# are then attached to this mass to measure its absolute position, speed, and acceleration, respectively.
# The `metadata` section includes a test case that checks if the sensor outputs match these expected dynamics.
test component SensorsTest
# Represents the translational mass being sensed.
body = Mass(m=1, L=0)
# Represents the force applied to the mass.
force = Force()
# Provides a constant signal input (k=1) to the force component.
constant1 = BlockComponents.Constant(k=1)
# Represents the fixed mechanical ground.
ground = Fixed()
# Sensor to measure the absolute speed of the connected flange.
speed_sensor = SpeedSensor()
# Sensor to measure the absolute position of the connected flange.
position_sensor = PositionSensor()
# Sensor to measure the absolute acceleration of the connected flange.
acceleration_sensor = AccelerationSensor()
relations
connect(constant1.y, force.f)
connect(force.flange_b, body.flange_a)
connect(force.flange_a, ground.flange)
connect(body.flange_b, speed_sensor.flange, position_sensor.flange, acceleration_sensor.flange)
metadata {
"Dyad": {
"tests": {
"case1": {
"stop": 5,
"initial": {"body.s": 0},
"expect": {
"final": {
"body.s": 12.49999999,
"body.v": 4.99999999,
"body.a": 1,
"position_sensor.s": 12.49999999,
"speed_sensor.v": 4.99999999,
"acceleration_sensor.a": 1
}
}
}
}
}
}
end
Test Cases
This is setup code, that must be run before each test case.
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
@mtkbuild model_case1 = SensorsTest()
u0_case1 = [model_case1.body.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]
[9.999999999999996e-5, 4.9999999999999695e-9]
[0.0010999999999999996, 6.049999999999966e-7]
[0.011099999999999994, 6.160499999999969e-5]
[0.11109999999999993, 0.00617160499999997]
[1.1110999999999993, 0.6172716049999964]
[4.999999999999999, 12.499999999999947]
Related
Examples
Experiments
Analyses
Tests