@@ -20,17 +20,16 @@ use kfilter::measurement::LinearMeasurement;
2020use kfilter:: system:: LinearNoInputSystem ;
2121use nalgebra:: { Matrix1 , Matrix1x2 , Matrix2 , UnitQuaternion , Vector2 , Vector3 } ;
2222
23-
2423mod registers;
2524
2625const ICM_ADDR : u8 = 0b1101000 ;
2726const WHO_AM_I_EXPECTED : u8 = 0x42 ;
2827const LSB_TO_G : f32 = 16.0 / 32768.0 ;
29-
3028const LSB_TO_DPS : f32 = 1000.0 / 32768.0 ;
3129
32- const TICK_PERIOD : rtic_monotonics:: systick:: fugit:: Duration < u32 , 1 , 1000 > = <rtic_monotonics:: systick:: Systick as Monotonic >:: TICK_PERIOD ;
33- const SECONDS_PER_TICK : f32 = TICK_PERIOD . to_micros ( ) as f32 / 1_000_000.0 ;
30+ const TICK_PERIOD : rtic_monotonics:: systick:: fugit:: Duration < u32 , 1 , 1000 > =
31+ <rtic_monotonics:: systick:: Systick as Monotonic >:: TICK_PERIOD ;
32+ const SECONDS_PER_TICK : f32 = TICK_PERIOD . to_micros ( ) as f32 / 1_000_000.0 ;
3433
3534/// Convert the high and low bits obtained from the IMU into a gyrometer
3635/// reading (in degrees per second).
@@ -86,7 +85,7 @@ pub struct IMU<I2C> {
8685
8786 /// State for the accel Y-axis filter
8887 x_ay : Vector2 < f32 > ,
89- p_ay : Matrix2 < f32 > ,
88+ p_ay : Matrix2 < f32 > ,
9089
9190 //Timstamp of last update. For dt calculation
9291 /// Timestamp of the last gyro Z update.
@@ -123,7 +122,7 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
123122 p_ay : Matrix2 :: identity ( ) ,
124123 last_update_gz : None ,
125124 last_update_ax : None ,
126- last_update_ay : None ,
125+ last_update_ay : None ,
127126 }
128127 }
129128
@@ -184,7 +183,6 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
184183 self . calibrate_offsets ( delay, 1000 ) ;
185184
186185 //Initialize KALMAN FILTERS
187-
188186 let initial_covariance = Matrix2 :: identity ( ) * 1000.0 ; // High initial uncertainty
189187
190188 self . x_gz = Vector2 :: zeros ( ) ;
@@ -210,8 +208,7 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
210208 // CALCULATE DT
211209 let now_tick = Systick :: now ( ) . ticks ( ) ;
212210
213-
214- let dt_ticks: f32 = ( now_tick - self . last_update_gz . unwrap_or ( now_tick) ) as f32 ; // Convert milliseconds to seconds
211+ let dt_ticks: f32 = ( now_tick - self . last_update_gz . unwrap_or ( now_tick) ) as f32 ; // Convert milliseconds to seconds
215212 let dt: f32 = dt_ticks * SECONDS_PER_TICK ;
216213
217214 // CREATE MATRICES FOR THIS STEP
@@ -244,7 +241,7 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
244241
245242 // RETURN THE FILTERED VALUE
246243 Ok ( self . x_gz [ 0 ] )
247- }
244+ }
248245
249246 /// Read the acceleration in the x direction
250247 pub fn accel_x ( & mut self ) -> Result < f32 , ImuError < E > > {
@@ -278,14 +275,14 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
278275 self . last_update_ax = Some ( now_tick) ;
279276
280277 Ok ( self . x_ax [ 0 ] )
281- }
278+ }
282279
283280 /// Read the acceleration in the y direction
284281 pub fn accel_y ( & mut self ) -> Result < f32 , ImuError < E > > {
285282 if !self . initialized {
286283 return Err ( ImuError :: Uninitialized ) ;
287284 }
288-
285+
289286 let now_tick = Systick :: now ( ) . ticks ( ) ;
290287 let dt_ticks: f32 = ( now_tick - self . last_update_ay . unwrap_or ( now_tick) ) as f32 ;
291288 let dt: f32 = dt_ticks * SECONDS_PER_TICK ;
@@ -313,7 +310,6 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
313310 Ok ( self . x_ay [ 0 ] )
314311 }
315312
316-
317313 /// Write the raw register address and data over the i2c line
318314 fn raw_write ( & mut self , register_addr : u8 , data : u8 ) -> Result < ( ) , ImuError < E > > {
319315 self . i2c
@@ -353,7 +349,6 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
353349 Ok ( ( ) )
354350 }
355351
356-
357352 /// Without the Kalman Filter, read the gyro velocity in the z direction
358353 fn raw_gyro_z ( & mut self ) -> Result < f32 , ImuError < E > > {
359354 if !self . initialized {
@@ -426,8 +421,8 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
426421 Ok ( reading_to_accel ( hi, lo) )
427422 }
428423
429-
430424 /// Calibrate the offsets for the gyro and accelerometer
425+ /// IMU axes are adjusted to robot frame
431426 /// MAKE SURE IMU IS STILL during this time
432427 fn calibrate_offsets ( & mut self , delay : & mut impl DelayMs < u8 > , cal_time_ms : u32 ) {
433428 let t0 = Systick :: now ( ) . ticks ( ) ;
@@ -443,19 +438,21 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
443438 let gx = self . raw_gyro_x ( ) . unwrap_or ( 0.0 ) ;
444439 let gy = self . raw_gyro_y ( ) . unwrap_or ( 0.0 ) ;
445440 let gz = self . raw_gyro_z ( ) . unwrap_or ( 0.0 ) ;
446-
441+
447442 let ax = self . raw_accel_x ( ) . unwrap_or ( 0.0 ) ;
448443 let ay = self . raw_accel_y ( ) . unwrap_or ( 0.0 ) ;
449444 let az = self . raw_accel_z ( ) . unwrap_or ( 0.0 ) ;
450445
451446 sum_gyro += Vector3 :: new ( gx, gy, gz) ;
452447 sum_accel += Vector3 :: new ( ax, ay, az) ;
453-
448+
454449 count += 1 ;
455450 delay. delay_ms ( 5 ) ;
456451 }
457452
458- if count == 0 { return ; }
453+ if count == 0 {
454+ return ;
455+ }
459456
460457 // GYRO BIAS (Average noise)
461458 self . gyro_bias = sum_gyro / ( count as f32 ) ;
@@ -475,24 +472,25 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
475472 let target_direction = Vector3 :: z_axis ( ) ; // Points to +Z (0, 0, 1)
476473
477474 // This creates a rotation that aligns "Measured" -> "Target"
478- self . accel_correction = UnitQuaternion :: rotation_between ( & measured_direction, & target_direction)
479- . unwrap_or ( UnitQuaternion :: identity ( ) ) ;
475+ self . accel_correction =
476+ UnitQuaternion :: rotation_between ( & measured_direction, & target_direction)
477+ . unwrap_or ( UnitQuaternion :: identity ( ) ) ;
480478 }
481479
482480 /// Reads raw Accel, applies Scaling, then Rotation
483481 fn get_corrected_accel_vector ( & mut self ) -> Result < Vector3 < f32 > , ImuError < E > > {
484482 let ax = self . raw_accel_x ( ) ?;
485483 let ay = self . raw_accel_y ( ) ?;
486484 let az = self . raw_accel_z ( ) ?;
487-
485+
488486 let raw = Vector3 :: new ( ax, ay, az) ;
489-
487+
490488 // Scale (fix magnitude error)
491489 let scaled = raw * self . accel_scale ;
492490
493491 // Rotate (fix mounting tilt)
494492 let rotated = self . accel_correction * scaled;
495-
493+
496494 Ok ( rotated)
497495 }
498496
@@ -501,7 +499,7 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
501499 let gx = self . raw_gyro_x ( ) ?;
502500 let gy = self . raw_gyro_y ( ) ?;
503501 let gz = self . raw_gyro_z ( ) ?;
504-
502+
505503 let raw = Vector3 :: new ( gx, gy, gz) ;
506504
507505 // Remove Bias
@@ -512,7 +510,6 @@ impl<I2C: i2c::Write<Error = E> + i2c::Read<Error = E>, E: Debug> IMU<I2C> {
512510
513511 Ok ( rotated)
514512 }
515-
516513}
517514
518515#[ cfg( test) ]
0 commit comments