5

I do not know if here is the best place to post this question. I have a 9-DOF sensor (MPU 9150) and I want to use its magnetometer to retrieve the rotation angle, from 0 to 359. However, its library returns three values: x, y and z.

I do not know how to transform this into a single value. There's any way to do this?

  • It is giving you the vector that runs along the local magnetic field -- ideally it points to magnetic north. The rotation mapping between the measurement and magnetic north should give you your heading. – BrettAM Dec 12 '15 at 4:43
  • 3
    Reading the datasheet could be very enlightening. Have you tried it? – Majenko Dec 12 '15 at 11:16
  • @Majenko Yes, but nothing really useful. – Guilherme Dec 13 '15 at 1:56
9

The answer given by Dave X, namely

az=90-atan(y/x)*180/pi

is on the right track. However, it cannot work as is for two reasons:

  1. The formula fails badly whenever x = 0,

  2. It gives the same azimuth for x = y = 1 and for x = y = −1. In other words, it cannot differentiate between north-east and south-west.

The formula could be fixed by replacing atan(y/x) by a function that returns:

  • atan(y/x) if x > 0
  • π/2 if x = 0 and y > 0
  • −π/2 if x = 0 and y < 0
  • atan(y/x) + π if x < 0 and y ≥ 0
  • atan(y/x) − π if x < 0 and y < 0

That would be essentially a two-argument, “quadrant-aware” version of the arctangent, yielding results in (-π, π] instead of (−π/2, π/2).

Fortunately, such function already exists. It is a very standard function of the C library and is called atan2(). Thus, the correct formula is

az = 90 - atan2(y, x) * 180 / M_PI;

Now, since atan(y/x) = π/2 − atan(x/y), the formula above can be simplified into

az = atan2(x, y) * 180 / M_PI;

This formula gives the direction of the (x, y) vector counted clockwise from the y axis. It is likely that the OP wants instead to know the heading of some kind of vehicle. The correct formula depends then on how the magnetometer has been mounted relative to the vehicle. If we assume that the x axis of the magnetometer points forward and the y axis points to the left, then:

  • the vehicle is heading north (0° magnetic heading) when the horizontal component of the magnetic field is along +x
  • the vehicle is heading east (90° magnetic heading) when the horizontal component of the magnetic field is along +y

From here, it follows that the vehicle heading is

heading = atan2(y, x) * 180 / M_PI;

Obviously, the magnetic declination has to be added if one is interested in the true (rather than magnetic) heading.

| improve this answer | |
2

This is a basic math problem.

az=90-atan(y/x)*180/pi

"atan()" is arctangent and converts from coordinate variables to angle in radians, *180/pi converts from radians to degrees, and 90- converts from degrees math angles (degrees CCW from the x axis) to azimuth (degrees CW from north).

Calibrating the sensor is a trickier problem.

| improve this answer | |
2

You first need calibration (either using the device calibration method, or, by rotating it in a complete circle to ascertain the full field strengths).

You can then use the X, Y, and Z readings to compute the location of "north" relative to whichever way you're pointing (keeping in mind that "north" changes as much as 30 degrees depending where on earth you are (called declination - see http://www.magnetic-declination.com/), and that it's not horizontal to the ground either (confusingly, called "inclination" - see https://physics.stackexchange.com/questions/283761/if-you-hold-a-compass-needle-vertical-does-it-point-down-or-up-differently-on-wh/283782 )

If you picture the X, Y, and Z readings as meaning "where on an imaginary circle is the direction of magnetism - where those 3 circles are on 3 different planes, you can compute the logical position of a vector pointing towards magnetic north in 3 dimensions.

The bad news - there's no simple way to do what you want accurately, and definitely not one that's going to work without trig math on all 3 readings (you can't just use X and/or Y alone).

The good news - you probably don't really need to know an actual "heading" - most applications just need some local relative direction as compared with something you already know.

Beware that strong magnets (such as ones in all modern motors) seriously mess with readings from a long way away (10 or more foot even) - grab an app from your phone store that shows your phone magnetometer readings, and wave a magnet around to see this easily!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.