Skip to content

(Updated) Implement Mahony filter for attitude estimation- #142 - #156

Merged
NArmistead merged 53 commits into
feature/gravity-compensated-accelerationfrom
bb-na/mahony-attitude-filter
Sep 29, 2026
Merged

NArmistead merged 53 commits into
feature/gravity-compensated-accelerationfrom
bb-na/mahony-attitude-filter

Conversation

@NArmistead

@NArmistead NArmistead commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

This description is from @bjornhbengtsson's PR

My changes were to flip the sign on z-axis gravity in a lot of places and disable yaw correction.
Child: FCF

Description

Implements the Mahony attitude filter for quaternion based attitude estimation.

The filter maintains a body-to-world attitude quaternion using body frame gyroscope propagation with proportional and integral accelerometer feedback.

Made with assistance of ChatGPT 5.6.

Changes include:

  • Added a standalone Mahony attitude filter module
  • Added gyroscope based quaternion propagation
  • Added proportional accelerometer correction
  • Added integral gyro bias correction
  • Added per axis integral anti windup limits
  • Added accelerometer magnitude validity gating
  • Added support for externally enabling or disabling accelerometer correction
  • Added gyro only fallback when accelerometer samples are disabled or invalid
  • Added input validation and quaternion normalization
  • Integrated the Mahony attitude estimate into sensor state estimation
  • Added gain selection rationale and implementation comments
  • Added diagnostic Euler angle and quaternion output tests
  • Added comprehensive Mahony unit tests
  • Converted the stored attitude and quaternion rotation helpers to a body-to-world convention

Issue Link

Depends on #138
#138

Closes #141
#141

Testing

  • Unit tests added

Mahony unit test results:
image
-------------Test Complete--------------

Passes: 140
Fails: 0
Result: PASS

Math test results:

-------------Test Complete--------------

Passes: 26
Fails: 0
Result: PASS

Other

Accelerometer correction is only applied when:

  • the caller enables accelerometer feedback
  • the sample contains finite values
  • the measured acceleration magnitude lies within the configured near 1 g validity range

When accelerometer feedback is disabled or rejected, the filter continues using gyroscope only propagation.

The current accelerometer thresholds, proportional gain, integral gain, and integral limit are initial software values. Final tuning should be performed using hardware characterization, vibration testing, and flight data.

Deferred work includes:

  • Final flight state accelerometer correction policy
  • Hardware and flight data gain tuning
  • Additional correction validity telemetry
  • Calibrated magnetometer feedback for yaw correction

Reviewer Checklist

Standards

  • Follows FCF Architectural Standards
  • Follows SDR Coding Standards
  • Code complexity/function Size is minimized
  • Code is testable
  • Code is readable and commented properly
  • License terms are respected

Error Handling

  • Potentially unsafe functions return a status code
  • Error returns properly handled

Memory

  • Stack allocated memory is scoped correctly
  • Heap allocated memory is avoided
  • Globally allocated memory is minimized except when necessary
  • Pointers are used correctly
  • Concurrency has been considered

Performance

  • Rate limiters are respected
  • Busy waiting is avoided
  • "Delay" calls are not used in performance sensitive code

@NArmistead NArmistead changed the title Bb na/mahony attitude filter (Updated) Implement Mahony filter for attitude estimation- #142 Sep 25, 2026
@NArmistead
NArmistead added this pull request to stack #157 September 25, 2026 08:05
@NArmistead
NArmistead requested a review from ETSells September 25, 2026 09:02
@NArmistead

Copy link
Copy Markdown
Member Author

I still need to fix a bunch of unit tests because everything broke

@ETSells

ETSells commented Sep 26, 2026

Copy link
Copy Markdown
Member

@NArmistead Please rebase the base of this stack as too many intermediate revisions have been picked up for me to review.

@ETSells ETSells left a comment

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.

Let's do a rebase before I can pick this up. There's too many intermediate revs since the base of this stack is too far back.

Alternatively, merge main into both branches (or just the base) to take out all of these intermediate revs.

@NArmistead
NArmistead force-pushed the bb-na/mahony-attitude-filter branch from 935e1c6 to b7dce3f Compare September 27, 2026 22:36
@NArmistead

Copy link
Copy Markdown
Member Author

that was horrible and it probably broke something

@ETSells ETSells left a comment •

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.

ship this, damn. insanely good work as always nick

as part of QA for mod, we'll want to add system requirements and verification.

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.

shouldnt be needed, test framework gitignore should handle this

Comment thread mahony/mahony.c Outdated
@@ -0,0 +1,311 @@
/**
******************************************************************************
* @file : mahony.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.

nit: .c

Comment thread mahony/mahony.c
*/
MAHONY_STATUS mahony_init
(
MAHONY_FILTER *filter,

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.

this is so based alexa play despacito

this is a phenomenal example of what we should do for libraries

Comment thread mahony/mahony.c Outdated
}

if ( !isfinite(proportional_gain)
|| !isfinite(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: my pref for conditionals is to have each predicate start at the same column UNLESS its nested

Comment thread mahony/mahony.c
*/
if ( apply_accel && vector_normalize(&accel_body) )
{
/*

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: alignment

Comment thread mahony/mahony.c

/*
* Accelerometer feedback is optional. If the measurement is invalid or has
* zero magnitude, continue with gyroscope-only propagation.

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.

love the robustness here, W

Comment thread sensor/sensor.c
* Converted gyro data is in degrees per second. Mahony requires radians per
* second.
*/
gyro_body_rad_s.x = deg_to_rad(imu_converted->gyro_x);

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.

is there a reason why our internal unit is not consistent across APIs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

we get degrees per sec from gyro, but honestly we should just convert it to radians and use that consistently

@NArmistead

Copy link
Copy Markdown
Member Author

Just tested it and it works, so im gonna ship it. Most of the actual work here was done by @bjornhbengtsson so credit to him

This will break CI for a bit though

@NArmistead

Copy link
Copy Markdown
Member Author

actually this is breaking the emulator build somehow

@NArmistead
NArmistead merged commit 08ad7a1 into main Sep 29, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sensor: Implement Mahony filter for attitude estimations

3 participants