Search code examples
androidsensorsmagnetometer

getting info from magnetic sensors


I'm listening for sensor values, and getting them OK in my handler with

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
       for (i=0;i<3; i++) mag[i] = event.values[i];

The developer docs say for Sensor.TYPE_MAGNETIC_FIELD you get: "All values are in micro-Tesla (uT) and measure the ambient magnetic field in the X, Y and Z axis."

How can I turn that info into azimuth, pitch, and roll? How do I turn it into compass magnetic heading?

Does just provide the same info you can get from the ORIENTATION sensor, and from the ACCELEROMETER sensor once you have done the matrix transforms?


Solution

  • If you have access to the accelerometer, you can use the getOrientation() method, which returns azimuth, pitch, and roll as determined from the two sensors, and calculated via the getRotationMatrix() method.

    EDITS

    Response to comments... I see your question now. Sorry. With just the 3-axis magnetometer, you can't accurately get azimuth, pitch, and roll except when the device is parallel to the surface of the earth. Remember using a needle compass and having it either a) on a spindle so you can see and manually compensate for tilt, or b) sealed in liquid so you can get the bubble level to the center? To get azimuth:

    azimuth = arctan(y/x)
    

    Now, if you tilt the phone, the errors in the x/y plane sensing can get big. Now you need to figure out the tilt (with the accelerometer), then measure the Z-axis magnetic strength, and then use the three to compensate. This is essentially what you know as getRotationMatrix() and getOrientation().

    Part of the reason I think Sensor.TYPE_ORIENTATION was deprecated in API 8 was because there isn't a separate orientation sensor per se. It is a combination of magnetic field and acceleration sensing.

    A good paper that I dug up that covers it all:

    http://www.ssec.honeywell.com/position-sensors/datasheets/sae.pdf