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;
9pub mod body_rate;
10mod derivative;
11pub mod lateral;
12pub(crate) mod low_pass;
13pub mod plane_rate;
14pub mod yaw;
15
16pub use altitude::{
17 AltitudeController, AltitudeControllerConfig, AltitudeEstimate, AltitudeTarget,
18 DEFAULT_HOVER_THRUST,
19};
20pub use attitude::{AttitudeController, AttitudeControllerConfig};
21pub use body_rate::{BodyRateController, BodyRateControllerConfig, RATE_I_PER_P, YAW_RATE_I_PER_P};
22pub use lateral::{LateralController, LateralControllerConfig, LateralEstimate, LateralTarget};
23pub use plane_rate::{PlaneRateController, PlaneRateControllerConfig, RateGains};
24pub use yaw::{YawController, YawControllerConfig};
25
26#[cfg(test)]
27mod tests;
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct InvalidConfig {
35 pub section: Option<&'static str>,
39 pub field: &'static str,
41 pub requirement: &'static str,
43}
44
45impl InvalidConfig {
46 pub(crate) fn new(field: &'static str, requirement: &'static str) -> Self {
47 Self {
48 section: None,
49 field,
50 requirement,
51 }
52 }
53
54 pub(crate) fn in_section(self, section: &'static str) -> Self {
56 Self {
57 section: Some(section),
58 ..self
59 }
60 }
61}
62
63impl fmt::Display for InvalidConfig {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 if let Some(section) = self.section {
66 write!(f, "{section}.")?;
67 }
68 write!(f, "{} must be {}", self.field, self.requirement)
69 }
70}
71
72impl core::error::Error for InvalidConfig {}
73
74pub(crate) fn check_limit(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
76 check(
77 field,
78 value >= 0.0 && value.is_finite(),
79 "non-negative and finite",
80 )
81}
82
83pub(crate) fn check_positive(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
85 check(
86 field,
87 value > 0.0 && value.is_finite(),
88 "positive and finite",
89 )
90}
91
92pub(crate) fn check_unit_interval(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
94 check(field, value > 0.0 && value <= 1.0, "in (0, 1]")
95}
96
97pub(crate) fn check_tilt(field: &'static str, value: f32) -> Result<(), InvalidConfig> {
99 check(
100 field,
101 value > 0.0 && value < core::f32::consts::FRAC_PI_2,
102 "in (0, π/2) rad",
103 )
104}
105
106pub(crate) fn check(
108 field: &'static str,
109 ok: bool,
110 requirement: &'static str,
111) -> Result<(), InvalidConfig> {
112 if ok {
113 Ok(())
114 } else {
115 Err(InvalidConfig::new(field, requirement))
116 }
117}
118
119pub(crate) fn rescale_integral(integral: f32, old_i: f32, new_i: f32) -> f32 {
126 if old_i == new_i {
127 return integral;
128 }
129 if old_i == 0.0 || new_i == 0.0 {
130 return 0.0;
131 }
132 let rescaled = integral * (old_i / new_i);
133 if rescaled.is_finite() { rescaled } else { 0.0 }
134}
135
136#[derive(Default, Clone, Copy, Debug, PartialEq)]
138pub struct Gains {
139 pub p: f32,
140 pub i: f32,
141 pub d: f32,
142}
143
144pub fn low_pass_filter_vec3(prev: Vec3, new: Vec3, alpha: f32) -> Vec3 {
146 Vec3::new(
147 alpha * new.x + (1.0 - alpha) * prev.x,
148 alpha * new.y + (1.0 - alpha) * prev.y,
149 alpha * new.z + (1.0 - alpha) * prev.z,
150 )
151}