use crate::VehicleState;
use crate::controllers::RateGains;
use crate::tuning::{LoopSignals, Probe, ProbeConfig, ProbeStatus, Tunable, WingIdentifyProbe};
use super::{PlaneStabilizer, PlaneStabilizerConfig};
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PlaneLoop {
RollRate,
PitchRate,
Roll,
Pitch,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PlaneAxisGains {
pub rate: RateGains,
pub time_constant_s: f32,
}
impl PlaneLoop {
pub fn axis(self) -> usize {
match self {
Self::RollRate | Self::Roll => 0,
Self::PitchRate | Self::Pitch => 1,
}
}
pub fn is_rate(self) -> bool {
matches!(self, Self::RollRate | Self::PitchRate)
}
pub fn inner(self) -> Option<Self> {
match self {
Self::Roll => Some(Self::RollRate),
Self::Pitch => Some(Self::PitchRate),
Self::RollRate | Self::PitchRate => None,
}
}
pub fn gains(self, config: &PlaneStabilizerConfig) -> PlaneAxisGains {
match self.axis() {
0 => PlaneAxisGains {
rate: config.rate.roll,
time_constant_s: config.time_constant_s.x,
},
_ => PlaneAxisGains {
rate: config.rate.pitch,
time_constant_s: config.time_constant_s.y,
},
}
}
pub fn set_gains(self, config: &mut PlaneStabilizerConfig, gains: PlaneAxisGains) {
match self.axis() {
0 => {
config.rate.roll = gains.rate;
config.time_constant_s.x = gains.time_constant_s;
}
_ => {
config.rate.pitch = gains.rate;
config.time_constant_s.y = gains.time_constant_s;
}
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct Stage {
target: PlaneLoop,
probe: StageProbe,
handed_back: bool,
interrupted: bool,
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, PartialEq)]
enum StageProbe {
Wing(WingIdentifyProbe),
Other(Probe),
}
impl StageProbe {
fn new(config: ProbeConfig) -> Self {
match config {
ProbeConfig::IdentifyWing(config) => Self::Wing(WingIdentifyProbe::new(config)),
config => Self::Other(Probe::new(config)),
}
}
fn step(&mut self, dt: f32, signals: &LoopSignals) -> f32 {
match self {
Self::Wing(wing) => {
let offset = wing.step(
dt,
signals.rate_radps,
signals.last_moment,
signals.alpha_rad,
);
signals.setpoint + offset
}
Self::Other(probe) => probe.step(dt, signals),
}
}
fn status(&self) -> ProbeStatus {
match self {
Self::Wing(wing) => wing.status().into(),
Self::Other(probe) => probe.status(),
}
}
}
impl Stage {
fn status(&self) -> ProbeStatus {
if self.interrupted {
ProbeStatus::Interrupted
} else {
self.probe.status()
}
}
}
impl Tunable<PlaneLoop> for PlaneStabilizer {
fn start_probe(&mut self, target: PlaneLoop, config: ProbeConfig) {
self.stop_probe();
self.stage = Some(Stage {
target,
probe: StageProbe::new(config),
handed_back: false,
interrupted: false,
});
}
fn probe(&self) -> Option<(PlaneLoop, ProbeStatus)> {
self.stage
.as_ref()
.map(|stage| (stage.target, stage.status()))
}
fn stop_probe(&mut self) -> Option<(PlaneLoop, ProbeStatus)> {
self.stage
.take()
.map(|stage| (stage.target, stage.status()))
}
}
impl PlaneStabilizer {
pub(crate) fn interrupt_stage(&mut self) {
if let Some(stage) = self.stage.as_mut().filter(|stage| !stage.handed_back) {
stage.handed_back = true;
stage.interrupted = true;
}
}
pub(super) fn interrupt_angle_stage(&mut self) {
if self.running_stage().is_some_and(|target| !target.is_rate()) {
self.interrupt_stage();
}
}
fn running_stage(&self) -> Option<PlaneLoop> {
self.stage
.as_ref()
.filter(|stage| !stage.handed_back)
.map(|stage| stage.target)
}
pub(super) fn probe_angles(
&mut self,
dt: f32,
angles_rad: &mut [f32; 2],
error_rad: [f32; 2],
rates_radps: &crate::Vec3,
) {
let Some(target) = self.running_stage().filter(|target| !target.is_rate()) else {
return;
};
let axis = target.axis();
let signals = LoopSignals {
setpoint: angles_rad[axis],
error: error_rad[axis],
saturated: self.moment_clipped(axis),
rate_radps: rates_radps[axis],
last_moment: self.last_moments[axis],
alpha_rad: 0.0,
};
if let Some(angle_rad) = self.step_stage(dt, &signals) {
angles_rad[axis] = angle_rad;
}
}
pub(super) fn probe_rates(
&mut self,
dt: f32,
desired_radps: &mut crate::Vec3,
state: &VehicleState,
) {
let Some(target) = self.running_stage().filter(|target| target.is_rate()) else {
return;
};
let axis = target.axis();
let rates_radps = &state.angular_velocity_radps;
let alpha_rad = match target {
PlaneLoop::PitchRate => self.angle_of_attack_rad(state),
_ => 0.0,
};
let signals = LoopSignals {
setpoint: desired_radps[axis],
error: desired_radps[axis] - rates_radps[axis],
saturated: self.moment_clipped(axis),
rate_radps: rates_radps[axis],
last_moment: self.last_moments[axis],
alpha_rad,
};
if let Some(rate_radps) = self.step_stage(dt, &signals) {
desired_radps[axis] = rate_radps;
}
}
pub(super) fn finish_stage(&mut self) {
if let Some(stage) = self.stage.as_mut()
&& !stage.handed_back
&& !stage.probe.status().is_running()
{
stage.handed_back = true;
}
}
fn step_stage(&mut self, dt: f32, signals: &LoopSignals) -> Option<f32> {
let stage = self.stage.as_mut().filter(|stage| !stage.handed_back)?;
Some(stage.probe.step(dt, signals))
}
fn moment_clipped(&self, axis: usize) -> bool {
self.last_moments[axis].abs() >= 1.0
}
}