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