use assert2::check;
use super::*;
use crate::nalgebra::Matrix2;
fn wind(toward_deg: f32, speed_mps: f32, sd_mps: f32) -> WindEstimate {
let toward_rad = toward_deg.to_radians();
WindEstimate {
velocity_mps: Vec2::new(libm::cosf(toward_rad), libm::sinf(toward_rad)) * speed_mps,
covariance_m2ps2: Matrix2::identity() * sd_mps * sd_mps,
}
}
fn on(min_speed_mps: f32) -> CopterGuidanceConfig {
CopterGuidanceConfig {
wind_heading_min_mps: min_speed_mps,
..Default::default()
}
}
fn deg(rad: Option<f32>) -> Option<f32> {
rad.map(|r| wrap_angle(r).to_degrees())
}
fn near(target: Option<f32>, expected: Option<f32>) -> bool {
match (target, expected) {
(Some(t), Some(e)) => (t - e).abs() < 1e-3,
_ => target == expected,
}
}
type Row = (&'static str, f32, Option<WindEstimate>, f32, Option<f32>);
#[test]
fn picks_the_wingtip_heading_nearer_the_vehicle() {
let cases: [Row; 9] = [
(
"belly 3° off the wind",
1.0,
Some(wind(217.0, 2.0, 0.1)),
40.0,
Some(117.0),
),
(
"nearer the other wingtip",
1.0,
Some(wind(217.0, 2.0, 0.1)),
330.0,
Some(-43.0),
),
("off", 0.0, Some(wind(217.0, 2.0, 0.1)), 40.0, None),
("no estimate", 1.0, None, 40.0, None),
(
"slower than the least",
1.0,
Some(wind(217.0, 0.9, 0.01)),
40.0,
None,
),
(
"direction just known",
1.0,
Some(wind(217.0, 2.0, 0.5)),
40.0,
Some(117.0),
),
(
"direction uncertain",
1.0,
Some(wind(217.0, 2.0, 0.55)),
40.0,
None,
),
(
"non-finite wind",
1.0,
Some(WindEstimate {
velocity_mps: Vec2::new(f32::NAN, 1.0),
..wind(0.0, 2.0, 0.1)
}),
40.0,
None,
),
(
"non-finite heading picks clockwise",
1.0,
Some(wind(0.0, 2.0, 0.1)),
f32::NAN,
Some(100.0),
),
];
for (case, min_speed_mps, wind, heading_deg, expected) in cases {
let mut heading = WindHeading::default();
let target =
deg(heading.target_rad(&on(min_speed_mps), wind.as_ref(), heading_deg.to_radians()));
check!(near(target, expected), "{case}: {target:?} vs {expected:?}");
}
}
#[test]
fn keeps_its_side_and_heading_through_small_changes() {
let mut heading = WindHeading::default();
let first = heading.target_rad(&on(1.0), Some(&wind(0.0, 2.0, 0.1)), 0.3);
check!(deg(first).is_some_and(|h| (h - 100.0).abs() < 1e-3));
let steps: [(&str, f32, f32, f32, f32); 4] = [
("vehicle now nearer the other side", 0.0, 0.1, -80.0, 100.0),
(
"wind turned 9°: inside the deadband",
9.0,
0.1,
100.0,
100.0,
),
("wind turned 11°: moves", 11.0, 0.1, 100.0, 111.0),
(
"uncertain: keeps the latched heading",
60.0,
5.0,
100.0,
111.0,
),
];
for (case, toward_deg, sd_mps, heading_deg, expected) in steps {
let target = deg(heading.target_rad(
&on(1.0),
Some(&wind(toward_deg, 2.0, sd_mps)),
heading_deg.to_radians(),
));
check!(
(target.unwrap_or(f32::NAN) - expected).abs() < 1e-3,
"{case}: {target:?}"
);
}
}
#[test]
fn follows_the_configured_angle_uncertainty_and_deadband() {
let rows: [(&str, CopterGuidanceConfig, f32, f32, Option<f32>); 4] = [
(
"square on at 90°",
CopterGuidanceConfig {
wind_heading_from_wind_rad: 90.0_f32.to_radians(),
..on(1.0)
},
0.0,
0.1,
Some(90.0),
),
(
"no deadband: 9° moves it",
CopterGuidanceConfig {
wind_heading_deadband_rad: 0.0,
..on(1.0)
},
9.0,
0.1,
Some(109.0),
),
(
"wider deadband: 11° doesn't",
CopterGuidanceConfig {
wind_heading_deadband_rad: 12.0_f32.to_radians(),
..on(1.0)
},
11.0,
0.1,
Some(100.0),
),
(
"looser uncertainty: a 1 m/s sd still turns it",
CopterGuidanceConfig {
wind_heading_max_sd_rad: 90.0_f32.to_radians(),
..on(1.0)
},
0.0,
1.0,
Some(100.0),
),
];
for (case, config, toward_deg, sd_mps, expected) in rows {
let mut heading = WindHeading::default();
let first = deg(heading.target_rad(&config, Some(&wind(0.0, 2.0, sd_mps)), 0.3));
let target = if toward_deg == 0.0 {
first
} else {
deg(heading.target_rad(&config, Some(&wind(toward_deg, 2.0, sd_mps)), 0.3))
};
check!(near(target, expected), "{case}: {target:?} vs {expected:?}");
}
}