AnyLeaf Blog

Estimating drone attitude

Author: David O'Connor

Written on July 1, 2022, 3:37 p.m.
Updated June 21, 2023, 6:46 p.m.

Overview

This article describes approaches used by Attitude and Heading Reference Systems (AHRS) on small unmanned aircraft. It provides general information: It's applicable to anyone writing or modifying firmware for AHRS sensors or UAV firmware, and those looking to understand more about these systems. Some flight controller firmware, like Ardupilot and PX4, offer multiple implementations; this article may assist in selecting one.

This article focuses on unmanned aircraft, but the same concepts apply to manned aircraft, ground vehicles, biometric sensors, robotics, and other applications.


The challenge

Accurately measuring attitude is a surprisingly subtle problem. To build intuition about why, consider what attitude is measured in relation to: the earth's surface. What if the terrain slopes? What if the craft is really, really high up? What if it's in orbit? Most attitude systems use the center of earth's gravity as a reference for down. To find this, we need to measure acceleration. We'll define level as when earth's center of gravity is directly below the aircraft. We'll use whatever definition of below makes sense for a given aircraft. For a quad, this will be on the bottom of the frame. (This is also a bit subtle, but usually has an intuitive answer. We won't go into why here, but think about what makes the "bottom" of an aircraft the bottom. A quadcopter? An airliner? A fixed-wing drone? You may find yourself including both biology and aircraft parts in your answer!)


The solution

To estimate attitude, we combine information from several sensors that complement each others' weaknesses. In sections below, we'll discuss how to estimate attitude from each sensor, and how to combine these estimations into a reliable solution. Here's a summary of the most important sensors:

Gyroscope (3-axis)

Gyroscopes measure attitude by integrating (adding) angular rate over time, from a known starting attitude. Their information is relative, so require an absolute reference. Their estimated attitude accumulates errors proportional to time running and sensor noise. Gyroscopes add a consistent error to the solution, so they can be relied upon for short durations while others sensors are experiencing more serious errors.

Accelerometer (3-axis)

Accelerometers measure acceleration. When the device isn't under linear acceleration (eg from increasing or decreasing speed, ballistic flight, or maneuvers), this is a measure of gravity. In this way, accelerometers determine which way "up" is relative to the aircraft. This is an absolute measurement that can be used as a reference for gyroscope. When linear acceleration is detected, it can be compensated for, or the accelerometer can be ignored for its duration. Problems occur during extended periods of linear acceleration (eg a continuous turn), and when it's not possible to separate linear and earth acceleration.

Accelerometers provide the up (or equivalently down) direction, but provide no information about rotation around this axis. (ie heading)

Magnetometer (3-axis)

Like accelerometers, magnetometers use a property of the earth to provide an absolute attitude, with an ambiguity around an axis. In this case, they determine the direction of the magnetic field, which points north, and into the earth at an inclination angle that varies based on the earth's position. They provide no information about rotations around this direction.

Because the earth's magnetic field is weak relative to interference sources (such as motors, electric current, and metal), magnetometers require careful calibration, and health-monitoring. It's important for an AHRS system to know when to include magnetometer information, and when to ignore it or weight it weakly.

Magnetometers are sometimes used only to provide heading information, but they're valuable attitude sources - especially under periods when accelerometer weaknesses are exposed.


An aside on biology

To appreciate the attitude problem, consider how you determine your own orientation. Your body uses 2 approaches: a visual depiction of the horizon, and the vestibular system in your inner ear. The former works well when outside with a clear view of the horizon. If indoors, or if the horizon is obscured by terrain, buildings etc, you can still determine 'up' visually from cues in the scene like the straight lines on the ground, walls etc. Or by the orientation of people, animals, and objects you see. This can be deceived by optical illusions, like false horizons, sloping cloud decks, lights on the ground or sea that mimic stars etc.

The vestibular system lets you know your orientation, even if your eyes are closed. It uses moving fluids in your inner ear to measure the direction of gravity, and rotations. It works very well when you're stationary, or moving with a little acceleration (eg walking or running). As every pilot knows, the vestibular system is misleading while under the accelerations and rotations aircraft experience - it didn't evolve for those conditions.

These two biological systems provide a good analogy for approaches we could use in aircraft instruments. In practice, aircraft systems don't use a visual system - this would be tough to implement, but is possible. The inner ear's approach of measuring acceleration and rotation is the standard one.

Back to aircraft

The Inertial Measurement Units(IMUs) in most drone flight controllers can measure both 3 axes of angular rate (via gyroscope), and 3 axes of acceleration (via accelerometer). Some can also measure magnetic heading (via magnetometer) Determining attitude from this is referred to as an Attitude and Heading Reference System (AHRS).

The task: Determine attitude based on noisy accelerator and gyro measurements. Military and commercial aircraft often use Ring Laser Gyroscopes for this - these are comparatively stable, and have little drift. Unfortunately, these are too large and expensive for quads, so we settle for the ubiquitous, cheap, and good-enough MEMS-based IMUs. We mitigate their noisy measurements using digital filtering - either built into the IMU, or in the FC's firmware. For example, the CMSIS-DSP library works on all Cortex-M MCUs, which are common on FCs.


Coordinate systems, briefly

We need to consider 2 coordinate systems: The aircraft's, and the earth's. For the purpose of this article, we'll define the X axis as left and right, Y as forward and aft, and Z as up and down, for both systems. This is arbitrary, but you may find it intuitive. The important part is consistency. The IMU measures angular rates and accelerations in the aircraft's coordinate system. Attitude and heading are in relation to the earth's system.


Attitude estimation using gyroscopes

Gyroscopes have a powerful advantage compared to other sensors: They don't rely on external information, so they can continue to measure attitude in scenarios where other systems, like accelerometers and magenetomers, are biased. This makes them a good baseline attitude sensor. The MEMS gyroscopes common in commercial IMUs accumulate errors (called drift) relatively quickly. Ring Laser Gyroscopes used in aerospace applications drift less, but suffer from this same inherent limitation.

Attitude determined from gyroscopes is relative, so it must be regularly updated with an absolute reference.. This source is typically a combination of accelerometers, magnetometers, and GNSS. The nature of how these updates are performed vary, but the amount of update taken into account should be based on the amount of errors these are receiving: If there is little (or a known amount of) linear acceleration - ie the aircraft is straight and level, or in a turn of known radius - the accelerometer is a good absolute reference. Magnetometers are a good backup reference in cases of linear acceleration, and low magnetic interference. In periods when these update sources aren't suitable, the gyros will continue to provide an accurate attitude. We discuss how the updates are performed later in this article.

Here's how gyro-based attitude estimation can work:

Drift arises because the measurements will have errors. Each individual error will be small, but each update accumulates error: gyroscope-determined attitudes with recently-updated references accurate; ones with a longer time since a reference update are not. Gyroscopes can be used to estimate attitude (in conjunction with a known starting attitude) for short durations, but their errors add up quickly.


Attitude estimation using accelerometers

Given we're using earth's gravity as our reference for attitude, it makes sense to use acceleration to determine attitude. If your aircraft (more specifically, it's IMU) is stationary (or in steady flight) and level, we expect to read 0 acceleration on the x and y axes, and 9.8m/s² (1G) acceleration upwards. (This is the approximate acceleration due to gravity at the earth's surface) This reading is because in order to not be falling at this acceleration, the aircraft must resist it with an equal and opposite acceleration. Again, subtle. If the aircraft were in free-fall, eg dropping like a rock (You had a very bad flight), or has achieved orbit (You had a very good flight!), your accelerator would read near 0 on all axes.

Consider how you might use 3-axis acceleration measurements to determine attitude: If you measure +1G of acceleration upwards on the Z axis, the craft is level. If you measure -1G on the same axis, we can reason that the aircraft is upside-down. If you measure +1G on the X axis, the aircraft is oriented right-side-up.

We can assume, for a stationary aircraft, or one in steady flight, that total acceleration is exactly 1G. (You'll notice differences from this due to measurement error, and non-gravitational acceleration, which we discuss below). It's unlikely this 1G will be exactly along an axis. You can think of it as a unit vector pointing towards the (gravitational) center of the Earth. With this in mind, calculating attitude from accelerator readings is a matter of trigonometry or linear algebra. Let's consider another intuitive example. If you read +0.71G on the X axis, 0 on the Y axis, +0.71G on the Z axis, we can reason that the craft is in a 45° bank left. We notice this has 0° of pitch, due to reading 0 on the Y axis.

We can confirm that our force of Gravity measured is indeed 1G, using the Pythagorean formula: $$ \sqrt{0.71^{2} + 0.71^{2}} = 1 $$

We can't rely on accelerators alone to estimate attitude: Any type of aircraft maneuver imparts acceleration on an aircraft. (We call this linear acceleration, and distinguish it from gravitational acceleration. For example, while executing a turn by pitching up while in a bank, the aircraft will measure an acceleration downwards relative to its IMU. This is the G-force fighter pilots feel, and how vomit-comet maneuvers cause oscillations between 0 and 2G. A surprising and fundamental part of physics: acceleration from maneuvers is indistinguishable from acceleration due to gravity. Here's your cue to drop down the relativity rabbit-hole, but this article won't take you there, Alice.

Accelerometers can be used to estimate attitude, but aircraft maneuvers confound these results.

Note that we have no notion of yaw using an accelerometer. For that we need to use the gyroscope, or better, gyroscope + magnetometer:


Attitude estimation using a magnetometer

Magnetometers measure the strength of magnetic fields, usually in 3 axes. When local magnetic interference sources (Including metal and magnets on and around the AHRS device) are compensated for, this points along the earth's magnetic field lines.

Accelerometers indicate what direction is up. Magnetometers indicate what direction the earth's magnetic field is. This is determined by heading, and by something called magnetic inclination: The angle the magnetic field meets with the earth's surface. inclination varies with position on earth; especially latitude. With known (or estimated using other sensors) inclination, magnetometers can estimate attitude up to a rotation around the magnetic field vector. This is analogous to accelerometer ambiguity around "up."

Magnetometer data can be used to augment acc/gyro attitude with heading information, or to fuse as its own attitude solution.

Magnetometers are vulnerable to interference from magnets, ferromagnetic materials, and electric currents. This includes from motors and motor controllers. For this reason, it's important to place them as far away from these as practical. The electronics on a sensor's PCB is usually enough to cause interference that must be compensated for. Calibration processes involve estimating errors after rotating the sensor to many attitudes, then zeroing-out errors. (Keywords: "hard iron" and "soft iron" offsets) These can ideally be performed automatically by your sensor or flight control firmware.

Magnetic and true north.

For most applications, north referenced to the earth's geographic north (True north) pole is ideal. The earth's magnetic field doesn't point to this: they point to its magnetic north, which is slightly different from geographic north, and varies over time. THings are further complicated due to the Earth's non-uniform magnetic field. this article goes into details. You can find charts showing the difference in various parts of the earth; you can use these charts to calculate true north from magnetic measurements, by adding a fixed amount to the magnetic north heading. Note that magnetic heading is used directly in some cases; notably air traffic control instructions.

Magnetometers provide a way to determine attitude (or just heading), but are vulnerable to interference, and must be calibrated


Separating linear and gravitational acceleration using GNSS

A GNSS (eg GPS) may be used to separate linear from gravitational acceleration. A good use case of when this is important is when the aircraft is in a continuous turn. In this situation, linear acceleration continues indefinitely, so no accurate updates can be provided to the gyroscope.

We can estimate linear acceleration using GNSS velocities: We estimate lineare acceleration by taking the difference between GNSS velocity, and dividing it by the difference in GNSS timestamp from these fixes. These velocities (And our inferred acceleration) are in reference to Earth's coordinate system (Usually reported as NED, for North, Earth, Down). We convert this to the aircraft's coordinate system (For example, by rotating it using the inverse of the aircraft's attitude quaternion) to estimate linear acceleration. We can then subtract this from the accelerometer readings to determine earth's gravitational axis, leaving us with the "up" direction we need to update the gyro readings.


Calibration, and updating sensor biases

(Coming soon)


Fusion algorithms

Given 3 axis measurements of acceleration, angular rate, and perhaps magnetic field, how do we estimate attitude? We described above why we have enough information to do so, but glossed over implementation details; especially how we balance the 2 or 3 types of measurement. There's no correct answer, and different algorithms may be more accurate in different cases. When choosing an algorithm, we need to consider things like:

There are a number of academic papers available (Search AHRS algorithms on Google Scholar, for example) explaining individual algorithms, and comparing them to each other. Here's an example, comparing several of the types described below. The more popular ones (suitable for drones use) are summarized here. All of these approaches tackle the problem of in what proportions under which circumstances do we blend our 3 sets of measurements?

They take approaches such as:

Extended Kalman Filter (EKF)

The Kalman filter is the best known algorithm for sensor fusion in general. Its basic form is suitable for linear problems. To use for an AHRS, we need to use its non-linear variant, the Extended Kalman Filter (EKF). Compared to other algorithms, the EKF has the potential to be the most flexible and accurate. This comes at a cost of complex implementation code.

Kalman filters are rooted in Bayesian inference; they maintain a model of the values they track, and confidence level in the values. As more information becomes available, they update the value and confidence, taking into account the confidence of the new (ie measured) data. More information provided to a Kalman filter always helps it - as long as it knows how reliable the information is.

Kalman filters are the most general of those listed here. While the others are specific to fusing gyro, accelerometer, and magnetometer readings, Kalman filters can be modified to use any info available. For example, pitot airspeed measurements, altimeters, and GPS. As described later in this article, we can also use control inputs directly to improve accuracy of the Kalman filter. A properly configured filter can take any information we have about the system, and use it to improve the attitude estimate.

Complementary filter

The Complementary filter is simple to implement, and reason about. Compared to the others we describe, it's the least accurate, but may be good enough. This is a good starting point if coding an AHRS yourself. After estimating the attitude by integrating angular rates, and independently from acceleration measurements, it uses linear interpretation to fuse the attitude into a best-estimate. It uses quaternions internally, and the fusion algorithm can be described as this:

$$ q = (1 - α) q_ω + αq_{am} $$

Where \(\omega\) is attitude from angular rates, and \(αq_{a m}\) is attitude from acceleration and magnetometer reading. \(\alpha\) is the filter's gain; it's a user-adjustable value that balances the two attitude sources. Note that this balance value is constant: This is responsible for the complementary filter's simplicity, and its naivety.

Madgwick filter, and improved versions

The Madgwick filter provides accurate results, and has a straightforward implementation. This paper by Sebastian Madgwick describes the algorithm in detail. It uses a quaternion representation of attitude, and supports fusion between 3-axis accelerometers, gyroscopes, and magnetometers.

Mahony filter

The Mahony filter is an extension of the complementary filter. It dynamically chooses weights for accelerometer, gyroscope, and magnetometer measurements. This AHRS document describes it in a detailed, approachable way.

Other approaches

The named approaches above are well documented, but other approaches may be used. The essence of attitude determining is combining the attitude sources listed above (Accelerometer, magnetometer, gyroscope, GSS) in a way that leverages each's strengths, and mitigates each other's weaknesses. There are many approaches to this; the AHRS implementation should include both this fusing, and calibration of the sensors.


Quaternions (Or other Rotors) vs Euler angles

An intuitive way to represent angles is pitch, roll, and yaw. There are called Euler angles, are measure rotation around 3 linearly-independent axes; ideally orthogonal ones. They can be represented as a vector of 3 values, and rotated by multiplying by a rotation matrix. Euler angles aren't a great way to represent and manipulate attitudes, because they can be subject to singularities; ie gimbal lock, wherein they lose a degree of freedom. They also run into ambiguities about how to combine rotations on the 3 axes, since there's no single answer for in which order to apply the 3 rotation matrices (one for each axis).

Quaternions provide an elegant alternative. A quaternion can be thought of as a collection of 4 numbers that describe orientation. Attitude can be represented by a single quaternion, and manipulated using operations between quaternions and vectors. Quaternions have a reputation for being unintuitive, but when though of as mapping directly to orientation or rotation, they're straightforward to use. Our article on them summarizes how to use them for practical purposes, like representing attitude.


Fusing with GPS (GNSS)

We can combine IMU and magnetometer data with GPS, for even more accurate data. GPS readings have a comparatively slow update rate, and GPS units use a lot of power. Their advantage is in very accurate position and velocity data. By fusing this with other readings, we can refine our AHRS solution further, in addition to providing accurate location and altitude information. This can be used, for example, to estimate heading in fixed-wing aircraft.

Some GPS receivers can output carrier wave data directly; this can be used to determine attitude, using approaches beyond this article's scope.

Multiple high-precision (ie cm-grade) GPS sensors can be placed on different parts of the aircraft; attitude can be determined by their relative position.


Taking advantage of control inputs

Given we have control over drones - either directly by manual control inputs, or through an autopilot system, we have more information available than sensor readings alone. Pause for a second, and consider how you might use this information to improve the above filters. (EKF implementations may include control inputs already, which is an advantage we glossed over above).

Here's an example: If throttle setting is high, and the pitch, roll, and yaw settings are neutral, we can guess that the aircraft is accelerating away from its orientation. This means we know it probably has linear acceleration up, in the aircraft's own coordinate system. This helps us separate the linear acceleration from gravitational acceleration, improving our accelerator-based readings.

We could also attempt to guess when the aircraft is accelerating forward/back/left/right based on a high throttle setting in conjunction with pitch and roll inputs. In addition to separating acceleration components, we can weight gyro measurements more, and accel less, if an aggressive maneuver is commanded.

For fixed wing aircraft, it's easier to take advantage of this. For example, we can estimate how many linear Gs the aircraft pulls in a turn, based on airspeed, pitch rate commanded, and if we're losing/gaining altitude - we can then subtract this from measured acceleration.

References