Skip to main content

FailsafeCommander

Struct FailsafeCommander 

Source
pub struct FailsafeCommander<P, F> { /* private fields */ }
Expand description

Flies a link-fed primary while it is fresh, and a fallback otherwise.

  • Starts in the fallback: until the primary is heard, nothing it says is trusted.
  • Falls back as soon as the primary has gone timeout_s without a message, engaging the fallback with the current state and arm state.
  • Returns once the primary has been fresh for recover_after_s without a break and the fallback can release. To an armed vehicle, sticks (an attitude or rate command) take over only once an axis has moved takeover_stick_delta from where it was when the primary recovered, so a joystick left at low throttle can’t drop the vehicle out of a hold; a waypoint, or anything to a disarmed vehicle, takes over at once.
  • At the handover, the primary’s arm state is synced to the vehicle’s (disarmed if either the vehicle or the primary is), so an arm request made while the fallback flew doesn’t arm the vehicle; a new one does. Its regime is synced to the one the fallback flew (a HoldCommander flies FlightRegime::Copter), so a VTOL brought back to a hover stays there until the primary requests plane again.
  • Arming: the fallback may keep the vehicle armed or disarm it, but never arms it (a HoldCommander keeps the arm state it was engaged with), and a fresh primary’s disarm is always honored, even while it waits to take over.
  • Landing: while an armed land (ControlInput::Land) flies, from either commander, a TouchdownDetector watches it. On touchdown the vehicle disarms and the primary’s arm is synced to it, so it stays disarmed until the primary arms again. A land that carries on across a handover (a HoldCommander finishes the primary’s land when the link drops) keeps its detector; anything else (a hold, a disarm, another input) starts it over, so a hold that follows a descent can’t read as touchdown.

The primary runs every tick, flying or not, so it keeps draining its link and timing its freshness. failsafe reports which commander flew.

§Examples

use stable_flight::commanders::{
    FailsafeCommander, FailsafeConfig, HoldCommander, LinkedCommander,
};
use stable_flight::{Command, Commander, FailsafeState, FlightRegime, VehicleState};

/// A link that was last heard `since_s` ago.
struct Link {
    since_s: Option<f32>,
}

impl Commander for Link {
    fn next_command(&mut self, _dt: f32, _state: &VehicleState) -> Command {
        Command::default()
    }
}

impl LinkedCommander for Link {
    fn since_update_s(&self) -> Option<f32> {
        self.since_s
    }

    fn sync_arm(&mut self, _armed: bool) {}

    fn sync_regime(&mut self, _regime: FlightRegime) {}
}

let link = Link { since_s: None };
let mut commander =
    FailsafeCommander::new(link, HoldCommander::default(), FailsafeConfig::default())?;
commander.next_command(0.004, &VehicleState::default());
assert_eq!(commander.failsafe(), Some(FailsafeState::Fallback)); // never heard

Implementations§

Source§

impl<P: LinkedCommander, F: Fallback> FailsafeCommander<P, F>

Source

pub fn new( primary: P, fallback: F, config: FailsafeConfig, ) -> Result<Self, InvalidConfig>

Flies primary, falling back to fallback per config.

§Errors

InvalidConfig if config fails FailsafeConfig::validate.

Source

pub fn primary(&self) -> &P

The primary commander.

Source

pub fn fallback(&self) -> &F

The fallback commander.

Trait Implementations§

Source§

impl<P: LinkedCommander, F: Fallback> Commander for FailsafeCommander<P, F>

Source§

fn next_command(&mut self, dt: f32, current_state: &VehicleState) -> Command

Produces the next command to be executed by the flight control system. Read more
Source§

fn failsafe(&self) -> Option<FailsafeState>

Which commander a failsafe let fly on the last next_command call, for a commander that has one (FailsafeCommander). FlightLoop puts it in the tick’s TelemetryRecord::failsafe. The default reports none, so a commander that wraps another must forward this to report the inner one’s.
Source§

impl<P: Debug, F: Debug> Debug for FailsafeCommander<P, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, F> Freeze for FailsafeCommander<P, F>
where P: Freeze, F: Freeze,

§

impl<P, F> RefUnwindSafe for FailsafeCommander<P, F>

§

impl<P, F> Send for FailsafeCommander<P, F>
where P: Send, F: Send,

§

impl<P, F> Sync for FailsafeCommander<P, F>
where P: Sync, F: Sync,

§

impl<P, F> Unpin for FailsafeCommander<P, F>
where P: Unpin, F: Unpin,

§

impl<P, F> UnsafeUnpin for FailsafeCommander<P, F>
where P: UnsafeUnpin, F: UnsafeUnpin,

§

impl<P, F> UnwindSafe for FailsafeCommander<P, F>
where P: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.