Bad compass headings from 20948 on Sparkfun auto-phat magnetometer

Hi,

I have a problem using the Sparkfun auto-phat. I cannot get reasonable compass headings from the raw IMU magnetometer

data. My code is below. With a regular Silva compass, when I point the phat towards magnetic North I get a heading around

92-95. When I point it due South, I get a heading around 66-68. Never does the heading get above 100.

Here is the code which I swiped and edited from the example for the qwiic board.

I suspect I have to pre-process the magnetometer data. But I tried that too. Waving my robot around in the air and rotating on the (I think) x-axis, then getting a min-max range and then getting the proportions that represent rawMx and rawMy. That didn’t work either. Any help or suggestions would be greatly appreciated.

import qwiic_icm20948
import time
import sys
import math

def runExample():
	print("\nSparkFun 9DoF ICM-20948 Sensor  Example 1\n")
	IMU = qwiic_icm20948.QwiicIcm20948()
	if IMU.connected == False:
		print("The Qwiic ICM20948 device isn't connected to the system. 
Please check your connection", \
			file=sys.stderr)
		return
	IMU.begin()
	while True:
		if IMU.dataReady():
			IMU.getAgmt() # read all axis and temp from sensor, note
 this also updates all instance variables
			deg = math.atan2(IMU.myRaw,IMU.mxRaw) * 180/math.pi
			print("HEADING: ",deg)
			time.sleep(0.03)
		else:
			print("Waiting for data")
			time.sleep(0.5)
 
if __name__ == '__main__':
	try:
		runExample()
	except (KeyboardInterrupt, SystemExit) as exErr:
		print("\nEnding Example 1")
		sys.exit(0)

Magnetometers don’t work out of the box, and need to be carefully calibrated to be useful. Various approaches are described in this comprehensive tutorial:

https://thecavepearlproject.org/2015/05 … r-arduino/