use crate::{Vec2, Vec3};
use crate::{
AttitudeSetpoint, Configurable, ControlOutput, RateSetpoint, Saturation, Setpoint, Stabilizer,
VehicleState,
};
use crate::controllers::*;
use crate::controllers::{check_limit, check_positive};
use crate::math::{assert_dt, euler_angles, saturate_throttle, saturate_unit};
mod params;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CopterStabilizerConfig {
pub body_rate: BodyRateControllerConfig,
pub attitude: AttitudeControllerConfig,
pub yaw: YawControllerConfig,
pub full_scale_rates_radps: Vec3,
pub thrust_curve_exponent: f32,
}
impl Default for CopterStabilizerConfig {
fn default() -> Self {
CopterStabilizerConfig {
body_rate: BodyRateControllerConfig::default(),
attitude: AttitudeControllerConfig::default(),
yaw: YawControllerConfig::default(),
full_scale_rates_radps: Vec3::new(3.0, 3.0, 3.0),
thrust_curve_exponent: 2.0,
}
}
}
impl CopterStabilizerConfig {
pub fn validate(&self) -> Result<(), InvalidConfig> {
self.body_rate
.validate()
.map_err(|e| e.in_section("body_rate"))?;
self.attitude
.validate()
.map_err(|e| e.in_section("attitude"))?;
self.yaw.validate().map_err(|e| e.in_section("yaw"))?;
let scales = &self.full_scale_rates_radps;
check_limit("full_scale_rates_radps.x", scales.x)?;
check_limit("full_scale_rates_radps.y", scales.y)?;
check_limit("full_scale_rates_radps.z", scales.z)?;
check_positive("thrust_curve_exponent", self.thrust_curve_exponent)
}
}
pub struct CopterStabilizer {
config: CopterStabilizerConfig,
body_rate: BodyRateController,
attitude: AttitudeController,
yaw: YawController,
saturation: Saturation,
}
impl CopterStabilizer {
pub fn new(config: CopterStabilizerConfig) -> Self {
if let Err(error) = config.validate() {
panic!("invalid CopterStabilizerConfig: {error}");
}
CopterStabilizer {
config,
body_rate: BodyRateController::default(),
attitude: AttitudeController::default(),
yaw: YawController::default(),
saturation: Saturation::default(),
}
}
}
impl Configurable for CopterStabilizer {
type Config = CopterStabilizerConfig;
fn config(&self) -> &CopterStabilizerConfig {
&self.config
}
fn set_config(&mut self, config: CopterStabilizerConfig) -> Result<(), InvalidConfig> {
config.validate()?;
let old = &self.config;
self.body_rate
.rescale_integral(&old.body_rate, &config.body_rate);
self.attitude
.rescale_integral(&old.attitude, &config.attitude);
self.yaw.rescale_integral(&old.yaw, &config.yaw);
self.config = config;
Ok(())
}
}
impl Stabilizer for CopterStabilizer {
fn stabilize(
&mut self,
dt: f32,
setpoint: &Setpoint,
estimated_state: &VehicleState,
) -> ControlOutput {
assert_dt(dt);
self.saturation = Saturation::default();
let output = match setpoint {
Setpoint::Attitude(attitude) => self.track_attitude(dt, attitude, estimated_state),
Setpoint::Rate(rates) => {
self.attitude = AttitudeController::default();
self.yaw = YawController::default();
self.track_rates(dt, rates, estimated_state)
}
};
self.saturation.moments = output.moments.map(|m| m.abs() >= 1.0).into();
output
}
fn prime(&mut self, _state: &VehicleState) {
self.body_rate = BodyRateController::default();
self.attitude = AttitudeController::default();
self.yaw = YawController::default();
self.saturation = Saturation::default();
}
fn saturation(&self) -> Saturation {
self.saturation
}
}
impl CopterStabilizer {
fn track_attitude(
&mut self,
dt: f32,
setpoint: &AttitudeSetpoint,
estimated_state: &VehicleState,
) -> ControlOutput {
let desired_angles_rad = Vec2::new(setpoint.roll_rad, setpoint.pitch_rad);
let max_angles_rad = self.config.attitude.max_angles_rad;
self.saturation.tilt = desired_angles_rad.x.abs() > max_angles_rad.x
|| desired_angles_rad.y.abs() > max_angles_rad.y;
let (roll, pitch, current_heading) = euler_angles(&estimated_state.attitude);
let current_angles = Vec2::new(roll, pitch);
let config = &self.config;
let desired_rates =
self.attitude
.control(&config.attitude, dt, desired_angles_rad, current_angles);
let desired_yaw_rate =
self.yaw
.control(&config.yaw, dt, setpoint.heading_rad, current_heading);
let current_rates = Vec3::new(
estimated_state.angular_velocity_radps.x,
estimated_state.angular_velocity_radps.y,
estimated_state.angular_velocity_radps.z,
);
let desired_rates = Vec3::new(desired_rates.x, desired_rates.y, desired_yaw_rate);
let moments = self
.body_rate
.control(&config.body_rate, dt, desired_rates, current_rates);
ControlOutput {
thrust: self.fit_to_thrust_curve(setpoint.thrust),
moments,
}
}
fn track_rates(
&mut self,
dt: f32,
setpoint: &RateSetpoint,
estimated_state: &VehicleState,
) -> ControlOutput {
let desired_rates = Vec3::new(
saturate_unit(setpoint.roll_rate),
saturate_unit(setpoint.pitch_rate),
saturate_unit(setpoint.yaw_rate),
)
.component_mul(&self.config.full_scale_rates_radps);
let current_rates = Vec3::new(
estimated_state.angular_velocity_radps.x,
estimated_state.angular_velocity_radps.y,
estimated_state.angular_velocity_radps.z,
);
let moments =
self.body_rate
.control(&self.config.body_rate, dt, desired_rates, current_rates);
ControlOutput {
thrust: self.fit_to_thrust_curve(setpoint.thrust),
moments,
}
}
fn fit_to_thrust_curve(&mut self, linear_thrust: f32) -> f32 {
self.saturation.thrust = !(0.0..=1.0).contains(&linear_thrust) && !linear_thrust.is_nan();
let nonlinear_thrust = libm::powf(
saturate_throttle(linear_thrust),
self.config.thrust_curve_exponent,
);
nonlinear_thrust.clamp(0.0, 1.0)
}
}