Comment 2 for bug 928360

Revision history for this message
Kalle Vahlman (kvahlman) wrote :

First of all, the sensors are not enabled in any of the u8500_android_defconfigs of IK stable branches, so if Benn didn't enable that before testing it's not really surprising that there was no or incorrect data.

Secondly, for me using the steps at the magnetometer page does work, the values react to changing the orientation of the device. Can we get more details about in what configuration/situation the readout does not work?

But let's break the magnetometer operation down:

What it does is measure the ambient magnetic field in Gauss. According to the table at

  http://en.wikipedia.org/wiki/Orders_of_magnitude_%28magnetic_field%29

the reading should be in the ballpark of 300-600 mG depending where on the globe you are, but will of course also vary due to everything around the device, electrical devices (including itself), the surrounding material etc.

The readings from the magnetometer will have gain that changes with the measurement range (sensitivity).

The values from the sensor are read from two 8-bit registers which store the 16-bit value in 2's complement form:

   http://www.igloocommunity.org/gitweb/?p=kernel/igloo-kernel.git;a=blob;f=drivers/hwmon/lsm303dlh_m.c;hb=refs/heads/st-mems-sensors#l323

and are then indeed printed out as 32-bit hexadecimal values:

  http://www.igloocommunity.org/gitweb/?p=kernel/igloo-kernel.git;a=blob;f=drivers/hwmon/lsm303dlh_m.c;hb=refs/heads/st-mems-sensors#l405

As for the reasoning for the 32-bit vs 16-bit representation, I have no idea. It seems unnecessary, though doesn't really hurt either. Here's an example output with default settings and the device "facing down" (green leds towards the desk), which should mean that the z-axis is pointing roughly towards the center of the earth:

     14b:ffffff6d: 213

So that's x:y:z, from which we see that z-axis has the value 0x213 or 531G. This obviously is few magnitudes greater than expected, but that's because of the gain. For the default settings, the gain for z-axis is 950 so we can calculate:

 513G / 950 = 0.54G = 540mG

which seems to fit nicely to the value suggested by the wikipedia page for the latitude of roughly 61° which is where the values were recorded.

The y value above is ffffff6d which indicates that it is negative. As the datasheet states, the values are stored in two's complement form which uses the most significant bit to express negativity:

  http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

And finally, here's the libsensors implementation for GB:

  http://www.igloocommunity.org/gitweb/?p=android/platform/vendor/st-ericsson/hardware/libsensors.git;a=blob;f=sensors.c;hb=refs/heads/igloo-dev#l340

It converts the values to µTesla which is what the Android API uses (1G == 100µT).

I hope this helps making sense of the data, I will add the explanation to the wiki page too.