1. Introduction
The accurate estimation of the orientation of a rigid body, relative to an inertial frame, is required for a wide range of applications. For the purpose of navigation, such estimation has employed high precision inertial and magnetic sensors, but the recent development of low-cost and light-weight MicroElectroMechanical Systems (MEMS) has allowed smaller and cheaper inertial sensors to be adopted for a wider range of applications and even in the daily use of consumer electronics such as game consoles and mobile devices. In robotics, the evolution of Micro Aerial Vehicles (MAVs) has increased in the last decade, and research is moving toward their full autonomization. Navigation and control of MAVs are possible thanks to the MEMS-based Inertial Measurement Units (IMU) that meet the MAVs’ limited size and payload requirement.
Data provided by low-cost IMU is affected by high noise levels and time-varying biases. Therefore, a sensor-fusion algorithm must be used to process the data to obtain a smooth and bias-free estimation of the orientation maintaining a low computational cost for running on the onboard processor. The orientation can be generally represented in three principal forms: Euler angles, quaternion, and Direction Cosine Matrix (DCM). The orientation in Euler form is expressed using three angles; it is conceptually easy to understand, but may reach a singularity state commonly referred as “gimbal lock”. DCM and quaternion do not incur a singularity state but the DCM represents the orientation by a 3 × 3 matrix. Furthermore, the quaternion representation offers a linear formulation of the orientation dynamics.
In this article, we propose a deterministic approach to solve Whaba’s problem [
1] given gravity and magnetic field observations provided by the MARG sensor. This method returns an estimation of the attitude in quaternion form without leading to ambiguous results and avoiding singularity problems. Moreover, it does not need a predefined knowledge of the magnetic field direction. We also propose a new approach to a complementary filter for fusing together gyroscope data with acceleration and magnetic field measurements from IMU and MARG sensors to obtain an orientation estimation in quaternion form. This algorithm is meant to be used onboard MAVs because of its low computational burden, robustness, and fast convergence. However, we believe our contribution is applicable beyond the field of MAV and aerial robotics.
This article is organized as follows.
Section 2 explores the literature of attitude estimation methods previously proposed as solution to Whaba’s problem. Further, in the same section, we briefly survey several fusion algorithms that exploit the measurements given by gyroscope, accelerometer, and magnetometer of an IMU/MARG sensor, to obtain a more accurate evaluation of the orientation.
Section 3 briefly provides an overview of some properties of unit quaternions and how they can be used to define rotations in the 3D space.
Section 4 analyzes the novel approach taken to obtain the quaternion orientation of a rigid body from earth-field observations as a closed-form solution.
Section 5 explains the novel quaternion-based complementary filter.
Section 6 is dedicated to the experiments and results interpretations, and
Section 7 concludes the article by summarizing the findings.
2. Previous Work
The problem of finding the optimal attitude estimation, given multiple unit vector correspondences across two frames, was formulated by Whaba in 1965 [
1]. Whaba’s problem was meant to estimate the attitude of a satellite by finding the optimal orthogonal matrix (the attitude matrix) that minimizes a least-square loss function using
n numbers of unit vectors pairs where
. Many algorithmic solutions of Whaba’s problem have been proposed. They are generally classified into two categories: deterministic and optimal, according to the popular definition by Wertz [
2]. Deterministic algorithms use a minimal set of data and derive the attitude by solving nonlinear equations, whereas optimal algorithms use more than a minimal set of measurements and compute the attitude by minimizing an appropriate cost function.
A very well known deterministic algorithm is the three axis attitude determination (TRIAD) [
3]. It constructs two triads of orthonormal unit vectors by combining the normalized measurement of two nonparallel reference vectors and provides an estimation of the attitude matrix. QUaternion ESTimator (QUEST) [
3] is a famous optimal algorithm that produces the attitude estimation in quaternion form given a set of 3D reference unit vectors in a fixed frame and their corresponding observations in the local frame. It is based on Davenport’s q-method [
4] that finds the optimal quaternion by minimizing a quadratic gain, which is a transformation of Whaba’s loss function through the parametrization of the orientation matrix by the unit quaternion. Many other techniques have been proposed, such as Singular Value decomposition (SVD) [
5], Polar Decomposition (PD) [
6], Euler-n [
7], Fast Optimal Matrix Algorithm (FOAM) [
8], and Energy Approach Algorithm (EAA) [
9]. All of them produce an optimal attitude estimation and they differ from each other in their computational speed. A complete survey and analysis of attitude estimation methods in quaternion form using vector observation is provided by Markley and Mortari in [
10].
It is well known that in applications based on inertial/magnetic sensors, to estimate the attitude, the two local observations of the earth’s fields (gravity and local magnetic field) are compared to the reference vectors that are supposed to be fixed. This assumption can lead to problems when the local magnetic field is perturbed by ferromagnetic objects or electrical appliances. In these cases, when algorithms such as QUEST are employed, the attitude estimation would be subject to errors not only in the yaw component but also in the pitch and roll. To avoid this problem, Yun
et al. [
11] proposed the Factored Quaternion Algorithm (FQA), which computes the orientation of a rigid body based on earth gravity and magnetic field measurements. The quaternion orientation is estimated by analyzing a series of three sequential rotations. This approach allows to reduce the orientation error, caused by the presence of local magnetic disturbances, only into the error in the yaw component maintaining the accuracy of the QUEST algorithm. To obtain the quaternion orientation from gravity and magnetic field observations, the method presented in this article solves the problem described above by separating the quaternion in only two parts, one for the roll and pitch components and one for the yaw. Both quaternions are found as an algebraic solution of a system instead of the result of an optimization problem. As inertial sensors provide at most the observation of 2 pairs of vectors (given a priori the global-frame gravity and magnetic field vectors), the use of optimal algorithms does not produce any improvement but only reduces the convergence speed of the orientation.
In the class of deterministic methods, Euler-2 by Daniele Mortari [
7] is a mathematical approach to compute the Euler axis whose application is restricted when only two unit-vector pairs are available. Overall, besides the TRIAD algorithm, deterministic attitude-determination algorithms are not very frequent and their study is not extensive. Nonetheless, in IMU applications, only two sets of vector observations are provided, thus even the optimal approaches would have the same level of accuracy as a deterministic method.
To obtain a better estimation of the orientation, acceleration and magnetic field data are fused together with angular rate readings from a gyroscope. Although many approaches have been adopted for filtering gyroscope data with inertial measurements, the most commonly used techniques are Extended Kalman filtering (EKF) and complementary filters. A survey of other nonlinear attitude estimation methods can be found in [
12].
Kalman filtering based techniques adopt a probabilistic determination of the state modeled as a Gaussian distribution given the system’s model. They are widely used in aerospace applications [
13,
14], human motion analysis [
15,
16,
17] and robotics [
18,
19].
A Complementary filter is a common alternative to the EKF because of its simplicity and effectiveness. It uses an analysis in the frequency domain to filter the signals and combine them together to obtain an estimation of the orientation without any statistical description. In UAV applications, the use of complementary filters [
20,
21,
22] is often preferred to the EKF because EKFs can be complicated to implement and the convergence is slower because of the time required for the linear regression iterations.
Most of the recent sensor fusion algorithms for inertial/magnetic sensors provide orientation estimation in quaternion form. Quaternions are a useful mathematical tool that require less computation time because of their minimal number of parameters and do not result in singularity configurations as the Euler representation does. Further, rotations of vectors are simply performed by quaternion multiplications.
Marins
et al. [
23] propose two different Kalman filter approaches to estimate the orientation in quaternion form from a MARG sensor. Both methods have the same 7-state (angular velocities and quaternion) process model but different measurement models. The first measurement model uses each MARG output in a 9-element measurement vector resulting in a complicated EKF. The second approach uses an external Gauss-Newton algorithm to directly estimate the quaternion measurement that will be part, along with the angular velocities, of the measurement vector. In this case, the relation between the process and measurement model is linear allowing the use of a simpler Kalman Filter. The quaternion-based EKF presented by Sabatini [
16] is similar to the first approach of Marins
et al. [
23], but the state is composed by the unit quaternion augmented with accelerometer and magnetometer bias vectors for on-line calibration. In [
24] Sabatini presents a similar EKF where the state is augmented with magnetic distortions vector, modeled as a Gauss-Markov process, to reduce the heading drift in magnetically non-homogeneous environments. Choukroun
et al. [
14] present a novel linear pseudo-measurement model that combined with the linear measurement model, yields a linear Kalman Filter algorithm that eliminates the linearization procedure of an EKF. Bachman
et al. [
25] present an efficient quaternion-based complementary filter for human-body-motion tracking.
Euston
et al. [
21] present a nonlinear quaternion-based complementary filter to estimate the attitude of a UAV given measurement from a low-cost IMU. The filter is augmented by a first-order model of the vehicle dynamics to compensate for external centripetal acceleration. In the work by Madgwick
et al. [
26], a constant gain filter is adopted to estimate the attitude in quaternion form of a rigid body by using data from a MARG sensor. A first quaternion estimation is obtained by gyroscope output integration and then it is corrected by a quaternion from the accelerometer and magnetometer data computed through a gradient descent algorithm. Madgwick’s method ensures good attitude estimation at low computational cost. Further, it addresses the problem of the local magnetic disturbances that, when present, affect all the orientation components (roll, pitch, and yaw). By reducing the constraint of the magnetic field vector rotation, it is able to limit the effect of the magnetic disturbances to only affect the yaw component of the orientation. The last two constant gain filters, by Euston
et al. [
21], and by Madgwick
et al. [
26], are commonly used because they offer good performances at low computational cost and a comparative analysis of the two algorithms is presented in [
27]. The adaptive-gain complementary filter proposed by Tian
et al. [
28] fuses a quaternion estimation from fast moving gyroscope signal with a quaternion, from slow moving accelerometer and magnetometer signals, computed through a Gauss-Newton algorithm. For more robust results, the gain is adaptively adjusted according to the convergence rate of the low-frequency estimation and the divergence rate of the high-frequency estimation. Fourati
et al. [
29] combine the output of a Levenberg-Marquardt algorithm, the inputs for which are acceleration and local magnetic field measurements, with the angular rate measurements in a complementary observer based on the multiplicative correction technique.
To improve the performance of rigid body orientation estimation from low cost IMU/MARG under dynamic motion, different approaches can be adopted. A simple switching method, as the one proposed in the complementary filter presented in [
30], can be used to determine the gain value under varying dynamics sensed by the accelerometer. Another common method includes an adaptive measurement noise covariance matrix that, in a Kalman filter framework, can be tuned adaptively to yield optimal performance during the dynamic periods as proved in [
31,
32]. Alternatively, the acceleration can be modeled to directly estimate the external acceleration and thus used to reduce the attitude error, as in the Kalman filter proposed in [
33].
The quaternion-based complementary filter proposed in this article can be used for both IMU and MARG sensors. It ensures fast convergence and robustness thanks to the analytical derivation of the correction quaternion. We address the problem of the magnetic disturbances by separating the quaternion corrections in two different correction sequences and adopting the method presented in [
26] that also makes the algorithm independent on external parameters. Further, we adopt an adaptive-gain approach to reduce the estimation error during high dynamic motion.
3. Background Theory
Any arbitrary orientation in the 3D space of a frame
A with respect to a frame
B can be represented by a unit quaternion
defined as following:
where α is the rotation angle and
e is the unit vector that represents the rotation axis. The quaternion conjugate of
, given its unit norm, is equivalent to the inverse quaternion and describes the inverse rotation. Therefore, the conjugate quaternion can be used to represent the orientation of frame
B relative to frame
A, as defined below.
The orientation quaternion after a sequence of rotations can be easily found by quaternion multiplication where each quaternion represents the orientation of a frame with respect to the rotated one. For example, given three frames
A,
B and
C, and given the quaternion
orientation of frame
A expressed with respect to frame
B and given the quaternion
orientation of frame
B expressed with respect to frame
C, the orientation of frame
A with respect to frame
C is characterized by:
where quaternion multiplication, given two quaternions
p and
q, is defined as
Unit quaternions can be applied to operate rotations of 3D vectors. For example a vector
Av, expressed with respect to the
A frame, can be expressed with respect to the
B frame by the following operation:
where the symbol ⊗ indicates the quaternion multiplication, and
Avq and
Bvq are the observations of the vector
v, in the two reference frames, written as pure quaternions as shown in Equation (
6).
The inverse rotation that describes the vector
Bv relative to the frame
A can be easily found by using the property of the conjugate quaternion and it is presented in Equation (
7).
The rotation defined in Equation (
5) can be written in matrix form as in Equation (
8)
where
, which belongs to the 3D special orthogonal group
, is the direct cosine matrix (DCM) given in terms of the orientation quaternion
as shown below.
Given the properties of the elements of the
, the inverse rotation can be defined as:
4. Quaternion from Earth-Field Observations
In this section, we analyze the algebraic derivation of a quaternion from the observation of the earth’s fields. For a clear understanding of the following derivation, let us first define a notation that will be used throughout this article. We refer to the local (sensor) frame as
L, and the global (earth) frame as
G. We can define the measured acceleration
and the true earth gravitational acceleration
as the unit vectors:
Similarly, we define the measured local magnetic field
and the true magnetic field
as the unit vectors:
Finally, the gyroscopes measure the angular velocity
around the three sensor frame axes:
Note that most IMUs usually measure a non-normalized vector a and m. However, for the purposes of derivation in this article, we assume that the quantities have been normalized. The only relevant units are those of ω, which we assume are radians per second.
We present an algebraic derivation of the orientation quaternion
, of the global frame
relative to the local frame
, as a function of
and
. We have two independent sensors observing two independent fields; a straightforward way to formulate the quaternion is through the inverse orientation which rotates the measured quantities
and
into the reference quantities
and
:
This system, however, is overdetermined - each of the two equations provides two independent constraints on the orientation , whereas the orientation only has three degrees of freedom. In the case when there is a disagreement between the gravitational and magnetometer readings, the system will not have a solution. The disagreement could arise from random sensor noise or unmodeled field disturbances (nongravitational accelerations or magnetic field variations). A possible solution would be to define an error metric and find the quaternion which minimizes this error. However, this could still result in disturbances in the magnetic field affecting the roll and pitch, which we are trying to avoid.
To address this problem, we present a modified system of equations. The definition of the system (but not its solution) is based on the approach presented in [
26]. First, we redefine the global coordinate frame
G to be aligned with the magnetic north. Specifically, the global frame’s
x-axis points in the same direction as the local magnetic field (the
z-axis remains vertical). Obviously, this global frame is only “fixed” in the case when the local magnetic field does not change its heading.
Next, we modify the system in Equation (
11) so that the second equation provides only one constraint. Let
be the half-plane which contains all points that lie in the global
-plane such that
x is non-negative. We require that the magnetic reading, when rotated into the global frame, lies on the half-plane
. Thus, we guarantee that the heading will be measured with respect to magnetic north, but do not enforce a constraint on the magnetic inclination.
Note that when defined in this manner, the system no longer needs a priori knowledge of the direction of the earth’s magnetic field .
In the remainder of the section, we present a novel algebraic solution to obtain
as a function of
and
. We begin by decomposing
into two auxiliary quaternions,
and
, such that:
and:
We further define
to have only a single degree of freedom, by setting it to:
It follows from the quaternion definition in Equation (
1) that
represents a rotation around the
z-axis only. Informally,
rotates a vector from the sensor frame to the horizontal plane of the global frame, and
rotates it around the
z axis to point North. In the following subsections, we present an algebraic derivation of
and
.
4.1. Quaternion from Accelerometer Readings
In this subsection, we present a derivation for the auxiliary quaternion
as a function of
. The observations of the gravity vector in the two reference frames allows us to find the quaternion that performs the transformation between the two representations. The rotation in the first equation of system Equation (
12) can be re-written as:
and decomposed by using Equation(
14) obtaining:
The representation of the gravity vector in the global frame only has a component on the
z-axis; therefore any rotation about this axis does not produce any change on it. Consequently, Equation (
17) is equivalent to:
expanding the multiplication we obtain the following system:
It is clear that the above system is underdetermined and has an infinite number of solutions. This is not an unexpected result because the alignment of the gravity vector from its representation in the global frame into the local frame does not give any information about the rotation around the z-axis (yaw). Thus, such alignment can be achieved by infinite rotations with definite roll and pitch angles and arbitrary yaw. To restrict the solutions to a finite number we choose
simplifying the system in Equation (
19) to:
The above system is fully determined; solving it results in four solutions for
. Two can be discarded since they have a negative norm. The remaining two are equivalent, with all the quaternion elements switching signs between one solution and the other. For convenience, we choose the solution with positive quaternion scalar (
), which corresponds to the shortest-path quaternion formulation [
34]. Thus, we get:
The formulation in Equation (
21) is valid for all values of
except
in which it has a singularity. Further, it may arise numerical instability when in proximity of the singularity point. To address this issue we provide an alternative solution to the system in Equation (
19). By simply setting
instead of
in Equation (
19) we obtain the reduced system:
which admits the following two real solutions:
and
and we choose the solution in Equation (
23). This formulation for
has a singularity at
. Therefore, the final formulation of
that avoids the singularity problem can be obtained by combining Equations (
21) and (
23):
Effectively, we solve the singularity problem by having two separate formulations for depending on the hemisphere in which a is pointing. Note that, defined in this manner, is not continuous at the point. However, we will demonstrate that this problem is resolved with the formulation of in the following subsection.
4.2. Quaternion from Magnetometer Readings
In this subsection, we present a derivation for the auxiliary quaternion
as a function of
and
. First we use the quaternion
to rotate the body frame magnetic field vector
into an intermediate frame whose
z-axis is the same as the global coordinate frame with orthogonal
x-,
y- axes pointing in unknown directions due to the unknown yaw of
.
where
l is the rotated magnetic field vector. Next, we find the quaternion (
) that rotates the vector
l into the vector that lies on the
of Equation (
12) using the following system:
where
This quaternion performs a rotation only about the global
z-axis by aligning the
x-axis of the intermediate frame into the positive direction of the magnetic north pole, which coincides with the
x direction of our global frame. This rotation will only change the heading component of the orientation without affecting the roll and pitch components. Therefore, in presence of magnetic disturbances, their influence is only limited on affecting the heading angle. The quaternion
has the following form:
By reordering the system in Equation (
27) and substituting
with its components we find the following simplified system:
the solution of the above system which ensures the shortest rotation is the following:
It is clear from the formulation of
that the latter quaternion incurs in a singularity state for negative
and zero
. To avoid the singularity of
we prevent the
l vector from having negative
x- component using the following procedure. If
we rotate
l of 180° around the world
z- frame, applying the quaternion
. Finally the rotated vector is used to find
, which has the same form of Equation (
31) and aligns
l with the magnetic north. The sequences of rotations are summarized in the quaternion multiplications below:
where
is the local magnetic field vector written as pure quaternion and
. For the sake of simplicity we consider the quaternion product between
and
as the alternative formulation of
in the case of
as following.
The result of the above multiplication is shown in Equation (
34):
The complete formulation of
that avoids the singularity problem discussed above, is eventually obtained by combining Equation (
31) with Equation (
34):
Finally, we can generalize the quaternion orientation of the global frame relative to the local frame as the multiplication of the two quaternions
and
as below.
Note that the quaternion
does not suffer from the discontinuity problem of the yaw angle given by the switching formulation of
of Equation (
25) thanks to the multiplication with
, which performs the alignment of the intermediate frame into the global frame as previously discussed.
Figure 1.
Simulated experiment showing the orientation output in quaternion form (left) and in Euler representation (right). (a) Orientation reference; (b) Orientation output of the presented method with noisy acceleration data and noise-free magnetometer data; (c) Orientation output of the presented method with noise-free acceleration data and magnetic readings affected by magnetic disturbances.
Figure 1.
Simulated experiment showing the orientation output in quaternion form (left) and in Euler representation (right). (a) Orientation reference; (b) Orientation output of the presented method with noisy acceleration data and noise-free magnetometer data; (c) Orientation output of the presented method with noise-free acceleration data and magnetic readings affected by magnetic disturbances.
To test the effectiveness of our method, we implemented a MatLab simulation. The results of which are shown in
Figure 1. We simulate three consecutive constant velocity (1 rad/s) rotations of 360° about each axis. First, we test the result of the quaternion solution of our proposed method by using perfect accelerometer and magnetometer data obtained by rotating the global frame vectors (gravity and magnetic field) into the local frame by using the orientation reference computed through the integration of the angular velocity vector, obtaining the same output of
Figure 1a. Note, on the right side of
Figure 1, that during the rotation about the
y-axis, besides the variation of the pitch angle, we also observe an instantaneous jump of the roll and yaw angles because of the singularity configuration that affects the Euler representation. However, as shown on the left side of the figure, the quaternion representation does not incur in a singularity state; moreover, our formulation ensures the continuity of the quaternion throughout the rotation. In
Figure 1b we prove that our method works with noisy acceleration data affected by Gaussian noise. When the sensor is close to some configuration where the yaw angle’s value is either π or −π, because of the noise, the yaw flips between the two values, which are two alternate representation of the same rotation. Finally, in the simulation of
Figure 1c, we show the result of our orientation estimation from noise-free acceleration data and magnetometer data affected by magnetic disturbances. It is clear, from the Euler representation of the orientation, that the magnetic disturbances affect only the yaw angle.
5. Quaternion-Based Complementary Filter
A complementary filter uses an analysis in the frequency domain of the signals to combine them to obtain a better estimation of a particular quantity. If the signals are affected by noises with different frequency, two filters, with an appropriate bandwidth, can be applied such that the sum of the two filtered signals cover the full range of useful frequency. For attitude estimation from IMU readings, a complementary filter performs high-pass filtering on the orientation estimated from gyroscope data affected by low-frequency noise, and low-pass filter on accelerometer data affected by high-frequency noise. The fusion between the two filtered estimations will ideally obtain an all-pass and noise-free attitude estimation. The term “complementary” derives from the cut-off frequency value, which is the same for both filters. Thus, its correct value is found as a trade-off between the preserved bandwidth of each single signal.
The complementary filter we propose in this article can be used for both IMU and MARG sensors, and is described in the diagrams of
Figure 2 and
Figure 3. It fuses attitude estimation in quaternion form from gyroscope data, with accelerometer data in the form of a delta quaternion, which serves as correction only for roll and pitch components of the attitude maintaining the yaw estimation from the gyroscope. If magnetometer data is provided, a second step is added to the algorithm where a delta quaternion, from magnetic field readings, is derived to correct the heading of the previous estimation by performing a small rotation about the global
z-axis in order to align the current frame with the magnetic field.
Figure 2.
Block diagram of the complementary filter for IMU implementation (no magnetometer data).
Figure 2.
Block diagram of the complementary filter for IMU implementation (no magnetometer data).
Figure 3.
Block diagram of the complementary filter for MARG implementation (with magnetometer data).
Figure 3.
Block diagram of the complementary filter for MARG implementation (with magnetometer data).
5.1. Prediction
In the prediction step, the angular velocity vector, measured by the tri-axis gyroscope, is used to compute a first estimation of the orientation in quaternion form. Assuming known the initial conditions, we first calculate the quaternion describing the rate of change of the orientation as a quaternion derivative, by multiplying the previous state with the angular velocity vector arranged as a pure quaternion as in Equation (
6). In the literature, the quaternion derivative from an angular rate measurement is usually calculated for the forward quaternion, that is, the one representing the orientation of the local frame with respect to the global frame. Because in this article we use the inverse orientation, for the sake of clarity, we compute the quaternion derivative for our convention, starting from the formula for the forward quaternion.
It is well known that the angular rate
and the “forward” quaternion derivative are related by the following identity:
where
is the angular velocity vector arranged as pure quaternion at time instant
and
is the previous estimate of the orientation. In our case, we want to determine the derivative of the inverse unit quaternion, which is simply the conjugate of Equation (
37), therefore:
as the conjugate of the pure quaternion
is just its negative (zero scalar component), we can finally write:
The above equation can be rewritten in matrix form as shown in Equation (
39):
where
and, regardless of the time dependence, the term
denotes the cross-product matrix that is associated with
and is equal to:
The orientation of the global frame relative to local frame at time
can be finally computed by numerically integrating the quaternion derivative using the sampling period
.
5.2. Correction
The correction adopted is based on a multiplicative technique. The predicted quaternion
(for clarity we omit the time
t) is corrected by means of two delta quaternions as in Equation (
43):
Each delta quaternion is computed and filtered by the high-frequency noise independently; thus we divide the correction into two separated steps. The first step corrects the predicted quaternion only in the roll and pitch components by the application of computed with data from the accelerometer. The second step is used only when the magnetic field readings are provided and corrects the yaw component of the quaternion orientation by applying .
5.2.1. Accelerometer-Based Correction
We use the inverse predicted quaternion
to rotate the normalized body frame gravity vector
(measured by the accelerometer) into the global frame as shown below.
obtaining the vector
that we call “predicted gravity”. The predicted gravity will have a small deviation from the real gravity vector; therefore, we compute the delta quaternion,
, which rotates
into
(because we represent the orientation of the global frame relative to the local frame), by using the following system:
where
Note that the system in Equation (
45) is similar to the system in Equation (
18); therefore, we proceed in the same manner to find a closed-form solution. We define the component
thus obtaining the following simplified system.
Proceeding as for system in Equation (
18) we find the following solution:
which is the one that ensures the shortest rotation. Although Equation (
47) has a singularity for
, we do not resolve it because the delta quaternion is applied at each step to correct the small deviation between the predicted gravity and the real gravity. Therefore, the value of
will always be very close to 1. However, for a more rigorous formulation, the approach used in Equation (
22) can be adopted.
As the delta quaternion is affected by the accelerometer’s high frequency noise, before applying it to the predicted quaternion, we scale it down by using an interpolation with the identity quaternion
. We use two different interpolation approaches based on the angle between
and
. Given two quaternions
p and
q, the cosine of the angle Ω subtended by the arc between them is equal to the dot product of the two quaternions as shown below:
In our case, as the identity quaternion has the following components
the dot product is equal to the
component of
. If
, where
ϵ is a threshold value (in our case
), we use a simple Linear intERPolation (LERP) as in the equation below.
where
is the gain that characterizes the cut-off frequency of the filter [
35]. LERP does not maintain the unit norm of the delta quaternion
that is then normalized immediately after:
If
, we use the Spherical Linear intERPolation (SLERP) [
36]. The SLERP algorithm gives a correct evaluation of the weighted average of two points lying on a curve. In the case of quaternions, the points lie on the surface of the 4D sphere (hypersphere). A simple 2D visualization of LERP and SLERP mapping processes is shown in
Figure 4. The SLERP formula we used is the following:
The
may have a large value when the predicted gravity has a significant deviation from the real gravity. If that condition does not occur, the delta quaternion is very small; thus, we prefer using the LERP formula because it is computationally more efficient. Finally, the quaternion estimated from gyroscope data is multiplied by the filtered delta quaternion, providing correction in the roll and pitch component, while the heading is preserved from the predicted orientation.
Figure 4.
2-D representation of two different interpolation methods between quaternion qa and qb resulting in quaternion qint. On the left is represented the linear interpolation (LERP), whereas on the right the spherical linear interpolation (SLERP) showing the correct result.
Figure 4.
2-D representation of two different interpolation methods between quaternion qa and qb resulting in quaternion qint. On the left is represented the linear interpolation (LERP), whereas on the right the spherical linear interpolation (SLERP) showing the correct result.
5.2.2. Magnetometer-Based Correction
If magnetic field data is provided, a second step performs the correction on the heading component. We proceed in the same way as in the first step by computing a delta quaternion. First, we use the quaternion inverse of
, calculated in Equation (
53), to rotate the body frame magnetic field vector
into the world frame magnetic vector.
Next, we find the delta quaternion (
), which rotates the vector
l into the vector that lies on the
-semiplane defined in
Section 4 such that:
This delta quaternion performs a rotation only about the global
z-axis by aligning the global
x-axis into the positive direction of the magnetic north pole. Further, with this formulation, such rotation does not affect the roll and pitch components even in the presence of magnetic disturbances, limiting their influence only on the heading angle. Thus, the delta quaternion
has the following shape:
By rearranging the system in Equation (
55) and substituting
with its component, we find the following simplified system:
the solution of the above system that ensures the shortest rotation is the following:
with Γ as per Equation (
28). The delta quaternion
is affected by the noise of the magnetometer, which is filtered by using the procedure we adopted for
, switching between LERP and SLERP according to the same criterion. Another advantage of using two correction steps is the possibility to apply two different filtering gains. As the two delta quaternions are completely independent to each other, and each of them is related to a particular sensor (accelerometer and magnetometer), they might be affected by different frequency noise. Thus, The LERP and SLERP formulas applied to
are the same as the ones in Equations (
50) and (
52), respectively, where the gain α is replaced with β, obtaining
. Eventually, the delta quaternion
is applied to the quaternion in Equation (
53) obtaining the final quaternion, which expresses the orientation of the global frame with respect to the local frame.
5.3. Adaptive Gain
A drawback of a typical implementation of the complementary filter is the constant gain that often causes inaccurate orientation estimation during highly dynamic motion. When the vehicle moves with high acceleration, the magnitude and direction of the total measured acceleration vector are different from gravity; therefore the attitude is evaluated using a false reference, resulting in significant, possibly critical errors. However, the gyroscope readings are not affected by linear acceleration, thus they can still be used to compute a relatively accurate orientation estimation that, under this condition, should be treated as the main source of the estimate. A constant gain fusion algorithm cannot overcome the aforementioned problem if the optimal gain has been evaluated for static conditions. In this paper we address this issue adopting an adaptive gain whose strategy is slightly different from the switching approach proposed in [
30].
First we define the magnitude error
as in the following equation:
where
is the norm of the measured local frame acceleration vector before normalization and
g = 9.81 m/s
. Given Equations (
50) and (
52), we make the filtering gain α function of the magnitude error
through the gain factor
f, meaning:
where
is the constant value that gives the best filtering result in static conditions and
is what we call the “gain factor”, which is a piecewise continuous function of the magnitude error as shown in
Figure 5. The gain factor is constant and equal to 1 when the magnitude of the nongravitational acceleration is not high enough to overcome the acceleration of gravity and the value of the error magnitude does not reach the first threshold. If the nongravitational acceleration rises and the error magnitude exceeds that first threshold, the gain factor decreases linearly with the increase of the magnitude error until reaching zero for error magnitude equal to the second threshold and over. Empirically, we found that the two thresholds’ values that give the best results are respectively 0.1 and 0.2.
Figure 5.
The gain factor function used in the adaptive gain to reduce the attitude error deriving from the external linear acceleration because of vehicle motion.
Figure 5.
The gain factor function used in the adaptive gain to reduce the attitude error deriving from the external linear acceleration because of vehicle motion.
In this article we do not address the problem of the centripetal acceleration; however, if conditions warrant, one can easily add the centripetal force model used in [
21].
5.4. Filter Initialization and Bias Estimation
The complementary filter proposed in this article is initialized with the orientation quaternion calculated using the procedure explained in
Section 4. The values of the current body-frame acceleration and magnetic field vectors are used to produce the quaternion representing the initial orientation of the rigid body in any configuration. Therefore, for the initialization, the filter does not need any assumption and it is performed in one single step.
Before using the angular velocity vector for the quaternion prediction, we correct it for the bias. The bias of a sensor’s reading is a slow-varying signal considered as low frequency noise. Therefore, we adopt a low-pass filter to separate the bias from the actual angular velocity reading. To avoid filtering useful information, the low-pass filtering is applied only when the sensor is in a steady-state condition that is previously checked. If the sensor is in the steady-state condition, the bias is updated, otherwise it is assumed to be equal to the previous step value. The estimated bias is then subtracted from the gyroscope reading obtaining a bias-free angular velocity measurement. The low-pass filter cut-off frequency can be selected by the user as well as parameters for the steady-state condition, to apply the bias-estimation to different sensors.