use crate::Vec3;
use crate::tuning::{LoopSignals, Probe, ProbeConfig, ProbeStatus, Tunable};
use super::{CopterStabilizer, CopterStabilizerConfig};
use crate::controllers::Gains;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TunedLoop {
RollRate,
PitchRate,
YawRate,
Roll,
Pitch,
Heading,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Location {
Rate(usize),
Angle(usize),
Heading,
}
impl TunedLoop {
pub fn gains(self, config: &CopterStabilizerConfig) -> Gains {
let mut config = *config;
*self.gains_mut(&mut config)
}
pub fn gains_mut(self, config: &mut CopterStabilizerConfig) -> &mut Gains {
match self {
Self::RollRate => &mut config.body_rate.roll_gains,
Self::PitchRate => &mut config.body_rate.pitch_gains,
Self::YawRate => &mut config.body_rate.yaw_gains,
Self::Roll => &mut config.attitude.roll_gains,
Self::Pitch => &mut config.attitude.pitch_gains,
Self::Heading => &mut config.yaw.gains,
}
}
pub fn is_rate(self) -> bool {
matches!(self.location(), Location::Rate(_))
}
pub fn inner(self) -> Option<Self> {
match self {
Self::Roll => Some(Self::RollRate),
Self::Pitch => Some(Self::PitchRate),
Self::Heading => Some(Self::YawRate),
Self::RollRate | Self::PitchRate | Self::YawRate => None,
}
}
pub fn outer(self) -> Option<Self> {
match self {
Self::RollRate => Some(Self::Roll),
Self::PitchRate => Some(Self::Pitch),
Self::YawRate => Some(Self::Heading),
Self::Roll | Self::Pitch | Self::Heading => None,
}
}
pub(super) fn location(self) -> Location {
match self {
Self::RollRate => Location::Rate(0),
Self::PitchRate => Location::Rate(1),
Self::YawRate => Location::Rate(2),
Self::Roll => Location::Angle(0),
Self::Pitch => Location::Angle(1),
Self::Heading => Location::Heading,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct Stage {
pub(super) target: TunedLoop,
pub(super) probe: Probe,
handed_back: bool,
interrupted: bool,
}
impl Stage {
pub(super) fn is_running(&self) -> bool {
!self.handed_back
}
fn status(&self) -> ProbeStatus {
if self.interrupted {
ProbeStatus::Interrupted
} else {
self.probe.status()
}
}
}
impl Tunable for CopterStabilizer {
type Target = TunedLoop;
fn start_probe(&mut self, target: TunedLoop, config: ProbeConfig) {
self.stop_probe();
self.stage = Some(Stage {
target,
probe: Probe::new(config),
handed_back: false,
interrupted: false,
});
}
fn probe(&self) -> Option<(TunedLoop, ProbeStatus)> {
self.stage
.as_ref()
.map(|stage| (stage.target, stage.status()))
}
fn stop_probe(&mut self) -> Option<(TunedLoop, ProbeStatus)> {
self.hand_back();
self.stage
.take()
.map(|stage| (stage.target, stage.status()))
}
}
impl CopterStabilizer {
pub(super) fn hand_back(&mut self) {
let Some(stage) = &mut self.stage else {
return;
};
if stage.handed_back {
return;
}
stage.handed_back = true;
match stage.target.location() {
Location::Rate(axis) => self.body_rate.hand_back(axis),
Location::Angle(axis) => self.attitude.hand_back(axis),
Location::Heading => self.yaw.hand_back(),
}
}
pub(super) fn interrupt_attitude_stage(&mut self) {
let Some(stage) = &mut self.stage else {
return;
};
let angle_loop = !matches!(stage.target.location(), Location::Rate(_));
if angle_loop && stage.is_running() {
stage.interrupted = true;
self.hand_back();
}
}
pub(super) fn running_stage(&self) -> Option<Location> {
self.stage
.as_ref()
.filter(|stage| stage.is_running())
.map(|stage| stage.target.location())
}
fn step_stage(&mut self, dt: f32, signals: &LoopSignals) -> Option<f32> {
let stage = self.stage.as_mut().filter(|stage| stage.is_running())?;
Some(stage.probe.step(dt, signals))
}
pub(super) fn finish_stage(&mut self) {
let finished = self
.stage
.as_ref()
.is_some_and(|stage| stage.is_running() && !stage.probe.status().is_running());
if finished {
self.hand_back();
}
}
pub(super) fn probe_attitude(
&mut self,
dt: f32,
setpoints: &mut Vec3,
error: &Vec3,
rates_radps: &Vec3,
) {
let Some(location) = self.running_stage() else {
return;
};
let saturated = self.loop_saturated(location);
let axis = match location {
Location::Angle(axis) => axis,
Location::Heading => 2,
Location::Rate(_) => return,
};
let signals = LoopSignals {
setpoint: setpoints[axis],
error: error[axis],
saturated,
rate_radps: rates_radps[axis],
last_moment: self.last_moments[axis],
};
self.inject(dt, axis, &signals, setpoints)
}
pub(super) fn probe_rates(
&mut self,
dt: f32,
setpoints: &mut Vec3,
filtered_rates_radps: &Vec3,
raw_rates_radps: &Vec3,
) {
let Some(location @ Location::Rate(axis)) = self.running_stage() else {
return;
};
let signals = LoopSignals {
setpoint: setpoints[axis],
error: setpoints[axis] - filtered_rates_radps[axis],
saturated: self.loop_saturated(location),
rate_radps: raw_rates_radps[axis],
last_moment: self.last_moments[axis],
};
self.inject(dt, axis, &signals, setpoints)
}
fn inject(&mut self, dt: f32, axis: usize, signals: &LoopSignals, setpoints: &mut Vec3) {
if let Some(setpoint) = self.step_stage(dt, signals) {
setpoints[axis] = setpoint;
}
}
fn loop_saturated(&self, location: Location) -> bool {
let clipped = &self.clipped;
match location {
Location::Rate(axis) => clipped.moments[axis],
Location::Angle(axis) => clipped.rates[axis] || clipped.moments[axis],
Location::Heading => clipped.rates[2] || clipped.moments[2],
}
}
}