Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert raw xyz Magnetometer data to a heading?

I'm using an embedded device with a simple 3-axis Magnetometer on it. I have it currently displaying the X Y Z values in micro Teslas but how do I convert these into a compass heading? I have tried looking it up on Google but everything I find seems extremely complicated/poorly explained.

If possible I'd like to know how to do it both tilt-compensated and also without compensating for tilt.

The values I'm currently getting on a flat surface for X, Y, Z are 70,0.8 and 34.1 respectively in case that somehow helps.

P.S In case it helps here is a snippet of the code I'm using for the magnetometer:

mSensor.enable();

while(true){
    wait(1);
    mSensor.getAxis(mData);
    lcd.cls();
    lcd.locate(0,3);
    lcd.printf("X=%4.1f micro-Tesla\n\rY=%4.1f micro-Tesla\n\rZ=%4.1f micro-Tesla", mData.x, mData.y, mData.z);
like image 647
Cypher236 Avatar asked Feb 24 '16 11:02

Cypher236


People also ask

What is XYZ on a magnetometer?

Sparki has a 3-Axis magnetometer. It is used to detect the magnetic field Sparki is experiencing. This can mean the earth's magnetic field, the field generated by Sparki's motors and wires, or many other sources. It can do this in all 3 XYZ axis, which are left/right (X), forward/backwards (Y), and up/down (Z).

What is magnetometer data?

The magnetometer measures the intensity of the magnetic fields around the camera. By measuring the earth's magnetic field, the sensor can estimate the camera's absolute orientation according to the north magnetic pole. Magnetometer values are stored in the SensorData::MagnetometerData which can be accessed with: C++


1 Answers

Without compensation for tilt it's not too complicated:

angle = atan2(Y, X);

That's actually the same as converting a vector in cartesian coordinates [X, Y] to a vector in polar coordinates [length, angle], without needing the length.

For the tilt-compensated version, you need to project the 3D-vector [X, Y, Z] onto a plane [X2, Y2] first and then use the same formula as before. But to do that you would need an accelerometer to know the amount of tilt.

like image 155
alain Avatar answered Nov 06 '22 01:11

alain