stable_flight/controllers/
mod.rs1#![doc = include_str!("README.md")]
2
3use core::fmt;
4
5use crate::Vec3;
6
7pub mod altitude;
8pub mod attitude;
9mod biquad;
10pub mod body_rate;
11mod derivative;
12pub mod gyro;
13pub mod lateral;
14pub(crate) mod low_pass;
15pub mod notch;
16pub mod plane_rate;
17pub mod yaw;
18
19pub use altitude::{
20 AltitudeController, AltitudeControllerConfig, AltitudeEstimate, AltitudeTarget,
21 DEFAULT_HOVER_THRUST, DEFAULT_INTEGRATION_BAND_M,
22};
23pub use attitude::{AttitudeController, AttitudeControllerConfig};
24pub use body_rate::{BodyRateController, BodyRateControllerConfig, RATE_I_PER_P, YAW_RATE_I_PER_P};
25pub use gyro::GyroFilters;
26pub use lateral::{
27 DEFAULT_BRAKING_SHARE, LateralController, LateralControllerConfig, LateralEstimate,
28 LateralTarget,
29};
30pub use notch::NotchConfig;
31pub use plane_rate::{PlaneRateController, PlaneRateControllerConfig, RateGains};
32pub use yaw::{YawController, YawControllerConfig};
33
34#[cfg(test)]
35mod tests;
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct InvalidConfig {
43 pub section: Option<&'static str>,
47 pub field: &'static str,
49 pub requirement: &'static str,
51}
52
53impl InvalidConfig {
54 pub(crate) fn new(field: &'static str, requirement: &'static str) -> Self {
55 Self {
56 section: None,
57 field,
58 requirement,
59 }
60 }
61
62 pub(crate) fn in_section(self, section: &'static str) -> Self {
64 Self {
65 section: Some(section),
66 ..self
67 }
68 }
69}
70
71impl fmt::Display for InvalidConfig {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 if let Some(section) = self.section {
74 write!(f, "{section}.")?;
75 }
76 write!(f, "{} must be {}", self.field, self.requirement)
77 }
78}
79
80impl core::error::Error for InvalidConfig {}
81
82pub(crate) fn check_limit(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
84 check(
85 field,
86 value >= 0.0 && value.is_finite(),
87 "non-negative and finite",
88 )
89}
90
91pub(crate) fn check_positive(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
93 check(
94 field,
95 value > 0.0 && value.is_finite(),
96 "positive and finite",
97 )
98}
99
100pub(crate) fn check_unit_interval(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
102 check(field, value > 0.0 && value <= 1.0, "in (0, 1]")
103}
104
105pub(crate) fn check_tilt(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
107 check(
108 field,
109 value > 0.0 && value < core::f32::consts::FRAC_PI_2,
110 "in (0, π/2) rad",
111 )
112}
113
114pub(crate) fn check(
116 field: &'static str,
117 ok: bool,
118 requirement: &'static str,
119) -> Result<(), InvalidConfig> {
120 if ok {
121 Ok(())
122 } else {
123 Err(InvalidConfig::new(field, requirement))
124 }
125}
126
127pub(crate) fn rescale_integral(integral: f32, old_i: f32, new_i: f32) -> f32 {
134 if old_i == new_i {
135 return integral;
136 }
137 if old_i == 0.0 || new_i == 0.0 {
138 return 0.0;
139 }
140 let rescaled = integral * (old_i / new_i);
141 if rescaled.is_finite() { rescaled } else { 0.0 }
142}
143
144#[derive(Default, Clone, Copy, Debug, PartialEq)]
146pub struct Gains {
147 pub p: f32,
148 pub i: f32,
149 pub d: f32,
150}
151
152pub fn low_pass_filter_vec3(prev: Vec3, new: Vec3, alpha: f32) -> Vec3 {
154 Vec3::new(
155 alpha * new.x + (1.0 - alpha) * prev.x,
156 alpha * new.y + (1.0 - alpha) * prev.y,
157 alpha * new.z + (1.0 - alpha) * prev.z,
158 )
159}