use lin_alg::f32::{Quaternion, Vec3};
use crate::{ppks::PositFused, Ahrs, DeviceOrientation, ImuReadings};
#[derive(Default, Clone)]
pub struct Params {
pub posit_fused: PositFused,
pub alt_msl_baro: f32,
pub alt_tof: Option<f32>,
pub attitude: Quaternion,
pub accel_linear: Vec3,
pub v_x: f32,
pub v_y: f32,
pub v_z: f32,
pub v_z_baro: f32,
pub v_pitch: f32,
pub v_roll: f32,
pub v_yaw: f32,
pub a_x: f32,
pub a_y: f32,
pub a_z: f32,
pub a_pitch: f32,
pub a_roll: f32,
pub a_yaw: f32,
}
impl Params {
pub fn update_from_imu_readings(
&mut self,
imu_readings: &ImuReadings,
mag_readings: Option<Vec3>,
ahrs: &mut Ahrs,
) {
let (accel_data, gyro_data, mag_data) = match ahrs.config.orientation {
DeviceOrientation::YFwdXRight => {
let accel_data = Vec3 {
x: imu_readings.a_x,
y: imu_readings.a_y,
z: imu_readings.a_z,
};
let gyro_data = Vec3 {
x: imu_readings.v_pitch,
y: imu_readings.v_roll,
z: imu_readings.v_yaw,
};
let mag_data = mag_readings.map(|m| Vec3 {
x: -m.x, y: -m.y,
z: m.z,
});
(accel_data, gyro_data, mag_data)
}
DeviceOrientation::YLeftXFwd => {
let accel_data = Vec3 {
x: -imu_readings.a_y,
y: imu_readings.a_x,
z: imu_readings.a_z,
};
let gyro_data = Vec3 {
x: -imu_readings.v_roll,
y: imu_readings.v_pitch,
z: imu_readings.v_yaw,
};
let mag_data = mag_readings.map(|m| Vec3 {
x: m.y,
y: -m.x,
z: m.z,
});
(accel_data, gyro_data, mag_data)
}
};
ahrs.update(gyro_data, accel_data, mag_data);
self.attitude = ahrs.attitude;
self.accel_linear = ahrs.lin_acc_fused;
let acc_calibrated = ahrs.acc_calibrated;
let gyro_calibrated = ahrs.gyro_calibrated;
self.a_x = acc_calibrated.x;
self.a_y = acc_calibrated.y;
self.a_z = acc_calibrated.z;
self.a_pitch = (gyro_calibrated.x - self.v_pitch) / ahrs.dt;
self.a_roll = (gyro_calibrated.y - self.v_roll) / ahrs.dt;
self.a_yaw = (gyro_calibrated.z - self.v_yaw) / ahrs.dt;
self.v_pitch = gyro_calibrated.x;
self.v_roll = gyro_calibrated.y;
self.v_yaw = gyro_calibrated.z;
}
}