pub struct FlightLoop<C, V, G, S, M, T> { /* private fields */ }Expand description
Sequences one control tick: commander → estimate → guide → stabilize → mix → actuate → telemetry.
Call prime once before the first step.
The Command’s arm gates the motors: a disarmed tick skips guidance,
the stabilizer, and the mixer and actuates ActuatorCommand::default (motors off). The
first armed tick after a disarmed one (including the first tick) re-primes guidance and
the stabilizer, so integrators start from zero.
§Examples
use std::time::Duration;
use stable_flight::guidance::copter::{CopterGuidance, CopterGuidanceConfig};
use stable_flight::mixers::QuadCopterMixer;
use stable_flight::stabilizers::copter::{CopterStabilizer, CopterStabilizerConfig};
use stable_flight::{
Actuator, ActuatorCommand, AttitudeCommand, Command, Commander, ControlInput, FlightLoop,
StateSource, VehicleState,
};
struct Hover;
impl Commander for Hover {
fn next_command(&mut self, _dt: f32, _state: &VehicleState) -> Command {
Command::armed(ControlInput::Attitude(AttitudeCommand {
throttle: 0.5,
..Default::default()
}))
}
}
struct StillAir;
impl StateSource for StillAir {
fn state(&mut self, _dt: Duration) -> VehicleState {
VehicleState::default()
}
}
impl Actuator for StillAir {
fn actuate(&mut self, _command: &ActuatorCommand) {}
}
let mut flight_loop = FlightLoop::new(
Hover,
StillAir,
CopterGuidance::new(CopterGuidanceConfig::default()),
CopterStabilizer::new(CopterStabilizerConfig::default()),
QuadCopterMixer::default(),
(), // no telemetry
);
flight_loop.prime();
for _ in 0..3 {
flight_loop.step(Duration::from_millis(4)); // 250 Hz
}The struct itself is unbounded (bounds live on the impl), so types that hold a
FlightLoop — runners, firmware statics — needn’t repeat the six trait bounds.
Implementations§
Source§impl<C, V, G, S, M, T> FlightLoop<C, V, G, S, M, T>
impl<C, V, G, S, M, T> FlightLoop<C, V, G, S, M, T>
Sourcepub fn new(
commander: C,
vehicle: V,
guidance: G,
stabilizer: S,
mixer: M,
telemetry: T,
) -> Self
pub fn new( commander: C, vehicle: V, guidance: G, stabilizer: S, mixer: M, telemetry: T, ) -> Self
Assembles a loop from its six components. Does not touch the vehicle.
Sourcepub fn prime(&mut self)
pub fn prime(&mut self)
Takes the initial estimate (with dt = Duration::ZERO, per the StateSource
convention) and primes guidance, then the stabilizer, from it. Call once before the
first tick, so the commander’s first call sees a real state. The first armed tick
primes them again from its fresh estimate (see the arm gate on FlightLoop).
Sourcepub fn step(&mut self, dt: Duration)
pub fn step(&mut self, dt: Duration)
Runs one tick with loop period dt.
The commander sees the state from the previous tick; guidance and the stabilizer see
the fresh estimate. Telemetry receives a TelemetryRecord after actuation, with the
Saturation guidance, the stabilizer, and the mixer report, merged. A disarmed tick
actuates ActuatorCommand::default and records a neutral setpoint, zero control, and
no saturation; see FlightLoop for the arm gate. Allocation-free.
§Panics
If dt is zero on an armed tick: guidance and the stabilizer require a positive, finite step (a
Duration is never negative or non-finite).
Sourcepub fn state(&self) -> &VehicleState
pub fn state(&self) -> &VehicleState
The most recent state estimate.
Sourcepub fn stabilizer(&self) -> &S
pub fn stabilizer(&self) -> &S
The stabilizer, e.g. to read its config.
Sourcepub fn set_guidance_config(
&mut self,
config: G::Config,
) -> Result<(), InvalidConfig>where
G: Configurable,
pub fn set_guidance_config(
&mut self,
config: G::Config,
) -> Result<(), InvalidConfig>where
G: Configurable,
Retunes the guidance between ticks (Configurable::set_config): an invalid config is
rejected and the current one kept.
Sourcepub fn set_stabilizer_config(
&mut self,
config: S::Config,
) -> Result<(), InvalidConfig>where
S: Configurable,
pub fn set_stabilizer_config(
&mut self,
config: S::Config,
) -> Result<(), InvalidConfig>where
S: Configurable,
Retunes the stabilizer between ticks (Configurable::set_config): an invalid config
is rejected and the current one kept.
use stable_flight::{Configurable, FlightLoop};
flight_loop.step(Duration::from_millis(4));
let mut retuned = *flight_loop.stabilizer().config();
retuned.body_rate.gains.p = 0.2;
flight_loop.set_stabilizer_config(retuned).expect("valid config");
flight_loop.step(Duration::from_millis(4)); // flies the new gainsSourcepub fn into_parts(self) -> (C, V, G, S, M, T)
pub fn into_parts(self) -> (C, V, G, S, M, T)
Splits the loop back into (commander, vehicle, guidance, stabilizer, mixer, telemetry),
e.g. to inspect test doubles after a run or to hand the vehicle to another runner.
use stable_flight::FlightLoop;
/// Counts actuations.
#[derive(Default)]
struct Bench {
actuations: u32,
}
impl Actuator for Bench {
fn actuate(&mut self, _: &ActuatorCommand) {
self.actuations += 1;
}
}
let mut flight_loop = FlightLoop::new(
Hover,
Bench::default(),
CopterGuidance::new(CopterGuidanceConfig::default()),
CopterStabilizer::new(CopterStabilizerConfig::default()),
QuadCopterMixer::default(),
(),
);
flight_loop.prime();
flight_loop.step(Duration::from_millis(4));
let (_, bench, _, _, _, _) = flight_loop.into_parts();
assert_eq!(bench.actuations, 1);Trait Implementations§
Source§impl<C, V, G, S, M, T> Params for FlightLoop<C, V, G, S, M, T>
The guidance’s params, then the stabilizer’s (see crate::params): combined indices
run through the guidance’s first, then the stabilizer’s. Nothing checks that their names
are disjoint: on a duplicate, name lookup finds the guidance’s and the stabilizer’s is
reachable only by index, so table authors keep names unique across components (the copter
tables use GUID_/STAB_ prefixes).
impl<C, V, G, S, M, T> Params for FlightLoop<C, V, G, S, M, T>
The guidance’s params, then the stabilizer’s (see crate::params): combined indices
run through the guidance’s first, then the stabilizer’s. Nothing checks that their names
are disjoint: on a duplicate, name lookup finds the guidance’s and the stabilizer’s is
reachable only by index, so table authors keep names unique across components (the copter
tables use GUID_/STAB_ prefixes).
use stable_flight::params::Params;
use stable_flight::{Configurable, FlightLoop};
flight_loop.set_param("GUID_ALT_P", 2.5).expect("valid value");
flight_loop.set_param("STAB_RATE_P", 0.2).expect("valid value");
assert_eq!(flight_loop.guidance().config().altitude.gains.p, 2.5);
assert_eq!(flight_loop.param("STAB_RATE_P"), Some(0.2));Source§fn param_count(&self) -> usize
fn param_count(&self) -> usize
0..param_count().Source§fn param_name(&self, index: usize) -> Option<&'static str>
fn param_name(&self, index: usize) -> Option<&'static str>
index, or None if it is out of range.Source§fn param_at(&self, index: usize) -> Option<f32>
fn param_at(&self, index: usize) -> Option<f32>
index, or None if it is out of range.Source§fn set_param_at(&mut self, index: usize, value: f32) -> Result<(), ParamError>
fn set_param_at(&mut self, index: usize, value: f32) -> Result<(), ParamError>
index to value. Out of range is ParamError::Unknown. On error
the config is unchanged.Source§fn param_index(&self, name: &str) -> Option<usize>
fn param_index(&self, name: &str) -> Option<usize>
name, or None if there is no such param. On a
duplicate name, the first index.Source§fn param(&self, name: &str) -> Option<f32>
fn param(&self, name: &str) -> Option<f32>
name, or None if there is no such param.Auto Trait Implementations§
impl<C, V, G, S, M, T> Freeze for FlightLoop<C, V, G, S, M, T>
impl<C, V, G, S, M, T> RefUnwindSafe for FlightLoop<C, V, G, S, M, T>where
C: RefUnwindSafe,
V: RefUnwindSafe,
G: RefUnwindSafe,
S: RefUnwindSafe,
M: RefUnwindSafe,
T: RefUnwindSafe,
impl<C, V, G, S, M, T> Send for FlightLoop<C, V, G, S, M, T>
impl<C, V, G, S, M, T> Sync for FlightLoop<C, V, G, S, M, T>
impl<C, V, G, S, M, T> Unpin for FlightLoop<C, V, G, S, M, T>
impl<C, V, G, S, M, T> UnsafeUnpin for FlightLoop<C, V, G, S, M, T>where
C: UnsafeUnpin,
V: UnsafeUnpin,
G: UnsafeUnpin,
S: UnsafeUnpin,
M: UnsafeUnpin,
T: UnsafeUnpin,
impl<C, V, G, S, M, T> UnwindSafe for FlightLoop<C, V, G, S, M, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.