GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/sot/core/madgwickahrs.hh Lines: 2 2 100.0 %
Date: 2023-03-13 12:09:37 Branches: 0 0 - %

Line Branch Exec Source
1
//========================================================================
2
//
3
// Implementation of Madgwick's IMU and AHRS algorithms.
4
// See: http://www.x-io.co.uk/node/8#open_source_ahrs_and_imu_algorithms
5
//
6
// Date       Author        Notes
7
// 29/09/2011 SOH Madgwick  Initial release
8
// 02/10/2011 SOH Madgwick  Optimised for reduced CPU load
9
// 11/05/2017 T Flayols     Make it a dynamic graph entity
10
// 26/03/2019 G Buondonno   Converted to double
11
//
12
//========================================================================
13
14
/*
15
 * Copyright 2017, Thomas Flayols, LAAS-CNRS
16
 *
17
 * This file is part of sot-torque-control.
18
 * sot-torque-control is free software: you can redistribute it and/or
19
 * modify it under the terms of the GNU Lesser General Public License
20
 * as published by the Free Software Foundation, either version 3 of
21
 * the License, or (at your option) any later version.
22
 * sot-torque-control is distributed in the hope that it will be
23
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Lesser General Public License for more details.  You should
26
 * have received a copy of the GNU Lesser General Public License along
27
 * with sot-torque-control.  If not, see <http://www.gnu.org/licenses/>.
28
 */
29
30
#ifndef __sot_torque_control_madgwickahrs_H__
31
#define __sot_torque_control_madgwickahrs_H__
32
33
/* --------------------------------------------------------------------- */
34
/* --- API ------------------------------------------------------------- */
35
/* --------------------------------------------------------------------- */
36
37
#if defined(WIN32)
38
#if defined(madgwickahrs_EXPORTS)
39
#define SOTMADGWICKAHRS_EXPORT __declspec(dllexport)
40
#else
41
#define SOTMADGWICKAHRS_EXPORT __declspec(dllimport)
42
#endif
43
#else
44
#define SOTMADGWICKAHRS_EXPORT
45
#endif
46
47
/* --------------------------------------------------------------------- */
48
/* --- INCLUDE --------------------------------------------------------- */
49
/* --------------------------------------------------------------------- */
50
51
#include <dynamic-graph/signal-helper.h>
52
53
#include <map>
54
#include <sot/core/matrix-geometry.hh>
55
56
#include "boost/assign.hpp"
57
58
#define betaDef 0.01  // 2 * proportional g
59
60
namespace dynamicgraph {
61
namespace sot {
62
/** \addtogroup Filters
63
\section subsec_madgwickahrs MadgwickAHRS filter
64
\class MadgwickARHS
65
This class implements the MadgwickAHRS filter as described
66
in http://x-io.co.uk/res/doc/madgwick_internal_report.pdf
67
This method uses a gradient descent approach to compute the orientation
68
from an IMU.
69
70
The signals input are:
71
<ul>
72
<li>m_accelerometerSIN: \f$[a_x, a_y, a_z]^T\f$ in \f$m.s^{-2}\f$</li>
73
<li>m_gyroscopeSIN: \f$[g_x, g_y, g_z]^T\f$ in \f$rad.s^{-1}\f$</li>
74
<li>m_imu_quatSOUT: \f$[q_0, q_1, q_2, q_3]^T\f$ </li> estimated rotation
75
as a quaternion</li>
76
</ul>
77
78
The internal parameters are:
79
<ul>
80
<li>\f$Beta\f$: Gradient step weight (default to 0.01) </li>
81
<li>\f$m_sampleFref\f$: Sampling Frequency computed from the control
82
period when using init.</li>
83
</ul>
84
*/
85
class SOTMADGWICKAHRS_EXPORT MadgwickAHRS : public ::dynamicgraph::Entity {
86
  typedef MadgwickAHRS EntityClassName;
87
3
  DYNAMIC_GRAPH_ENTITY_DECL();
88
89
 public:
90
2
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
91
92
  /* --- CONSTRUCTOR ---- */
93
  MadgwickAHRS(const std::string &name);
94
95
  void init(const double &dt);
96
  void set_beta(const double &beta);
97
98
  /// Set the quaternion as [w,x,y,z]
99
  void set_imu_quat(const dynamicgraph::Vector &imu_quat);
100
101
  /* --- SIGNALS --- */
102
  /// ax ay az in m.s-2
103
  DECLARE_SIGNAL_IN(accelerometer, dynamicgraph::Vector);
104
  /// gx gy gz in rad.s-1
105
  DECLARE_SIGNAL_IN(gyroscope, dynamicgraph::Vector);
106
  /// Estimated orientation of IMU as a quaternion
107
  DECLARE_SIGNAL_OUT(imu_quat, dynamicgraph::Vector);
108
109
 protected:
110
  /* --- COMMANDS --- */
111
  /* --- ENTITY INHERITANCE --- */
112
  virtual void display(std::ostream &os) const;
113
114
  /* --- METHODS --- */
115
  double invSqrt(double x);
116
  void madgwickAHRSupdateIMU(double gx, double gy, double gz, double ax,
117
                             double ay, double az);
118
119
 protected:
120
  /// true if the entity has been successfully initialized
121
  bool m_initSucceeded;
122
  /// 2 * proportional gain (Kp)
123
  double m_beta;
124
  /// quaternion of sensor frame
125
  double m_q0, m_q1, m_q2, m_q3;
126
  /// sample frequency in Hz
127
  double m_sampleFreq;
128
129
};  // class MadgwickAHRS
130
}  // namespace sot
131
}  // namespace dynamicgraph
132
133
#endif  // #ifndef __sot_torque_control_madgwickahrs_H__