Instrument Neutral Distributed Interface INDI 2.2.5
imusensorfusion.h
Go to the documentation of this file.
1/*
2 IMU Sensor Fusion - Madgwick and Mahony AHRS filters
3 Copyright (C) 2026 Jasem Mutlaq ([email protected])
4
5 Based on:
6 - Madgwick, S.O.H. (2010). "An efficient orientation filter for inertial and inertial/magnetic sensor arrays"
7 - Mahony, R. et al. (2008). "Nonlinear Complementary Filters on the Special Orthogonal Group"
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22*/
23
24#pragma once
25
26#include <cmath>
27
28namespace INDI
29{
30
44{
45 public:
46 virtual ~IMUSensorFusion() = default;
47
63 virtual void update(double gx, double gy, double gz,
64 double ax, double ay, double az,
65 double mx, double my, double mz,
66 double dt, bool useMag = true) = 0;
67
75 virtual void getQuaternion(double &w, double &i, double &j, double &k) const = 0;
76
80 virtual void reset() = 0;
81};
82
83// ─────────────────────────────────────────────────────────────────────────────
100{
101 public:
107 explicit MadgwickFilter(double beta = 0.1);
108 virtual ~MadgwickFilter() = default;
109
110 void update(double gx, double gy, double gz,
111 double ax, double ay, double az,
112 double mx, double my, double mz,
113 double dt, bool useMag = true) override;
114
115 void getQuaternion(double &w, double &i, double &j, double &k) const override;
116
117 void reset() override;
118
120 void setBeta(double beta)
121 {
122 m_beta = beta;
123 }
124 double getBeta() const
125 {
126 return m_beta;
127 }
128
129 private:
130 double m_beta;
131 double m_q0, m_q1, m_q2, m_q3;
132
133 // 9-DOF update (with magnetometer)
134 void update9DOF(double gx, double gy, double gz,
135 double ax, double ay, double az,
136 double mx, double my, double mz,
137 double dt);
138
139 // 6-DOF update (without magnetometer)
140 void update6DOF(double gx, double gy, double gz,
141 double ax, double ay, double az,
142 double dt);
143};
144
145// ─────────────────────────────────────────────────────────────────────────────
163{
164 public:
169 explicit MahonyFilter(double kp = 2.0, double ki = 0.005);
170 virtual ~MahonyFilter() = default;
171
172 void update(double gx, double gy, double gz,
173 double ax, double ay, double az,
174 double mx, double my, double mz,
175 double dt, bool useMag = true) override;
176
177 void getQuaternion(double &w, double &i, double &j, double &k) const override;
178
179 void reset() override;
180
182 void setKp(double kp)
183 {
184 m_kp = kp;
185 }
186 double getKp() const
187 {
188 return m_kp;
189 }
190
192 void setKi(double ki)
193 {
194 m_ki = ki;
195 }
196 double getKi() const
197 {
198 return m_ki;
199 }
200
201 private:
202 double m_kp, m_ki;
203 double m_q0, m_q1, m_q2, m_q3;
204 double m_integralFBx, m_integralFBy, m_integralFBz;
205};
206
207} // namespace INDI
Abstract base class for IMU sensor fusion algorithms.
virtual void getQuaternion(double &w, double &i, double &j, double &k) const =0
Return the current orientation quaternion.
virtual ~IMUSensorFusion()=default
virtual void reset()=0
Reset the filter to the identity quaternion.
virtual void update(double gx, double gy, double gz, double ax, double ay, double az, double mx, double my, double mz, double dt, bool useMag=true)=0
Perform one filter update step.
Madgwick AHRS filter (9-DOF and 6-DOF).
void getQuaternion(double &w, double &i, double &j, double &k) const override
Return the current orientation quaternion.
void update(double gx, double gy, double gz, double ax, double ay, double az, double mx, double my, double mz, double dt, bool useMag=true) override
Perform one filter update step.
void reset() override
Reset the filter to the identity quaternion.
virtual ~MadgwickFilter()=default
void setBeta(double beta)
Set the filter gain β.
Mahony AHRS filter (9-DOF and 6-DOF).
void reset() override
Reset the filter to the identity quaternion.
void setKp(double kp)
Set proportional gain Kp.
void setKi(double ki)
Set integral gain Ki.
double getKi() const
virtual ~MahonyFilter()=default
double getKp() const
void getQuaternion(double &w, double &i, double &j, double &k) const override
Return the current orientation quaternion.
void update(double gx, double gy, double gz, double ax, double ay, double az, double mx, double my, double mz, double dt, bool useMag=true) override
Perform one filter update step.
Namespace to encapsulate INDI client, drivers, and mediator classes.