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:
- Ensures that magnetic heading is adjusted to a value 0º ≤ hdg ≤ 360º
- 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.