Evaluation of spatial orientation, or How not to be afraid of filters Mahoney and Majvika
- Tutorial
What are we talking about
The appearance on Habré of the post about Majvik's filter was in its own way a symbolic event. Apparently, the universal enthusiasm for drones has revived interest in the problem of estimating the orientation of a body from inertial measurements. At the same time, traditional methods based on the Kalman filter have ceased to satisfy the public, either because of the high requirements for computing resources that are unacceptable for drones, or because of the complex and non-intuitive adjustment of parameters.
The post was accompanied by a very compact and efficient implementation of the C filter. However, judging by the comments, the physical meaning of this code, as well as the whole article, remained vague for someone. Well, let's face it honestly: the Madjwick filter is the most sophisticated of a group of filters based on very simple and elegant principles. I will consider these principles in my post. Code will not be here. My post is not a story about a specific implementation of an orientation estimation algorithm, but rather an invitation to invent my own variations on a given topic, which can be very much.

Orientation view
Recall the basics. To estimate the orientation of a body in space, one must first choose some parameters that, in aggregate, uniquely determine this orientation, i.e. essentially the orientation of the associated coordinate system
Euler angles - roll (roll,
Rotation Matrix - Matrix
Here
The rotation matrix is a little less obvious than the Euler angles, but unlike them, it allows you to directly transform vectors and does not lose meaning at any angular position. From a computational point of view, its main drawback is redundancy: for the sake of three degrees of freedom, nine parameters are entered at once, and all of them need to be updated according to the kinematic equation. The task can be slightly simplified by using the orthogonality of the matrix.
Turn quaternion is a radical, but very non-intuitive remedy against redundancy and degeneration. This is a four component object.
Filter schemes
The most naive approach to calculating an orientation is to arm yourself with a kinematic equation and update any set of parameters we like in accordance with it. For example, if we selected a rotation matrix, then we can write a cycle with something in the spirit
C += С * Omega * dt
. The result will disappoint. Gyroscopes, especially MEMS, have large and unstable zero offsets — as a result, even at complete rest, the computed orientation will have an unlimited accumulating error (drift). All the tricks invented by Mahoney, Majwick, and many others, not excluding me, were aimed at compensating for this drift by involving measurements from accelerometers, magnetometers, GNSS receivers, lags, etc. Thus was born the whole family of orientation filters, based on a simple basic principle.The basic principle. To compensate for the orientation drift, add an additional control angular velocity to the angular velocity measured by the gyros, based on the vector measurements of other sensors. The vector of control angular velocity must strive to combine the directions of the measured vectors with their known true directions.Here a completely different approach is concluded than in the construction of the corrective addendum of the Kalman filter. The main difference is that the controlling angular velocity is not a term, but a factor at the estimated value (matrix or quaternion). From here important advantages follow:
- The rating filter can be built for the orientation itself, and not for small deviations of the orientation from that given by the gyroscopes. In this case, the estimated values will automatically satisfy all the requirements imposed by the task: the matrix will be orthogonal, the quaternion will be normalized.
- The physical meaning of the control angular velocity is much clearer than the corrective term in the Kalman filter. All manipulations are done with vectors and matrices in the usual three-dimensional physical space, and not in the abstract multi-dimensional state space. This greatly simplifies the refinement and configuration of the filter, and as a bonus allows you to get rid of large-dimension matrices and heavy matrix libraries.
Now let's see how this idea is implemented in specific variants of filters.
Filter Mahoney. All the furious mathematics of the original article by Mahoney is written to justify simple equations (32). Rewrite them in our notation. If we ignore the estimation of gyro zero offsets, then two key equations will remain - the actual kinematic equation for the rotation matrix (with the controlling angular velocity in the form of
Of course, in the Mahoney filter it is not necessary to use the rotation matrix. There are non-canonical quaternion variants.
Virtual gyro platform. In the Mahoney filter, we attached a control angular velocity.

www.theairlinepilots.com
The task of the platform there was the materialization of the geographical coordinate system. The orientation of the carrier was measured relative to this platform by angle sensors on the suspension frames. If the gyroscopes drifted, then the platform drifted after them, and errors accumulated in the readings of the angle sensors. To eliminate these errors, feedback was introduced from accelerometers installed on the platform. For example, the deviation of the platform from the horizon around the northern axis was perceived by the accelerometer of the eastern axis. This signal allowed to set the control angular velocity
We can use the same visual concepts in our task. The written kinematic equation must then be read as follows: the rate of change in orientation is the difference between two rotational movements — the absolute movement of the carrier (the first term) and the absolute movement of the virtual gyroplatform (the second term). The analogy can be extended to the law of formation of the control angular velocity. Vector
The first hint of a useful analogy of platform and free-form inertial navigation appears, apparently, in the ancient patent of Boeing . Then this idea was actively developed by Salychev , and more recently, by me too . The obvious advantages of this approach are:
- The control angular velocity can be formed on the basis of clear physical principles.
- The horizontal and course channels, which are very different in their properties and methods of correction, are naturally separated. In the filter Mahoney they are mixed.
- It is convenient to compensate for the effects of accelerations by attracting GNSS data, which are issued in geographic and not related axes.
- It is easy to generalize the algorithm to the case of high-precision inertial navigation, where the shape and rotation of the Earth must be taken into account. How to do it in the Mahoney scheme, I can not imagine.
Filter Majvika. Madzhvik chose a difficult path . If Mahoney, apparently, intuitively came to his decision, and then substantiated it mathematically, then Madjvik from the very beginning proved to be a formalist. He undertook to solve the optimization problem. He reasoned so. Define the orientation with a quaternion of rotation. In the ideal case, the estimated direction of some measurable vector (let it be
Gradient descent ultimately leads to the following condition: to compensate for orientation drift, you need to add to the rate of change of the quaternion from the kinematic equation a new negative term, proportional to
Impact of acceleration
Until now, it was assumed that there are no true accelerations and accelerometers measure only the acceleration of gravity
This is easily illustrated using the analogy of a virtual gyro platform. Its correction system is designed so that the platform stops at that angular position, in which the signals of accelerometers supposedly installed on it, i.e. when measured vector

In the comments to the post about the filter Majvika flashed the question of whether it is possible to hope that this filter is less susceptible to acceleration than, for example, the Mahoney filter. Alas, all the filters described here exploit the same physical principles and therefore suffer from the same problems. To deceive physics by mathematics is impossible. What then to do?
The easiest and rough way was invented back in the middle of the last century for aviation gyroverticules: to reduce or completely zero the control angular velocity in the presence of accelerations or angular velocity of the course (which indicates the entrance to the turn). The same method can be transferred to the current freeware systems. Accelerations should be judged by the values.
A more accurate method is based on using external velocity measurements from a GNSS receiver. If speed is known
Sensor Zero Offsets
The sad feature of consumer-grade gyroscopes and accelerometers is the large volatility of zero offsets in time and temperature. To eliminate them, it is not enough to have only factory or laboratory calibration - you need to reassess during operation.
Gyroscopes. Understand the zero offsets of the gyros

Mahony et al., 2008
Accelerometers. Rate accelerometer zero offset

Instead of a conclusion: what about the Kalman filter?
I have no doubt that the filters described here will almost always have an advantage over the traditional Kalman filter in terms of speed, compactness of the code and convenience of customization - for this they were created. As for the accuracy of estimation, everything is not so clear. I met the unsuccessfully designed Kalman filters, which, in terms of accuracy, lost noticeably to the filter with the virtual gyroplatform. Madzhvik also argued the benefits of his filter with respect to some Kalman estimates. However, for the same orientation estimation task, at least a dozen different Kalman filter schemes can be constructed, and each will have an infinite number of tuning options. I have no reason to think that the Mahoney or Majvik filter will be more accurate than the best possibleKalman filters. And of course, the Kalman approach will always remain the advantage of universality: it does not impose any rigid restrictions on the specific dynamic properties of the system being evaluated.