use thiserror::Error;
use crate::{Vec3, VehicleState};
use super::Path;
#[cfg(doc)]
use super::{ManeuverConfig, ManeuverRunner};
pub const MAX_SEGMENTS: usize = 16;
pub const DOUBLET_PAUSE_S: f32 = 1.0;
pub const DEFAULT_DOUBLET_HALF_S: f32 = 0.5;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
Roll,
Pitch,
Yaw,
}
impl Axis {
pub(super) fn unit(self) -> Vec3 {
match self {
Axis::Roll => Vec3::x(),
Axis::Pitch => Vec3::y(),
Axis::Yaw => Vec3::z(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RateLaw {
Constant {
radps: f32,
},
Radius {
radius_m: f32,
},
}
impl RateLaw {
pub(super) fn radps(self, state: &VehicleState) -> f32 {
match self {
RateLaw::Constant { radps } => radps,
RateLaw::Radius { radius_m } => speed_mps(state) / radius_m,
}
}
pub(super) fn direction(self) -> f32 {
match self {
RateLaw::Constant { radps } => radps.signum(),
RateLaw::Radius { radius_m } => radius_m.signum(),
}
}
fn value(self) -> f32 {
match self {
RateLaw::Constant { radps } => radps,
RateLaw::Radius { radius_m } => radius_m,
}
}
}
pub(super) fn speed_mps(state: &VehicleState) -> f32 {
state
.valid_airspeed_mps()
.unwrap_or_else(|| state.velocity_mps.norm())
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SegmentEnd {
Angle {
rad: f32,
},
Duration {
s: f32,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Thrust {
Maneuver,
Cruise,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum Segment {
Rotate {
axis: Axis,
rate: RateLaw,
end: SegmentEnd,
thrust: Thrust,
path: Path,
},
Pause {
duration_s: f32,
thrust: Thrust,
},
}
impl Segment {
fn check(&self) -> Result<(), &'static str> {
match *self {
Segment::Rotate {
axis,
rate,
end,
path,
..
} => {
let value = rate.value();
if !(value.is_finite() && value != 0.0) {
return Err(match rate {
RateLaw::Constant { .. } => "rate not finite and nonzero",
RateLaw::Radius { .. } => "radius not finite and nonzero",
});
}
if path == Path::Circle {
if !matches!(rate, RateLaw::Radius { .. }) {
return Err("circle without a radius");
}
if axis == Axis::Roll {
return Err("circle about the roll axis");
}
}
match end {
SegmentEnd::Angle { rad } if !positive(rad) => {
Err("angle not positive and finite")
}
SegmentEnd::Duration { s } if !positive(s) => {
Err("duration not positive and finite")
}
_ => Ok(()),
}
}
Segment::Pause { duration_s, .. } if !positive(duration_s) => {
Err("duration not positive and finite")
}
Segment::Pause { .. } => Ok(()),
}
}
}
pub(super) fn positive(value: f32) -> bool {
value > 0.0 && value.is_finite()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum ManeuverError {
#[error("a maneuver needs a segment")]
Empty,
#[error("a maneuver holds at most {max} segments, got {count}", max = MAX_SEGMENTS)]
TooManySegments {
count: usize,
},
#[error("segment {index}: {reason}")]
InvalidSegment {
index: usize,
reason: &'static str,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Maneuver {
segments: [Segment; MAX_SEGMENTS],
len: usize,
}
impl Maneuver {
pub fn new(segments: &[Segment]) -> Result<Self, ManeuverError> {
let count = segments.len();
if count == 0 {
return Err(ManeuverError::Empty);
}
if count > MAX_SEGMENTS {
return Err(ManeuverError::TooManySegments { count });
}
for (index, segment) in segments.iter().enumerate() {
segment
.check()
.map_err(|reason| ManeuverError::InvalidSegment { index, reason })?;
}
let mut maneuver = Maneuver {
segments: [UNUSED; MAX_SEGMENTS],
len: count,
};
maneuver.segments[..count].copy_from_slice(segments);
Ok(maneuver)
}
pub fn roll(rate_radps: f32) -> Result<Self, ManeuverError> {
Maneuver::new(&[Segment::Rotate {
axis: Axis::Roll,
rate: RateLaw::Constant { radps: rate_radps },
end: SegmentEnd::Angle { rad: FULL_TURN_RAD },
thrust: Thrust::Maneuver,
path: Path::Line,
}])
}
pub fn loop_(radius_m: f32) -> Result<Self, ManeuverError> {
Maneuver::new(&[Segment::Rotate {
axis: Axis::Pitch,
rate: RateLaw::Radius { radius_m },
end: SegmentEnd::Angle { rad: FULL_TURN_RAD },
thrust: Thrust::Maneuver,
path: Path::Circle,
}])
}
pub fn doublet(axis: Axis, rate_radps: f32, half_s: f32) -> Result<Self, ManeuverError> {
let half = |radps| Segment::Rotate {
axis,
rate: RateLaw::Constant { radps },
end: SegmentEnd::Duration { s: half_s },
thrust: Thrust::Cruise,
path: Path::Free,
};
Maneuver::new(&[
half(rate_radps),
half(-rate_radps),
Segment::Pause {
duration_s: DOUBLET_PAUSE_S,
thrust: Thrust::Cruise,
},
])
}
pub fn pause(duration_s: f32) -> Result<Self, ManeuverError> {
Maneuver::new(&[Segment::Pause {
duration_s,
thrust: Thrust::Cruise,
}])
}
pub fn segments(&self) -> &[Segment] {
&self.segments[..self.len]
}
}
pub(super) const UNUSED: Segment = Segment::Pause {
duration_s: 0.0,
thrust: Thrust::Cruise,
};
const FULL_TURN_RAD: f32 = core::f32::consts::TAU;