Drone compass goes off, heading shows the wrong direction
Compass calibration passed, offsets are normal, and the nose heading is still off. The cause is most often motor and power cable interference. How to measure it and how to fix it.
Mission Planner is open and the drone sits on the ground with its nose pointing north. The compass on screen shows a completely different heading. You've repeated the calibration for the third time and nothing changes.
At this point most teams try calibration once more. But if calibration already passed, the problem isn't the compass itself, it's what surrounds the compass.
Is the deviation constant, or does it change with throttle
Compass deviation has three different signatures, and each points to something different. First you need to find out which one you're dealing with; the fix comes after.
- The deviation is constant and always in the same direction: the mounting orientation is wrong. Either the orientation setting or the external compass's physical placement is faulty.
- Correct while the drone sits on the ground, drifts when you open the throttle: motor and power cable interference. The most common case, and the one that wastes the most time.
- The deviation is random and doesn't repeat: the calibration was really done badly or a sensor is failing.
This article focuses on the second item, because on a system that passed calibration, it's the most frequent cause.
What disturbs the compass
The magnetometer measures the Earth's magnetic field. This field is weak. The current drawn by the motors and the power cable also produces a magnetic field, and the compass can't tell the two apart; it measures their sum.
What the compass measures depending on where it sits
Look at the dashed rings in the figure. The internal compass inside the body stays in the power cable's field; the external compass on top of the GPS mast is outside that field.
What matters is distance. Because field strength falls with the cube of distance, when you move the compass twice as far from the power cable, the interference drops to about one eighth. In practice this means taking the compass out of the body and moving it to the GPS mast.
How much the interference drops as you move the compass away
The curve shows why increasing the distance is the first fix: even moving the compass a little farther away quickly reduces interference.
How to measure on your own board
You can only find out how much interference there is on your own board, with real hardware. If the magnetic field strength |B| climbs as throttle increases, there is interference. That difference is what you need to measure.
The code below connects to the autopilot, reads the throttle level and the magnetic field strength at the same time, and prints what percent it has deviated from the baseline value.
The most common mistake: testing with the propellers on. Remove the propellers, secure the drone to the ground, and only then open the throttle gradually. A throttle test done with the propellers on causes serious injury.
# Compass interference measurement - run it on your own board
# REMOVE the propellers, secure the drone to the ground, then raise the throttle gradually.
import math
from pymavlink import mavutil
BAGLANTI = 'tcp:127.0.0.1:5760' # telemetry: '/dev/ttyUSB0' or 'udp:0.0.0.0:14550'
SURE_SN = 45
arac = mavutil.mavlink_connection(BAGLANTI, baud=57600)
arac.wait_heartbeat(timeout=30)
print('Connected. sysid=%d' % arac.target_system)
# Without this line telemetry does not flow
arac.mav.request_data_stream_send(
arac.target_system, arac.target_component,
mavutil.mavlink.MAV_DATA_STREAM_ALL, 10, 1)
gaz, olcumler = 0, []
import time
bitis = time.time() + SURE_SN
print('%-8s %-9s %-9s' % ('throttle%', '|B|', 'deviation%'))
while time.time() < bitis:
m = arac.recv_match(type=['SERVO_OUTPUT_RAW', 'SCALED_IMU2'],
blocking=True, timeout=2)
if m is None:
continue
if m.get_type() == 'SERVO_OUTPUT_RAW':
gaz = max(0, min(100, (m.servo1_raw - 1000) / 10.0))
continue
x, y, z = m.xmag, m.ymag, m.zmag
if x == 0 and y == 0 and z == 0:
continue
B = math.sqrt(x*x + y*y + z*z)
olcumler.append((gaz, B))
if len(olcumler) % 25 == 0:
taban = olcumler[0][1]
sapma = 100.0 * (B - taban) / taban
print('%-8.0f %-9.1f %-+9.2f' % (gaz, B, sapma))
if olcumler:
B0 = olcumler[0][1]
en_kotu = max(olcumler, key=lambda t: abs(t[1] - B0))
fark = 100.0 * abs(en_kotu[1] - B0) / B0
print('\nBaseline |B| = %.1f mGauss (throttle %.0f%%)' % (B0, olcumler[0][0]))
print('Largest deviation = %%%.2f (throttle %%%.0f, |B| = %.1f)'
% (fark, en_kotu[0], en_kotu[1]))
print('SONUC: %s' % ('INTERFERENCE - move the compass away' if fark > 5
else 'interference is acceptable'))Change the connection line to match your own link: /dev/ttyUSB0 for USB, udp:0.0.0.0:14550 for a telemetry bridge. Open the throttle slowly and wait a few seconds at each step.
Easy to interpret. A deviation of up to five percent from the baseline is livable. If it exceeds ten percent, the autopilot loses its heading in flight, and in LOITER mode the widening circles known as toilet bowling begin.
What to do if the measurement is bad
Don't change the order. First increase the distance, then fix the software side. A physical problem you try to cover up with a parameter comes back.
- Move the compass to the GPS mast, at least 10–15 cm away from the power cable.
- Twist the power cables together. When the outgoing and return wires are bundled together, the fields they produce largely cancel each other out.
- Use the external compass and disable the internal compass inside the body.
- Verify the COMPASS_ORIENT value. A wrong orientation comes back as an inconsistency between the two compasses.
These are the parameters to check after the physical changes:
COMPASS_EXTERNAL = 1 # external compass is used
COMPASS_ORIENT = 0 # mounting orientation (a wrong one gives inconsistency)
COMPASS_USE = 1 # use the primary compass
COMPASS_USE2 = 0 # disable the internal compass
COMPASS_OFFS_MAX = 1800 # offset upper limit (default)After changing the parameters, restart the autopilot and redo the compass calibration from scratch. The old offsets don't fit the new placement.
Error Messages and Their Meanings
The messages you'll see in Mission Planner's message window and what they mean.
PreArm: Compass not calibrated
The compass was never calibrated, or the calibration values were erased. Complete the live calibration in Mission Planner and restart the autopilot.
PreArm: Compasses inconsistent
There are multiple compasses and the heading difference between them exceeds the allowed limit. The cause is almost always a wrong COMPASS_ORIENT or one compass being under interference.
PreArm: Compass offsets too high
The offset value from the calibration result exceeded the COMPASS_OFFS_MAX limit (default 1800). This generally shows the compass is inside a constant magnetic field: the battery, the power distribution board, or a magnet in the body.
PreArm: Check mag field
The measured magnetic field strength is outside the expected range. The message writes the measured value together with the accepted lower and upper limits. A very high value means nearby metal or current; a very low value usually means a cable or sensor fault.
Bad Compass Health
The autopilot can't get healthy data from the compass. First check the I2C or CAN cable and its solder joints. A connector that works loose under vibration is the most common cause of this message.
What changes in the field
A measurement you make on the bench doesn't tell the whole story of the field. Rebar in the floor, parking lots, containers and vehicles deflect the compass while on the ground. This effect drops quickly once the drone is airborne.
Choose the takeoff point away from reinforced concrete floors and large metal structures. If a small deviation you see on the ground corrects itself in the air, the cause is the ground, not the drone itself.
Example: In the pit area you put the drone right next to a metal container and the service vehicle, and the arrow on the map deviates noticeably from the direction the nose is pointing. Before rushing to calibration, move the vehicle a few meters forward onto open ground and look at the arrow again. If the deviation shrinks, your compass is fine and the ground was deflecting it. Take the takeoff point onto that open ground too, not next to the container.
At the competition site, don't forget this too: the magnetic declination angle changes from city to city. The COMPASS_AUTODEC parameter calculates it automatically from the GPS position; don't turn it off by hand.
Which course includes this topic
A step-by-step guide to compass calibration, board-by-board differences and diagnosing the other sensors is covered in the Calibration section of the Drone Basics: From Sensors to Flight course.
Was this article helpful?
Learn this topic from start to finish
Season plan: all six courses for 9 months, ₺790 a month. ₺390 a month for high school and university students, ₺351 the first month with your member code. You can stop in any month you like. Start the Season plan · I'm a student