Page 1 of 1

Sense Hat Magnetometer - Reporting incorrect degrees

Posted: Fri Jul 14, 2017 3:49 pm
by bonzaixx1
I've seen similar issues posted around but I have been unable to get anything to work.

My project is a Pi with a Sense Hat sitting on top of a Roomba, which is controlled through the Pi. I am trying to have the Roomba rotate 90 degrees to the left from where it starts and then stop. I have it rotating and reading in the current degree, but the magnetometer never reports a value outside of the range 70-150, when it hits the edge of that range it just goes in reverse from there (ex: 71 -> 70 -> 71 again).

I have calibrated the sense hat twice now, same results both times. I am noticing one thing, which is that in the calibration example on the Pi site (https://www.raspberrypi.org/documentati ... sense-hat/) it shows the following values:
Min x: 51.60 min y: 69.39 min z: 65.91
Max x: 53.15 max y: 70.97 max z: 67.97

and the values I got were never more than low 40s. Did I somehow calibrate it wrong? I moved it every way I possibly could and the values stopped changing.

I am using dir = sense.get_compass() to get the degree value.

Re: Sense Hat Magnetometer - Reporting incorrect degrees

Posted: Fri Jul 14, 2017 7:39 pm
by bensimmo
Is there any big magnets near it (like motors etc).
They may be interfering.

Does it do the same not on the roomba?

Wouldn't sense.get_orientation() work better for this task. Using all three sensors to work it out.
You also need to keep the polling loops quite fast iirc.

Re: Sense Hat Magnetometer - Reporting incorrect degrees

Posted: Thu Aug 10, 2017 8:54 am
by Davespice
Hi there

So that function turns off the gyroscope and accelerometer then only uses the magnetometer to give you the angle. The magnetometer is actually perturbed by all the metal in the USB ports and any casing you've got. Calibration helps to mitigate it but doesn't make it perfect.

What I think will help you is to also use gyroscope and accelerometer to get the angle. The get_orientation function will give you pitch, roll and yaw - and yaw is basically the same as the compass angle.

Try this:

Code: Select all

self.set_imu_config(True, True, True)  # Turns on gyroscope, accelerometer and magnetometer

while True:
    o = get_orientation()
    dir = o["yaw"]
    print(dir)

Re: Sense Hat Magnetometer - Reporting incorrect degrees

Posted: Fri Aug 18, 2017 9:44 am
by bensimmo
Is it possible to use the runtime calibration with the sensehat?
So I can do the old figure of 8 we do with mobile phones?

https://github.com/RTIMULib/RTIMULib2/b ... /README.md

Re: Sense Hat Magnetometer - Reporting incorrect degrees

Posted: Fri Aug 18, 2017 3:57 pm
by Davespice
There is the ellipsoid fit calibration you can use, more info on this thread.
viewtopic.php?f=104&t=109064&p=750616#p810193

Re: Sense Hat Magnetometer - Reporting incorrect degrees

Posted: Sat Aug 19, 2017 7:40 am
by bensimmo
Yes, but that's not at runtime and would need to be done every time you move it to a different environment if you are having problems or wanted a more 'accurate' reading.
If it was at run time, you could do the figure of 8's as a quick calibration and be on your way. It also says it can increase the boundaries as it goes.
Being in my lab is different to being out in a field and in fact here in the classrooms at a school quite different from room to room as there are a lot of equipment around and as anyone trying to use plotting compasses for empa/isa/iaa practicals would have found out, it can be a nightmare for magnetic readings ;-)

Would that need the sensehat API updating to use V2 or does it already (it just say custom in the python code). Though neither versions are developed by the original person anymore.

Just thinking of ways to make this easier than write some code to read write do save etc, given the option is there in the Lib2