use core::time::Duration;
use crate::controllers::InvalidConfig;
use crate::params::{ParamError, Params, locate};
use crate::tuning::{ProbeConfig, ProbeStatus, Tunable};
use crate::{
ActuatorCommand, AttitudeSetpoint, Commander, Configurable, ControlOutput, FlightModeReport,
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,
setpoint: Setpoint,
}
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,
setpoint: Setpoint::Attitude(AttitudeSetpoint {
idle: true,
..Default::default()
}),
}
}
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 copter_only = FlightModeReport::copter_only(command.regime);
let (setpoint, control, actuators, saturation, flight_mode) = if command.arm {
if !self.armed {
self.guidance.prime(&self.state);
self.stabilizer.prime(&self.state);
}
let setpoint =
self.guidance
.guide(dt_secs, command.regime, &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());
let flight_mode = self.guidance.flight_mode().unwrap_or(copter_only);
(setpoint, control, actuators, saturation, flight_mode)
} else {
let setpoint = Setpoint::Attitude(AttitudeSetpoint {
heading_rad: self.state.heading_rad,
idle: true,
..Default::default()
});
(
setpoint,
ControlOutput::default(),
ActuatorCommand::default(),
Saturation::default(),
copter_only,
)
};
self.armed = command.arm;
self.setpoint = setpoint;
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(),
flight_mode,
wind: self.guidance.wind(),
});
}
}
impl<C, V, G, S, M, T> FlightLoop<C, V, G, S, M, T> {
pub fn state(&self) -> &VehicleState {
&self.state
}
pub fn setpoint(&self) -> &Setpoint {
&self.setpoint
}
pub fn commander(&self) -> &C {
&self.commander
}
pub fn vehicle(&self) -> &V {
&self.vehicle
}
pub fn guidance(&self) -> &G {
&self.guidance
}
pub fn stabilizer(&self) -> &S {
&self.stabilizer
}
pub fn armed(&self) -> bool {
self.armed
}
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> FlightLoop<C, V, G, S, M, T> {
pub fn start_probe<L: Copy>(&mut self, target: L, config: ProbeConfig)
where
S: Tunable<L>,
{
self.stabilizer.start_probe(target, config);
}
pub fn probe<L: Copy>(&self) -> Option<(L, ProbeStatus)>
where
S: Tunable<L>,
{
self.stabilizer.probe()
}
pub fn stop_probe<L: Copy>(&mut self) -> Option<(L, ProbeStatus)>
where
S: Tunable<L>,
{
self.stabilizer.stop_probe()
}
}
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 locate(&[&self.guidance, &self.stabilizer], index)? {
(0, i) => self.guidance.param_name(i),
(_, i) => self.stabilizer.param_name(i),
}
}
fn param_at(&self, index: usize) -> Option<f32> {
match locate(&[&self.guidance, &self.stabilizer], index)? {
(0, i) => self.guidance.param_at(i),
(_, i) => self.stabilizer.param_at(i),
}
}
fn set_param_at(&mut self, index: usize, value: f32) -> Result<(), ParamError> {
match locate(&[&self.guidance, &self.stabilizer], index).ok_or(ParamError::Unknown)? {
(0, i) => self.guidance.set_param_at(i, value),
(_, i) => self.stabilizer.set_param_at(i, value),
}
}
}