use nalgebra::UnitQuaternion;
use crate::controllers::InvalidConfig;
use crate::tuning::{ProbeConfig, ProbeStatus, Tunable};
use crate::{
Configurable, ControlInput, ControlOutput, CruiseOutput, FlightModeReport, FlightRegime,
Guidance, Saturation, Setpoint, Stabilizer, VehicleState, WindEstimate,
};
#[cfg(test)]
mod tests;
const AXIS_SHARE: f32 = 1e-3;
pub fn tailsitter_rotation() -> UnitQuaternion<f32> {
UnitQuaternion::from_euler_angles(0.0, -core::f32::consts::FRAC_PI_2, 0.0)
}
#[derive(Debug, Clone, PartialEq)]
pub struct View<T> {
inner: T,
rotation: UnitQuaternion<f32>,
}
impl<T> View<T> {
pub fn new(inner: T, rotation: UnitQuaternion<f32>) -> Self {
View { inner, rotation }
}
pub fn tailsitter(inner: T) -> Self {
Self::new(inner, tailsitter_rotation())
}
pub fn rotation(&self) -> &UnitQuaternion<f32> {
&self.rotation
}
pub fn inner(&self) -> &T {
&self.inner
}
pub(crate) fn inner_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn into_inner(self) -> T {
self.inner
}
fn view_state(&self, state: &VehicleState) -> VehicleState {
state.rotate_body(&self.rotation)
}
}
impl<G: Guidance> Guidance for View<G> {
fn guide(
&mut self,
dt: f32,
regime: FlightRegime,
input: &ControlInput,
state: &VehicleState,
) -> Setpoint {
let state = self.view_state(state);
self.inner.guide(dt, regime, input, &state)
}
fn prime(&mut self, state: &VehicleState) {
let state = self.view_state(state);
self.inner.prime(&state);
}
fn saturation(&self) -> Saturation {
self.inner.saturation()
}
fn flight_mode(&self) -> Option<FlightModeReport> {
self.inner.flight_mode()
}
fn wind(&self) -> Option<WindEstimate> {
self.inner.wind()
}
}
impl<S: Stabilizer> Stabilizer for View<S> {
fn stabilize(&mut self, dt: f32, setpoint: &Setpoint, state: &VehicleState) -> ControlOutput {
let state = self.view_state(state);
let output = self.inner.stabilize(dt, setpoint, &state);
ControlOutput {
moments: self.rotation * output.moments,
cruise: output.cruise.map(|cruise| CruiseOutput {
moments: self.rotation * cruise.moments,
..cruise
}),
..output
}
}
fn prime(&mut self, state: &VehicleState) {
let state = self.view_state(state);
self.inner.prime(&state);
}
fn saturation(&self) -> Saturation {
let inner = self.inner.saturation();
let to_body = self.rotation.to_rotation_matrix();
let moments = core::array::from_fn(|body| {
(0..3).any(|view| inner.moments[view] && to_body[(body, view)].abs() > AXIS_SHARE)
});
Saturation { moments, ..inner }
}
}
impl<T: Configurable> Configurable for View<T> {
type Config = T::Config;
fn config(&self) -> &T::Config {
self.inner.config()
}
fn set_config(&mut self, config: T::Config) -> Result<(), InvalidConfig> {
self.inner.set_config(config)
}
}
impl<S: Tunable> Tunable for View<S> {
type Target = S::Target;
fn start_probe(&mut self, target: S::Target, config: ProbeConfig) {
self.inner.start_probe(target, config);
}
fn probe(&self) -> Option<(S::Target, ProbeStatus)> {
self.inner.probe()
}
fn stop_probe(&mut self) -> Option<(S::Target, ProbeStatus)> {
self.inner.stop_probe()
}
fn probe_frame(&self) -> UnitQuaternion<f32> {
self.rotation * self.inner.probe_frame()
}
}