# Continuously Self Calibrating UAV Compass

## Can we automate compass calibration?

In this article I propose a system that continuously and dynamically self calibrates the magnetometers on a flying UAV so that manual calibration is no longer ever needed.

With traditional UAVs, one the most important steps before launching your UAV is calibrating the magnetometers. However, magnetometers are also one of the most unpredictable and troublesome sensors on your UAV. Electric motors, environmental factors, and many other things can significantly interfere with the accuracy and consistency of the magnetometer readings. Your UAV uses the magnetometer (electronic compass) to compute its heading and thus navigate correctly through the sky.

## We can accurately estimate heading without a compass

Most UAV autopilots do depend on the magnetometer to determine heading. However, their is a class of ‘attitude estimators’ that run entirely on gyros, accelerometers, and gps. These estimators can accurately compute roll, pitch, and yaw by fitting the predicted position and velocity to the actual position and velocity each time a new gps measurement is received. In order for these estimators to work, they require some amount of variation in gps velocity. In other words, they don’t estimate heading very well when you are sitting motionless on the ground, hovering, or flying exactly straight and level for a long time.

## A compass is still helpful!

For fixed wing UAVs it is possible to fly entirely without a magnetometer and still estimate the aircraft attitude accurately throughout the flight. However, this requires some carefully planned motion before launch to help the attitude estimate converge. A compass is obviously still helpful to improve the attitude estimate before launch and in the initial moments of the launch – before the inertial only system sees enough change in velocity to converge well on it’s own. A compass is also important for low dynamic vehicles like a hovering aircraft or a surface vehicle.

## Maths and stuff…

The inertial only (no compass) attitude estimator I fly is developed at the University of Minnesota UAV lab. It is a 15-state kalman filter and has been at the core of our research here for over a decade. A kalman filter is a bit like a fancy on-the-fly least squares fit of a bunch of complicated interrelated parameters. The kalman filter is based on minimizing statistical measures and is pretty amazing when you see it in action. However, no amount of math can overcome bad or poorly calibrated sensor data.

## So Here We Go…

First of all, we assume we have a kalman filter (attitude and location estimator) that works really well when the aircraft is moving, but not so well when the aircraft is stationary or hovering. (And we have that.)

We know our location and time from the gps, so we can use the World Magnetic Model 2015 to compute the expected 3d vector that points at the north pole. We only have to do this once at the start of the flight because for a line of sight UAV, position and time doesn’t change all that much (relative to the magnetic pole) during a single flight.

During the flight we know our yaw, pitch, and roll. We know the expected direction of the magnetic north pole, so we can combine all this to compute what our expected magnetometer reading should be. This predicted magnetometer measurement will change as the aircraft orientation changes.

The predicted magnetometer measurement and the actual magnetometer measurements are both 3d vectors. We can separate these into their individual X, Y, and Z components and build up a linear fit in each axis separately. This fit can be efficiently refined during flight in a way that expires older data and factors in new data. When something changes (like we travel to a new operating location, or rearrange something on our UAV) the calibration will always be fixing and improving itself as soon as we launch our next flight.

Once we have created the linear fit between measured magnetometer value and expected magnetometer value in all 3 axes, we can use this to convert our raw magnetometer measurements into calibrate our magnetometer measurements. We can feed the calibrated magnetometer measurements back into our EKF to improve our heading estimate when the UAV is on the ground or hovering.

## Does this actually work?

Yes it does. In my own tests, I have found that my on-the-fly calibration produces a calibrated magnetometer vector that is usually within 5 degrees of the predicted magnetometer vector. This error is on par with the typical cumulative attitude errors of a small UAS kalman filter and on par with a typical hand calibration that is traditionally performed.

## Source Code

The source code for the University of Minnesota 15-state kalman filter, along with a prototype self calibration system, and much more can be found at my AuraUAS github page here: https://github.com/AuraUAS/navigation

This article is fairly rough and I’ve done quite a bit of hand waving throughout. If you have questions or comments I would love to hear them and use that as motivation to improve this article. Thanks for reading!