BB.Estimator.Ahrs.Math (bb_estimator_ahrs v0.1.0)

Copy Markdown View Source

Stateless mathematical utilities shared across the AHRS filters.

All inputs and outputs are in SI units (rad/s for angular velocity, m/s² for linear acceleration, radians for angles). Unit conversion is the responsibility of the sensor driver populating the inbound BB.Message.Sensor.Imu payload.

Ported from gworkman/ahrs' Ahrs.Math.

Summary

Functions

Calculates roll and pitch in radians from a 3-axis acceleration vector. Yaw is unobservable from acceleration alone.

Converts Euler angles in radians to a quaternion (Z-Y-X Tait-Bryan).

Standard gravity in m/s².

Returns true if the magnitude of an accel vector in m/s² is within threshold of standard gravity, where threshold is a fractional value (e.g. 0.1 means ±10% around 1 g).

Normalises an angle to the range (-π, π].

Converts a quaternion to Euler angles using the Z-Y-X Tait-Bryan convention. Returns {roll, pitch, yaw} in radians, unless :degrees is passed.

Rotates a 3D {x, y, z} vector by a quaternion. Equivalent to q * v * conjugate(q).

Functions

accel_to_tilt(ax, ay, az)

@spec accel_to_tilt(float(), float(), float()) :: {roll :: float(), pitch :: float()}

Calculates roll and pitch in radians from a 3-axis acceleration vector. Yaw is unobservable from acceleration alone.

Returns {roll, pitch}. The accel components are in any unit; only their relative magnitudes matter.

euler_to_quaternion(roll, pitch, yaw)

@spec euler_to_quaternion(roll :: float(), pitch :: float(), yaw :: float()) ::
  BB.Estimator.Ahrs.Quaternion.t()

Converts Euler angles in radians to a quaternion (Z-Y-X Tait-Bryan).

gravity()

@spec gravity() :: float()

Standard gravity in m/s².

gravity_only?(ax, ay, az, threshold)

@spec gravity_only?(float(), float(), float(), float()) :: boolean()

Returns true if the magnitude of an accel vector in m/s² is within threshold of standard gravity, where threshold is a fractional value (e.g. 0.1 means ±10% around 1 g).

Used by Madgwick and Mahony to reject readings dominated by linear acceleration rather than gravity.

normalize_angle(angle)

@spec normalize_angle(float()) :: float()

Normalises an angle to the range (-π, π].

quaternion_to_euler(quaternion, opts \\ [])

@spec quaternion_to_euler(
  BB.Estimator.Ahrs.Quaternion.t(),
  keyword()
) :: {roll :: float(), pitch :: float(), yaw :: float()}

Converts a quaternion to Euler angles using the Z-Y-X Tait-Bryan convention. Returns {roll, pitch, yaw} in radians, unless :degrees is passed.

rotate_vector(arg, quaternion)

@spec rotate_vector({float(), float(), float()}, BB.Estimator.Ahrs.Quaternion.t()) ::
  {float(), float(), float()}

Rotates a 3D {x, y, z} vector by a quaternion. Equivalent to q * v * conjugate(q).