Skip to content

Commit fc4556b

Browse files
committed
Refactor strapdown equations #57
1 parent 120f7b1 commit fc4556b

3 files changed

Lines changed: 187 additions & 226 deletions

File tree

core/src/filter.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! measurements in the local level frame (i.e. a GPS fix).
1717
use crate::earth::METERS_TO_DEGREES;
1818
use crate::linalg::matrix_square_root;
19-
use crate::{IMUData, StrapdownState, wrap_to_2pi, wrap_to_360};
19+
use crate::{IMUData, StrapdownState, forward, wrap_to_2pi, wrap_to_360};
2020
use nalgebra::{DMatrix, DVector, Rotation3, SVector};
2121
use rand;
2222
use rand_distr::{Distribution, Normal};
@@ -146,11 +146,11 @@ impl SigmaPoint {
146146
/// let imu_data = IMUData::new_from_vec(vec![0.0, 0.0, 0.0], vec![0.0, 0.0, 0.0]);
147147
/// let dt = 0.1;
148148
///
149-
/// sigma_point.forward(&imu_data, dt, None);
149+
/// sigma_point.forward(imu_data, dt, None);
150150
/// ```
151-
pub fn forward(&mut self, imu_data: &IMUData, dt: f64, noise: Option<Vec<f64>>) {
151+
pub fn forward(&mut self, imu_data: IMUData, dt: f64, noise: Option<Vec<f64>>) {
152152
// Propagate the strapdown state using the strapdown equations
153-
self.nav_state.forward(imu_data, dt);
153+
self.nav_state = forward(self.nav_state, imu_data, dt);
154154
let noise_vect = match noise {
155155
None => return, // UKF mode, does not use noise in propagation
156156
Some(v) => v, // Particle filter mode, uses noise in propagation
@@ -465,7 +465,7 @@ impl UKF {
465465
let mut sigma_points = self.get_sigma_points();
466466
for sigma_point in &mut sigma_points {
467467
println!("{}", sigma_point.get_string());
468-
sigma_point.forward(imu_data, dt, None);
468+
sigma_point.forward(*imu_data, dt, None);
469469
}
470470
// Update the mean state as mu_bar
471471
let mut mu_bar = DVector::<f64>::zeros(self.state_size);
@@ -624,7 +624,7 @@ impl ParticleFilter {
624624
/// * `imu_data` - The IMU measurements to propagate the particles with.
625625
pub fn propagate(&mut self, imu_data: &IMUData, dt: f64) {
626626
for particle in &mut self.particles {
627-
particle.forward(imu_data, dt, None);
627+
particle.forward(*imu_data, dt, None);
628628
}
629629
}
630630
/// Update the weights of the particles based on a measurement
@@ -907,8 +907,6 @@ impl GPS for UKF {
907907
/// Tests
908908
#[cfg(test)]
909909
mod tests {
910-
use std::os::unix::process;
911-
912910
use crate::earth;
913911

914912
use super::*;
@@ -935,7 +933,7 @@ mod tests {
935933
gyro: Vector3::new(0.0, 0.0, 0.0),
936934
};
937935
let dt = 1.0;
938-
sigma_point.forward(&imu_data, dt, None);
936+
sigma_point.forward(imu_data.clone(), dt, None);
939937

940938
let position = [0.0, 0.0, 0.0];
941939
let velocity = [0.0, 0.0, 0.0];
@@ -955,7 +953,7 @@ mod tests {
955953
0.01, 0.01, 0.01, // IMU biases noise
956954
0.01, 0.01, 0.01, // Measurement bias noise
957955
];
958-
sigma_point.forward(&imu_data, dt, Some(noise));
956+
sigma_point.forward(imu_data.clone(), dt, Some(noise));
959957
// Check that the state has changed
960958
assert!(sigma_point.nav_state.latitude != position[0]);
961959
assert!(sigma_point.nav_state.longitude != position[1]);

core/src/sim.rs

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
1717
use crate::earth;
1818
use crate::earth::METERS_TO_DEGREES;
1919
use crate::filter::{GPS, StrapdownParams, UKF};
20-
use crate::{IMUData, StrapdownState};
20+
use crate::{IMUData, StrapdownState, forward};
2121
/// Struct representing a single row of test data from the CSV file.
2222
///
2323
/// Fields correspond to columns in the CSV, with appropriate renaming for Rust style.
@@ -103,54 +103,6 @@ impl TestDataRecord {
103103
/// # Returns
104104
/// * `Ok(Vec<TestDataRecord>)` if successful.
105105
/// * `Err` if the file cannot be read or parsed.
106-
///
107-
/// # Example
108-
///
109-
/// ```
110-
/// use strapdown::sim::TestDataRecord;
111-
/// use std::path::Path;
112-
///
113-
/// let record = TestDataRecord {
114-
/// time: chrono::Utc::now(),
115-
/// bearing_accuracy: 0.1,
116-
/// speed_accuracy: 0.1,
117-
/// vertical_accuracy: 0.1,
118-
/// horizontal_accuracy: 0.1,
119-
/// speed: 1.0,
120-
/// bearing: 90.0,
121-
/// altitude: 100.0,
122-
/// longitude: -122.0,
123-
/// latitude: 37.0,
124-
/// qz: 0.0,
125-
/// qy: 0.0,
126-
/// qx: 0.0,
127-
/// qw: 1.0,
128-
/// roll: 0.0,
129-
/// pitch: 0.0,
130-
/// yaw: 0.0,
131-
/// acc_z: 9.81,
132-
/// acc_y: 0.0,
133-
/// acc_x: 0.0,
134-
/// gyro_z: 0.01,
135-
/// gyro_y: 0.01,
136-
/// gyro_x: 0.01,
137-
/// mag_z: 50.0,
138-
/// mag_y: -30.0,
139-
/// mag_x: -20.0,
140-
/// relative_altitude: 0.0,
141-
/// pressure: 1013.25,
142-
/// grav_z: 9.81,
143-
/// grav_y: 0.0,
144-
/// grav_x: 0.0,
145-
/// };
146-
/// let records = vec![record];
147-
/// TestDataRecord::to_csv(&records, "data.csv")
148-
/// .expect("Failed to write test data to CSV");
149-
/// let read_records = TestDataRecord::from_csv("data.csv")
150-
/// .expect("Failed to read test data from CSV");
151-
/// // doctest cleanup
152-
/// std::fs::remove_file("data.csv").unwrap();
153-
/// ```
154106
pub fn from_csv<P: AsRef<std::path::Path>>(
155107
path: P,
156108
) -> Result<Vec<Self>, Box<dyn std::error::Error>> {
@@ -657,12 +609,12 @@ impl From<(&DateTime<Utc>, &UKF, &IMUData, &f64, &f64, &f64, &f64)> for Navigati
657609
.sqrt(),
658610
),
659611
mag_anomaly: earth::magnetic_anomaly(
660-
&state[0].to_radians(),
661-
&state[1].to_radians(),
662-
&state[2],
663-
mag_x,
664-
mag_y,
665-
mag_z,
612+
state[0].to_radians(),
613+
state[1].to_radians(),
614+
state[2],
615+
*mag_x,
616+
*mag_y,
617+
*mag_z,
666618
),
667619
}
668620
}
@@ -744,12 +696,12 @@ impl
744696
.sqrt(),
745697
),
746698
mag_anomaly: earth::magnetic_anomaly(
747-
&state.latitude,
748-
&state.longitude,
749-
&state.altitude,
750-
mag_x,
751-
mag_y,
752-
mag_z,
699+
state.latitude,
700+
state.longitude,
701+
state.altitude,
702+
*mag_x,
703+
*mag_y,
704+
*mag_z,
753705
),
754706
}
755707
}
@@ -821,7 +773,7 @@ pub fn dead_reckoning(records: &[TestDataRecord]) -> Vec<NavigationResult> {
821773
vec![record.gyro_x, record.gyro_y, record.gyro_z],
822774
);
823775
// Propagate the state forward (replace with stub for now)
824-
state.forward(&imu_data, dt);
776+
state = forward(state, imu_data, dt);
825777
results.push(NavigationResult::from((
826778
&current_time,
827779
&state,

0 commit comments

Comments
 (0)