use core::time::Duration;
use crate::controllers::{BodyRateController, BodyRateControllerConfig};
use crate::{
Actuator, ActuatorCommand, Command, Commander, ControlOutput, FailsafeState, FlightModeReport,
Saturation, Setpoint, StateSource, TelemetryPublisher, TelemetryRecord, Vec3, VehicleState,
WindEstimate,
};
pub mod conformance;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Default)]
pub struct NullVehicle {
state: VehicleState,
state_reads: u64,
actuations: u64,
last_actuation: Option<ActuatorCommand>,
}
impl NullVehicle {
pub fn new() -> Self {
Self::default()
}
pub fn with_state(state: VehicleState) -> Self {
NullVehicle {
state,
..Self::default()
}
}
pub fn state_reads(&self) -> u64 {
self.state_reads
}
pub fn actuations(&self) -> u64 {
self.actuations
}
pub fn last_actuation(&self) -> Option<&ActuatorCommand> {
self.last_actuation.as_ref()
}
}
impl StateSource for NullVehicle {
fn state(&mut self, _dt: Duration) -> VehicleState {
self.state_reads += 1;
self.state.clone()
}
}
impl Actuator for NullVehicle {
fn actuate(&mut self, actuator_command: &ActuatorCommand) {
self.actuations += 1;
self.last_actuation = Some(*actuator_command);
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct ConstantCommander {
command: Command,
}
impl ConstantCommander {
pub fn new(command: Command) -> Self {
ConstantCommander { command }
}
}
impl Commander for ConstantCommander {
fn next_command(&mut self, _dt: f32, _current_state: &VehicleState) -> Command {
self.command
}
}
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct RecordedTick {
pub dt: Duration,
pub command: Command,
pub setpoint: Setpoint,
pub state: VehicleState,
pub control: ControlOutput,
pub actuators: ActuatorCommand,
pub saturation: Saturation,
pub failsafe: Option<FailsafeState>,
pub flight_mode: FlightModeReport,
pub wind: Option<WindEstimate>,
}
impl RecordedTick {
pub fn as_record(&self) -> TelemetryRecord<'_> {
TelemetryRecord {
dt: self.dt,
command: &self.command,
setpoint: &self.setpoint,
state: &self.state,
control: &self.control,
actuators: &self.actuators,
saturation: self.saturation,
failsafe: self.failsafe,
flight_mode: self.flight_mode,
wind: self.wind,
}
}
}
impl From<&TelemetryRecord<'_>> for RecordedTick {
fn from(record: &TelemetryRecord<'_>) -> Self {
RecordedTick {
dt: record.dt,
command: *record.command,
setpoint: *record.setpoint,
state: record.state.clone(),
control: *record.control,
actuators: *record.actuators,
saturation: record.saturation,
failsafe: record.failsafe,
flight_mode: record.flight_mode,
wind: record.wind,
}
}
}
#[derive(Debug, Clone)]
pub struct RecordingTelemetry<const N: usize> {
ticks: [Option<RecordedTick>; N],
total: u64,
}
impl<const N: usize> RecordingTelemetry<N> {
pub fn new() -> Self {
RecordingTelemetry {
ticks: core::array::from_fn(|_| None),
total: 0,
}
}
pub fn total(&self) -> u64 {
self.total
}
pub fn last(&self) -> Option<&RecordedTick> {
let newest = self.total.checked_sub(1)?;
self.slot(newest)
}
pub fn iter(&self) -> impl Iterator<Item = &RecordedTick> {
let kept = self.total.min(N as u64);
(self.total - kept..self.total).filter_map(|index| self.slot(index))
}
fn slot(&self, index: u64) -> Option<&RecordedTick> {
if N == 0 {
return None;
}
self.ticks[(index % N as u64) as usize].as_ref()
}
}
impl<const N: usize> Default for RecordingTelemetry<N> {
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> TelemetryPublisher for RecordingTelemetry<N> {
fn publish(&mut self, record: &TelemetryRecord) {
if N > 0 {
self.ticks[(self.total % N as u64) as usize] = Some(RecordedTick::from(record));
}
self.total += 1;
}
}
const YAW_MODEL_GAIN: f32 = 20.0;
const YAW_MODEL_DT: f32 = 0.004;
const YAW_MODEL_DURATION_S: f32 = 60.0;
pub fn settled_yaw_rate(config: &BodyRateControllerConfig, disturbance: f32) -> f32 {
let mut controller = BodyRateController::default();
let mut rate_radps = 0.0;
for _ in 0..(YAW_MODEL_DURATION_S / YAW_MODEL_DT) as usize {
let moment = controller.control(
config,
YAW_MODEL_DT,
Vec3::zeros(),
Vec3::new(0.0, 0.0, rate_radps),
);
rate_radps += YAW_MODEL_GAIN * (moment.z - disturbance) * YAW_MODEL_DT;
}
rate_radps
}