Complementary Filter#

The ComplementaryFilter provides a simple implementation of a filter specifically designed to combine the outputs of a gyroscope and an accelerometer to produce a more accurate estimate of the orientation of an object. This filter is provided for completeness and simplicity, but the KalmanFilter or MadgwickFilter are generally preferred for this purpose.

API Reference#

Header File#

Classes#

class ComplementaryFilter#

Complementary filter for estimating pitch and roll angles from accelerometer and gyroscope readings. The filter is defined by the following equation: angle = alpha * (angle + gyro * dt) + (1 - alpha) * accel where:

  • angle is the estimated angle,

  • alpha is the filter coefficient (0 < alpha < 1),

  • gyro is the gyroscope reading,

  • accel is the accelerometer reading,

  • dt is the time step.

Public Functions

inline explicit ComplementaryFilter(float alpha = 0.98f)#

Constructor

Parameters:

alpha – Filter coefficient (0 < alpha < 1)

inline void update(const std::span<const float, 3> &accel, const std::span<const float, 3> &gyro, float dt)#

Update the filter with accelerometer and gyroscope readings.

Note

The accelerometer and gyroscope readings must be in the same coordinate system.

Parameters:
  • accel – Accelerometer readings (m/s^2)

  • gyro – Gyroscope readings (degrees/s)

  • dt – Time step (s)

inline void update(float ax, float ay, float az, float gx, float gy, float gz, float dt)#

Update the filter with accelerometer and gyroscope readings.

Note

The accelerometer and gyroscope readings must be in the same coordinate system.

Parameters:
  • ax – Accelerometer x-axis reading (m/s^2)

  • ay – Accelerometer y-axis reading (m/s^2)

  • az – Accelerometer z-axis reading (m/s^2)

  • gx – Gyroscope x-axis reading (degrees/s)

  • gy – Gyroscope y-axis reading (degrees/s)

  • gz – Gyroscope z-axis reading (degrees/s)

  • dt – Time step (s)

inline float get_pitch() const#

Get the estimated pitch angle (degrees)

Returns:

The estimated pitch angle

inline float get_roll() const#

Get the estimated roll angle (degrees)

Returns:

The estimated roll angle