Skip to main content

stable_flight/
tuning.rs

1//! PID autotune: measure a loop, turn the measurement into gains, and run it per loop.
2//!
3//! - [`Probe`]: one loop's excitation and measurement, run inside the tick: a
4//!   [`StepProbe`] whose [`StepMetrics`] describe the loop's step response, or an
5//!   [`IdentifyProbe`] that fits its axis's [`RateModel`] (a wing's: a [`WingIdentifyProbe`]
6//!   and its [`WingModel`]).
7//! - [`design_rate`] and [`design_angle_p`]: gains from a [`RateModel`] by loop shaping.
8//! - [`Tunable`]: a stabilizer that hosts a probe on one of its loops
9//!   ([`CopterStabilizer`](crate::stabilizers::copter::CopterStabilizer),
10//!   [`PlaneStabilizer`](crate::stabilizers::plane::PlaneStabilizer)).
11//! - [`autotune::Autotune`]: the copter supervisor, run between ticks: executes each stage's
12//!   procedure (which probes to run, what gains follow), guards the envelope, and commits or
13//!   reverts gains.
14//!
15//! Method and rules: `docs/architecture/autotune.md#autotune`.
16//!
17//! # Examples
18//!
19//! ```
20//! use stable_flight::tuning::autotune::COPTER_RATE_STEP;
21//! use stable_flight::tuning::{LoopSignals, Probe, ProbeConfig};
22//!
23//! // A 1 rad/s step on a rate loop at rest: the loop runs on the stepped setpoint.
24//! let mut probe = Probe::new(ProbeConfig::Step(COPTER_RATE_STEP));
25//! let setpoint = probe.step(0.004, &LoopSignals::default());
26//! assert_eq!(setpoint, COPTER_RATE_STEP.amplitude);
27//! ```
28
29use nalgebra::UnitQuaternion;
30
31pub mod autotune;
32pub mod design;
33pub mod identify;
34pub mod probe;
35pub mod step;
36pub mod wing;
37
38#[cfg(test)]
39pub(crate) mod plant;
40
41pub use design::{DesignError, LoopShapingConfig, design_angle_p, design_rate};
42pub use identify::{
43    IdentifyAbort, IdentifyConfig, IdentifyExtension, IdentifyProbe, IdentifyStatus,
44    MAX_DELAY_TICKS, RateModel,
45};
46pub use probe::{LoopSignals, Measurement, Probe, ProbeAbort, ProbeConfig, ProbeStatus, Progress};
47pub use step::{Stat, StepAbort, StepConfig, StepMetrics, StepProbe, StepStatus, StepSummary};
48pub use wing::design::{WingDesign, WingDesignConfig, WingDesignStatus};
49pub use wing::{WingIdentifyProbe, WingModel, WingStatus};
50
51/// A stabilizer that hosts autotune stages: one of its loops handed to a [`Probe`] inside the
52/// tick, then handed back. [`FlightLoop`](crate::FlightLoop) forwards these between ticks
53/// ([`FlightLoop::start_probe`](crate::FlightLoop::start_probe) and friends), so a supervisor
54/// never needs `&mut` access to the stabilizer itself.
55///
56/// `L` names the loops a stage can replace: a stabilizer with two sets of loops (a VTOL's
57/// hover and plane loops) implements it once for each.
58///
59/// Implemented by [`CopterStabilizer`](crate::stabilizers::copter::CopterStabilizer) for
60/// [`TunedLoop`](crate::stabilizers::copter::TunedLoop)s and by
61/// [`PlaneStabilizer`](crate::stabilizers::plane::PlaneStabilizer) for
62/// [`PlaneLoop`](crate::stabilizers::plane::PlaneLoop)s.
63pub trait Tunable<L: Copy> {
64    /// Starts a probe on `target` with `config`, from the next tick. A running probe is handed
65    /// back first.
66    ///
67    /// # Panics
68    ///
69    /// If `config` is invalid ([`ProbeConfig::validate`]).
70    fn start_probe(&mut self, target: L, config: ProbeConfig);
71
72    /// The current probe, if any: running, or finished and not yet stopped.
73    fn probe(&self) -> Option<(L, ProbeStatus)>;
74
75    /// Ends the probe, handing the loop back to its controller if the probe still flies it,
76    /// and returns its last status.
77    fn stop_probe(&mut self) -> Option<(L, ProbeStatus)>;
78
79    /// The frame the probed loops fly in, expressed in the body frame: identity for loops that
80    /// fly the body frame, a [`View`](crate::view::View)'s rotation for loops run in it (nose-up
81    /// hover reads level in a tailsitter's view). The autotune supervisor reads tilt and
82    /// per-axis rates in this frame, so its envelope and settle judge the axes the loops fly.
83    ///
84    /// # Examples
85    ///
86    /// ```
87    /// use stable_flight::stabilizers::copter::{CopterStabilizer, CopterStabilizerConfig};
88    /// use stable_flight::tuning::Tunable;
89    /// use stable_flight::view::{View, tailsitter_rotation};
90    ///
91    /// let hover = View::tailsitter(CopterStabilizer::new(CopterStabilizerConfig::default()));
92    /// assert_eq!(hover.probe_frame(), tailsitter_rotation());
93    /// ```
94    fn probe_frame(&self) -> UnitQuaternion<f32> {
95        UnitQuaternion::identity()
96    }
97}