25
25
#if defined(USE_NAV )
26
26
27
27
#include "build/build_config.h"
28
+ #include "build/debug.h"
28
29
29
30
#include "common/axis.h"
30
31
#include "common/log.h"
@@ -354,8 +355,37 @@ static bool gravityCalibrationComplete(void)
354
355
return zeroCalibrationIsCompleteS (& posEstimator .imu .gravityCalibration );
355
356
}
356
357
357
- static void updateIMUTopic ( void )
358
+ static void updateIMUEstimationWeight ( const float dt )
358
359
{
360
+ bool isAccClipped = accIsClipped ();
361
+
362
+ // If accelerometer measurement is clipped - drop the acc weight to zero
363
+ // and gradually restore weight back to 1.0 over time
364
+ if (isAccClipped ) {
365
+ posEstimator .imu .accWeightFactor = 0.0f ;
366
+ }
367
+ else {
368
+ const float relAlpha = dt / (dt + INAV_ACC_CLIPPING_RC_CONSTANT );
369
+ posEstimator .imu .accWeightFactor = posEstimator .imu .accWeightFactor * (1.0f - relAlpha ) + 1.0f * relAlpha ;
370
+ }
371
+
372
+ // DEBUG_VIBE[0-3] are used in IMU
373
+ DEBUG_SET (DEBUG_VIBE , 4 , posEstimator .imu .accWeightFactor * 1000 );
374
+ }
375
+
376
+ float navGetAccelerometerWeight (void )
377
+ {
378
+ const float accWeightScaled = posEstimator .imu .accWeightFactor * positionEstimationConfig ()-> w_xyz_acc_p ;
379
+ DEBUG_SET (DEBUG_VIBE , 5 , accWeightScaled * 1000 );
380
+
381
+ return accWeightScaled ;
382
+ }
383
+
384
+ static void updateIMUTopic (timeUs_t currentTimeUs )
385
+ {
386
+ const float dt = US2S (currentTimeUs - posEstimator .imu .lastUpdateTime );
387
+ posEstimator .imu .lastUpdateTime = currentTimeUs ;
388
+
359
389
if (!isImuReady ()) {
360
390
posEstimator .imu .accelNEU .x = 0 ;
361
391
posEstimator .imu .accelNEU .y = 0 ;
@@ -364,6 +394,9 @@ static void updateIMUTopic(void)
364
394
restartGravityCalibration ();
365
395
}
366
396
else {
397
+ /* Update acceleration weight based on vibration levels and clipping */
398
+ updateIMUEstimationWeight (dt );
399
+
367
400
fpVector3_t accelBF ;
368
401
369
402
/* Read acceleration data in body frame */
@@ -435,12 +468,6 @@ static bool navIsHeadingUsable(void)
435
468
}
436
469
}
437
470
438
- float navGetAccelerometerWeight (void )
439
- {
440
- // TODO(digitalentity): consider accelerometer health in weight calculation
441
- return positionEstimationConfig ()-> w_xyz_acc_p ;
442
- }
443
-
444
471
static uint32_t calculateCurrentValidityFlags (timeUs_t currentTimeUs )
445
472
{
446
473
/* Figure out if we have valid position data from our data sources */
@@ -768,6 +795,7 @@ void initializePositionEstimator(void)
768
795
posEstimator .est .eph = positionEstimationConfig ()-> max_eph_epv + 0.001f ;
769
796
posEstimator .est .epv = positionEstimationConfig ()-> max_eph_epv + 0.001f ;
770
797
798
+ posEstimator .imu .lastUpdateTime = 0 ;
771
799
posEstimator .gps .lastUpdateTime = 0 ;
772
800
posEstimator .baro .lastUpdateTime = 0 ;
773
801
posEstimator .surface .lastUpdateTime = 0 ;
@@ -778,6 +806,8 @@ void initializePositionEstimator(void)
778
806
posEstimator .est .flowCoordinates [X ] = 0 ;
779
807
posEstimator .est .flowCoordinates [Y ] = 0 ;
780
808
809
+ posEstimator .imu .accWeightFactor = 0 ;
810
+
781
811
restartGravityCalibration ();
782
812
783
813
for (axis = 0 ; axis < 3 ; axis ++ ) {
@@ -806,7 +836,7 @@ void FAST_CODE NOINLINE updatePositionEstimator(void)
806
836
const timeUs_t currentTimeUs = micros ();
807
837
808
838
/* Read updates from IMU, preprocess */
809
- updateIMUTopic ();
839
+ updateIMUTopic (currentTimeUs );
810
840
811
841
/* Update estimate */
812
842
updateEstimatedTopic (currentTimeUs );
0 commit comments