Skip to content

Commit ab6a74e

Browse files
authored
Merge pull request #68 from jbrodovsky:jbrodovsky/issue65
Refactor StrapdownState to use Default and new() properly
2 parents 983b3ac + c324a84 commit ab6a74e

2 files changed

Lines changed: 24 additions & 28 deletions

File tree

core/src/sim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ mod tests {
12461246
}
12471247
#[test]
12481248
fn test_navigation_result_new_from_nav_state() {
1249-
let mut state = StrapdownState::new();
1249+
let mut state = StrapdownState::default();
12501250
state.latitude = 1.0;
12511251
state.longitude = 2.0;
12521252
state.altitude = 3.0;

core/src/strapdown.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,6 @@ impl Debug for StrapdownState {
256256

257257
impl Default for StrapdownState {
258258
fn default() -> Self {
259-
Self::new()
260-
}
261-
}
262-
263-
impl StrapdownState {
264-
/// Create a new StrapdownState with all zeros
265-
pub fn new() -> StrapdownState {
266259
StrapdownState {
267260
latitude: 0.0,
268261
longitude: 0.0,
@@ -274,6 +267,9 @@ impl StrapdownState {
274267
coordinate_convention: true, // NED by default
275268
}
276269
}
270+
}
271+
272+
impl StrapdownState {
277273
/// Create a new StrapdownState from explicit position and velocity components, and attitude
278274
///
279275
/// # Arguments
@@ -286,7 +282,7 @@ impl StrapdownState {
286282
/// * `attitude` - Rotation3<f64> attitude matrix.
287283
/// * `in_degrees` - If true, angles are provided in degrees and will be converted to radians.
288284
/// * `ned` - If true, the coordinate convention is NED (North, East, Down), otherwise ENU (East, North, Up).
289-
pub fn new_from_components(
285+
pub fn new(
290286
latitude: f64,
291287
longitude: f64,
292288
altitude: f64,
@@ -445,7 +441,7 @@ impl StrapdownState {
445441
/// ```rust
446442
/// use strapdown::{StrapdownState, IMUData, forward};
447443
/// use nalgebra::Vector3;
448-
/// let mut state = StrapdownState::new();
444+
/// let mut state = StrapdownState::default();
449445
/// let imu_data = IMUData::new_from_vector(
450446
/// Vector3::new(0.0, 0.0, -9.81), // free fall acceleration in m/s^2
451447
/// Vector3::new(0.0, 0.0, 0.0) // No rotation
@@ -734,7 +730,7 @@ mod tests {
734730

735731
#[test]
736732
fn test_strapdown_state_new() {
737-
let state = StrapdownState::new();
733+
let state = StrapdownState::default();
738734
assert_eq!(state.latitude, 0.0);
739735
assert_eq!(state.longitude, 0.0);
740736
assert_eq!(state.altitude, 0.0);
@@ -745,7 +741,7 @@ mod tests {
745741
}
746742
#[test]
747743
fn test_to_vector_zeros() {
748-
let state = StrapdownState::new();
744+
let state = StrapdownState::default();
749745
let state_vector = state.to_vector(true);
750746
let zeros: SVector<f64, 9> = SVector::zeros();
751747
assert_eq!(state_vector, zeros);
@@ -781,7 +777,7 @@ mod tests {
781777
#[test]
782778
fn test_dcm_to_vector() {
783779
let attitude = Rotation3::from_euler_angles(0.0, 0.0, 0.0);
784-
let state: StrapdownState = StrapdownState::new_from_components(
780+
let state: StrapdownState = StrapdownState::new(
785781
0.0,
786782
(1.0_f64).to_degrees(),
787783
2.0,
@@ -820,7 +816,7 @@ mod tests {
820816
// Test the forward mechanization (basic structure, not full dynamics)
821817
fn test_forward_freefall_stub() {
822818
let attitude = Rotation3::identity();
823-
let state = StrapdownState::new_from_components(
819+
let state = StrapdownState::new(
824820
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true, // NED convention
825821
);
826822
// This is a stub: actual forward propagation logic should be tested in integration with the mechanization equations.
@@ -836,7 +832,7 @@ mod tests {
836832
fn rest() {
837833
// Test the forward mechanization with a state at rest
838834
let attitude = Rotation3::identity();
839-
let mut state = StrapdownState::new_from_components(
835+
let mut state = StrapdownState::new(
840836
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true, // NED convention
841837
);
842838
assert_eq!(state.velocity_north, 0.0);
@@ -871,7 +867,7 @@ mod tests {
871867
fn yawing() {
872868
// Testing the forward mechanization with a state that is yawing
873869
let attitude = Rotation3::from_euler_angles(0.0, 0.0, 0.1); // 0.1 rad yaw
874-
let state = StrapdownState::new_from_components(
870+
let state = StrapdownState::new(
875871
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, // angles provided in radians
876872
true, // NED convention
877873
);
@@ -887,7 +883,7 @@ mod tests {
887883
fn rolling() {
888884
// Testing the forward mechanization with a state that is yawing
889885
let attitude = Rotation3::from_euler_angles(0.1, 0.0, 0.0); // 0.1 rad yaw
890-
let state = StrapdownState::new_from_components(
886+
let state = StrapdownState::new(
891887
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, // angles provided in radians
892888
true, // NED convention
893889
);
@@ -903,7 +899,7 @@ mod tests {
903899
fn pitching() {
904900
// Testing the forward mechanization with a state that is yawing
905901
let attitude = Rotation3::from_euler_angles(0.0, 0.1, 0.0); // 0.1 rad yaw
906-
let state = StrapdownState::new_from_components(
902+
let state = StrapdownState::new(
907903
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, // angles provided in radians
908904
true, // NED convention
909905
);
@@ -972,7 +968,7 @@ mod tests {
972968
#[test]
973969
fn test_velocity_update_zero_force() {
974970
// Zero specific force, velocity should remain unchanged
975-
let state = StrapdownState::new();
971+
let state = StrapdownState::default();
976972
let f = nalgebra::Vector3::new(
977973
0.0,
978974
0.0,
@@ -987,7 +983,7 @@ mod tests {
987983
#[test]
988984
fn test_velocity_update_constant_force() {
989985
// Constant specific force in north direction, expect velocity to increase linearly
990-
let state = StrapdownState::new();
986+
let state = StrapdownState::default();
991987
let f = nalgebra::Vector3::new(1.0, 0.0, earth::gravity(&0.0, &0.0)); // 1 m/s^2 north
992988
let dt = 2.0;
993989
let v_new = velocity_update(&state, f, dt);
@@ -999,7 +995,7 @@ mod tests {
999995
#[test]
1000996
fn test_velocity_update_initial_velocity() {
1001997
// Initial velocity, zero force, should remain unchanged
1002-
let mut state = StrapdownState::new();
998+
let mut state = StrapdownState::default();
1003999
state.velocity_north = 5.0;
10041000
state.velocity_east = -3.0;
10051001
state.velocity_down = 2.0;
@@ -1013,7 +1009,7 @@ mod tests {
10131009
#[test]
10141010
fn vertical_acceleration() {
10151011
// Test vertical acceleration
1016-
let mut state = StrapdownState::new();
1012+
let mut state = StrapdownState::default();
10171013
state.velocity_north = 0.0;
10181014
state.velocity_east = 0.0;
10191015
state.velocity_down = 0.0;
@@ -1026,7 +1022,7 @@ mod tests {
10261022
fn test_forward_yawing() {
10271023
// Yaw rate only, expect yaw to increase by gyro_z * dt
10281024
let attitude = nalgebra::Rotation3::identity();
1029-
let mut state = StrapdownState::new_from_components(
1025+
let mut state = StrapdownState::new(
10301026
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true,
10311027
);
10321028
let imu_data = IMUData::new_from_vec(vec![0.0, 0.0, 0.0], vec![0.0, 0.0, 0.1]);
@@ -1040,7 +1036,7 @@ mod tests {
10401036
fn test_forward_rolling() {
10411037
// Roll rate only, expect roll to increase by gyro_x * dt
10421038
let attitude = nalgebra::Rotation3::identity();
1043-
let mut state = StrapdownState::new_from_components(
1039+
let mut state = StrapdownState::new(
10441040
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true,
10451041
);
10461042
let imu_data = IMUData::new_from_vec(vec![0.0, 0.0, 0.0], vec![0.1, 0.0, 0.0]);
@@ -1056,7 +1052,7 @@ mod tests {
10561052
fn test_forward_pitching() {
10571053
// Pitch rate only, expect pitch to increase by gyro_y * dt
10581054
let attitude = nalgebra::Rotation3::identity();
1059-
let mut state = StrapdownState::new_from_components(
1055+
let mut state = StrapdownState::new(
10601056
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true,
10611057
);
10621058
let imu_data = IMUData::new_from_vec(vec![0.0, 0.0, 0.0], vec![0.0, 0.1, 0.0]);
@@ -1070,7 +1066,7 @@ mod tests {
10701066
fn test_forward_velocity_north() {
10711067
// Constant acceleration north, expect velocity_north to increase by accel * dt
10721068
let attitude = nalgebra::Rotation3::identity();
1073-
let mut state = StrapdownState::new_from_components(
1069+
let mut state = StrapdownState::new(
10741070
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true,
10751071
);
10761072
let imu_data = IMUData::new_from_vec(vec![1.0, 0.0, 0.0], vec![0.0, 0.0, 0.0]);
@@ -1083,7 +1079,7 @@ mod tests {
10831079
fn test_forward_velocity_east() {
10841080
// Constant acceleration east, expect velocity_east to increase by accel * dt
10851081
let attitude = nalgebra::Rotation3::identity();
1086-
let mut state = StrapdownState::new_from_components(
1082+
let mut state = StrapdownState::new(
10871083
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true,
10881084
);
10891085
let imu_data = IMUData::new_from_vec(vec![0.0, 1.0, 0.0], vec![0.0, 0.0, 0.0]);
@@ -1096,7 +1092,7 @@ mod tests {
10961092
fn test_forward_velocity_down() {
10971093
// Constant acceleration down, expect velocity_down to increase by accel * dt
10981094
let attitude = nalgebra::Rotation3::identity();
1099-
let mut state = StrapdownState::new_from_components(
1095+
let mut state = StrapdownState::new(
11001096
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, attitude, false, true,
11011097
);
11021098
let imu_data = IMUData::new_from_vec(

0 commit comments

Comments
 (0)