Handling of Magnetic Compass HMC5883, possibly others

Post Reply
fokat
Posts: 4
Joined: Tue Jul 15, 2014 12:38 am

Handling of Magnetic Compass HMC5883, possibly others

Post by fokat »

My HK MultiWii Pro flight controller shows a consistent difference in the magnetic heading readings. Apparently this issues has been experienced by other users of the HMC5883 module. In my case (as well as reported by other users of the module) the error is in the order of 30º, very noticeable in the telemetry instrumentation. I've found cases with errors that are much bigger. This issues also sporadically come up with other controllers as well.

Also, I believe that under some circumstances, the magnetic heading calculation might fall out of range.

The patch below from my private git repo introduces the MAG_FIXUP macro, which when defined does two things:


  1. Ensures that magnetic heading is adjusted to a value 0º ≤ hdg ≤ 360º
  2. Adds a custom constant to the magnetic heading calculation, allowing for per-controller correction. The constant is to be specified in degrees of angle

The patch is below

diff --git a/MultiWii/IMU.cpp b/MultiWii/IMU.cpp
index 5e68840..a42a8dd 100644
--- a/MultiWii/IMU.cpp
+++ b/MultiWii/IMU.cpp
@@ -236,6 +236,14 @@ void getEstimatedAttitude(){
EstM32.V.Z * EstG32.V.X - EstM32.V.X * EstG32.V.Z,
(EstM.V.Y * sqGX_sqGZ - (EstM32.V.X * EstG32.V.X + EstM32.V.Z * EstG32.V.Z) * EstG.V.Y)*invG );
att.heading += conf.mag_declination; // Set from GUI
+ #ifdef MAG_FIXUP
+ att.heading = ( att.heading + MAG_FIXUP * 10 );
+
+ // Avoid modulus to speed things up
+ if ( att.heading > 3600 ) att.heading -= 3600;
+ else if ( att.heading < 0 ) att.heading += 3600;
+
+ #endif /* MAG_FIXUP */
att.heading /= 10;
#endif


In my case, (HK MultiWii Pro, using #define HW_MultiWii_328P) I have to set MAG_FIXUP to -32 in order to get a reasonably accurate magnetic reading.
Last edited by fokat on Mon Jul 28, 2014 7:24 pm, edited 1 time in total.

jihlein
Posts: 27
Joined: Sat Sep 08, 2012 3:10 pm

Re: Handling of Magnetic Compass HMC5883, possibly others

Post by jihlein »

I'm not familiar with the HK MultiWii Pro controller, nor the software you're running on it, so take the following for what it's worth......

This sounds like an issue with the HMC5883 scale factors and biases being incorrectly calculated, or not calculated at all. Without the proper scale factors and biases, the HMC5883/HMC5983 will behave exactly as you describe. I have about 12 or samples of these magnetometers, and everyone of them behaves like you describe, until the calibration routines are run, after which they report heading just fine.

The method to calibrate the scale factors can be found in the HMC5883/HMC5983 data sheet, and there are a number of different ways to compute the biases.

fokat
Posts: 4
Joined: Tue Jul 15, 2014 12:38 am

Re: Handling of Magnetic Compass HMC5883, possibly others

Post by fokat »

jihlein wrote:This sounds like an issue with the HMC5883 scale factors and biases being incorrectly calculated, or not calculated at all. Without the proper scale factors and biases, the HMC5883/HMC5983 will behave exactly as you describe. I have about 12 or samples of these magnetometers, and everyone of them behaves like you describe, until the calibration routines are run, after which they report heading just fine.


I agree with your assessment. However my rationale is that if this were the case with the MultiWii code, there would be more reports of this happening.

jihlein wrote:The method to calibrate the scale factors can be found in the HMC5883/HMC5983 data sheet, and there are a number of different ways to compute the biases.


I'll probably revisit this once I get more testing. Thank you for the pointer!

Post Reply