Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
35bccef
Start Mahony attitude filter with gyro propagation
bjornhbengtsson Jul 24, 2026
41be529
Add proportional accelerometer correction to Mahony filter
bjornhbengtsson Jul 25, 2026
c21ff34
Add Mahony attitude diagnostic output tests
bjornhbengtsson Jul 25, 2026
d47061a
Add accelerometer validity gating to Mahony filter
bjornhbengtsson Jul 25, 2026
a770955
Add integral correction and anti-windup to Mahony filter
bjornhbengtsson Jul 25, 2026
eaa16ed
Integrate Mahony filter into sensor state estimation
bjornhbengtsson Jul 26, 2026
3e82cb8
Document Mahony gain tuning rationale
bjornhbengtsson Jul 27, 2026
4fe0097
Convert Mahony attitude to world-to-body convention
bjornhbengtsson Aug 3, 2026
5677f33
Merge remote-tracking branch 'origin/feature/gravity-compensated-acce…
bjornhbengtsson Aug 3, 2026
f911c2d
Update Mahony tests for renamed framework
bjornhbengtsson Aug 3, 2026
13ce68c
Revert "Convert Mahony attitude to world-to-body convention"
bjornhbengtsson Aug 7, 2026
ee214a3
Add Mahony status enum
bjornhbengtsson Aug 9, 2026
c8efc99
Handle Mahony status in sensor integration
bjornhbengtsson Aug 9, 2026
c9858e9
Fix Mahony status check in release build
bjornhbengtsson Aug 9, 2026
d4e0e78
Sync sensor orientation and filter cleanup
bjornhbengtsson Aug 14, 2026
8189fd0
Propagate Mahony update failures
bjornhbengtsson Aug 14, 2026
e15f587
Make identity quaternion const
bjornhbengtsson Aug 14, 2026
870a43f
Format Mahony logical operators
bjornhbengtsson Aug 14, 2026
d292452
Document Mahony helper functions
bjornhbengtsson Aug 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
506 changes: 506 additions & 0 deletions mahony/mahony.c

Large diffs are not rendered by default.

162 changes: 162 additions & 0 deletions mahony/mahony.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*******************************************************************************

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of this file uses the new doxygen convention except for the header. The header must stay uniform.

*
* FILE:
* mahony.h
*
* DESCRIPTION:
* Mahony attitude filter interface.
*
******************************************************************************/

#ifndef MAHONY_H
#define MAHONY_H

#ifdef __cplusplus
extern "C"
{
#endif

/*------------------------------------------------------------------------------
Standard Includes
------------------------------------------------------------------------------*/
#include <stdbool.h>

/*------------------------------------------------------------------------------
Project Includes
------------------------------------------------------------------------------*/
#include "math_sdr.h"

/*------------------------------------------------------------------------------
Typedefs
------------------------------------------------------------------------------*/

/**
* @brief Three-dimensional floating-point vector.
*/
typedef struct _VECTOR_3F
{
float x;
float y;
float z;
} VECTOR_3F;

/**
* @brief Mahony attitude filter status codes.
*/
typedef enum
{
MAHONY_OK = 0, //success (SDR convention)
MAHONY_NULL_POINTER, //filter == NULL
MAHONY_INVALID_QUATERNION, //attitude contains NaN/Inf
MAHONY_NONFINITE_GAIN, //Kp or Ki is NaN/Inf
MAHONY_NEGATIVE_GAIN, //Kp or Ki is below zero
MAHONY_INVALID_GYRO, //gyro contains NaN/Inf
MAHONY_INVALID_DELTA_TIME //dt is NaN/Inf, zero, or negative
// no MAHONY_INVALID_ACCEL because Invalid acceleration is..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: SDR considers "//" comments to be temporary comments. Even if on one line, please use /* */ comments.

// deliberately treated as "don't use accel correction; continue gyro-only,"..
// not as a failed Mahony update.
} MAHONY_STATUS;

/**
* @brief State and gains for a Mahony attitude filter.
*/
typedef struct _MAHONY_FILTER
{
/**
* Body-to-world attitude quaternion.
*/
QUAT attitude;

/**
* Accumulated attitude error used for integral gyro-bias correction.
*/
VECTOR_3F integral_error;

float proportional_gain;
float integral_gain;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space

} MAHONY_FILTER;

/*------------------------------------------------------------------------------
Function Prototypes
------------------------------------------------------------------------------*/

/**
* @brief Initializes a Mahony attitude filter.
*
* @param filter Filter instance to initialize.
* @param initial_attitude Initial body-to-world attitude quaternion.
* @param proportional_gain Proportional correction gain.
* @param integral_gain Integral correction gain.
*
* @return MAHONY_OK when initialization succeeds; otherwise a Mahony status
* code describing the failure.
*/
MAHONY_STATUS mahony_init
(
MAHONY_FILTER *filter,
QUAT initial_attitude,
float proportional_gain,
float integral_gain
);

/**
* @brief Propagates attitude using body-frame gyroscope measurements.
*
* The attitude quaternion represents the body-to-world rotation. The angular
* velocity vector must be expressed in the body frame in radians per second.
*
* @param filter Initialized filter instance.
* @param gyro_body_rad_s Body-frame angular velocity in radians per second.
* @param delta_time_s Elapsed time in seconds.
*
* @return MAHONY_OK when initialization succeeds; otherwise a Mahony status
code describing the failure.
*/
MAHONY_STATUS mahony_update_gyro
(
MAHONY_FILTER *filter,
VECTOR_3F gyro_body_rad_s,
float delta_time_s
);

/**
* @brief 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.
*
* @param filter Initialized filter instance.
* @param gyro_body_rad_s Body-frame angular velocity in radians per second.
* @param accel_body Body-frame accelerometer measurement.
* @param delta_time_s Elapsed time in seconds.
* @param use_accel Whether accelerometer feedback should be applied.
*
* @return MAHONY_OK when the attitude was updated; otherwise a Mahony status
code describing the failure.
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove space

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
);

#ifdef __cplusplus
}
#endif

#endif /* MAHONY_H */

/*******************************************************************************
* END OF FILE
******************************************************************************/
4 changes: 3 additions & 1 deletion math_sdr/math_sdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ extern "C" {
/*------------------------------------------------------------------------------
Includes
------------------------------------------------------------------------------*/
#include <string.h>
#include <stdbool.h>
#include <stddef.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these additional defs necessary?

#include <stdint.h>
#include <string.h>


/*------------------------------------------------------------------------------
Expand Down
Loading