Skip to the content
FixTheRobot Explore Robotics / UAV / Module 1 / M1-T8

UAV Systems Engineering · Module 1

M1-T8: IMU, barometer, magnetometer and GNSS

Each sensor sees one thing well and is fooled by another: knowing both halves is the start of sensor fusion.

  • Lesson 8 of 12
  • UAV Fundamentals and System Architecture
  • Established engineering
  • About 6 minutes to read

Every autopilot runs on these four sensors. None is sufficient alone, and each has a characteristic way of lying. This lesson sets out what each measures, its dominant error, and the role it plays in the estimator, so the fusion lessons that follow have something concrete to combine.

Learning objectives

After this lesson, you should be able to:

  • State what each of the IMU, barometer, magnetometer and GNSS measures and in which frame.
  • Name the dominant error of each and the environment that triggers it.
  • Convert pressure to altitude and compute a tilt-compensated heading.
  • Explain which state each sensor makes observable and which it does not.

Conceptual explanation

The IMU measures angular rate and specific force at high rate in the body frame. The barometer measures static pressure, which maps to altitude. The magnetometer measures the magnetic field vector in the body frame, which gives heading when the field is known. GNSS measures position and velocity in a global frame.

Sensors and the states they informIMU informs attitude, velocity and position by integration; barometer informs altitude; magnetometer informs heading; GNSS informs absolute position and velocity. All feed the estimator.IMUrate + force, 100–1000 HzBarometerpressure → heightMagnetometerfield → headingGNSSposition, velocityEstimatorEKF2 / fusioncomplementary: fast and drifting versus slow and absolute
Each sensor constrains a different part of the state.
Core sensors, with typical rates (check datasheets)
SensorMeasuresRate (typical)Dominant errorFooled by
IMU (gyro + accel)angular rate, specific force100–1000 Hzbias, noise, vibrationvibration, temperature
Barometerstatic pressure10–100 Hzweather drift, propwashwind gusts, enclosure pressure, temperature
Magnetometermagnetic field vector10–100 Hzhard- and soft-iron, motor currentssteel structures, power wires, magnets
GNSSposition, velocity, time1–20 Hzmultipath, atmospheric delay, blockageurban canyons, jamming, spoofing
Animation of IMU dead-reckoning drift for a stationary vehicle. An accelerometer bias makes the position estimated from the IMU alone drift away from the true position along a one-half b t squared curve, reaching about ten metres in twenty seconds, while an estimate that also uses one-hertz GNSS position fixes stays within about one metre. A chart shows both error traces and the theoretical curve.
IMU drift and the GNSS correction (looping, about eight seconds). The vehicle is stationary, so the measured acceleration is only bias and noise; integrating twice gives ½ b t2b\,t^{2}. One absolute fix per second keeps the fused estimate bounded. Simulated with illustrative values (bias 0.05 m/s20.05\,\mathrm{m}/\mathrm{s}^{2}, GNSS noise 1 m); a schematic drawn for this course.
Animation of barometric altitude. As the vehicle climbs from zero to 120 metres the static pressure falls along the International Standard Atmosphere curve and the barometer reading matches the true height. Then the vehicle hovers at 120 metres while the pressure falls by up to six hectopascals because of weather, and the barometer reading rises by about fifty metres although the true height has not changed.
The barometer measures pressure, not height (looping, about eight seconds). Climbing, the reading follows the ISA curve; hovering, a weather change of a few hectopascals (about 8 m per hPa near sea level) moves the reading with no change in true height, so barometric altitude is a relative measurement anchored at take-off. The 6 hPa change is an illustrative size.
Animation of magnetometer heading. First the vehicle turns while level and both the naive heading and the tilt-compensated heading follow the true heading. Then the heading is held at 40 degrees while roll and pitch increase to about 28 and 18 degrees; the naive heading swings by almost 100 degrees while the tilt-compensated heading stays correct. A chart plots both heading errors.
Why the magnetometer needs roll and pitch (looping, about eight seconds). The Earth field points steeply downward at mid-latitudes (inclination 60∘60^{\circ} here, illustrative), so tilting the vehicle puts the large vertical component into the body xx and yy axes. The lesson’s tilt-compensation formulas remove it, if the attitude estimate is right.
Animation of a two-dimensional satellite position fix with an unknown receiver clock error. Three satellites each give a pseudorange circle that is too large by the same clock error, so the circles do not meet at one point. Gauss-Newton iterations estimate the position and the clock bias together; after about three iterations the corrected circles meet at the true position and the residual falls to zero.
A GNSS fix as an estimation problem (looping, about eight seconds). Each pseudorange is the true range plus the same unknown receiver clock error, so position and clock are solved together; in 3-D that means at least four satellites. A schematic 2-D version (not to scale, noise-free) computed by Gauss-Newton.

Engineering intuition

Think of two kinds of information. Relative sensors (IMU) are smooth and fast but drift; absolute sensors (GNSS, barometer, magnetometer) are noisy or slow but do not drift. A fusion filter uses the absolute ones to pin down what the relative one cannot hold, and the relative one to fill the gaps between absolute updates. The failure modes are complementary on purpose: an IMU never loses signal, GNSS never drifts.

The trap is that each absolute sensor is absolute about something specific: the barometer about pressure altitude (not height above ground), the magnetometer about the local field direction (not true north without declination), GNSS about the antenna position (not the centre of mass). Offsets and reference frames are where integration bugs live.

Mathematical formulation

Pressure to altitude

For the International Standard Atmosphere below 11 km, with sea-level pressure p0=101325p_{0}=101325 Pa:

ISA pressure altitude
h=44330[1−(pp0)1/5.255] (m)h = 44330\left[1 - \left(\frac{p}{p_0}\right)^{1/5.255}\right]\ \text{(m)}
∂h∂p≈−8.4 m per hPa near sea level\frac{\partial h}{\partial p} \approx -8.4\ \text{m per hPa near sea level}

A 1 hPa weather change moves the reading by roughly 8 m, so barometric altitude is a relative measurement over a flight, anchored at take-off.

Tilt-compensated heading

With roll ϕ\phi , pitch θ\theta and body-frame magnetometer m=(mx, my, mz)m=(m_{x},\,m_{y},\,m_{z}) in FRD, rotate the field into the horizontal plane then take the angle:

Tilt compensation
mh,x=mxcos⁡θ+mysin⁡θsin⁡ϕ+mzsin⁡θcos⁡ϕm_{h,x} = m_x\cos\theta + m_y\sin\theta\sin\phi + m_z\sin\theta\cos\phi
mh,y=mycos⁡ϕ−mzsin⁡ϕm_{h,y} = m_y\cos\phi - m_z\sin\phi
ψmag=atan2⁡ ⁣(−mh,y, mh,x)\psi_{\text{mag}} = \operatorname{atan2}\!\left(-m_{h,y},\ m_{h,x}\right)
Magnetic heading; add the declination for true heading.

GNSS accuracy

Position error ≈ DOP × range error (lesson M2-T1); velocity from Doppler is typically far more accurate than position differences.

p0p_{0}
reference sea-level pressure (Pa); actual local value differs by weather.
ϕ\phi , θ\theta
roll and pitch from the attitude estimate: a heading error appears if they are wrong.

Architecture and data flow

In PX4, drivers publish each sensor to uORB; EKF2 fuses them: IMU for propagation, barometer for height, magnetometer for yaw, GNSS for horizontal position and velocity (and optionally height), with innovation gating on each (lesson M6-T5). When you remove GNSS (lesson M2-T6) or distrust the magnetometer, other sources (vision, LiDAR, external yaw) must supply what they provided.

Real-world UAV applications

  • Understanding which EKF2 fusion switches to change for GNSS-denied flight.
  • Diagnosing altitude jumps from propwash on the barometer.
  • Calibrating the magnetometer away from the airframe wiring.
  • Choosing between barometer and rangefinder for height near the ground.

Implementation example

This runs as shown: ISA pressure-to-altitude, its sensitivity, and a tilt-compensated heading checked on a synthetic field (northern-hemisphere style field with inclination).

import numpy as np

def pressure_altitude(p_pa, p0=101325.0): return 44330.0 * (1.0 - (p_pa / p0) ** (1 / 5.255))
assert abs(pressure_altitude(101325.0)) < 1e-9
h = pressure_altitude(89875.0); assert 1000 < h < 1100                     # about 1 km: ISA pressure at 1000 m is 89875 Pa
dh = pressure_altitude(100325.0) - pressure_altitude(101325.0); assert 80 < dh < 90      # 10 hPa lower pressure is about 83 m higher (about 8.3 m per hPa): weather matters

def rot_frd(roll, pitch, yaw):     # body (FRD) -> NED, ZYX
    cr, sr, cp, sp, cy, sy = np.cos(roll), np.sin(roll), np.cos(pitch), np.sin(pitch), np.cos(yaw), np.sin(yaw)
    return np.array([[cy*cp, cy*sp*sr - sy*cr, cy*sp*cr + sy*sr], [sy*cp, sy*sp*sr + cy*cr, sy*sp*cr - cy*sr], [-sp, cp*sr, cp*cr]])

def heading(m, roll, pitch):
    mx, my, mz = m
    mhx = mx * np.cos(pitch) + my * np.sin(pitch) * np.sin(roll) + mz * np.sin(pitch) * np.cos(roll)
    mhy = my * np.cos(roll) - mz * np.sin(roll)
    return np.arctan2(-mhy, mhx)

B_ned = np.array([20.0, 0.0, 45.0])                                        # microtesla: north, east, down (inclination about 66 degrees)
for yaw_deg in (0, 45, 90, 200, -120):
    roll, pitch, yaw = np.radians([12, -8, yaw_deg])
    m_body = rot_frd(roll, pitch, yaw).T @ B_ned                            # the field as the body-fixed sensor sees it
    est = np.degrees(heading(m_body, roll, pitch)); err = (est - yaw_deg + 180) % 360 - 180
    assert abs(err) < 1e-9, (yaw_deg, est)
print("tilt-compensated heading recovers yaw for tilted attitudes")
m_body = rot_frd(*np.radians([12, -8, 45])).T @ B_ned
assert abs(np.degrees(heading(m_body, 0.0, 0.0)) - 45) > 3                  # ignoring tilt gives a visible heading error
Barometric altitude and tilt-compensated magnetometer heading, with tests.

Common engineering mistakes

Treating barometric altitude as height above ground

It is relative to a reference pressure that changes with weather; use a rangefinder near the ground.

Calibrating the magnetometer on the bench

The airframe’s iron and currents are part of the field: calibrate on the assembled vehicle, motors off, then check with motors on.

Using GNSS height as precise altitude

Vertical GNSS error is larger than horizontal and the reference is an ellipsoid or geoid.

Ignoring antenna and sensor offsets

Lever arms between GNSS antenna, IMU and centre of mass produce errors in rotation.

Assuming sensors share a clock

Each has latency and a different rate (lesson M7-T8).

Failure modes

Failure modes, symptoms and causes
Failure modeHow it shows upLikely cause
Altitude steps in hoverJumps when speed or wind changesBarometer in a pressure-sensitive location (propwash, airflow)
Heading drift with throttleYaw changes as motors spin upMagnetometer near power wires
GNSS jumpPosition jumps by metresMultipath or fix change
IMU clippingAttitude diverges in vibrationVibration at accelerometer saturation

Debugging approach

  1. Plot each sensor against the EKF estimate and against another sensor of the same quantity.
  2. Run the magnetometer-versus-throttle test.
  3. Inspect IMU spectra and clipping counters.
  4. Log GNSS accuracy estimates and satellite counts next to position error.

Practical exercises

  1. Convert 85000 Pa to altitude with the ISA formula and compare with 1 hPa steps.
  2. Show numerically how a 10∘10^{\circ} tilt error causes a heading error with an inclination of 66∘66^{\circ}.
  3. List which state each of the four sensors makes observable and which remain unobservable without another sensor.
  4. Estimate how fast barometer-only altitude can drift in a 10 hPa per hour weather change.

Interview questions

Write an answer in your own words, then reveal the model answer. Nothing is sent anywhere.

Question 1

What does each of the four core sensors measure and what is its main failure?

Model answer

IMU: rate and specific force, fails by bias drift and vibration; barometer: pressure/height, fails with weather and propwash; magnetometer: field direction, fails near iron and currents; GNSS: position and velocity, fails by blockage, multipath, jamming and spoofing.

Question 2

Why is barometric altitude a relative measurement?

Model answer

It depends on the local sea-level pressure, which changes with weather by several hectopascals per day, each about 8 m of apparent altitude.

Question 3

Why compensate the magnetometer for tilt?

Model answer

The field has a vertical component; when the vehicle tilts it projects into the horizontal sensor axes and produces a heading error unless roll and pitch are used to level the measurement.

References and further reading

Primary papers, official documentation and standards. Each claim above that is specific to a tool or version points here.

What’s next

Mark this lesson

Saved in this browser and synced to your account when you are signed in.

Next lesson

M1-T9: Companion computer architecture

Explain why a companion computer is separate from the flight controller and what crosses the boundary.