Skip to content

Commit 30974de

Browse files
committed
pbio/imu: Save one off bias calibration.
When the hub first boots, it is good to have an initial value for the bias to help suppress drift during the first nonstationary seconds. As an estimate, we can use the bias first saved on the previous boot.
1 parent 216a0b6 commit 30974de

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/pbio/src/imu.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,16 @@ static void pbio_imu_handle_stationary_data_func(const int32_t *gyro_data_sum, c
400400
// Update bias at decreasing rate.
401401
gyro_bias.values[i] = gyro_bias.values[i] * (1.0f - weight) + weight * average_now;
402402
}
403+
404+
// If persistent gyro bias has never been set, do so now and request saving.
405+
// This ensures a better starting point for the next boot. We do this only
406+
// once to avoid unnecessary writes on every shutdown. It can be further
407+
// refined with a calibration routine performed by the user.
408+
if (persistent_settings && !(persistent_settings->flags & PBIO_IMU_SETTINGS_FLAGS_GYRO_BIAS_INITIAL_SET) && stationary_counter > 2) {
409+
persistent_settings->angular_velocity_bias_start = gyro_bias;
410+
persistent_settings->flags |= PBIO_IMU_SETTINGS_FLAGS_GYRO_BIAS_INITIAL_SET;
411+
pbsys_storage_request_write();
412+
}
403413
}
404414

405415
/**

0 commit comments

Comments
 (0)