class mola::GravityLeveler

Overview

Causal gravity leveling of a trajectory produced by an odometry or a state estimator, without touching the estimator itself.

The trajectory’s frame may slowly tilt with respect to gravity (e.g. where the scans barely constrain pitch and roll), which turns horizontal motion into a vertical drift. This class estimates that tilt and rotates the trajectory increments to undo it:

  • Measurement, every epoch_period seconds: the mean specific force over a trailing window, each reading rotated into the trajectory frame with the trajectory’s attitude at its own time, minus the trajectory’s own mean acceleration over the same interval. Normalized, its horizontal part is the frame tilt tau (2 DoF, small angles), plus the effect of any body-fixed accelerometer error.

  • Readings beyond max_tilt, or with gravity not pointing up, are ignored: a frame tilt is small, and a large reading means the trajectory’s attitude itself is failing.

  • A Kalman filter with state [tau (2), b (3)] : tau a random walk with tilt_random_walk deg/sqrt(s), b a constant body-frame accelerometer error (bias plus IMU misalignment), observed through the mean attitude over the window. A body-fixed error turns with the platform and a frame tilt does not, which separates both while the platform turns. Readings beyond a chi-squared gate are ignored; after max_consecutive_rejections of them in a row the filter restarts from the current reading, so a wrong state cannot lock itself in.

  • Optionally, readings of the up direction measured from plumb structure (walls, columns, floors) seen in the trajectory frame, add_up_reading(). They observe the tilt directly, with no body-frame term, and with the covariance of their own estimate (plus up_reading_sigma_floor for what it cannot see, e.g. walls a little out of plumb), behind their own chi-squared gate. Their plausibility limit is max_tilt, and up to max_up_tilt where the accelerometer also sees the frame beyond max_tilt.

  • Output: the part of the estimated tilt beyond a deadband is removed by rotating every increment of the trajectory, and its attitude, by the small rotation that levels the frame. The deadband leaves alone a frame that is already level within the readings’ own error: deadband degrees for the accelerometer alone, up_reading_deadband while an up reading has been accepted within the last up_reading_hold seconds, since those readings are an order of magnitude more precise and a 2-degree deadband would hide the wander they measure. For the same reason the tilt’s random walk is then up_reading_random_walk. After the hold, the deadband grows to deadband over up_reading_ramp seconds, so the correction fades instead of dropping at once.

  • Up readings that come with their place, and that the filter accepted, are also kept by place: a pose within up_reading_place_radius (horizontally, and up_reading_place_max_dz vertically) of some of them is leveled with those readings instead of with the filter’s state, fading into the state toward their edge. The frame’s tilt depends on where the odometry is in its map, so a revisited place gets its own readings back, not the last one taken elsewhere, and a trailing window is applied where it was measured instead of a few meters later.

  • Optionally (revisit_radius), a revisited place takes the correction of its first visit, attitude and position, so both visits agree.

Only past data are used. Positions are corrected incrementally (and, on revisits, moved gradually toward the first visit’s), so the output is continuous even when the correction changes.

#include <GravityLeveler.h>

class GravityLeveler
{
public:
    // structs

    struct ImuSample;
    struct Parameters;
    struct PlaceTilt;
    struct PlacedReading;
    struct PoseSample;
    struct RevisitAnchor;
    struct RevisitCorrection;
    struct Stats;

    // construction

    GravityLeveler();
    GravityLeveler(const Parameters& p);

    // methods

    const Parameters& parameters() const;
    const Stats& stats() const;
    void add_imu(double t, const mrpt::math::TVector3D& specific_force);

    void add_up_reading(
        double t,
        const mrpt::math::TVector3D& up,
        const mrpt::math::CMatrixDouble22& up_cov,
        const std::optional<mrpt::math::TPoint3D>& place = std::nullopt
        );

    mrpt::poses::CPose3D add_pose(double t, const mrpt::poses::CPose3D& pose);
    std::optional<mrpt::math::TVector2D> tilt() const;
    std::optional<mrpt::math::CMatrixDouble22> tilt_cov() const;
    mrpt::math::TVector2D applied_tilt() const;
};

Construction

GravityLeveler(const Parameters& p)

Parameters:

std::exception

on invalid parameters

Methods

void add_imu(double t, const mrpt::math::TVector3D& specific_force)

Adds one accelerometer reading (specific force, in the vehicle frame)

void add_up_reading(
    double t,
    const mrpt::math::TVector3D& up,
    const mrpt::math::CMatrixDouble22& up_cov,
    const std::optional<mrpt::math::TPoint3D>& place = std::nullopt
    )

Adds a reading of the up direction of the trajectory frame at time t, e.g. from the walls and floors seen around the trajectory, with the covariance of its x and y components [rad^2]. It updates the filter at once, so it should come before the poses newer than t. place, if given, is where in the trajectory frame the reading was measured (e.g. the mean position of the scans behind it), see up_reading_place_radius.

mrpt::poses::CPose3D add_pose(double t, const mrpt::poses::CPose3D& pose)

Adds the next pose of the trajectory and returns it leveled. IMU readings up to t should have been added before. A pose not newer than the previous one is leveled with the current correction but does not update the filter.

std::optional<mrpt::math::TVector2D> tilt() const

The current tilt estimate, before the deadband: the x/z and y/z components of the frame’s up direction (tangents; the tilt angle is atan(|tilt|)). Empty until the first reading has been taken.

std::optional<mrpt::math::CMatrixDouble22> tilt_cov() const

Covariance of tilt() (tangents, approximately [rad^2] for small tilts). Empty until the first reading.

mrpt::math::TVector2D applied_tilt() const

The tilt being removed from the output (after the deadband), as tilt() (tangents)