# Reply To: BerryGPS-IMU compass “north”

Home Forums Forums Technical Support for BerryGPS and BerryGPS-IMU BerryGPS-IMU compass “north” Reply To: BerryGPS-IMU compass “north”

#17948
currydem
Participant

In the tutorial for the compass it is written in C and it has a section to compensate for soft iron distortion as well as magnetic declination. In the Python compass code it does not. I tried to mimic a soft iron distortion section but when I do this it doesn’t give me the correct heading. I also get a different heading (an incorrect one) when I use the original Python compass code even after calibration

code below:

Hard iron distortion

C code

magRaw[0] -= (magXmin + magXmax) /2 ;

magRaw[1] -= (magYmin + magYmax) /2 ;

magRaw[2] -= (magZmin + magZmax) /2

Python code

MAGx -= (magXmin + magXmax) /2

MAGy -= (magYmin + magYmax) /2

MAGz -= (magZmin + magZmax) /2

Soft iron distortion

C code

scaledMag[0]  = (float)(magRaw[0] – magXmin) / (magXmax – magXmin) * 2 – 1

scaledMag[1]  = (float)(magRaw[1] – magYmin) / (magYmax – magYmin) * 2 – 1

scaledMag[2]  = (float)(magRaw[2] – magZmin) / (magZmax – magZmin) * 2 – 1

Python code (what I tried to adapt to the Python code)

scaledMagX  = (MAGx – magXmin) / (magXmax – magXmin) * 2 – 1

scaledMagY = (MAGy – magYmin) / (magYmax – magYmin) * 2 – 1

scaledMagZ  = (MAGz – magZmin) / (magZmax – magZmin) * 2 – 1

C code

float heading = 180 * atan2(scaledMag[1],scaledMag[0])/M_PI

Python code (what I tried to adapt to the Python code)

float heading = 180 * atan2(scaledMagY,scaledMagX)/M_PI

*****original code*********

heading = 180 * math.atan2(MAGy,MAGx)/M_P

***************************