Search code examples
androidcordovaaccelerometerphonegap-build

PhoneGap / Android accelerometer showing strange values


I used information and code from this answer (rewriting code to Javascript) in my simple demo PhoneGap Buld application, to recalculate gravity (G) to real acceleration (m/s2) with 1 second frequency.

This is actual code (important part):

function onAccelerationSuccess(acceleration)
{
    var g = 9.80665;

    acceleration.x = (acceleration.x * g).toFixed(2) + ' m/s\u00b2';
    acceleration.y = (acceleration.y * g).toFixed(2) + ' m/s\u00b2';
    acceleration.z = ((acceleration.z + 1) * g).toFixed(2) + ' m/s\u00b2';
    ...
}

watchID = navigator.accelerometer.watchAcceleration(onAccelerationSuccess, onAccelerationError, {frequency: 1000});

Mentioned answer and many, many sources claims, that with my phone lying on the table face-up, I should get values of (0, 0, -1) G for the x, y, and z axes respectively. Assuming Earth's natural acceleration (g = 9.80665), I should see real acceleration values of (0, 0, 9.81) m/s2 and these values should not change (as phone is resting still). Am I right?

However, actually I'm seeing, that my real values are:

  • X axis: -1.87, -1.50, -2.25,
  • Y axis: 2.26, 1.88, 1.51, 0.76,
  • Z axis: 101.87, 101.49, 102.25, 102.62, 103.37.

These values are constantly changing, but only between these mentioned, and not every axis gets changed value each second. Sometimes, a value for some axis remains for 2-3 seconds.

What is happening? How can a phone variate its acceleration, if it is holding still on my desk? How can any device, that is not moving in any direction have such enormous acceleration like 100 m/s2?

I have heard that accelerators on-board mobile devices are more like toy than real a measurement device and that they're producing a lot of noise or jitter to returned values. But, for God sake, this is a complete garbage, that is making use of this function completely pointless.

I tested this code on Google Nexus (first edition) phone, with Android 4.2.2. App with Phonegap 2.9.0.

EDIT: I've tested my mobile application with Ripple Emulator and I'm getting perfectly valid values:

Acceleration in the X axis is 0.00 m/s².
Acceleration in the Y axis is 0.00 m/s².
Acceleration in the Z axis is 9.81 m/s².

Is something wrong with accelerometer / compass / gyro on-board my Nexus?


Solution

  • There is no bug. I've read many SO questions about iOS native programming in ObjectiveC, when I was dealing with accelerometer. And I missed PhoneGap API documentation, which says, that values passed are already recalculated:

    Acceleration values include the effect of gravity (9.81 m/s^2).

    After removing double gravity calculation all seems to be fine.

    I'm betting values +/-0.3 m/s2 for x and y axis and around 10.3-10.4 m/s2 for z axis, when phone is lying on my desk. But I assume, these are variations and mentioned noise, that is comming from fairly cheap accelerometer chip used in mobile devices.