#![no_std]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(clippy::all)]
extern crate alloc;
use alloc::alloc::{
alloc as rust_alloc, alloc_zeroed as rust_alloc_zeroed, dealloc as rust_dealloc,
};
use core::alloc::Layout;
#[doc(hidden)]
mod generated {
#![allow(rustdoc::broken_intra_doc_links)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[doc(inline)]
pub use generated::*;
#[cfg(all(target_os = "none", feature = "c-stubs"))]
mod stubs;
#[no_mangle]
pub unsafe extern "C" fn ekf2_rust_alloc(size: usize, align: usize) -> *mut u8 {
match Layout::from_size_align(size, align) {
Ok(layout) => unsafe { rust_alloc(layout) },
Err(_) => core::ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn ekf2_rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
match Layout::from_size_align(size, align) {
Ok(layout) => unsafe { rust_alloc_zeroed(layout) },
Err(_) => core::ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn ekf2_rust_dealloc(ptr: *mut u8, size: usize, align: usize) {
if ptr.is_null() {
return;
}
if let Ok(layout) = Layout::from_size_align(size, align) {
unsafe { rust_dealloc(ptr, layout) };
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HeightSensor {
Baro = 0,
Gnss = 1,
Range = 2,
Ev = 3,
Unknown = 4,
}
impl HeightSensor {
#[inline]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Baro,
1 => Self::Gnss,
2 => Self::Range,
3 => Self::Ev,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GlobalOrigin {
pub time_us: u64,
pub lat: f64,
pub lon: f64,
pub alt: f32,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionFrame {
LocalFrameNed = 0,
LocalFrameFrd = 1,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VelocityFrame {
LocalFrameNed = 0,
LocalFrameFrd = 1,
BodyFrameFrd = 2,
}
impl EkfImuSample {
#[inline]
pub const fn new(
time_us: u64,
delta_ang: [f32; 3],
delta_vel: [f32; 3],
delta_ang_dt: f32,
delta_vel_dt: f32,
) -> Self {
Self {
time_us,
delta_ang,
delta_vel,
delta_ang_dt,
delta_vel_dt,
delta_vel_clipping: [0; 3],
_pad: [0; 5],
}
}
#[inline]
pub const fn with_clipping(
time_us: u64,
delta_ang: [f32; 3],
delta_vel: [f32; 3],
delta_ang_dt: f32,
delta_vel_dt: f32,
delta_vel_clipping: [bool; 3],
) -> Self {
Self {
time_us,
delta_ang,
delta_vel,
delta_ang_dt,
delta_vel_dt,
delta_vel_clipping: [
delta_vel_clipping[0] as u8,
delta_vel_clipping[1] as u8,
delta_vel_clipping[2] as u8,
],
_pad: [0; 5],
}
}
}
#[cfg(feature = "gnss")]
impl EkfGnssSample {
#[inline]
#[allow(clippy::too_many_arguments)]
pub fn new(
time_us: u64,
lat: f64,
lon: f64,
alt: f32,
vel: [f32; 3],
hacc: f32,
vacc: f32,
sacc: f32,
pdop: f32,
fix_type: u8,
nsats: u8,
) -> Self {
Self {
time_us,
lat,
lon,
alt,
yaw: f32::NAN,
yaw_acc: f32::NAN,
yaw_offset: 0.0,
vel,
hacc,
vacc,
sacc,
pdop,
fix_type,
nsats,
spoofed: 0,
jammed: 0,
_pad: [0; 8],
}
}
}
#[cfg(feature = "magnetometer")]
impl EkfMagSample {
#[inline]
pub const fn new(time_us: u64, mag: [f32; 3]) -> Self {
Self {
time_us,
mag,
reset: 0,
_pad: [0; 3],
}
}
#[inline]
pub const fn with_reset(mut self) -> Self {
self.reset = 1;
self
}
}
#[cfg(feature = "barometer")]
impl EkfBaroSample {
#[inline]
pub const fn new(time_us: u64, hgt: f32) -> Self {
Self {
time_us,
hgt,
reset: 0,
_pad: [0; 3],
}
}
#[inline]
pub const fn with_reset(mut self) -> Self {
self.reset = 1;
self
}
}
#[cfg(feature = "airspeed")]
impl EkfAirspeedSample {
#[inline]
pub const fn new(time_us: u64, true_airspeed: f32, eas2tas: f32) -> Self {
Self {
time_us,
true_airspeed,
eas2tas,
}
}
}
#[cfg(feature = "range-finder")]
impl EkfRangeSample {
#[inline]
pub const fn new(time_us: u64, rng: f32, quality: i8) -> Self {
Self {
time_us,
rng,
quality,
_pad: [0; 3],
}
}
}
#[cfg(feature = "optical-flow")]
impl EkfFlowSample {
#[inline]
pub const fn new(
time_us: u64,
flow_rate_xy: [f32; 2],
gyro_rate_xyz: [f32; 3],
quality: u8,
) -> Self {
Self {
time_us,
flow_rate_xy,
gyro_rate_xyz,
quality,
_pad: [0; 3],
}
}
}
#[cfg(feature = "external-vision")]
impl EkfExtVisionSample {
#[inline]
#[allow(clippy::too_many_arguments)]
pub const fn new(
time_us: u64,
pos: [f32; 3],
quat: [f32; 4],
vel: [f32; 3],
pos_var: [f32; 3],
ang_var: [f32; 3],
vel_var: [f32; 3],
) -> Self {
Self {
time_us,
pos,
quat,
vel,
pos_var,
ang_var,
vel_var,
pos_frame: PositionFrame::LocalFrameFrd as u8,
vel_frame: VelocityFrame::BodyFrameFrd as u8,
reset_counter: 0,
quality: 0,
}
}
#[inline]
pub const fn with_frames(mut self, pos_frame: PositionFrame, vel_frame: VelocityFrame) -> Self {
self.pos_frame = pos_frame as u8;
self.vel_frame = vel_frame as u8;
self
}
#[inline]
pub const fn with_quality(mut self, quality: i8) -> Self {
self.quality = quality;
self
}
#[inline]
pub const fn with_reset_counter(mut self, reset_counter: u8) -> Self {
self.reset_counter = reset_counter;
self
}
}
#[cfg(feature = "aux-vel")]
impl EkfAuxVelSample {
#[inline]
pub const fn new(time_us: u64, vel: [f32; 2], vel_var: [f32; 2]) -> Self {
Self {
time_us,
vel,
vel_var,
}
}
}
impl EkfSystemFlagUpdate {
#[inline]
pub const fn new(time_us: u64) -> Self {
Self {
time_us,
at_rest: false,
in_air: true,
is_fixed_wing: false,
gnd_effect: false,
constant_pos: false,
in_transition_to_fw: false,
_pad: 0,
}
}
#[inline]
#[allow(clippy::too_many_arguments)]
pub const fn with_flags(
mut self,
at_rest: bool,
in_air: bool,
is_fixed_wing: bool,
gnd_effect: bool,
constant_pos: bool,
in_transition_to_fw: bool,
) -> Self {
self.at_rest = at_rest;
self.in_air = in_air;
self.is_fixed_wing = is_fixed_wing;
self.gnd_effect = gnd_effect;
self.constant_pos = constant_pos;
self.in_transition_to_fw = in_transition_to_fw;
self
}
}