-
Notifications
You must be signed in to change notification settings - Fork 2
Implement Mahony filter for attitude estimation #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
35bccef
41be529
c21ff34
d47061a
a770955
eaa16ed
3e82cb8
4fe0097
5677f33
f911c2d
13ce68c
ee214a3
c8efc99
c9858e9
d4e0e78
8189fd0
e15f587
870a43f
d292452
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /******************************************************************************* | ||
| * | ||
| * 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.. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
| */ | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ******************************************************************************/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,8 +41,10 @@ extern "C" { | |
| /*------------------------------------------------------------------------------ | ||
| Includes | ||
| ------------------------------------------------------------------------------*/ | ||
| #include <string.h> | ||
| #include <stdbool.h> | ||
| #include <stddef.h> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are these additional defs necessary? |
||
| #include <stdint.h> | ||
| #include <string.h> | ||
|
|
||
|
|
||
| /*------------------------------------------------------------------------------ | ||
|
|
||
There was a problem hiding this comment.
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.