M5Unit-IMU 0.0.2 git rev:e5b00aa
Loading...
Searching...
No Matches
mahony.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_IMU_IMU_FILTER_MAHONY_HPP
11#define M5_UNIT_IMU_IMU_FILTER_MAHONY_HPP
12
13#include <math/quaternion.hpp>
14#include <math/vector3.hpp>
15
16#include "filter_base.hpp"
17
18namespace m5 {
19namespace imu {
20
34public:
35 MahonyFilter() = default;
36
37 virtual void reset() override;
38 virtual void update(const float dt, const float accel[3], const float gyro[3], const float* mag) override;
39 virtual void quaternion(float& w, float& x, float& y, float& z) const override;
40
46 bool writeKp(const float kp);
48 inline float readKp() const
49 {
50 return _kp;
51 }
52
60 bool writeKi(const float ki);
62 inline float readKi() const
63 {
64 return _ki;
65 }
66
78 bool writeQuaternion(const float w, const float x, const float y, const float z);
79
80private:
81 m5::math::Quaternion _q{}; // Current attitude estimate (identity by default)
82 m5::math::Vector3 _integral_error{}; // Integral feedback accumulator (sum of e * dt)
83 float _kp{1.0f}; // Proportional feedback gain
84 float _ki{0.0f}; // Integral feedback gain
85};
86
87} // namespace imu
88} // namespace m5
89#endif
Framework-independent contract shared by every AHRS attitude filter.
Definition filter_base.hpp:81
Explicit complementary filter with proportional-integral feedback (Mahony et al. 2008)
Definition mahony.hpp:33
float readKi() const
Current integral feedback gain.
Definition mahony.hpp:62
bool writeKi(const float ki)
Set the integral feedback gain.
Definition mahony.cpp:150
virtual void reset() override
Reset the internal attitude estimate (and any filter state) to identity.
Definition mahony.cpp:36
virtual void update(const float dt, const float accel[3], const float gyro[3], const float *mag) override
Fuse one IMU sample into the attitude estimate.
Definition mahony.cpp:42
float readKp() const
Current proportional feedback gain.
Definition mahony.hpp:48
virtual void quaternion(float &w, float &x, float &y, float &z) const override
Current fused attitude.
Definition mahony.cpp:133
bool writeKp(const float kp)
Set the proportional feedback gain.
Definition mahony.cpp:141
bool writeQuaternion(const float w, const float x, const float y, const float z)
Overwrite the current attitude estimate directly.
Definition mahony.cpp:159
Common contract for AHRS attitude filters.
Top level namespace of M5Stack.
Definition ahrs_controller.cpp:26
Minimal quaternion arithmetic for attitude representation (framework independent)
Plain Hamilton quaternion (float) used to represent attitude.
Definition quaternion.hpp:31
Plain 3D vector (float)
Definition vector3.hpp:25
Minimal 3D vector arithmetic (framework independent)