use super::{
IdentifyAbort, IdentifyConfig, IdentifyProbe, IdentifyStatus, RateModel, StepAbort, StepConfig,
StepMetrics, StepProbe, StepStatus,
};
use crate::controllers::InvalidConfig;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ProbeConfig {
Step(StepConfig),
Identify(IdentifyConfig),
}
impl ProbeConfig {
pub fn validate(&self) -> Result<(), InvalidConfig> {
match self {
Self::Step(config) => config.validate(),
Self::Identify(config) => config.validate(),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct LoopSignals {
pub setpoint: f32,
pub error: f32,
pub saturated: bool,
pub rate_radps: f32,
pub last_moment: f32,
}
impl LoopSignals {
pub fn measurement(&self) -> f32 {
self.setpoint - self.error
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Progress {
Step {
elapsed_s: f32,
},
Identify {
elapsed_s: f32,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Measurement {
Step(StepMetrics),
Identify(RateModel),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProbeAbort {
Step(StepAbort),
Identify(IdentifyAbort),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ProbeStatus {
Running(Progress),
Done(Measurement),
Aborted(ProbeAbort),
Interrupted,
}
impl ProbeStatus {
pub fn is_running(&self) -> bool {
matches!(self, Self::Running(_))
}
}
impl From<StepStatus> for ProbeStatus {
fn from(status: StepStatus) -> Self {
match status {
StepStatus::Running { elapsed_s } => Self::Running(Progress::Step { elapsed_s }),
StepStatus::Done(metrics) => Self::Done(Measurement::Step(metrics)),
StepStatus::Aborted(abort) => Self::Aborted(ProbeAbort::Step(abort)),
}
}
}
impl From<IdentifyStatus> for ProbeStatus {
fn from(status: IdentifyStatus) -> Self {
match status {
IdentifyStatus::Running { elapsed_s } => {
Self::Running(Progress::Identify { elapsed_s })
}
IdentifyStatus::Done(model) => Self::Done(Measurement::Identify(model)),
IdentifyStatus::Aborted(abort) => Self::Aborted(ProbeAbort::Identify(abort)),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Probe {
kind: Kind,
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, PartialEq)]
enum Kind {
Step(StepProbe),
Identify(IdentifyProbe),
}
impl Probe {
pub fn new(config: ProbeConfig) -> Self {
let kind = match config {
ProbeConfig::Step(config) => Kind::Step(StepProbe::new(config)),
ProbeConfig::Identify(config) => Kind::Identify(IdentifyProbe::new(config)),
};
Self { kind }
}
pub fn step(&mut self, dt: f32, signals: &LoopSignals) -> f32 {
match &mut self.kind {
Kind::Step(step) => step.step(dt, signals.measurement(), signals.saturated),
Kind::Identify(identify) => {
let offset = identify.step(dt, signals.rate_radps, signals.last_moment);
signals.setpoint + offset
}
}
}
pub fn status(&self) -> ProbeStatus {
match &self.kind {
Kind::Step(step) => step.status().into(),
Kind::Identify(identify) => identify.status().into(),
}
}
}