use super::{AutotuneAbort, Method, Outcome, StepTestPlan};
use crate::controllers::Gains;
use crate::tuning::{Measurement, ProbeAbort, ProbeConfig, StepMetrics, StepSummary};
use design::Design;
use search::Search;
mod design;
mod search;
#[cfg(test)]
mod tests;
const MAX_MEASURE_STOPS: usize = 3;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Input {
Start {
gains: Gains,
inner: Option<Gains>,
run_start: Gains,
gyro_cutoff_hz: f32,
},
Measured {
measurement: Measurement,
gains: Gains,
stalled: bool,
},
Stalled { gains: Gains },
Aborted { abort: ProbeAbort, gains: Gains },
Breached { reason: AutotuneAbort, gains: Gains },
Held { settled: bool, gains: Gains },
}
#[cfg(test)]
use crate::stabilizers::copter::DEFAULT_GYRO_CUTOFF_HZ;
#[cfg(test)]
impl Input {
fn start(gains: Gains) -> Self {
Self::Start {
gains,
inner: None,
run_start: gains,
gyro_cutoff_hz: DEFAULT_GYRO_CUTOFF_HZ,
}
}
}
#[expect(
clippy::large_enum_variant,
reason = "returned once per decision, between ticks; the core has no allocator to box with"
)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Action {
Probe {
probe: ProbeConfig,
gains: Option<Gains>,
commit: Option<Gains>,
},
Finish {
gains: Gains,
outcome: Outcome,
steps: Option<StepSummary>,
baseline: Option<StepSummary>,
},
Hold { gains: Gains },
Fail(AutotuneAbort),
}
impl Action {
fn probe(probe: ProbeConfig) -> Self {
Self::Probe {
probe,
gains: None,
commit: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Verdict {
pub(super) gains: Gains,
pub(super) passed: bool,
pub(super) steps: StepSummary,
}
#[expect(
clippy::large_enum_variant,
reason = "one per run, replaced between stages; the core has no allocator to box with"
)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Procedure {
StepTest(Pairs),
Search(Search),
Design(Design),
}
impl Procedure {
pub(super) fn new(method: Method) -> Self {
match method {
Method::StepTest(plan) => Self::StepTest(Pairs::new(plan, plan.pairs)),
Method::Search(plan) => Self::Search(Search::new(plan)),
Method::Design(plan) => Self::Design(Design::new(plan)),
}
}
pub(super) fn next(&mut self, input: Input) -> Action {
match self {
Self::StepTest(pairs) => match input {
Input::Start { .. } => Action::probe(pairs.probe()),
Input::Measured {
measurement: Measurement::Step(metrics),
gains,
stalled,
} => match pairs.record(&metrics, stalled) {
Err(abort) => Action::Fail(abort),
Ok(()) if pairs.done() => Action::Finish {
gains,
outcome: Outcome::Measured,
steps: Some(pairs.summary),
baseline: None,
},
Ok(()) => Action::probe(pairs.probe()),
},
Input::Stalled { .. } => match pairs.stall() {
Err(abort) => Action::Fail(abort),
Ok(()) => Action::probe(pairs.probe()),
},
Input::Aborted { abort, .. } => Action::Fail(AutotuneAbort::Probe(abort)),
Input::Breached { reason, .. } => Action::Fail(reason),
Input::Measured { .. } | Input::Held { .. } => {
Action::Fail(AutotuneAbort::Interrupted)
}
},
Self::Search(search) => search.next(input),
Self::Design(design) => design.next(input),
}
}
pub(super) fn take_verdict(&mut self) -> Option<Verdict> {
match self {
Self::Search(search) => search.take_verdict(),
Self::StepTest(_) | Self::Design(_) => None,
}
}
}
fn measure_again(stops: &mut usize, probe: ProbeConfig) -> Action {
*stops += 1;
if *stops >= MAX_MEASURE_STOPS {
Action::Fail(AutotuneAbort::Stalled)
} else {
Action::probe(probe)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Pairs {
plan: StepTestPlan,
pairs: usize,
invalid: usize,
stalled: usize,
summary: StepSummary,
}
impl Pairs {
fn new(plan: StepTestPlan, pairs: usize) -> Self {
Self {
plan,
pairs,
invalid: 0,
stalled: 0,
summary: StepSummary::default(),
}
}
fn restart(&mut self, pairs: usize) {
*self = Self::new(self.plan, pairs);
}
fn probe(&self) -> ProbeConfig {
let step = if self.summary.count().is_multiple_of(2) {
self.plan.step
} else {
self.plan.step.reversed()
};
ProbeConfig::Step(step)
}
fn record(&mut self, metrics: &StepMetrics, stalled: bool) -> Result<(), AutotuneAbort> {
if stalled {
return self.stall();
}
self.stalled = 0;
if metrics.saturated_s > self.plan.max_saturated_s {
self.invalid += 1;
if self.invalid >= self.plan.max_invalid {
return Err(AutotuneAbort::Invalid);
}
return Ok(());
}
self.invalid = 0;
self.summary.add(metrics);
Ok(())
}
fn stall(&mut self) -> Result<(), AutotuneAbort> {
self.stalled += 1;
if self.stalled >= self.plan.max_stalled {
return Err(AutotuneAbort::Stalled);
}
Ok(())
}
fn done(&self) -> bool {
self.summary.count() >= 2 * self.pairs
}
fn take(&mut self, input: &Input) -> Option<Batch> {
let batch = match *input {
Input::Measured {
measurement: Measurement::Step(metrics),
stalled,
..
} => match self.record(&metrics, stalled) {
Err(abort @ AutotuneAbort::Stalled) => Batch::Fail(abort),
Err(_) => Batch::Ended { complete: false },
Ok(()) if self.done() => Batch::Ended { complete: true },
Ok(()) => Batch::Next,
},
Input::Stalled { .. } => match self.stall() {
Err(abort) => Batch::Fail(abort),
Ok(()) => Batch::Next,
},
Input::Aborted {
abort: ProbeAbort::Step(_),
..
} => Batch::Ended { complete: false },
_ => return None,
};
Some(batch)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Batch {
Next,
Ended { complete: bool },
Fail(AutotuneAbort),
}