use assert2::check;
use nalgebra::UnitQuaternion;
use std::f32::consts::FRAC_PI_2;
use super::*;
fn pushed(
config: &WindConfig,
heading_rad: f32,
pitch_rad: f32,
velocity_mps: Vec2,
wind_mps: Vec2,
) -> VehicleState {
let attitude = UnitQuaternion::from_euler_angles(0.0, pitch_rad, heading_rad);
let velocity_ned = Vec3::new(velocity_mps.x, velocity_mps.y, 0.0);
let relative_body =
attitude.inverse_transform_vector(&(velocity_ned - Vec3::new(wind_mps.x, wind_mps.y, 0.0)));
let drag = |i: usize| {
let k = if config.ballistic_kgpm2[i] > 0.0 {
0.5 * AIR_DENSITY_KGPM3 / config.ballistic_kgpm2[i]
} else {
0.0
};
let v = relative_body[i];
-(k * v.abs() + config.momentum_per_s[i]) * v
};
let force_body = Vec3::new(drag(0), drag(1), -GRAVITY / libm::cosf(pitch_rad));
let specific_ned = attitude.transform_vector(&force_body);
let acceleration_ned = specific_ned + Vec3::new(0.0, 0.0, GRAVITY);
VehicleState {
attitude,
velocity_mps: Vec3::new(velocity_mps.x, velocity_mps.y, 0.0),
acceleration_mps2: Vec3::new(acceleration_ned.x, acceleration_ned.y, -acceleration_ned.z),
..Default::default()
}
}
fn estimate_after(config: &WindConfig, state: &VehicleState, seconds: f32) -> Option<WindEstimate> {
let mut estimator = WindEstimator::default();
for _ in 0..(seconds / 0.004).round() as usize {
estimator.update(0.004, config, state);
}
estimator.estimate()
}
const SETTLED_SD_MPS: f32 = 0.35;
fn momentum(per_s: f32) -> WindConfig {
WindConfig {
momentum_per_s: Vec2::new(per_s, per_s),
..Default::default()
}
}
fn ballistic(kgpm2: f32) -> WindConfig {
WindConfig {
ballistic_kgpm2: Vec2::new(kgpm2, kgpm2),
..Default::default()
}
}
#[test]
fn converges_on_the_wind_with_drag_on_both_axes() {
let both = WindConfig {
ballistic_kgpm2: Vec2::new(20.0, 20.0),
momentum_per_s: Vec2::new(0.2, 0.2),
..Default::default()
};
let cases = [
(
"momentum, wind east",
momentum(0.3),
0.0,
0.0,
Vec2::zeros(),
Vec2::new(0.0, 2.0),
),
(
"momentum, turned",
momentum(0.3),
1.0,
0.0,
Vec2::zeros(),
Vec2::new(1.5, -1.0),
),
(
"momentum, tilted",
momentum(0.3),
0.5,
0.3,
Vec2::zeros(),
Vec2::new(-2.0, 1.0),
),
(
"momentum, moving",
momentum(0.3),
0.0,
0.0,
Vec2::new(3.0, 1.0),
Vec2::new(-1.0, 2.0),
),
(
"calm, moving",
momentum(0.3),
0.0,
0.0,
Vec2::new(3.0, 0.0),
Vec2::zeros(),
),
(
"ballistic",
ballistic(10.0),
2.0,
0.0,
Vec2::zeros(),
Vec2::new(3.0, 3.0),
),
(
"both",
both,
-1.0,
0.0,
Vec2::new(1.0, 0.0),
Vec2::new(0.0, -4.0),
),
];
for (case, config, heading, pitch, velocity, wind) in cases {
let state = pushed(&config, heading, pitch, velocity, wind);
let estimate = estimate_after(&config, &state, 10.0).expect("updated");
let error = (estimate.velocity_mps - wind).norm();
check!(error < 0.01, "{case}: {estimate:?}, expected {wind}");
check!(
estimate.sd_along_mps(Vec2::x()) < SETTLED_SD_MPS,
"{case}: {estimate:?}"
);
}
}
#[test]
fn an_axis_without_drag_leaves_its_wind_unknown_until_a_turn() {
let x_only = WindConfig {
ballistic_kgpm2: Vec2::new(5.0, 0.0),
momentum_per_s: Vec2::new(0.3, 0.0),
..Default::default()
};
let east = Vec2::new(0.0, 2.0);
let facing_north = pushed(&x_only, 0.0, 0.0, Vec2::zeros(), east);
let facing_east = pushed(&x_only, FRAC_PI_2, 0.0, Vec2::zeros(), east);
let mut estimator = WindEstimator::default();
for _ in 0..2500 {
estimator.update(0.004, &x_only, &facing_north);
}
let unseen = estimator.estimate().expect("updated");
check!(unseen.velocity_mps.norm() < 0.01, "{unseen:?}");
check!(
unseen.sd_along_mps(Vec2::y()) > 0.9 * INITIAL_SD_MPS,
"{unseen:?}"
);
check!(
unseen.sd_along_mps(Vec2::x()) < SETTLED_SD_MPS,
"{unseen:?}"
);
for _ in 0..2500 {
estimator.update(0.004, &x_only, &facing_east);
}
let seen = estimator.estimate().expect("updated");
check!((seen.velocity_mps - east).norm() < 0.05, "{seen:?}");
check!(seen.sd_along_mps(Vec2::y()) < SETTLED_SD_MPS, "{seen:?}");
}
#[test]
fn needs_a_drag_model_and_skips_non_finite_ticks() {
let wind = Vec2::new(0.0, 2.0);
let state = pushed(&momentum(0.3), 0.0, 0.0, Vec2::zeros(), wind);
check!(estimate_after(&WindConfig::default(), &state, 10.0) == None);
let config = momentum(0.3);
let cases = [
(
"NaN velocity",
VehicleState {
velocity_mps: Vec3::new(f32::NAN, 0.0, 0.0),
..state
},
),
(
"NaN acceleration",
VehicleState {
acceleration_mps2: Vec3::new(0.0, f32::INFINITY, 0.0),
..state
},
),
];
for (case, broken) in cases {
let mut estimator = WindEstimator::default();
for tick in 0..2500 {
let state = if tick % 3 == 0 { &broken } else { &state };
estimator.update(0.004, &config, state);
}
let estimate = estimator.estimate().expect("updated");
check!(
(estimate.velocity_mps - wind).norm() < 0.01,
"{case}: {estimate:?}"
);
check!(
estimate_after(&config, &broken, 1.0) == None,
"{case}: only broken ticks"
);
}
}
#[test]
fn rejects_an_outlier_past_the_gate() {
let config = momentum(0.3);
let wind = Vec2::new(0.0, 2.0);
let mut estimator = WindEstimator::default();
for _ in 0..2500 {
estimator.update(
0.004,
&config,
&pushed(&config, 0.0, 0.0, Vec2::zeros(), wind),
);
}
let wild = pushed(&config, 0.0, 0.0, Vec2::zeros(), Vec2::new(0.0, 40.0));
for _ in 0..50 {
estimator.update(0.004, &config, &wild);
}
let estimate = estimator.estimate().expect("updated");
check!((estimate.velocity_mps - wind).norm() < 0.01, "{estimate:?}");
}
#[test]
fn updates_by_time_not_ticks_and_resets() {
let config = momentum(0.3);
let state = pushed(&config, 0.0, 0.0, Vec2::zeros(), Vec2::new(1.0, 1.0));
let after = |dt: f32| {
let mut estimator = WindEstimator::default();
for _ in 0..(1.0 / dt).round() as usize {
estimator.update(dt, &config, &state);
}
estimator
};
let (fast, slow) = (after(0.002), after(0.01));
let (fast_estimate, slow_estimate) = (
fast.estimate().expect("updated"),
slow.estimate().expect("updated"),
);
check!((fast_estimate.covariance_m2ps2 - slow_estimate.covariance_m2ps2).norm() < 1e-3);
let mut reset = fast;
reset.reset();
check!(reset.estimate() == None);
}
#[test]
fn validate_rejects_bad_coefficients_and_noise() {
let bad = [
(
"ballistic_kgpm2.x",
WindConfig {
ballistic_kgpm2: Vec2::new(-1.0, 0.0),
..Default::default()
},
),
(
"ballistic_kgpm2.y",
WindConfig {
ballistic_kgpm2: Vec2::new(0.0, f32::NAN),
..Default::default()
},
),
(
"momentum_per_s.x",
WindConfig {
momentum_per_s: Vec2::new(f32::INFINITY, 0.0),
..Default::default()
},
),
(
"momentum_per_s.y",
WindConfig {
momentum_per_s: Vec2::new(0.0, -0.1),
..Default::default()
},
),
(
"force_noise_mps2",
WindConfig {
force_noise_mps2: 0.0,
..Default::default()
},
),
(
"process_noise_mps_per_rt_s",
WindConfig {
process_noise_mps_per_rt_s: -1.0,
..Default::default()
},
),
];
for (field, config) in bad {
check!(
config.validate().map_err(|e| e.field) == Err(field),
"{field}"
);
}
check!(WindConfig::default().validate().is_ok());
check!(!WindConfig::default().is_on());
check!(momentum(0.1).is_on());
}
#[test]
fn an_axis_with_little_drag_counts_for_little() {
let lopsided = WindConfig {
momentum_per_s: Vec2::new(0.3, 0.01),
..Default::default()
};
let state = pushed(&lopsided, 0.0, 0.0, Vec2::zeros(), Vec2::new(1.0, 2.0));
let estimate = estimate_after(&lopsided, &state, 10.0).expect("updated");
check!(
estimate.sd_along_mps(Vec2::x()) < SETTLED_SD_MPS,
"{estimate:?}"
);
check!(estimate.sd_along_mps(Vec2::y()) > 5.0, "{estimate:?}");
}
#[test]
fn skips_ticks_tilted_past_45_degrees() {
let config = momentum(0.3);
let wind = Vec2::new(0.0, 2.0);
for (pitch_rad, updated) in [(0.7, true), (0.9, false), (FRAC_PI_2, false)] {
let state = pushed(&config, 0.0, pitch_rad, Vec2::zeros(), wind);
let estimate = estimate_after(&config, &state, 1.0);
check!(estimate.is_some() == updated, "{pitch_rad}");
}
}