use core::f32::consts::FRAC_PI_2;
use super::{FallbackHold, ThrustHandoff, Transition, TransitionConfig, TransitionStep};
use crate::controllers::InvalidConfig;
use crate::guidance::copter::{CopterGuidance, CopterGuidanceConfig};
use crate::guidance::plane::{PlaneGuidance, PlaneGuidanceConfig};
use crate::math::{bank_rad, guard_sink};
use crate::params::{ParamError, Params, locate};
use crate::{
AbsoluteAttitudeCommand, AttitudeSetpoint, Configurable, ControlInput, FlightMode,
FlightModeReport, FlightRegime, Guidance, ModeRefusal, PlaneSetpoint, Saturation, Setpoint,
VehicleState, View, WindEstimate,
};
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TailsitterGuidanceConfig {
pub hover: CopterGuidanceConfig,
pub plane: PlaneGuidanceConfig,
pub transition: TransitionConfig,
}
impl TailsitterGuidanceConfig {
pub fn validate(&self) -> Result<(), InvalidConfig> {
self.hover.validate().map_err(|e| e.in_section("hover"))?;
self.plane.validate().map_err(|e| e.in_section("plane"))?;
self.transition
.validate()
.map_err(|e| e.in_section("transition"))
}
}
pub struct TailsitterGuidance {
config: TailsitterGuidanceConfig,
hover: View<CopterGuidance>,
plane: PlaneGuidance,
transition: Transition,
handoff: ThrustHandoff,
fallback_hold: FallbackHold,
mode: FlightMode,
last_thrust: f32,
forward_thrust: f32,
throttle_floor: Option<f32>,
sink_guarded: bool,
}
impl TailsitterGuidance {
pub fn new(config: TailsitterGuidanceConfig) -> Self {
if let Err(error) = config.validate() {
panic!("invalid TailsitterGuidanceConfig: {error}");
}
TailsitterGuidance {
config,
hover: View::tailsitter(CopterGuidance::new(config.hover)),
plane: PlaneGuidance::new(config.plane),
transition: Transition::new(config.transition),
handoff: ThrustHandoff::new(config.transition.handoff_s),
fallback_hold: FallbackHold::default(),
mode: FlightMode::Copter,
last_thrust: 0.0,
forward_thrust: 0.0,
throttle_floor: None,
sink_guarded: false,
}
}
fn step(&mut self, dt: f32, requested: FlightRegime, state: &VehicleState) -> TransitionStep {
let attitude = match self.mode {
FlightMode::ToPlane => Some(state.attitude * self.hover.rotation()),
FlightMode::ToCopter => Some(state.attitude),
_ => None,
};
let rolled_over = attitude.is_some_and(|attitude| {
bank_rad(&attitude).abs() > self.config.transition.roll_limit_rad
});
if rolled_over {
self.transition.cut_short(ModeRefusal::RollError)
} else {
self.transition.update(dt, requested, state)
}
}
fn hand_off(&mut self, to: FlightMode, state: &VehicleState) {
match to {
FlightMode::Plane => {
self.plane.prime(state);
self.throttle_floor = Some(self.config.plane.cruise_thrust);
}
_ => self.hover.prime(state),
}
self.handoff = ThrustHandoff::new(self.config.transition.handoff_s);
self.handoff.start(self.last_thrust);
}
fn floor_stick_throttle(&mut self, setpoint: Setpoint, input: &ControlInput) -> Setpoint {
let Some(floor) = self.throttle_floor else {
return setpoint;
};
if !input.is_sticks() || setpoint.thrust() >= floor {
self.throttle_floor = None;
return setpoint;
}
setpoint.with_thrust(floor)
}
fn back_thrust(&mut self, state: &VehicleState) -> f32 {
let transition = &self.config.transition;
let guarded = guard_sink(
transition.back_thrust,
state.velocity_mps.z,
transition.back_sink_limit_mps,
transition.back_sink_gain,
);
self.sink_guarded = guarded.is_some();
guarded.unwrap_or(transition.back_thrust)
}
fn sync_config(&mut self) {
self.config = TailsitterGuidanceConfig {
hover: *self.hover.config(),
plane: *self.plane.config(),
transition: *self.transition.config(),
};
}
}
fn transition_ramp(setpoint: Setpoint) -> Setpoint {
match setpoint {
Setpoint::Attitude(attitude) => Setpoint::Attitude(AttitudeSetpoint {
transition: true,
..attitude
}),
Setpoint::Plane(plane) => Setpoint::Plane(PlaneSetpoint {
transition: true,
..plane
}),
other => other,
}
}
impl Guidance for TailsitterGuidance {
fn guide(
&mut self,
dt: f32,
regime: FlightRegime,
input: &ControlInput,
state: &VehicleState,
) -> Setpoint {
let requested = if matches!(input, ControlInput::Land(_)) {
FlightRegime::Copter
} else {
regime
};
let step = self.step(dt, requested, state);
if step.handoff {
self.hand_off(step.mode, state);
}
self.hover
.inner_mut()
.set_wind_paused(step.mode != FlightMode::Copter);
if step.mode == FlightMode::ToPlane && self.mode != FlightMode::ToPlane {
self.forward_thrust = self.last_thrust.max(self.config.transition.forward_thrust);
}
self.sink_guarded = false;
let tilt_rad = step.tilt_target_rad.unwrap_or(0.0);
let setpoint = match step.mode {
FlightMode::ToPlane => {
let ramp = ControlInput::AbsoluteAttitude(AbsoluteAttitudeCommand {
roll_rad: 0.0,
pitch_rad: -tilt_rad,
heading_rad: f32::NAN,
thrust: self.forward_thrust,
});
transition_ramp(self.hover.guide(dt, FlightRegime::Copter, &ramp, state))
}
FlightMode::Plane => {
let setpoint = self.plane.guide(dt, FlightRegime::Plane, input, state);
self.floor_stick_throttle(setpoint, input)
}
FlightMode::ToCopter => {
let ramp = ControlInput::AbsoluteAttitude(AbsoluteAttitudeCommand {
roll_rad: 0.0,
pitch_rad: FRAC_PI_2 - tilt_rad,
heading_rad: f32::NAN,
thrust: self.back_thrust(state),
});
transition_ramp(self.plane.guide(dt, FlightRegime::Plane, &ramp, state))
}
_ => {
let fallen_back = self.transition.fallen_back();
let flown = self.fallback_hold.input(fallen_back, input, state);
self.hover.guide(dt, FlightRegime::Copter, &flown, state)
}
};
let setpoint = if self.handoff.active() {
setpoint.with_thrust(self.handoff.apply(dt, setpoint.thrust()))
} else {
setpoint
};
if step.mode != FlightMode::Plane {
self.throttle_floor = None;
}
self.mode = step.mode;
self.last_thrust = setpoint.thrust();
setpoint
}
fn prime(&mut self, state: &VehicleState) {
self.hover.prime(state);
self.plane.prime(state);
self.transition.prime(state);
self.handoff = ThrustHandoff::new(self.config.transition.handoff_s);
self.fallback_hold.clear();
self.mode = FlightMode::Copter;
self.last_thrust = 0.0;
self.throttle_floor = None;
self.sink_guarded = false;
}
fn saturation(&self) -> Saturation {
match self.mode {
FlightMode::Plane => self.plane.saturation(),
FlightMode::ToCopter => {
let saturation = self.plane.saturation();
Saturation {
thrust: saturation.thrust || self.sink_guarded,
..saturation
}
}
_ => self.hover.saturation(),
}
}
fn flight_mode(&self) -> Option<FlightModeReport> {
Some(self.transition.report())
}
fn wind(&self) -> Option<WindEstimate> {
self.hover.wind()
}
}
impl Configurable for TailsitterGuidance {
type Config = TailsitterGuidanceConfig;
fn config(&self) -> &TailsitterGuidanceConfig {
&self.config
}
fn set_config(&mut self, config: TailsitterGuidanceConfig) -> Result<(), InvalidConfig> {
config.validate()?;
self.hover.set_config(config.hover)?;
self.plane.set_config(config.plane)?;
self.transition.set_config(config.transition)?;
self.config = config;
Ok(())
}
}
impl Params for TailsitterGuidance {
fn param_count(&self) -> usize {
self.hover.param_count() + self.plane.param_count() + self.transition.param_count()
}
fn param_name(&self, index: usize) -> Option<&'static str> {
match locate(&[&self.hover, &self.plane, &self.transition], index)? {
(0, i) => self.hover.param_name(i),
(1, i) => self.plane.param_name(i),
(_, i) => self.transition.param_name(i),
}
}
fn param_at(&self, index: usize) -> Option<f32> {
match locate(&[&self.hover, &self.plane, &self.transition], index)? {
(0, i) => self.hover.param_at(i),
(1, i) => self.plane.param_at(i),
(_, i) => self.transition.param_at(i),
}
}
fn set_param_at(&mut self, index: usize, value: f32) -> Result<(), ParamError> {
let parts: [&dyn Params; 3] = [&self.hover, &self.plane, &self.transition];
let result = match locate(&parts, index).ok_or(ParamError::Unknown)? {
(0, i) => self.hover.set_param_at(i, value),
(1, i) => self.plane.set_param_at(i, value),
(_, i) => self.transition.set_param_at(i, value),
};
self.sync_config();
result
}
}