use assert2::check;
use super::*;
use crate::controllers::RATE_I_PER_P;
use crate::stabilizers::copter::TunedLoop;
use crate::tuning::autotune::DesignPlan;
use crate::tuning::{
DesignError, IdentifyAbort, RateModel, StepMetrics, design_angle_p, design_rate,
};
const CURRENT: Gains = Gains {
p: 9.0,
i: 8.0,
d: 7.0,
};
fn stepped(saturated_s: f32, stalled: bool) -> Input {
stepped_with(saturated_s, stalled, 0.0)
}
fn stepped_with(saturated_s: f32, stalled: bool, overshoot: f32) -> Input {
Input::Measured {
measurement: Measurement::Step(StepMetrics {
saturated_s,
overshoot,
..Default::default()
}),
gains: CURRENT,
stalled,
}
}
fn step_test(pairs: usize) -> (Procedure, StepTestPlan) {
let plan = StepTestPlan {
pairs,
..StepTestPlan::copter(TunedLoop::RollRate)
};
(Procedure::new(Method::StepTest(plan)), plan)
}
#[test]
fn step_test_alternates_then_finishes_with_the_same_gains() {
let (mut procedure, plan) = step_test(2);
let up = Action::probe(ProbeConfig::Step(plan.step));
let down = Action::probe(ProbeConfig::Step(plan.step.reversed()));
let clipped = plan.max_saturated_s + 0.01;
let mut actions = vec![procedure.next(Input::start(CURRENT))];
actions.push(procedure.next(stepped_with(clipped, false, 9.0))); for overshoot in [0.1, 0.2, 0.3, 0.4] {
actions.push(procedure.next(stepped_with(0.0, false, overshoot)));
}
let Some(Action::Finish {
gains,
outcome: Outcome::Measured,
steps: Some(steps),
baseline: None,
}) = actions.pop()
else {
panic!("no summary: {actions:?}");
};
check!(actions == [up, up, down, up, down]);
check!(gains == CURRENT);
check!(steps.count() == 4);
check!(steps.overshoot.max == 0.4);
}
#[test]
fn step_test_repeats_invalid_steps_and_fails_on_too_many() {
let (mut procedure, plan) = step_test(3);
let up = Action::probe(ProbeConfig::Step(plan.step));
let down = Action::probe(ProbeConfig::Step(plan.step.reversed()));
let clipped = plan.max_saturated_s + 0.01;
procedure.next(Input::start(CURRENT));
let script = [
(stepped(plan.max_saturated_s, false), down), (stepped(clipped, false), down),
(stepped(0.0, true), down), (stepped(clipped, false), down),
(stepped(0.0, false), up), (stepped(clipped, false), up),
(stepped(clipped, false), up),
(
stepped(clipped, false),
Action::Fail(AutotuneAbort::Invalid),
),
];
check!(plan.max_invalid == 3);
for (index, (input, expected)) in script.into_iter().enumerate() {
check!(procedure.next(input) == expected, "step {index}");
}
let (mut procedure, plan) = step_test(3);
procedure.next(Input::start(CURRENT));
for index in 1..plan.max_stalled {
check!(procedure.next(stepped(0.0, true)) == up, "stall {index}");
}
check!(procedure.next(stepped(0.0, true)) == Action::Fail(AutotuneAbort::Stalled));
}
#[test]
fn a_stopped_step_runs_again() {
let (mut procedure, plan) = step_test(3);
let down = Action::probe(ProbeConfig::Step(plan.step.reversed()));
procedure.next(Input::start(CURRENT));
check!(procedure.next(stepped(0.0, false)) == down);
check!(procedure.next(Input::Stalled { gains: CURRENT }) == down);
for index in 2..plan.max_stalled {
check!(procedure.next(stepped(0.0, true)) == down, "stall {index}");
}
check!(
procedure.next(Input::Stalled { gains: CURRENT }) == Action::Fail(AutotuneAbort::Stalled)
);
}
#[test]
fn a_mismatched_measurement_fails() {
let (mut step, _) = step_test(1);
step.next(Input::start(CURRENT));
check!(step.next(fitted(QUAD_X_FIT, false)) == Action::Fail(AutotuneAbort::Interrupted));
let mut design = Procedure::new(Method::Design(DesignPlan::copter()));
design.next(Input::start(CURRENT));
check!(design.next(stepped(0.0, false)) == Action::Fail(AutotuneAbort::Interrupted));
}
#[test]
fn step_test_and_design_fail_on_an_abort() {
let abort = ProbeAbort::Step(crate::tuning::StepAbort::Runaway);
let (step, _) = step_test(1);
let design = Procedure::new(Method::Design(DesignPlan::copter()));
for mut procedure in [step, design] {
procedure.next(Input::start(CURRENT));
let failed = procedure.next(Input::Aborted {
abort,
gains: CURRENT,
});
check!(failed == Action::Fail(AutotuneAbort::Probe(abort)));
check!(procedure.take_verdict() == None);
}
}
#[test]
fn clipped_ticks_within_the_allowance_stay_valid() {
let ticks = 10;
for target in [TunedLoop::RollRate, TunedLoop::Roll, TunedLoop::Heading] {
let plan = StepTestPlan::copter(target);
let clipped = |ticks| (0..ticks).fold(0.0_f32, |sum, _| sum + 0.004);
let up = Action::probe(ProbeConfig::Step(plan.step));
let down = Action::probe(ProbeConfig::Step(plan.step.reversed()));
let mut procedure = Procedure::new(Method::StepTest(plan));
procedure.next(Input::start(CURRENT));
check!(
procedure.next(stepped(clipped(ticks), false)) == down,
"{target:?}"
);
check!(
procedure.next(stepped(clipped(ticks + 1), false)) == down,
"{target:?}: reruns"
);
check!(procedure.next(stepped(0.0, false)) == up, "{target:?}");
}
}
#[test]
fn step_test_fails_on_a_breach() {
let (mut procedure, _) = step_test(1);
procedure.next(Input::start(CURRENT));
let failed = procedure.next(Input::Breached {
reason: AutotuneAbort::Tilt,
gains: CURRENT,
});
check!(failed == Action::Fail(AutotuneAbort::Tilt));
}
const QUAD_X_FIT: RateModel = RateModel {
rate_weights: [1.87437, -0.87447],
moment_weights: [0.0136, 0.0, 0.0],
delay_ticks: 1,
dt_s: 0.004,
};
fn fitted(model: RateModel, stalled: bool) -> Input {
Input::Measured {
measurement: Measurement::Identify(model),
gains: CURRENT,
stalled,
}
}
fn designed_rate(plan: &DesignPlan) -> Gains {
let rate = design_rate(&QUAD_X_FIT, CURRENT, DEFAULT_GYRO_CUTOFF_HZ, &plan.design)
.expect("rate gains");
Gains {
i: RATE_I_PER_P * rate.p,
..rate
}
}
#[test]
fn design_fits_holds_then_finishes_with_the_designed_gains() {
let plan = DesignPlan::copter();
let rate = designed_rate(&plan);
let angle_p = design_angle_p(
&QUAD_X_FIT,
rate,
CURRENT,
DEFAULT_GYRO_CUTOFF_HZ,
&plan.design,
)
.expect("angle P");
let rows = [
("rate", None, rate),
(
"angle",
Some(rate),
Gains {
p: angle_p,
..CURRENT
},
),
];
for (case, inner, designed) in rows {
let mut procedure = Procedure::new(Method::Design(plan));
let first = procedure.next(Input::Start {
gains: CURRENT,
inner,
run_start: CURRENT,
gyro_cutoff_hz: DEFAULT_GYRO_CUTOFF_HZ,
});
check!(
first == Action::probe(ProbeConfig::Identify(plan.identify)),
"{case}"
);
check!(
procedure.next(fitted(QUAD_X_FIT, false)) == Action::Hold { gains: designed },
"{case}"
);
check!(
procedure.next(held(true))
== Action::Finish {
gains: designed,
outcome: Outcome::Designed,
steps: None,
baseline: None,
},
"{case}"
);
}
}
fn held(settled: bool) -> Input {
Input::Held {
settled,
gains: CURRENT,
}
}
#[test]
fn design_fits_again_after_an_unusable_fit_or_design() {
let plan = DesignPlan::copter();
let identify = Action::probe(ProbeConfig::Identify(plan.identify));
let no_lag = fitted(
RateModel {
rate_weights: [2.2, -1.2],
..QUAD_X_FIT
},
false,
);
let usable = fitted(QUAD_X_FIT, false);
let breached = Input::Breached {
reason: AutotuneAbort::Rate,
gains: CURRENT,
};
let rate = designed_rate(&plan);
let rows = [
(
"an unusable fit, then a design that holds",
vec![no_lag, usable, held(true)],
Action::Finish {
gains: rate,
outcome: Outcome::Designed,
steps: None,
baseline: None,
},
),
("two unusable fits", vec![no_lag, no_lag], identify),
(
"three unusable fits",
vec![no_lag, no_lag, no_lag],
Action::Fail(AutotuneAbort::Design(DesignError::NotALag)),
),
(
"a design that doesn't hold",
vec![usable, held(false)],
identify,
),
(
"three designs that don't hold",
vec![
usable,
held(false),
usable,
held(false),
usable,
held(false),
],
Action::Fail(AutotuneAbort::NotSettled),
),
("a design that breaches", vec![usable, breached], identify),
(
"a design that breaches, then two unusable fits",
vec![usable, breached, no_lag, no_lag],
Action::Fail(AutotuneAbort::Design(DesignError::NotALag)),
),
];
for (case, inputs, end) in rows {
let mut procedure = Procedure::new(Method::Design(plan));
let mut action = procedure.next(Input::start(CURRENT));
for input in inputs {
action = procedure.next(input);
}
check!(action == end, "{case}");
}
let abort = ProbeAbort::Identify(IdentifyAbort::Unexcited);
let mut procedure = Procedure::new(Method::Design(plan));
procedure.next(Input::start(CURRENT));
check!(
procedure.next(Input::Aborted {
abort,
gains: CURRENT
}) == Action::Fail(AutotuneAbort::Probe(abort))
);
}
#[test]
fn design_fits_again_after_a_stall() {
let plan = DesignPlan::copter();
let identify = Action::probe(ProbeConfig::Identify(plan.identify));
let mut procedure = Procedure::new(Method::Design(plan));
procedure.next(Input::start(CURRENT));
check!(procedure.next(fitted(QUAD_X_FIT, true)) == identify);
for stop in 2..MAX_MEASURE_STOPS {
check!(
procedure.next(Input::Stalled { gains: CURRENT }) == identify,
"stop {stop}"
);
}
check!(
procedure.next(Input::Stalled { gains: CURRENT }) == Action::Fail(AutotuneAbort::Stalled)
);
}
#[test]
fn design_counts_only_stalls_in_a_row() {
let plan = DesignPlan::copter();
let identify = Action::probe(ProbeConfig::Identify(plan.identify));
let no_lag = RateModel {
rate_weights: [2.2, -1.2],
..QUAD_X_FIT
};
let mut procedure = Procedure::new(Method::Design(plan));
procedure.next(Input::start(CURRENT));
for stop in 1..MAX_MEASURE_STOPS {
check!(
procedure.next(Input::Stalled { gains: CURRENT }) == identify,
"stop {stop}"
);
}
check!(procedure.next(fitted(no_lag, false)) == identify);
for stop in 1..MAX_MEASURE_STOPS {
check!(
procedure.next(Input::Stalled { gains: CURRENT }) == identify,
"stop {stop} after the fit"
);
}
}