22

What is the difference between gravity and acceleration sensors in Android? From my point of view the physical value is the same in both cases.

Which one measures the force acting on unit mass inside the device?

ADDITION

The question is: what physical quantity is measured by these sensors? According to equivalence principle the acceleration and gravity are indistinguishable and the only way to measure both is by usual (but 3d) spring balance.

Suzan Cioc
  • 26,725
  • 49
  • 190
  • 355

2 Answers2

12

Acceleration sensor gives you back the sum of all forces applied to your device, while Gravity sensor returns only the influence of gravity. If you want to exclude the gravity from acceleration, you may use a high-pass filter or just subtract the gravity sensor values from acceleration sensor values -- not sure which method gives better precision.

Another method could be using Sensor.TYPE_LINEAR_ACCELERATION, which gives exactly (acceleration - gravity), however you should check if it's available on the device. I've found a few devices, which have ACCELERATION sensor working, but no response from GRAVITY or LINEAR_ACCELERATION sensors.

lenik
  • 21,662
  • 4
  • 29
  • 38
  • So you mean that gravity sensor is just acceleration sensor passed via low-pass filter? – Suzan Cioc May 04 '12 at 10:52
  • 2
    most probably, since GRAVITY became available since API version 9, and I don't think my phone got any hardware change when I have got it upgraded =) – lenik May 04 '12 at 11:00
  • well,i've just tested, my old phone does not return any GRAVITY updates, though it runs the code without complains. maybe GRAVITY is a separate sensor, but if you are planning to support Android 2.1, probably high-pass filter on ACCELERATION sensor is your best bet. – lenik May 04 '12 at 11:17
  • btw, why don't you use Sensor.TYPE_LINEAR_ACCELERATION instead? It gives exactly (acceleration - gravity), must be quite easy to use. – lenik May 05 '12 at 03:02
  • LINEAR_ACCELERATION is also available since API 9: Field requires API level 9 (current min is 8): android.hardware.Sensor#TYPE_LINEAR_ACCELERATION – fersarr Feb 05 '14 at 06:06
  • seems like a filter would produce a lot of potential error, especially if i want to try to double integrate acceleration to produce position. in my case at rest my calculations claim my device is travelling faster than light after only a few minutes! – Michael Oct 05 '14 at 00:13
  • 2
    Nice answer. FYI, TYPE_LINEAR_ACCELERATION and TYPE_GRAVITY are computed from accelerometer, magnetometer, and gyrometer data using a Kalman Filter, see source code https://github.com/android/platform_frameworks_base/tree/ics-mr1/services/sensorservice – dcoz Feb 08 '15 at 01:32
  • @lenik, do you mean `getDefaultSensor` is returning a non-`null` sensor for `TYPE_GRAVITY` and `registerListener` is returning `true` but `onSensorChanged` is never called? – Sam Nov 22 '15 at 06:43
3

This link might be helpful: http://www.sensorplatforms.com/which-sensors-in-android-gets-direct-input-what-are-virtual-sensors

The following excerpt summarize the answer for your question:

"The list... includes both physical sensors and sensor types with values derived from physical sensors, sometimes these are called virtual sensors... The virtual sensors types (gravity, linear acceleration, and rotation vector) provide values derived by combining the results from physical sensors intelligently... The rotation vector is a combination of the accelerometer, the magnetometer, and sometimes the gyroscope to determine the three-dimensional angle along which the Android device lays with respect to the Earth frame coordinates. By knowing the rotation vector of a device, accelerometer data can be separated into gravity and linear acceleration."

This link https://stackoverflow.com/a/8238389/604964 also says that the gravity values are computed using a Butterworth filter.

Community
  • 1
  • 1
cesarsalgado
  • 966
  • 6
  • 15