
• #2
Sun 2021.09.12
Hi @user134221 does this help?
Would need to convert to Javascript, as code reads pretty much the same.

• #3
. . . just found this post
Google: calculate magnometer site:espruino.com

• #4
Thanks, I'll have a look!

• #5
Thanks, that works! At least, when the Bangle's lying flat. Now I'd like to still get the right angle whatever way the watch is oriented. I'm supposing I could use the accelerometer to find out where down is, and use that to rotate the magnetometers' vector... I don't know if I'm even making sense.

• #6
Glad you got something working! Just some extra info if it helps:
mag.x/y/z
are the raw magnetic field strength values from the magnetometer, which can have some offsetmag.dx/dy/dz
are the 'calibrated' field values  after the magnetometer is on and you move around a bit, it works out the maximum and minimum values, then gives you dx/dy/dz relative to the center of that bounding box. That's what theheading
value works from.
So if you want to work out the actual rotation, with the Bangle still and after you've rotated it around a bit to calibrate the magnetometer, you should be able to:
 Treat
acc.x/y/z
as a vector pointing downwards  Treat
mag.dx/dy/dz
as a vector pointing towards north (or south? can't remember)  Then the crossproduct of those two vectors points east/west
Hi, I've been trying to figure out a way to calculate the orientation (both azimuth and altitude) of my Bangle using both the accelerometer and the magnetometer. The altitude was easy; I just multiply 90º by the accelerometer's Z value. However, to calculate the azimuth is proving to be trickier. I've tried reading the magnetometer's values at several orientations (the values mag.x, mag.y, mag.z, mag.dx, mag.dy, and mag.dz) but don't understand what they mean. Could someone please explain them to me so I can transform them into a vector to calculate the azimuth? Thanks for the help!