use super::CopterGuidanceConfig;
use super::wind_heading::WindHeading;
use crate::estimation::wind::WindEstimate;
use crate::math::{all_finite, clamp_symmetric};
use crate::{Vec2, VehicleState, WaypointCommand, wrap_angle};
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub(crate) struct LegHeading {
leg: Option<Leg>,
stuck_s: f32,
yielding: bool,
wind: WindHeading,
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct Leg {
to_m: Vec2,
bearing_rad: Option<f32>,
}
impl LegHeading {
pub(crate) fn target_rad(
&mut self,
dt: f32,
config: &CopterGuidanceConfig,
target_rad: f32,
waypoint: &WaypointCommand,
wind: Option<&WindEstimate>,
estimated_state: &VehicleState,
) -> f32 {
if let Some(heading_rad) = waypoint.heading_rad.filter(|h| h.is_finite()) {
self.reset();
return commanded_turn(dt, config, target_rad, heading_rad);
}
let bearing_rad = self.bearing_rad(config, waypoint.position_m, estimated_state);
let to_m = waypoint.position_m - estimated_state.position_m.xy();
let on_station = bearing_rad.is_none() || to_m.norm() <= config.waypoint_yaw_min_leg_m;
if on_station && let Some(heading_rad) = self.wind_rad(config, wind, estimated_state) {
return commanded_turn(dt, config, target_rad, heading_rad);
}
if self.yielded(dt, config, target_rad, estimated_state) {
return estimated_state.heading_rad;
}
let rate_radps = config.waypoint_yaw_radps;
let Some(bearing_rad) = bearing_rad.filter(|_| rate_radps > 0.0) else {
return target_rad;
};
turned_toward(target_rad, bearing_rad, rate_radps * dt)
}
pub(crate) fn land_target_rad(
&mut self,
dt: f32,
config: &CopterGuidanceConfig,
target_rad: f32,
wind: Option<&WindEstimate>,
estimated_state: &VehicleState,
) -> f32 {
self.clear();
self.wind_rad(config, wind, estimated_state)
.map_or(target_rad, |heading_rad| {
commanded_turn(dt, config, target_rad, heading_rad)
})
}
pub(crate) fn clear(&mut self) {
*self = Self {
wind: self.wind,
..Self::default()
};
}
pub(crate) fn reset(&mut self) {
*self = Self::default();
}
fn wind_rad(
&mut self,
config: &CopterGuidanceConfig,
wind: Option<&WindEstimate>,
estimated_state: &VehicleState,
) -> Option<f32> {
self.wind
.target_rad(config, wind, estimated_state.heading_rad)
}
fn bearing_rad(
&mut self,
config: &CopterGuidanceConfig,
waypoint_m: Vec2,
estimated_state: &VehicleState,
) -> Option<f32> {
if let Some(leg) = self.leg
&& leg.to_m == waypoint_m
{
return leg.bearing_rad;
}
self.clear();
let here_m = estimated_state.position_m.xy();
if !all_finite(&here_m) {
return None;
}
let leg_m = waypoint_m - here_m;
let bearing_rad =
(leg_m.norm() > config.waypoint_yaw_min_leg_m).then(|| libm::atan2f(leg_m.y, leg_m.x));
self.leg = Some(Leg {
to_m: waypoint_m,
bearing_rad,
});
bearing_rad
}
fn yielded(
&mut self,
dt: f32,
config: &CopterGuidanceConfig,
target_rad: f32,
estimated_state: &VehicleState,
) -> bool {
let heading_rad = estimated_state.heading_rad;
if !(config.heading_yield_s > 0.0 && heading_rad.is_finite()) {
return false;
}
if self.yielding {
return true;
}
let error_rad = wrap_angle(target_rad - heading_rad);
let toward_radps = error_rad.signum() * estimated_state.angular_velocity_radps.z;
let stuck = error_rad.abs() > config.heading_yield_rad
&& toward_radps < config.heading_yield_rate_radps;
self.stuck_s = if stuck { self.stuck_s + dt } else { 0.0 };
self.yielding = self.stuck_s > config.heading_yield_s;
self.yielding
}
}
fn commanded_turn(dt: f32, config: &CopterGuidanceConfig, from_rad: f32, to_rad: f32) -> f32 {
turned_toward(from_rad, to_rad, config.heading_rate_radps * dt)
}
fn turned_toward(from_rad: f32, to_rad: f32, step_rad: f32) -> f32 {
from_rad + clamp_symmetric(wrap_angle(to_rad - from_rad), step_rad)
}