New: the AI firewall watches the whole platform and explains each incident in plain words. See how it works

Compass heading lags behind on turns, is the filter wrong?

Compass heading lags behind on turns, is the filter wrong?

Solved 6 5

Translated from Türkçe by AI ·

We are working on a small autonomous boat for water quality measurement, which drives along a route to sampling points in ponds. I’m on the data side, but the electronics team is on holiday, so the problem landed on me.For the heading we have a QMC5883L magnetometer, and the ESP32 reads it at 10 Hz. When the boat goes straight the heading is fine, but on turns it lags 2-3 seconds behind: the boat has turned, the value still shows the old one, then slowly catches up. The delay seems different for right and left turns. Route tracking wobbles because of this.The values also go to Hermetry, where we draw the route on a map. There’s also something odd: in the weekly chart the heading sometimes appears around 180, although the boat went north and back that day.

♥ 0

Translated from Türkçe by AI ·

Do you apply a filter to the value that is read?

Sahadan selamlar.
♥ 0

Translated from Türkçe by AI ·

Yes, it’s the average of the last 20 readings. At 10 Hz it takes 2 seconds.

♥ 1

Translated from Türkçe by AI ·

Set the window to 50, it will be more stable.

Ağ geçidi yine bağlanıyor…
♥ 0

Translated from Türkçe by AI ·

Has calibration been done? If hard iron and soft iron calibration has not been performed, different errors appear depending on the turning direction, and the left-right difference may come from that.

♥ 0

Translated from Türkçe by AI ·

Widening the window increases the delay, you need to do the opposite. I went through the same thing once while working with drones.The source of the lag is the average itself: a 20-sample moving average comes in about 1 second late, and the error grows as the turn rate increases. The calibration deficiency Büşra Hanım mentioned also explains the difference between right and left.The solution is to combine it with the gyroscope. The gyroscope sees the rotation instantly but drifts slowly, the magnetometer is slow but does not drift. A complementary filter combines the two:

// dt = 0.1 s, gz = derece/s (z ekseni jiroskop)
float a = 0.98;
heading = a * (heading + gz * dt) + (1 - a) * magHeading;
// 0/360 geçişini düzelt
float fark = magHeading - heading;
if (fark > 180) heading += 360 * (1 - a);
if (fark < -180) heading -= 360 * (1 - a);
heading = fmod(heading + 360, 360);

If the board has no gyroscope, adding an MPU6050 is the cheapest way. First, turn the boat two full circles in the water and perform the hard/soft iron calibration.The 180 issue is separate: an angle cannot be averaged. The average of 359 and 1 comes out as 180. The weekly chart reads hourly aggregated values and the default function is average. On the heading chart, set the function to last, and for analysis send heading_sin and heading_cos alongside the heading; their average comes out correctly.

Kayıtta yoksa olmamıştır.
Accepted solution♥ 7

Translated from Türkçe by AI ·

With the complementary filter the delay has almost disappeared, and the team had already found an MPU6050 anyway. We also did the calibration and the left-right difference is gone. Doing last on the graph also got rid of the 180 nonsense. It was good that I read this post before trying Hakan Bey’s suggestion 🙂

♥ 2
Viewing 7 posts – 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Join the conversation

Sign in to ask and answer, like helpful posts and mark the reply that solved your problem. Every contribution earns points and levels.

Share