Skip to main content

stable_flight/guidance/vtol/
forward.rs

1//! [`ForwardThrust`]: a quadplane's pusher taking the nose-down part of copter flight, so the
2//! wing isn't flown at negative α.
3
4use core::f32::consts::{FRAC_PI_2, FRAC_PI_4};
5
6use crate::Configurable;
7use crate::controllers::{InvalidConfig, check, check_limit};
8use crate::math::DEG;
9
10mod params;
11#[cfg(test)]
12mod tests;
13
14/// [`ForwardThrust`] settings: ArduPilot's `Q_FWD_THR_GAIN` and `Q_FWD_PIT_LIM`
15/// (`ArduPlane/quadplane.cpp:516`, `:525`).
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct ForwardThrustConfig {
18    /// Pusher linear thrust per unit of `tan(pitch)` the hover asks for nose down; `0` (the
19    /// default) is off. ArduPilot sets it to the weight over the pusher's most thrust
20    /// (`quadplane.cpp:512`). Non-negative and finite.
21    pub gain: f32,
22    /// The most nose-down pitch copter flight keeps while forward thrust is on, in radians, in
23    /// `[0, π/2)`.
24    pub pitch_limit_rad: f32,
25}
26
27impl ForwardThrustConfig {
28    /// `Ok` when the gain is non-negative and finite and the pitch limit in `[0, π/2)`.
29    ///
30    /// # Errors
31    ///
32    /// [`InvalidConfig`] naming the first field out of range.
33    pub fn validate(&self) -> Result<(), InvalidConfig> {
34        check_limit("gain", self.gain)?;
35        check(
36            "pitch_limit_rad",
37            (0.0..FRAC_PI_2).contains(&self.pitch_limit_rad),
38            "in [0, π/2) rad",
39        )
40    }
41}
42
43impl Default for ForwardThrustConfig {
44    /// Off, as ArduPilot (`Q_FWD_THR_USE`, `quadplane.cpp:532`) and PX4 (`VT_FWD_THRUST_EN`,
45    /// `standard_params.yaml:5`) ship it, with ArduPilot's 3° pitch limit.
46    fn default() -> Self {
47        ForwardThrustConfig {
48            gain: 0.0,
49            pitch_limit_rad: 3.0 * DEG,
50        }
51    }
52}
53
54/// The pusher's share of copter flight on a quadplane: the hover's pitch held to
55/// [`pitch_limit_rad`](ForwardThrustConfig::pitch_limit_rad) nose down, and the pusher's
56/// thrust for what the hover asked, `gain · tan(nose-down pitch)` up to 1 with the pitch
57/// taken to at most 45° (ArduPilot's `assign_tilt_to_fwd_thr`, `ArduPlane/quadplane.cpp:2942-3058`,
58/// ArduPilot `368dc0c428`). Nose up is left alone: the pusher can't push back. A NaN pitch
59/// gives no thrust and stays NaN, which the stabilizer flies as level.
60///
61/// Without it, a quadplane flying as a copter tilts nose down to move ahead or into wind, and
62/// at speed the wing at negative α pushes it down (#427).
63///
64/// # Examples
65///
66/// ```
67/// use stable_flight::guidance::vtol::{ForwardThrust, ForwardThrustConfig};
68///
69/// let forward = ForwardThrust::new(ForwardThrustConfig { gain: 1.0, ..Default::default() });
70/// let split = forward.split(-20.0_f32.to_radians());
71/// assert!((split.pitch_rad - -3.0_f32.to_radians()).abs() < 1e-6);
72/// assert!((split.thrust - 20.0_f32.to_radians().tan()).abs() < 1e-6);
73/// ```
74#[derive(Debug, Clone, Copy, PartialEq)]
75pub struct ForwardThrust {
76    config: ForwardThrustConfig,
77}
78
79/// A hover pitch split between the tilt and the pusher.
80#[derive(Debug, Clone, Copy, PartialEq)]
81pub struct ForwardSplit {
82    /// The pitch the vehicle flies, in radians.
83    pub pitch_rad: f32,
84    /// The pusher's linear thrust, `[0, 1]`.
85    pub thrust: f32,
86}
87
88impl ForwardThrust {
89    /// Forward thrust with `config`.
90    ///
91    /// # Panics
92    ///
93    /// If `config` is invalid ([`ForwardThrustConfig::validate`]).
94    pub fn new(config: ForwardThrustConfig) -> Self {
95        if let Err(error) = config.validate() {
96            panic!("invalid ForwardThrustConfig: {error}");
97        }
98        ForwardThrust { config }
99    }
100
101    /// On: a positive gain.
102    pub fn enabled(&self) -> bool {
103        self.config.gain > 0.0
104    }
105
106    /// Splits the hover's `pitch_rad` between the tilt and the pusher; off, the pitch passes
107    /// through with no thrust.
108    pub fn split(&self, pitch_rad: f32) -> ForwardSplit {
109        if !self.enabled() || pitch_rad.is_nan() {
110            return ForwardSplit {
111                pitch_rad,
112                thrust: 0.0,
113            };
114        }
115        let nose_down_rad = (-pitch_rad).clamp(0.0, FRAC_PI_4);
116        ForwardSplit {
117            pitch_rad: pitch_rad.max(-self.config.pitch_limit_rad),
118            thrust: (self.config.gain * libm::tanf(nose_down_rad)).min(1.0),
119        }
120    }
121}
122
123impl Configurable for ForwardThrust {
124    type Config = ForwardThrustConfig;
125
126    fn config(&self) -> &ForwardThrustConfig {
127        &self.config
128    }
129
130    /// Validates with [`ForwardThrustConfig::validate`]; applies from the next tick.
131    fn set_config(&mut self, config: ForwardThrustConfig) -> Result<(), InvalidConfig> {
132        config.validate()?;
133        self.config = config;
134        Ok(())
135    }
136}