Mod Library
Sun Devil Rocketry's hardware agnostic utility library
Loading...
Searching...
No Matches
mahony.h File Reference

: Mahony attitude filter interface. More...

#include <stdbool.h>
#include "math_sdr.h"

Go to the source code of this file.

Classes

struct  _MAHONY_FILTER
 State and gains for a Mahony attitude filter. More...

Typedefs

typedef struct _MAHONY_FILTER MAHONY_FILTER
 State and gains for a Mahony attitude filter.

Enumerations

enum  MAHONY_STATUS {
  MAHONY_OK = 0 , MAHONY_NULL_POINTER , MAHONY_INVALID_QUATERNION , MAHONY_NONFINITE_GAIN ,
  MAHONY_NEGATIVE_GAIN , MAHONY_INVALID_GYRO , MAHONY_INVALID_DELTA_TIME
}
 Mahony attitude filter status codes.

Functions

MAHONY_STATUS mahony_init (MAHONY_FILTER *filter, QUAT initial_attitude, float proportional_gain, float integral_gain)
 Initializes a Mahony attitude filter.
MAHONY_STATUS mahony_update_gyro (MAHONY_FILTER *filter, VECTOR_3F gyro_body_rad_s, float delta_time_s)
 Propagates attitude using body-frame gyroscope measurements.
MAHONY_STATUS mahony_update_imu (MAHONY_FILTER *filter, VECTOR_3F gyro_body_rad_s, VECTOR_3F accel_body, float delta_time_s, bool use_accel)
 Updates attitude using gyroscope propagation and accelerometer proportional feedback.

Detailed Description

: Mahony attitude filter interface.

Copyright (c) 2025 Sun Devil Rocketry. All rights reserved.

This software is licensed under terms that can be found in the LICENSE file in the root directory of this software component. If no LICENSE file comes with this software, it is covered under the BSD-3-Clause.

https://opensource.org/license/bsd-3-clause

==============================================================================
                     ##### Mahony filter features #####
==============================================================================
[..]
(+) Quaternion-based gyroscope attitude propogation
(+) Accelerometer proportional and integral feedback filtering
******************************************************************************

Function Documentation

◆ mahony_init()

MAHONY_STATUS mahony_init ( MAHONY_FILTER * filter,
QUAT initial_attitude,
float proportional_gain,
float integral_gain )

Initializes a Mahony attitude filter.

Parameters
filterFilter instance to initialize.
initial_attitudeInitial body-to-world attitude quaternion.
proportional_gainProportional correction gain.
integral_gainIntegral correction gain.
Returns
MAHONY_OK when initialization succeeds; otherwise a Mahony status code describing the failure.

◆ mahony_update_gyro()

MAHONY_STATUS mahony_update_gyro ( MAHONY_FILTER * filter,
VECTOR_3F gyro_body_rad_s,
float delta_time_s )

Propagates attitude using body-frame gyroscope measurements.

Verify the filter, gyro, and timestep are valid. Convert the body-frame angular velocity into a pure quaternion. Use the quaternion differential equation to calculate how quickly the body-to-world attitude is changing. Multiply that derivative by the timestep, add it to the current attitude, and normalize the result.

Parameters
filterInitialized filter instance.
gyro_body_rad_sBody-frame angular velocity in radians per second.
delta_time_sElapsed time in seconds.
Returns
MAHONY_OK when initialization succeeds; otherwise a Mahony status code describing the failure.

◆ mahony_update_imu()

MAHONY_STATUS mahony_update_imu ( MAHONY_FILTER * filter,
VECTOR_3F gyro_body_rad_s,
VECTOR_3F accel_body,
float delta_time_s,
bool use_accel )

Updates attitude using gyroscope propagation and accelerometer proportional feedback.

The gyroscope must be expressed in the body frame in radians per second. The accelerometer must be expressed in the body frame. Its magnitude is removed internally because the filter uses only its measured direction.

Accelerometer feedback is applied only when the caller enables it and the measured acceleration magnitude falls within the configured validity range. Invalid accelerometer samples are ignored while gyro propagation continues.

Parameters
filterInitialized filter instance.
gyro_body_rad_sBody-frame angular velocity in radians per second.
accel_bodyBody-frame accelerometer measurement.
delta_time_sElapsed time in seconds.
use_accelWhether accelerometer feedback should be applied.
Returns
MAHONY_OK when the attitude was updated; otherwise a Mahony status code describing the failure.