use core::f32::consts::{FRAC_PI_2, FRAC_PI_4};
use crate::Configurable;
use crate::controllers::{InvalidConfig, check, check_limit};
use crate::math::DEG;
mod params;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ForwardThrustConfig {
pub gain: f32,
pub pitch_limit_rad: f32,
}
impl ForwardThrustConfig {
pub fn validate(&self) -> Result<(), InvalidConfig> {
check_limit("gain", self.gain)?;
check(
"pitch_limit_rad",
(0.0..FRAC_PI_2).contains(&self.pitch_limit_rad),
"in [0, π/2) rad",
)
}
}
impl Default for ForwardThrustConfig {
fn default() -> Self {
ForwardThrustConfig {
gain: 0.0,
pitch_limit_rad: 3.0 * DEG,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ForwardThrust {
config: ForwardThrustConfig,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ForwardSplit {
pub pitch_rad: f32,
pub thrust: f32,
}
impl ForwardThrust {
pub fn new(config: ForwardThrustConfig) -> Self {
if let Err(error) = config.validate() {
panic!("invalid ForwardThrustConfig: {error}");
}
ForwardThrust { config }
}
pub fn enabled(&self) -> bool {
self.config.gain > 0.0
}
pub fn split(&self, pitch_rad: f32) -> ForwardSplit {
if !self.enabled() || pitch_rad.is_nan() {
return ForwardSplit {
pitch_rad,
thrust: 0.0,
};
}
let nose_down_rad = (-pitch_rad).clamp(0.0, FRAC_PI_4);
ForwardSplit {
pitch_rad: pitch_rad.max(-self.config.pitch_limit_rad),
thrust: (self.config.gain * libm::tanf(nose_down_rad)).min(1.0),
}
}
}
impl Configurable for ForwardThrust {
type Config = ForwardThrustConfig;
fn config(&self) -> &ForwardThrustConfig {
&self.config
}
fn set_config(&mut self, config: ForwardThrustConfig) -> Result<(), InvalidConfig> {
config.validate()?;
self.config = config;
Ok(())
}
}