use core::time::Duration;
use crate::controllers::InvalidConfig;
use crate::params::{ParamError, Params};
use crate::{
ActuatorCommand, AttitudeSetpoint, Commander, Configurable, ControlOutput, Guidance, Mixer,
Saturation, Setpoint, Stabilizer, TelemetryPublisher, TelemetryRecord, Vehicle, VehicleState,
};
#[cfg(test)]
mod tests;
pub struct FlightLoop<C, V, G, S, M, T> {
commander: C,
vehicle: V,
guidance: G,
stabilizer: S,
mixer: M,
telemetry: T,
state: VehicleState,
armed: bool,
}
impl<C, V, G, S, M, T> FlightLoop<C, V, G, S, M, T>
where
C: Commander,
V: Vehicle,
G: Guidance,
S: Stabilizer,
M: Mixer,
T: TelemetryPublisher,
{
pub fn new(
commander: C,
vehicle: V,
guidance: G,
stabilizer: S,
mixer: M,
telemetry: T,
) -> Self {
FlightLoop {
commander,
vehicle,
guidance,
stabilizer,
mixer,
telemetry,
state: VehicleState::default(),
armed: false,
}
}
pub fn prime(&mut self) {
self.state = self.vehicle.state(Duration::ZERO);
self.guidance.prime(&self.state);
self.stabilizer.prime(&self.state);
}
pub fn step(&mut self, dt: Duration) {
let dt_secs = dt.as_secs_f32();
let command = self.commander.next_command(dt_secs, &self.state);
self.state = self.vehicle.state(dt);
let (setpoint, control, actuators, saturation) = if command.arm {
if !self.armed {
self.guidance.prime(&self.state);
self.stabilizer.prime(&self.state);
}
let setpoint = self.guidance.guide(dt_secs, &command.input, &self.state);
let control = self.stabilizer.stabilize(dt_secs, &setpoint, &self.state);
let actuators = self.mixer.mix(&control);
let saturation = self
.guidance
.saturation()
.merge(self.stabilizer.saturation())
.merge(self.mixer.saturation());
(setpoint, control, actuators, saturation)
} else {
let setpoint = Setpoint::Attitude(AttitudeSetpoint {
heading_rad: self.state.heading_rad,
..Default::default()
});
(
setpoint,
ControlOutput::default(),
ActuatorCommand::default(),
Saturation::default(),
)
};
self.armed = command.arm;
self.vehicle.actuate(&actuators);
self.telemetry.publish(&TelemetryRecord {
dt,
command: &command,
setpoint: &setpoint,
state: &self.state,
control: &control,
actuators: &actuators,
saturation,
failsafe: self.commander.failsafe(),
});
}
pub fn state(&self) -> &VehicleState {
&self.state
}
pub fn commander(&self) -> &C {
&self.commander
}
pub fn guidance(&self) -> &G {
&self.guidance
}
pub fn stabilizer(&self) -> &S {
&self.stabilizer
}
pub fn set_guidance_config(&mut self, config: G::Config) -> Result<(), InvalidConfig>
where
G: Configurable,
{
self.guidance.set_config(config)
}
pub fn set_stabilizer_config(&mut self, config: S::Config) -> Result<(), InvalidConfig>
where
S: Configurable,
{
self.stabilizer.set_config(config)
}
pub fn into_parts(self) -> (C, V, G, S, M, T) {
(
self.commander,
self.vehicle,
self.guidance,
self.stabilizer,
self.mixer,
self.telemetry,
)
}
}
impl<C, V, G, S, M, T> Params for FlightLoop<C, V, G, S, M, T>
where
G: Params,
S: Params,
{
fn param_count(&self) -> usize {
self.guidance.param_count() + self.stabilizer.param_count()
}
fn param_name(&self, index: usize) -> Option<&'static str> {
match self.split_param_index(index) {
ParamOwner::Guidance(i) => self.guidance.param_name(i),
ParamOwner::Stabilizer(i) => self.stabilizer.param_name(i),
}
}
fn param_at(&self, index: usize) -> Option<f32> {
match self.split_param_index(index) {
ParamOwner::Guidance(i) => self.guidance.param_at(i),
ParamOwner::Stabilizer(i) => self.stabilizer.param_at(i),
}
}
fn set_param_at(&mut self, index: usize, value: f32) -> Result<(), ParamError> {
match self.split_param_index(index) {
ParamOwner::Guidance(i) => self.guidance.set_param_at(i, value),
ParamOwner::Stabilizer(i) => self.stabilizer.set_param_at(i, value),
}
}
}
enum ParamOwner {
Guidance(usize),
Stabilizer(usize),
}
impl<C, V, G: Params, S, M, T> FlightLoop<C, V, G, S, M, T> {
fn split_param_index(&self, index: usize) -> ParamOwner {
let guidance_count = self.guidance.param_count();
if index < guidance_count {
ParamOwner::Guidance(index)
} else {
ParamOwner::Stabilizer(index - guidance_count)
}
}
}