stable-flight 0.3.0

no_std autopilot core for Stable Flight: copter, plane, tailsitter, and quadplane guidance and stabilizers, waypoint navigation, failsafe, autotune, mixers, and the per-tick flight loop
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Fixed-wing stabilization: [`PlaneStabilizer`] tracks a [`Setpoint::Plane`] attitude or a
//! [`Setpoint::PlaneRate`] body rate with control surfaces, scaled for airspeed.
//!
//! | Setpoint | Behavior |
//! |---|---|
//! | [`Setpoint::Plane`] | Bank and pitch (saturated to [`max_bank_rad`](PlaneStabilizerConfig::max_bank_rad) / [`max_pitch_rad`](PlaneStabilizerConfig::max_pitch_rad), pitch to ±π/2 for a [`transition`](PlaneSetpoint::transition) ramp) → rate targets = error / time constant, plus the coordinated-turn rates for the current bank and airspeed → rate loop |
//! | [`Setpoint::PlaneRate`] | Absolute body rates (saturated to [`max_body_rate_radps`](PlaneStabilizerConfig::max_body_rate_radps)) → rate loop, at any attitude: acro, rolls, loops |
//! | others | Neutral (zero thrust and moments) |
//!
//! The structure is PX4's fixed-wing attitude and rate control
//! (`FixedwingAttitudeControl.cpp`, `FixedwingRateControl.cpp`), described, not copied:
//!
//! - The attitude error is the yaw-free part of the reduced attitude error the copter uses:
//!   the rotation that turns the body `z` axis onto the setpoint's, in body axes, so it's
//!   defined at any attitude, inverted included.
//! - A level turn at bank φ and speed V turns at ψ̇ = g·tan φ / V, which in body axes is a pitch
//!   rate ψ̇·sin φ·cos θ and a yaw rate ψ̇·cos φ·cos θ; both are fed forward so the wing turns
//!   without slipping or losing its nose, faded out between 70° and 75° of bank (PX4's
//!   turn-coordination fade).
//! - The rate loop ([`PlaneRateController`]) scales by airspeed and freezes its integral
//!   below [`min_airspeed_mps`](PlaneStabilizerConfig::min_airspeed_mps). Without an airspeed
//!   it assumes trim airspeed (PX4).
//! - Each axis adds its [`trim`](PlaneRateControllerConfig::trim), the moment the wing needs
//!   in steady flight, scaled like the feedback (PX4's `TRIM_ROLL`/`_PITCH`/`_YAW`).
//!
//! Unlike the copter's, the integrals aren't cleared at zero thrust: gliding is normal flight.
//! Rate flight (acro, a maneuver) leaves them as it found them: the rate integrals are saved
//! when a [`Setpoint::PlaneRate`] follows any other setpoint and restored when a
//! [`Setpoint::Plane`] follows it, so what a roll or loop winds up doesn't carry into the
//! attitude flight after it.
//!
//! # Tuning
//!
//! An autotune stage ([`start_probe`](crate::tuning::Tunable::start_probe)) hands one loop to a
//! [`Probe`](crate::tuning::Probe). Before the loop's controller runs, the probe sees its
//! [`LoopSignals`](crate::tuning::LoopSignals) and replaces the loop's setpoint (a step, or an
//! identification's square wave).
//!
//! | [`PlaneLoop`] | Setpoint, error |
//! |---|---|
//! | `RollRate`, `PitchRate` | desired body rate; desired − measured |
//! | `Roll`, `Pitch` | saturated bank or pitch; the attitude error on that axis |
//!
//! The signals' `saturated` is the axis's moment at full deflection on the last tick: a step
//! clipped there measures the surfaces' limit, not the loop. An angle step's rate target at the
//! attitude rate limit isn't a clip: the limit is part of the loop the step measures, and a
//! 30° roll step asks for more than the default 70°/s at τ 0.4 s.
//!
//! - A rate stage flies on attitude and rate setpoints alike; an angle stage only on attitude
//!   setpoints: a [`Setpoint::PlaneRate`] idles the attitude loops and interrupts it
//!   ([`ProbeStatus::Interrupted`](crate::tuning::ProbeStatus::Interrupted)).
//! - Any other setpoint, or a [`transition`](PlaneSetpoint::transition) ramp, interrupts
//!   either: the plane side isn't flying, or is flying a handoff no step should disturb.
//! - **Handoff:** when the probe finishes, is stopped, or is interrupted, the loop takes its
//!   setpoint back on the next tick. The loops have no derivative to reset, and the rate
//!   integral stays.
//! - The stage is runtime state, not config; [`prime`](crate::Stabilizer::prime) ends it.

use crate::controllers::{
    InvalidConfig, PlaneRateController, PlaneRateControllerConfig, check, check_limit,
    check_positive,
};
use crate::math::{
    DEG, assert_dt, attitude_error, bank_rad, clamp_symmetric, pitch_limit_rad, saturate_rates,
    saturate_symmetric, thrust_curve,
};
use crate::{
    BodyRateSetpoint, Configurable, ControlOutput, GRAVITY, PlaneSetpoint, Saturation, Setpoint,
    Stabilizer, Vec2, Vec3, VehicleState,
};

mod params;
#[cfg(test)]
mod tests;
mod tuning;

pub use tuning::{PlaneAxisGains, PlaneLoop};

/// Bank where turn coordination starts to fade, and where it's gone (PX4's 70°–75°).
const COORDINATION_FADE_RAD: (f32, f32) = (70.0 * DEG, 75.0 * DEG);

/// [`PlaneStabilizer`] settings.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PlaneStabilizerConfig {
    /// The rate loop's gains and integral limit.
    pub rate: PlaneRateControllerConfig,
    /// How fast the attitude loops close their error, roll (`x`) and pitch (`y`), in s: the
    /// rate target is the error divided by it.
    pub time_constant_s: Vec2,
    /// Largest roll (`x`) and pitch (`y`) rate the attitude loops ask for, in rad/s.
    pub max_attitude_rate_radps: Vec2,
    /// Largest body rate an absolute-rate setpoint gets, per axis, in rad/s.
    pub max_body_rate_radps: Vec3,
    /// Largest bank an attitude setpoint gets, in radians.
    pub max_bank_rad: f32,
    /// Largest pitch, up or down, an attitude setpoint gets, in radians; a
    /// [`transition`](PlaneSetpoint::transition) ramp's isn't held to it.
    pub max_pitch_rad: f32,
    /// The airspeed the gains are tuned at (scale 1), in m/s; assumed without an airspeed.
    pub trim_airspeed_mps: f32,
    /// Below this airspeed the scale stops growing and the rate integrals hold, in m/s.
    pub min_airspeed_mps: f32,
    /// Above this airspeed the scale stops shrinking, in m/s.
    pub max_airspeed_mps: f32,
    /// Linear throttle → motor command exponent (1 passes throttle through). A tailsitter uses
    /// its hover stabilizer's, so a handoff keeps motor commands continuous.
    pub thrust_curve_exponent: f32,
}

impl Default for PlaneStabilizerConfig {
    /// PX4's fixed-wing defaults (time constants 0.4 s, attitude rate limits 70°/s roll and
    /// 60°/s pitch, `fw_att_control_params.yaml`), acro up to 180°/s roll and pitch and 60°/s
    /// yaw, 50° bank and 30° pitch, airspeeds 10/15/25 m/s, and throttle passed through.
    /// Placeholders until an airframe is tuned.
    fn default() -> Self {
        PlaneStabilizerConfig {
            rate: PlaneRateControllerConfig::default(),
            time_constant_s: Vec2::new(0.4, 0.4),
            max_attitude_rate_radps: Vec2::new(70.0 * DEG, 60.0 * DEG),
            max_body_rate_radps: Vec3::new(180.0 * DEG, 180.0 * DEG, 60.0 * DEG),
            max_bank_rad: 50.0 * DEG,
            max_pitch_rad: 30.0 * DEG,
            trim_airspeed_mps: 15.0,
            min_airspeed_mps: 10.0,
            max_airspeed_mps: 25.0,
            thrust_curve_exponent: 1.0,
        }
    }
}

impl PlaneStabilizerConfig {
    /// `Ok` when every field is in range: positive time constants, non-negative rate limits,
    /// bank and pitch limits in `[0, π]`, positive airspeeds, and a positive thrust exponent.
    /// Each is checked on its own; the scale clamps the airspeed to `[min, max]` with a `max`
    /// below `min` read as `min`, so out-of-order airspeeds degrade, not fail.
    ///
    /// # Errors
    ///
    /// [`InvalidConfig`] naming the first field out of range.
    pub fn validate(&self) -> Result<(), InvalidConfig> {
        self.rate.validate().map_err(|e| e.in_section("rate"))?;
        check_positive("time_constant_s.x", self.time_constant_s.x)?;
        check_positive("time_constant_s.y", self.time_constant_s.y)?;
        check_limit("max_attitude_rate_radps.x", self.max_attitude_rate_radps.x)?;
        check_limit("max_attitude_rate_radps.y", self.max_attitude_rate_radps.y)?;
        check_limit("max_body_rate_radps.x", self.max_body_rate_radps.x)?;
        check_limit("max_body_rate_radps.y", self.max_body_rate_radps.y)?;
        check_limit("max_body_rate_radps.z", self.max_body_rate_radps.z)?;
        let half_turn = |angle: f32| (0.0..=core::f32::consts::PI).contains(&angle);
        check(
            "max_bank_rad",
            half_turn(self.max_bank_rad),
            "in [0, π] rad",
        )?;
        check(
            "max_pitch_rad",
            half_turn(self.max_pitch_rad),
            "in [0, π] rad",
        )?;
        check_positive("trim_airspeed_mps", self.trim_airspeed_mps)?;
        check_positive("min_airspeed_mps", self.min_airspeed_mps)?;
        check_positive("max_airspeed_mps", self.max_airspeed_mps)?;
        check_positive("thrust_curve_exponent", self.thrust_curve_exponent)
    }
}

/// A fixed-wing stabilizer: attitude and rate loops on the control surfaces, with turn
/// coordination and airspeed scaling. See the [module docs](self).
///
/// # Examples
///
/// ```
/// use stable_flight::stabilizers::plane::{PlaneStabilizer, PlaneStabilizerConfig};
/// use stable_flight::{PlaneSetpoint, Setpoint, Stabilizer, VehicleState};
///
/// let mut stabilizer = PlaneStabilizer::new(PlaneStabilizerConfig::default());
/// let bank_right = Setpoint::Plane(PlaneSetpoint {
///     roll_rad: 0.3,
///     thrust: 0.6,
///     ..Default::default()
/// });
/// let output = stabilizer.stabilize(0.004, &bank_right, &VehicleState::default());
/// assert!(output.moments.x > 0.0); // roll right
/// assert_eq!(output.thrust, 0.6);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct PlaneStabilizer {
    config: PlaneStabilizerConfig,
    rate: PlaneRateController,
    /// The rate loop as attitude flight left it, while flying a [`Setpoint::PlaneRate`].
    attitude_rate: Option<PlaneRateController>,
    saturation: Saturation,
    /// The autotune stage, if one was started and not yet stopped.
    stage: Option<tuning::Stage>,
    /// The last tick's moments, normalized: what a probe reads as the loop's output.
    last_moments: Vec3,
}

impl PlaneStabilizer {
    /// A stabilizer with `config`.
    ///
    /// # Panics
    ///
    /// If `config` is invalid ([`PlaneStabilizerConfig::validate`]).
    pub fn new(config: PlaneStabilizerConfig) -> Self {
        if let Err(error) = config.validate() {
            panic!("invalid PlaneStabilizerConfig: {error}");
        }
        PlaneStabilizer {
            config,
            rate: PlaneRateController::default(),
            attitude_rate: None,
            saturation: Saturation::default(),
            stage: None,
            last_moments: Vec3::zeros(),
        }
    }

    /// Rate targets for an attitude setpoint: the error over the time constant, clamped, plus
    /// the coordinated-turn rates. A probe on an angle loop replaces its angle first.
    fn attitude_rates(&mut self, dt: f32, setpoint: &PlaneSetpoint, state: &VehicleState) -> Vec3 {
        let config = &self.config;
        let max_pitch_rad = pitch_limit_rad(config.max_pitch_rad, setpoint.transition);
        // `>` is false for NaN, which saturates to level.
        self.saturation.tilt = setpoint.roll_rad.abs() > config.max_bank_rad
            || setpoint.pitch_rad.abs() > max_pitch_rad;
        let mut angles_rad = [
            saturate_symmetric(setpoint.roll_rad, config.max_bank_rad),
            saturate_symmetric(setpoint.pitch_rad, max_pitch_rad),
        ];
        let mut error = attitude_error(&state.attitude, angles_rad[0], angles_rad[1], f32::NAN);
        if self.stage.is_some() {
            let rates_radps = state.angular_velocity_radps;
            self.probe_angles(dt, &mut angles_rad, [error.x, error.y], &rates_radps);
            error = attitude_error(&state.attitude, angles_rad[0], angles_rad[1], f32::NAN);
        }
        let config = &self.config;
        let correction = Vec2::new(
            clamp_symmetric(
                error.x / config.time_constant_s.x,
                config.max_attitude_rate_radps.x,
            ),
            clamp_symmetric(
                error.y / config.time_constant_s.y,
                config.max_attitude_rate_radps.y,
            ),
        );
        coordinated_turn_radps(state, self.airspeed_mps(state))
            + Vec3::new(correction.x, correction.y, 0.0)
    }

    /// Rate targets for an absolute-rate setpoint, saturated (NaN as zero).
    fn body_rates(&self, setpoint: &BodyRateSetpoint) -> Vec3 {
        saturate_rates(
            Vec3::new(
                setpoint.roll_radps,
                setpoint.pitch_radps,
                setpoint.yaw_radps,
            ),
            self.config.max_body_rate_radps,
        )
    }

    /// The airspeed the loops work with: the state's when valid, clamped to the configured
    /// range, else trim.
    fn airspeed_mps(&self, state: &VehicleState) -> f32 {
        let config = &self.config;
        state
            .valid_airspeed_mps()
            .map_or(config.trim_airspeed_mps, |airspeed_mps| {
                let max_mps = config.max_airspeed_mps.max(config.min_airspeed_mps);
                airspeed_mps.clamp(config.min_airspeed_mps, max_mps)
            })
    }

    /// The angle of attack, in rad: the pitch less the climb angle through the air, the climb
    /// rate over [`airspeed_mps`](Self::airspeed_mps) (the air holds no vertical wind). A
    /// climb angle from the ground track reads the angle of attack wrong in wind: in an 8 m/s
    /// crosswind the eXplora's ground speed was 6.9 m/s against 13.6 m/s through the air, and
    /// its pitch fit lost the wing's stiffness (#534).
    pub(super) fn angle_of_attack_rad(&self, state: &VehicleState) -> f32 {
        let climb_share = state.velocity_mps.z / self.airspeed_mps(state);
        state.pitch_rad - libm::asinf(clamp_symmetric(climb_share, 1.0))
    }

    /// The rate loop at `desired_radps`, scaled for airspeed, and the thrust.
    fn track(
        &mut self,
        dt: f32,
        desired_radps: Vec3,
        thrust: f32,
        state: &VehicleState,
    ) -> ControlOutput {
        let mut desired_radps = desired_radps;
        self.probe_rates(dt, &mut desired_radps, state);
        let config = &self.config;
        let scale = config.trim_airspeed_mps / self.airspeed_mps(state);
        // Without an airspeed there's nothing to hold the integral for.
        let integrate = state
            .valid_airspeed_mps()
            .is_none_or(|airspeed_mps| airspeed_mps >= config.min_airspeed_mps);
        let moments = self.rate.control(
            &config.rate,
            dt,
            desired_radps,
            state.angular_velocity_radps,
            scale,
            integrate,
        );
        let thrust_exponent = config.thrust_curve_exponent;
        self.last_moments = moments;
        self.finish_stage();
        self.saturation.thrust = !(0.0..=1.0).contains(&thrust) && !thrust.is_nan();
        ControlOutput {
            thrust: thrust_curve(thrust, thrust_exponent),
            moments,
            cruise: None,
        }
    }
}

/// The body rates of a level turn at the vehicle's bank and `airspeed_mps`: ψ̇ = g·tan φ / V,
/// as pitch rate ψ̇·sin φ·cos θ and yaw rate ψ̇·cos φ·cos θ, read from the rotation matrix's
/// bottom row (`cos θ sin φ`, `cos θ cos φ`) without Euler angles. Faded out between 70° and
/// 75° of bank; zero inverted or for a non-finite attitude.
fn coordinated_turn_radps(state: &VehicleState, airspeed_mps: f32) -> Vec3 {
    let m = state.attitude.to_rotation_matrix();
    let (sin_roll_cos_pitch, cos_roll_cos_pitch) = (m[(2, 1)], m[(2, 2)]);
    let bank_rad = bank_rad(&state.attitude).abs();
    let (start, end) = COORDINATION_FADE_RAD;
    let fade = ((end - bank_rad) / (end - start)).clamp(0.0, 1.0);
    // A NaN bank (non-finite attitude) fades to NaN: no feedforward either.
    if fade.is_nan() || fade <= 0.0 || !airspeed_mps.is_finite() {
        return Vec3::zeros();
    }
    // ψ̇·cos φ·cos θ = g·sin φ·cos θ / V, and ψ̇·sin φ·cos θ = that · tan φ.
    let yaw_radps = GRAVITY * sin_roll_cos_pitch / airspeed_mps;
    let pitch_radps = yaw_radps * sin_roll_cos_pitch / cos_roll_cos_pitch;
    Vec3::new(0.0, pitch_radps, yaw_radps) * fade
}

impl Configurable for PlaneStabilizer {
    type Config = PlaneStabilizerConfig;

    fn config(&self) -> &PlaneStabilizerConfig {
        &self.config
    }

    /// Validates with [`PlaneStabilizerConfig::validate`]; the rate integrals, and those saved
    /// for attitude flight, are rescaled to the new integral gains.
    fn set_config(&mut self, config: PlaneStabilizerConfig) -> Result<(), InvalidConfig> {
        config.validate()?;
        self.rate.rescale_integral(&self.config.rate, &config.rate);
        if let Some(saved) = &mut self.attitude_rate {
            saved.rescale_integral(&self.config.rate, &config.rate);
        }
        self.config = config;
        Ok(())
    }
}

impl Stabilizer for PlaneStabilizer {
    /// Flies a [`Setpoint::Plane`] or [`Setpoint::PlaneRate`]; neutral for any other.
    fn stabilize(&mut self, dt: f32, setpoint: &Setpoint, state: &VehicleState) -> ControlOutput {
        assert_dt(dt);
        self.saturation = Saturation::default();
        let output = match setpoint {
            Setpoint::Plane(plane) => {
                if plane.transition {
                    self.interrupt_stage();
                }
                if let Some(saved) = self.attitude_rate.take() {
                    self.rate = saved;
                }
                let rates = self.attitude_rates(dt, plane, state);
                self.track(dt, rates, plane.thrust, state)
            }
            Setpoint::PlaneRate(rates) => {
                self.interrupt_angle_stage();
                if self.attitude_rate.is_none() {
                    self.attitude_rate = Some(self.rate.clone());
                }
                let rates_radps = self.body_rates(rates);
                self.track(dt, rates_radps, rates.thrust, state)
            }
            _ => {
                self.interrupt_stage();
                self.last_moments = Vec3::zeros();
                return ControlOutput::default();
            }
        };
        self.saturation.moments = output.moments.map(|m| m.abs() >= 1.0).into();
        output
    }

    /// Clears the rate integrals, those saved for attitude flight, and saturation, and ends
    /// any autotune stage.
    fn prime(&mut self, _state: &VehicleState) {
        self.rate.clear_integral();
        self.attitude_rate = None;
        self.saturation = Saturation::default();
        self.stage = None;
        self.last_moments = Vec3::zeros();
    }

    /// `tilt` when an attitude setpoint's bank or pitch was beyond its limit, `thrust` when
    /// the setpoint's thrust was outside `[0, 1]`, and `moments` per axis at ±1.
    fn saturation(&self) -> Saturation {
        self.saturation
    }
}