stable-flight 0.1.0

no_std flight-control core for Stable Flight: pipeline traits, copter guidance, cascaded-PID stabilizers, 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Guidance for multirotors such as the QuadCopter X: waypoint navigation and pilot-stick
//! interpretation, feeding [`CopterStabilizer`](crate::stabilizers::copter::CopterStabilizer).
//!
//! | Control input | Setpoint |
//! |---|---|
//! | [`ControlInput::Rate`] | [`Setpoint::Rate`], sticks passed through; the heading-hold target follows the vehicle |
//! | [`ControlInput::Attitude`] | [`Setpoint::Attitude`]: roll/pitch sticks scaled to [`max_stick_angles_rad`](CopterGuidanceConfig::max_stick_angles_rad); yaw stick slews a heading-hold target; throttle boosted by 1/cos(tilt) so tilting doesn't sink (none when inverted) |
//! | [`ControlInput::AbsoluteAttitude`] | [`Setpoint::Attitude`]: roll/pitch passed through (the stabilizer saturates them); the heading replaces the heading-hold target (NaN holds it); thrust saturated to `[0, 1]`, not boosted |
//! | [`ControlInput::Waypoint`] | [`Setpoint::Attitude`]: lateral position → velocity → acceleration → roll/pitch (every [`navigation_period_s`](CopterGuidanceConfig::navigation_period_s)), altitude loop → thrust (every tick), heading held; descents slewed at [`max_descent_mps`](CopterGuidanceConfig::max_descent_mps) |
//! | [`ControlInput::Land`] | [`Setpoint::Attitude`]: as a waypoint at the land position, with an altitude target that keeps descending at [`land_speed_mps`](CopterGuidanceConfig::land_speed_mps), at most [`LAND_LEASH_M`] below the vehicle |
//!
//! Switching modes doesn't jump the heading: attitude, absolute-attitude, waypoint, and land
//! flight share one heading-hold target, and rate flight keeps it on the vehicle's heading. The
//! altitude and lateral loops start fresh each time waypoint or land flight is entered, with
//! the altitude target starting from the vehicle's altitude.

use core::f32::consts::FRAC_PI_4;

use crate::{GRAVITY, Vec2};

use crate::commanders::touchdown::DESCENDING_MPS;
use crate::controllers::*;
use crate::controllers::{check, check_limit, check_positive, check_tilt};
use crate::{
    AbsoluteAttitudeCommand, AttitudeCommand, AttitudeSetpoint, Configurable, ControlInput,
    Guidance, LandCommand, Saturation, Setpoint, VehicleState, WaypointCommand,
};

use super::rate_divider::RateDivider;
use crate::math::{
    all_finite, assert_dt, cap_magnitude, saturate_throttle, saturate_unit, tilt_boost, wrap_angle,
};

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

/// Heading-hold target slew rate at full yaw stick, in rad/s (~57°/s).
const HEADING_SLEW_RATE: f32 = 1.0;

/// Default [`CopterGuidanceConfig::max_tilt_rad`]: 27°, leaving thrust headroom to climb while
/// translating (60% of the default 45° stabilizer limit).
pub const DEFAULT_MAX_TILT_RAD: f32 = 27.0_f32.to_radians();

/// Default [`CopterGuidanceConfig::max_stick_angles_rad`] per axis: 45°, the default stabilizer
/// limit, so full stick is full tilt.
pub const DEFAULT_MAX_STICK_ANGLE_RAD: f32 = FRAC_PI_4;

/// Default [`CopterGuidanceConfig::navigation_period_s`]: 50 Hz, a fifth of the usual 250 Hz
/// inner loop.
pub const DEFAULT_NAVIGATION_PERIOD_S: f32 = 0.02;

/// Default [`CopterGuidanceConfig::max_descent_mps`]: fast enough for the figure-eight's
/// descending half (up to about 3 m/s at 7 m/s cruise).
pub const DEFAULT_MAX_DESCENT_MPS: f32 = 3.0;

/// Default [`CopterGuidanceConfig::land_speed_mps`], as PX4's `MPC_LAND_SPEED`.
pub const DEFAULT_LAND_SPEED_MPS: f32 = 0.7;

/// How far, in meters, a land's altitude target may run below the vehicle. Once the ground
/// stops the vehicle, the altitude loop then settles on a thrust below hover instead of winding
/// down toward none.
pub const LAND_LEASH_M: f32 = 1.0;

/// Configuration for [CopterGuidance]: the altitude and lateral position loops and the
/// waypoint-flight limits.
///
/// Plain data: [`CopterGuidance::new`] validates it ([`validate`](Self::validate)), and the
/// guidance passes the loop configs to its controllers every tick.
///
/// # Examples
///
/// ```
/// use stable_flight::controllers::Gains;
/// use stable_flight::guidance::copter::{CopterGuidance, CopterGuidanceConfig};
///
/// let mut config = CopterGuidanceConfig::default();
/// config.altitude.gains = Gains { p: 2.0, i: 0.2, d: 0.1 };
/// config.altitude.hover_thrust = 0.45; // airframe-specific
/// let guidance = CopterGuidance::new(config);
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CopterGuidanceConfig {
    /// Altitude loop: altitude error (m) → vertical acceleration (m/s²), fed to the thrust law
    /// with gravity and tilt compensation. Its `hover_thrust` defaults to
    /// [`DEFAULT_HOVER_THRUST`].
    pub altitude: AltitudeControllerConfig,
    /// Lateral loop: horizontal position error (m) → velocity (m/s, capped at the waypoint's
    /// `cruise_speed_mps`) → acceleration (m/s²), flown as a tilt of `atan(|a| / g)` toward it.
    pub lateral: LateralControllerConfig,
    /// Seconds between runs of the lateral position loop; its roll/pitch output is held in
    /// between. `0.0` runs it every tick. Rounded to the nearest tick (at a 4 ms loop, 20 ms
    /// is every 5th tick); each run steps over the actual elapsed time. The altitude loop runs
    /// every tick, since its tilt compensation needs the current attitude. Must be
    /// non-negative and finite. Defaults to [`DEFAULT_NAVIGATION_PERIOD_S`].
    pub navigation_period_s: f32,
    /// Largest waypoint-flight tilt in radians, in `(0, π/2)`. Caps the magnitude of the
    /// combined roll/pitch request, so a far waypoint can't ask for full tilt while the
    /// altitude loop also wants full climb. It limits the request, not the achieved tilt: the
    /// attitude loop can overshoot it briefly, and the stabilizer's own `max_angles_rad` still
    /// applies. Piloted attitude flight is not capped. Defaults to [`DEFAULT_MAX_TILT_RAD`].
    pub max_tilt_rad: f32,
    /// Roll and pitch in radians that full stick (±1) commands in piloted attitude flight;
    /// the stick scales linearly. The stabilizer saturates to its own `max_angles_rad`, so a
    /// larger value only reaches that limit sooner. Must be non-negative and finite. Defaults
    /// to [`DEFAULT_MAX_STICK_ANGLE_RAD`] on both axes.
    pub max_stick_angles_rad: Vec2,
    /// Fastest descent waypoint flight asks for, in m/s: a target below the vehicle is
    /// approached by an altitude target lowered at this rate, so a low waypoint descends
    /// instead of cutting thrust. Climbs aren't limited. It limits the target, not the achieved
    /// rate: the vehicle can exceed it briefly while catching up. Must be positive and finite.
    /// Defaults to [`DEFAULT_MAX_DESCENT_MPS`].
    pub max_descent_mps: f32,
    /// Descent rate of a land ([`ControlInput::Land`]), in m/s. Must be finite and above the
    /// touchdown detector's [`DESCENDING_MPS`] (0.2 m/s), so the detector sees the descent and
    /// the land ends in a disarm. Defaults to [`DEFAULT_LAND_SPEED_MPS`].
    pub land_speed_mps: f32,
}

impl Default for CopterGuidanceConfig {
    /// The controller defaults (flight-checked on RealFlight's QuadCopterX), a 50 Hz lateral
    /// loop, a 27° waypoint tilt cap, 45° at full stick, a 3 m/s descent limit, and a 0.7 m/s
    /// land.
    fn default() -> Self {
        CopterGuidanceConfig {
            altitude: AltitudeControllerConfig::default(),
            lateral: LateralControllerConfig::default(),
            navigation_period_s: DEFAULT_NAVIGATION_PERIOD_S,
            max_tilt_rad: DEFAULT_MAX_TILT_RAD,
            max_stick_angles_rad: Vec2::new(
                DEFAULT_MAX_STICK_ANGLE_RAD,
                DEFAULT_MAX_STICK_ANGLE_RAD,
            ),
            max_descent_mps: DEFAULT_MAX_DESCENT_MPS,
            land_speed_mps: DEFAULT_LAND_SPEED_MPS,
        }
    }
}

impl CopterGuidanceConfig {
    /// Checks both loop configs ([`AltitudeControllerConfig::validate`],
    /// [`LateralControllerConfig::validate`]; errors carry the loop as their `section`), that
    /// `navigation_period_s` and `max_stick_angles_rad` are non-negative and finite, that
    /// `max_tilt_rad` is in `(0, π/2)`, that `max_descent_mps` is positive and finite, and that
    /// `land_speed_mps` is finite and above [`DESCENDING_MPS`].
    ///
    /// # Examples
    ///
    /// ```
    /// use stable_flight::guidance::copter::CopterGuidanceConfig;
    ///
    /// let mut config = CopterGuidanceConfig::default();
    /// assert!(config.validate().is_ok());
    /// config.altitude.hover_thrust = 1.5;
    /// assert_eq!(config.validate().unwrap_err().to_string(), "altitude.hover_thrust must be in (0, 1]");
    /// ```
    pub fn validate(&self) -> Result<(), InvalidConfig> {
        self.altitude
            .validate()
            .map_err(|e| e.in_section("altitude"))?;
        self.lateral
            .validate()
            .map_err(|e| e.in_section("lateral"))?;
        check_limit("navigation_period_s", self.navigation_period_s)?;
        check_limit("max_stick_angles_rad.x", self.max_stick_angles_rad.x)?;
        check_limit("max_stick_angles_rad.y", self.max_stick_angles_rad.y)?;
        check_tilt("max_tilt_rad", self.max_tilt_rad)?;
        check_positive("max_descent_mps", self.max_descent_mps)?;
        check(
            "land_speed_mps",
            self.land_speed_mps > DESCENDING_MPS && self.land_speed_mps.is_finite(),
            "above the touchdown detector's DESCENDING_MPS and finite",
        )
    }
}

/// Copter guidance: altitude and lateral position loops for waypoints, and heading hold for
/// piloted attitude flight.
///
/// # Examples
///
/// ```
/// use stable_flight::guidance::copter::{CopterGuidance, CopterGuidanceConfig};
/// use stable_flight::{AttitudeCommand, ControlInput, Guidance, Setpoint, VehicleState};
///
/// let mut guidance = CopterGuidance::new(CopterGuidanceConfig::default());
/// let state = VehicleState { heading_rad: 1.0, ..Default::default() };
/// guidance.prime(&state);
///
/// let input = ControlInput::Attitude(AttitudeCommand { throttle: 0.5, ..Default::default() });
/// let Setpoint::Attitude(setpoint) = guidance.guide(0.004, &input, &state) else {
///     unreachable!("attitude commands yield attitude setpoints");
/// };
/// assert!((setpoint.heading_rad - 1.0).abs() < 1e-6); // holds the primed heading
/// assert_eq!(setpoint.thrust, 0.5);
/// ```
pub struct CopterGuidance {
    /// Loop configs and limits, passed into the controllers every tick.
    config: CopterGuidanceConfig,

    altitude: AltitudeController,
    lateral: LateralController,
    /// Where the altitude loop is steered, in meters: the waypoint's altitude, or on the way
    /// down to it, slewed at the descent limit. `None` until waypoint or land flight latches
    /// it from the vehicle's altitude.
    altitude_target_m: Option<f32>,

    /// Heading-hold target for attitude, absolute-attitude, waypoint, and land commands.
    heading: f32,

    /// Paces the lateral loop.
    lateral_rate: RateDivider,
    /// The lateral loop's last roll/pitch in radians, held between its runs.
    tilt_rad: Vec2,
    /// The held tilt was capped at `max_tilt_rad` when the lateral loop last ran.
    tilt_capped: bool,

    /// What the last `guide` call clipped.
    saturation: Saturation,
}

impl CopterGuidance {
    /// Creates a new [CopterGuidance] with the specified configuration.
    ///
    /// # Panics
    ///
    /// If `config` is invalid ([`CopterGuidanceConfig::validate`]).
    pub fn new(config: CopterGuidanceConfig) -> Self {
        if let Err(error) = config.validate() {
            panic!("invalid CopterGuidanceConfig: {error}");
        }
        CopterGuidance {
            config,
            altitude: AltitudeController::default(),
            lateral: LateralController::default(),
            altitude_target_m: None,

            heading: 0.0,

            lateral_rate: RateDivider::default(),
            tilt_rad: Vec2::zeros(),
            tilt_capped: false,
            saturation: Saturation::default(),
        }
    }
}

impl Configurable for CopterGuidance {
    type Config = CopterGuidanceConfig;

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

    /// Validates with [`CopterGuidanceConfig::validate`]. The heading-hold target and the
    /// lateral loop's timing are kept; a new `navigation_period_s` applies from the next tick,
    /// and the held waypoint tilt is re-capped to a lowered `max_tilt_rad` at once.
    fn set_config(&mut self, config: CopterGuidanceConfig) -> Result<(), InvalidConfig> {
        config.validate()?;
        let old = &self.config;
        self.altitude
            .rescale_integral(&old.altitude, &config.altitude);
        self.lateral.rescale_integral(&old.lateral, &config.lateral);
        self.config = config;
        if self.tilt_rad.norm() > config.max_tilt_rad {
            self.tilt_capped = true;
        }
        self.tilt_rad = cap_magnitude(self.tilt_rad, config.max_tilt_rad);
        Ok(())
    }
}

impl Guidance for CopterGuidance {
    fn guide(&mut self, dt: f32, input: &ControlInput, estimated_state: &VehicleState) -> Setpoint {
        assert_dt(dt);
        self.saturation = Saturation::default();

        if !matches!(input, ControlInput::Waypoint(_) | ControlInput::Land(_)) {
            // The position loops are idle: clear them so re-entering waypoint mode starts
            // fresh, runs the lateral loop at once instead of holding a stale tilt, carries no
            // integral from an earlier leg, and starts its altitude target from the vehicle.
            self.altitude = AltitudeController::default();
            self.lateral = LateralController::default();
            self.altitude_target_m = None;
            self.lateral_rate.reset();
        }

        match input {
            ControlInput::Attitude(attitude_command) => {
                Setpoint::Attitude(self.attitude_setpoint(dt, attitude_command, estimated_state))
            }
            ControlInput::Rate(rate_command) => {
                // Track the vehicle so attitude or waypoint flight holds the heading it has
                // when rate flight ends, not the one it had when rate flight began.
                if estimated_state.heading_rad.is_finite() {
                    self.heading = wrap_angle(estimated_state.heading_rad);
                }
                Setpoint::Rate(rate_command.into())
            }
            ControlInput::Waypoint(waypoint_command) => {
                Setpoint::Attitude(self.navigate(dt, waypoint_command, estimated_state))
            }
            ControlInput::AbsoluteAttitude(command) => {
                Setpoint::Attitude(self.absolute_attitude_setpoint(command, estimated_state))
            }
            ControlInput::Land(command) => {
                Setpoint::Attitude(self.land(dt, command, estimated_state))
            }
        }
    }

    /// Clears the altitude and lateral controller state, the altitude target, and any held
    /// waypoint tilt, and latches the current heading as the yaw-hold target.
    fn prime(&mut self, state: &VehicleState) {
        self.altitude = AltitudeController::default();
        self.lateral = LateralController::default();
        self.altitude_target_m = None;
        self.heading = state.heading_rad;
        self.lateral_rate.reset();
        self.tilt_rad = Vec2::zeros();
        self.tilt_capped = false;
        self.saturation = Saturation::default();
    }

    /// `tilt` when the waypoint tilt request was capped at
    /// [`max_tilt_rad`](CopterGuidanceConfig::max_tilt_rad) (held with the tilt between
    /// lateral-loop runs), and `thrust` when the altitude loop's thrust, attitude mode's
    /// boosted throttle, or an absolute attitude's thrust was clipped to `[0, 1]`. A tilt
    /// beyond the stabilizer's limit in an absolute attitude is the stabilizer's to flag.
    fn saturation(&self) -> Saturation {
        self.saturation
    }
}

impl CopterGuidance {
    /// Scales the roll/pitch sticks to radians, boosts throttle for the measured tilt, and
    /// slews the heading-hold target with the yaw stick.
    fn attitude_setpoint(
        &mut self,
        dt: f32,
        attitude_command: &AttitudeCommand,
        estimated_state: &VehicleState,
    ) -> AttitudeSetpoint {
        // Commands from input devices can be out of range or NaN; saturate rather than panic
        // (see `crate::math`).
        let yaw = saturate_unit(attitude_command.yaw);
        let max_stick = self.config.max_stick_angles_rad;
        self.set_heading_target(self.heading + yaw * HEADING_SLEW_RATE * dt, estimated_state);

        AttitudeSetpoint {
            roll_rad: saturate_unit(attitude_command.roll) * max_stick.x,
            pitch_rad: saturate_unit(attitude_command.pitch) * max_stick.y,
            heading_rad: self.heading,
            // Angle boost: hold the vertical thrust the stick asks for while tilted, as the
            // altitude loop does (at most 2× past 60°, none when inverted). Saturated to
            // [0, 1]; NaN means none.
            thrust: self
                .clip_thrust(attitude_command.throttle * tilt_boost(&estimated_state.attitude)),
        }
    }

    /// Flies an absolute attitude: the heading becomes the heading-hold target, so stick
    /// flight afterward keeps it.
    fn absolute_attitude_setpoint(
        &mut self,
        command: &AbsoluteAttitudeCommand,
        estimated_state: &VehicleState,
    ) -> AttitudeSetpoint {
        // A non-finite heading holds the current target.
        let target_rad = if command.heading_rad.is_finite() {
            command.heading_rad
        } else {
            self.heading
        };
        self.set_heading_target(target_rad, estimated_state);
        AttitudeSetpoint {
            roll_rad: command.roll_rad,
            pitch_rad: command.pitch_rad,
            heading_rad: self.heading,
            thrust: self.clip_thrust(command.thrust),
        }
    }

    /// Sets the heading-hold target to `target_rad`, wrapped.
    fn set_heading_target(&mut self, target_rad: f32, estimated_state: &VehicleState) {
        // Wrapped so sustained yaw input can't grow the target without bound; the yaw
        // controller works on the wrapped error either way.
        let target = wrap_angle(target_rad);
        if target.is_finite() {
            self.heading = target;
        } else if estimated_state.heading_rad.is_finite() {
            // A NaN target (e.g. primed from a NaN estimate) would otherwise disable heading
            // hold for good; re-latch from the current estimate once it's valid.
            self.heading = wrap_angle(estimated_state.heading_rad);
        }
    }

    /// Flies toward the waypoint, holding the heading-hold target: the lateral loop gives
    /// roll/pitch (once per navigation period, held in between) and the altitude loop gives
    /// thrust (every tick), toward an altitude target that descends no faster than
    /// `max_descent_mps`.
    fn navigate(
        &mut self,
        dt: f32,
        waypoint_command: &WaypointCommand,
        estimated_state: &VehicleState,
    ) -> AttitudeSetpoint {
        let target = self.descend_toward(
            dt,
            waypoint_command.altitude_m,
            estimated_state.position_m.z,
        );
        self.fly_to(dt, waypoint_command, target, estimated_state)
    }

    /// Lands at the command's position: flies there as to a waypoint, with an altitude target
    /// that keeps descending at `land_speed_mps`, leashed [`LAND_LEASH_M`] below the vehicle.
    fn land(
        &mut self,
        dt: f32,
        land_command: &LandCommand,
        estimated_state: &VehicleState,
    ) -> AttitudeSetpoint {
        let altitude_m = estimated_state.position_m.z;
        let speed_mps = self.config.land_speed_mps;
        // `f32::max` ignores a NaN side: a NaN altitude keeps lowering a latched target (the
        // altitude loop holds hover meanwhile). With nothing latched, the NaN target holds hover.
        let target_m = self
            .lowered_altitude_target_m(dt, speed_mps, altitude_m)
            .map_or(f32::NAN, |t| t.max(altitude_m - LAND_LEASH_M));
        self.altitude_target_m = Some(target_m).filter(|t| t.is_finite());
        let waypoint = WaypointCommand {
            position_m: land_command.position_m,
            ..Default::default()
        };
        let target = AltitudeTarget {
            altitude_m: target_m,
            velocity_mps: -speed_mps,
        };
        self.fly_to(dt, &waypoint, target, estimated_state)
    }

    /// The altitude loop's target on the way to `waypoint_m`: the waypoint itself when it is
    /// above the latched target or within one step of it, otherwise the latched target lowered
    /// by `max_descent_mps · dt`, with that descent rate fed forward.
    fn descend_toward(&mut self, dt: f32, waypoint_m: f32, altitude_m: f32) -> AltitudeTarget {
        let max_descent_mps = self.config.max_descent_mps;
        let level = AltitudeTarget {
            altitude_m: waypoint_m,
            velocity_mps: 0.0,
        };
        let Some(step_m) = self.lowered_altitude_target_m(dt, max_descent_mps, altitude_m) else {
            return level;
        };
        // `<` is false for NaN: a non-finite waypoint passes through, and the altitude loop
        // holds hover on it.
        let target = if waypoint_m < step_m {
            AltitudeTarget {
                altitude_m: step_m,
                velocity_mps: -max_descent_mps,
            }
        } else {
            level
        };
        self.altitude_target_m = Some(target.altitude_m).filter(|t| t.is_finite());
        target
    }

    /// The latched altitude target, or with none latched the vehicle's `altitude_m`, lowered
    /// by `rate_mps · dt`. `None` until there is a finite altitude to descend from; the
    /// altitude loop holds hover on a non-finite estimate meanwhile (input policy).
    fn lowered_altitude_target_m(&self, dt: f32, rate_mps: f32, altitude_m: f32) -> Option<f32> {
        self.altitude_target_m
            .or(Some(altitude_m).filter(|a| a.is_finite()))
            .map(|start_m| start_m - rate_mps * dt)
    }

    /// Flies toward the waypoint's position and the altitude `target`, holding the heading-hold
    /// target.
    fn fly_to(
        &mut self,
        dt: f32,
        waypoint_command: &WaypointCommand,
        target: AltitudeTarget,
        estimated_state: &VehicleState,
    ) -> AttitudeSetpoint {
        self.set_heading_target(self.heading, estimated_state);
        let tilt_rad = self.tilt_toward(dt, waypoint_command, estimated_state);

        let linear_thrust = self.altitude.control(
            &self.config.altitude,
            dt,
            target,
            AltitudeEstimate {
                altitude_m: estimated_state.position_m.z,
                velocity_mps: estimated_state.velocity_mps.z,
                attitude: estimated_state.attitude,
            },
        );

        // The altitude loop clamps its thrust to [0, 1]; landing on a bound means it clipped.
        self.saturation.thrust = linear_thrust <= 0.0 || linear_thrust >= 1.0;
        self.saturation.tilt = self.tilt_capped;

        AttitudeSetpoint {
            roll_rad: tilt_rad.x,
            pitch_rad: tilt_rad.y,
            heading_rad: self.heading,
            thrust: linear_thrust,
        }
    }

    /// Saturates a thrust request to `[0, 1]` (NaN means none), flagging `thrust` when it
    /// was outside.
    fn clip_thrust(&mut self, thrust: f32) -> f32 {
        self.saturation.thrust = !(0.0..=1.0).contains(&thrust) && !thrust.is_nan();
        saturate_throttle(thrust)
    }

    /// Roll/pitch in radians toward the waypoint from the lateral loop, run when
    /// [`lateral_rate`](Self::lateral_rate) is due and held otherwise.
    fn tilt_toward(
        &mut self,
        dt: f32,
        waypoint_command: &WaypointCommand,
        estimated_state: &VehicleState,
    ) -> Vec2 {
        let Some(step) = self.lateral_rate.due(dt, self.config.navigation_period_s) else {
            return self.tilt_rad;
        };

        let target = LateralTarget {
            position_m: waypoint_command.position_m,
            velocity_mps: waypoint_command.velocity_mps.unwrap_or_else(Vec2::zeros),
            max_speed_mps: waypoint_command.cruise_speed_mps,
        };
        let estimate = LateralEstimate {
            position_m: estimated_state.position_m.xy(),
            velocity_mps: estimated_state.velocity_mps.xy(),
        };
        let heading_rad = estimated_state.heading_rad;

        // A non-finite target or estimate gets level for this tick only (input policy). The
        // loop doesn't run, so the neutral output isn't held for a period and the next tick
        // retries over the combined time.
        let error = target.position_m - estimate.position_m;
        if !(all_finite(&error)
            && all_finite(&target.velocity_mps)
            && all_finite(&estimate.velocity_mps)
            && heading_rad.is_finite())
        {
            self.tilt_capped = false;
            return Vec2::zeros();
        }

        let accel_mps2 = self
            .lateral
            .control(&self.config.lateral, step, target, estimate);
        let requested = tilt_for_acceleration(accel_mps2, heading_rad);
        self.tilt_capped = requested.norm() > self.config.max_tilt_rad;
        self.tilt_rad = cap_magnitude(requested, self.config.max_tilt_rad);
        self.lateral_rate.ran();
        self.tilt_rad
    }
}

/// Roll/pitch in radians, at `heading_rad`, that points thrust to give horizontal
/// acceleration `accel_mps2` (north, east) at constant altitude: a tilt of `atan(|a| / g)`
/// toward it. Ahead is nose down (negative pitch), to the right is right roll. The tilt is a
/// vector of that length, split between the axes, exact for single-axis requests and close for
/// diagonal ones at waypoint tilts. A non-finite acceleration or heading gives level.
fn tilt_for_acceleration(accel_mps2: Vec2, heading_rad: f32) -> Vec2 {
    let accel_norm = accel_mps2.norm();
    if !accel_norm.is_finite() || accel_norm == 0.0 || !heading_rad.is_finite() {
        return Vec2::zeros();
    }
    let tilt_rad = libm::atanf(accel_norm / GRAVITY);
    // Rotate (north, east) into (ahead, right) of the heading.
    let (sin, cos) = (libm::sinf(heading_rad), libm::cosf(heading_rad));
    let ahead = accel_mps2.x * cos + accel_mps2.y * sin;
    let right = -accel_mps2.x * sin + accel_mps2.y * cos;
    Vec2::new(right, -ahead) * (tilt_rad / accel_norm)
}