mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-26 01:00:36 +08:00
test(ekf2): cover the gyro bias with a late at-rest status
A stand-in for the land detector provides the at-rest status with its latency, so the simulator gets a switch to stop clearing that status on its own. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: bresch <brescianimathieu@gmail.com>
This commit is contained in:
1 parent
6cdd21a369
commit
11eded4345
3 files changed
+134
-3
No files matched your search
@@ -172,7 +172,7 @@ void SensorSimulator::runMicroseconds(uint32_t duration)
|
||||
updateSensors();
|
||||
|
||||
if (update_imu) {
|
||||
if (_imu.moving()) {
|
||||
if (_motion_clears_at_rest && _imu.moving()) {
|
||||
_ekf->set_vehicle_at_rest(false);
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ void SensorSimulator::runReplayMicroseconds(uint32_t duration)
|
||||
updateSensors();
|
||||
|
||||
if (update_imu) {
|
||||
if (_imu.moving()) {
|
||||
if (_motion_clears_at_rest && _imu.moving()) {
|
||||
_ekf->set_vehicle_at_rest(false);
|
||||
}
|
||||
|
||||
@@ -345,7 +345,7 @@ void SensorSimulator::runTrajectoryMicroseconds(uint32_t duration)
|
||||
updateSensors();
|
||||
|
||||
if (update_imu) {
|
||||
if (_imu.moving()) {
|
||||
if (_motion_clears_at_rest && _imu.moving()) {
|
||||
_ekf->set_vehicle_at_rest(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -120,6 +120,10 @@ public:
|
||||
|
||||
void setImuBias(Vector3f accel_bias, Vector3f gyro_bias);
|
||||
|
||||
// The at-rest status is cleared as soon as the IMU data moves, ahead of the fusion time
|
||||
// horizon. A test that provides the status with setSystemFlagData() has to disable it.
|
||||
void setMotionClearsAtRest(bool enabled) { _motion_clears_at_rest = enabled; }
|
||||
|
||||
void simulateOrientation(Quatf orientation);
|
||||
void setOrientation(const Quatf &orientation) { _R_body_to_world = Dcmf(orientation); }
|
||||
void setOrientation(const Dcmf &orientation) { _R_body_to_world = orientation; }
|
||||
@@ -154,6 +158,7 @@ private:
|
||||
std::vector<sensor_info> _replay_data{};
|
||||
|
||||
bool _has_replay_data{false};
|
||||
bool _motion_clears_at_rest{true};
|
||||
|
||||
uint64_t _current_replay_data_index{0};
|
||||
uint64_t _time{0}; // microseconds
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
* Gyro-related tests (bias convergence, ...)
|
||||
*/
|
||||
|
||||
#include <functional>
|
||||
#include <gtest/gtest.h>
|
||||
#include "EKF/ekf.h"
|
||||
#include "sensor_simulator/sensor_simulator.h"
|
||||
@@ -113,3 +114,128 @@ TEST_F(EkfGyroscopeTest, biasEstimateNegative)
|
||||
testBias(bias, 30, Vector3f(0.0008f, 0.0008f, 0.004f));
|
||||
}
|
||||
}
|
||||
|
||||
// Stand-in for the rotation check of the land detector: it needs both the bias-corrected
|
||||
// angular velocity (vehicle_angular_velocity) and the uncorrected one to rotate, sees them
|
||||
// with some latency, and its output reaches the EKF through the time-aligned system flag
|
||||
// buffer, as in EKF2.
|
||||
class GroundedVehicle
|
||||
{
|
||||
public:
|
||||
GroundedVehicle():
|
||||
_ekf{std::make_shared<Ekf>()},
|
||||
_sensor_simulator(_ekf)
|
||||
{
|
||||
_ekf->init(0);
|
||||
_sensor_simulator.setMotionClearsAtRest(false);
|
||||
|
||||
parameters *params = _ekf->getParamHandle();
|
||||
params->ekf2_mag_type = MagFuseType::INIT;
|
||||
params->ekf2_gbias_init = 0.04f;
|
||||
params->ekf2_gyr_b_lim = 0.15f;
|
||||
}
|
||||
|
||||
void run(float duration, const std::function<Vector3f(float)> &angular_rate, bool handled = false)
|
||||
{
|
||||
static constexpr uint32_t kImuIntervalUs = 5'000;
|
||||
|
||||
for (float t_end = _time + duration; _time < t_end; _time += kImuIntervalUs * 1e-6f) {
|
||||
const Vector3f rate = angular_rate(_time);
|
||||
|
||||
const Vector3f rate_seen = _rate_history[_history_index];
|
||||
_rate_history[_history_index] = rate;
|
||||
_history_index = (_history_index + 1) % kDetectorLatencySamples;
|
||||
|
||||
const uint64_t now = _sensor_simulator.getTime();
|
||||
|
||||
const bool rotating = (rate_seen - _ekf->getGyroBias()).longerThan(math::radians(3.f))
|
||||
&& rate_seen.longerThan(math::radians(1.5f));
|
||||
|
||||
if (handled || rotating) {
|
||||
_time_last_move_us = now;
|
||||
}
|
||||
|
||||
systemFlagUpdate flags{};
|
||||
flags.time_us = now;
|
||||
flags.in_air = false;
|
||||
flags.at_rest = (now - _time_last_move_us) > 1'000'000;
|
||||
_ekf->setSystemFlagData(flags);
|
||||
|
||||
if (flags.at_rest && !PX4_ISFINITE(_time_at_rest)) {
|
||||
_time_at_rest = _time;
|
||||
}
|
||||
|
||||
_sensor_simulator._imu.setGyroData(rate);
|
||||
_sensor_simulator.runMicroseconds(kImuIntervalUs);
|
||||
|
||||
_gyro_bias_z_max = fmaxf(_gyro_bias_z_max, fabsf(gyroBiasZ()));
|
||||
}
|
||||
}
|
||||
|
||||
float gyroBiasZ() const { return _ekf->getGyroBias()(2); }
|
||||
float gyroBiasZMax() const { return _gyro_bias_z_max; }
|
||||
bool atRest() const { return _ekf->control_status_flags().vehicle_at_rest; }
|
||||
float timeAtRest() const { return _time_at_rest; }
|
||||
|
||||
private:
|
||||
static constexpr size_t kDetectorLatencySamples = 5; // 25 ms: rate filtering + scheduling
|
||||
|
||||
std::shared_ptr<Ekf> _ekf;
|
||||
SensorSimulator _sensor_simulator;
|
||||
|
||||
Vector3f _rate_history[kDetectorLatencySamples] {};
|
||||
size_t _history_index{0};
|
||||
uint64_t _time_last_move_us{0};
|
||||
float _time{0.f};
|
||||
float _time_at_rest{NAN};
|
||||
float _gyro_bias_z_max{0.f};
|
||||
};
|
||||
|
||||
static void expectYawRotationIsNotLearnedAsGyroBias(const std::function<float(float)> &yaw_rate)
|
||||
{
|
||||
// The rotation is real motion: the true gyro bias stays zero. Sweep the onset over one
|
||||
// 0.2 s zero gyro update window, as the outcome depends on where the window ends.
|
||||
for (float onset = 0.f; onset < 0.2f; onset += 0.005f) {
|
||||
GroundedVehicle vehicle;
|
||||
vehicle.run(5.f, [](float) { return Vector3f(); }, true);
|
||||
// Rest starts after 1 s and has to resume after the rotation
|
||||
vehicle.run(4.f, [&vehicle, &yaw_rate, onset](float t) {
|
||||
const float rotation_time = t - (vehicle.timeAtRest() + onset);
|
||||
return Vector3f(0.f, 0.f, (rotation_time >= 0.f) ? yaw_rate(rotation_time) : 0.f);
|
||||
});
|
||||
ASSERT_TRUE(PX4_ISFINITE(vehicle.timeAtRest()));
|
||||
|
||||
EXPECT_LT(vehicle.gyroBiasZMax(), math::radians(0.1f)) << "onset " << onset << " s, bias "
|
||||
<< math::degrees(vehicle.gyroBiasZMax()) << " deg/s";
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EkfGyroscopeAtRest, yawTwistIsNotLearnedAsGyroBias)
|
||||
{
|
||||
expectYawRotationIsNotLearnedAsGyroBias([](float t) { return (t < 0.04f) ? math::radians(100.f) : 0.f; });
|
||||
}
|
||||
|
||||
TEST(EkfGyroscopeAtRest, slowYawOnsetIsNotLearnedAsGyroBias)
|
||||
{
|
||||
// The rotation stays below the at-rest threshold for its first 0.1 s
|
||||
expectYawRotationIsNotLearnedAsGyroBias([](float t) { return (t < 1.f) ? math::radians(30.f) * t : 0.f; });
|
||||
}
|
||||
|
||||
TEST(EkfGyroscopeAtRest, gyroBiasErrorAboveRotationThresholdRecovers)
|
||||
{
|
||||
// A rotation slower than the at-rest threshold is indistinguishable from a gyro bias:
|
||||
// a turntable spinning up in steps gets learned as bias, then it stops and the estimate
|
||||
// is off by more than the threshold
|
||||
GroundedVehicle vehicle;
|
||||
vehicle.run(2.f, [](float) { return Vector3f(); });
|
||||
vehicle.run(20.f, [](float) { return Vector3f(0.f, 0.f, math::radians(2.5f)); });
|
||||
vehicle.run(40.f, [](float) { return Vector3f(0.f, 0.f, math::radians(5.f)); });
|
||||
ASSERT_GT(vehicle.gyroBiasZ(), math::radians(3.5f));
|
||||
|
||||
// Only the bias-corrected rate still rotates
|
||||
vehicle.run(2.f, [](float) { return Vector3f(); });
|
||||
EXPECT_TRUE(vehicle.atRest());
|
||||
|
||||
vehicle.run(40.f, [](float) { return Vector3f(); });
|
||||
EXPECT_LT(fabsf(vehicle.gyroBiasZ()), math::radians(3.f)) << math::degrees(vehicle.gyroBiasZ()) << " deg/s";
|
||||
}
|
||||
Reference in new issue
Block a user