astro-pi / python-sense-emu

An emulator for the Raspberry Pi Sense HAT
https://sense-emu.readthedocs.io/
Other
40 stars 25 forks source link

Compass readings when pitch and roll are modified #17

Open eah13 opened 7 years ago

eah13 commented 7 years ago

Thanks for a great project!

Currently, the compass reading simply returns the yaw value (as in the sense_hat module, but see below for why this won't work for emulators). This can lead to incorrect values any time roll and pitch aren't set to zero.

First noted in https://github.com/trinketapp/3D-testing/issues/11

I believe the root of this that the sense_hat module reads the yaw from the magnetometer only, and in the underlying RTIMU library this solution uses quaternions to normalize the magnetometer's yaw based on the device's pose, producing the correct output for all poses. You don't use quaternions internally (I think - and neither do we), so might be a tough fix.

I believe there is a non-quaternion calculation that could achieve the same effect of normalizing the magnetometer's readings but it's beyond my mathematical skills to derive it. If I've diagnosed this correctly and you can determine how to correctly read the compass when pitch and roll are set, we'd definitely piggy back on your code to fix this in our emulator. Likewise, if we can produce a solution I'll leave a note here.

eah13 commented 7 years ago

I found a formula that seems to correctly derive the heading, given the accelerometer's tilt. We're probably going with this as a fix as it seems to produce the expected values. The math was totally lifted though :)

https://github.com/trinketapp/skulpt/pull/38/files?w=1#diff-91b04424786e96fdd31847054a33059fR269

Edit: that math wasn't quite right. After much banging of the head against trigonometric walls, this should be the correct code: https://github.com/trinketapp/skulpt/pull/38/commits/ad8c30b1838e8acaba2f722f793e8528b2cace90

Hope that helps!

TweetPete commented 7 years ago

So you needed to transform the raw magnetometer values into the proper plane, I guess? I'm kinda doing the same algorithms in my thesis project so the sense-hat-emulator is a interesting application for me. really cool !

eah13 commented 7 years ago

@TweetPete correct. The tilt compensation translates the magnetometer's readings into the horizontal plane.