Search code examples
androidaccelerometerandroid-sensorscoordinate-systemscoordinate-transformation

Android rotation matrix for accelerometer transformation


If I wanted to transform my acceleration values from the phone coordinate system to the world coordinate system, which rotation matrix should I be using from the sensor manager? Furthermore, if I were to multiple my accelerometer values by each of the below, what would I actually get as a result?

There are quite a few listed in the dev docs:

  • TYPE_ROTATION_VECTOR
  • TYPE_GAME_ROTATION_VECTOR
  • TYPE_GEOMAGNETIC_ROTATION_VECTOR
  • TYPE_ORIENTATION
  • There also seems to be a getRotationMatrix method offered by the API but I'm not sure whether this is a combination of the above, or something entirely different

I've seen a few threads on this (e.g. Acceleration from device's coordinate system into absolute coordinate system) but they're fairly old and I'm not sure if anything new/more useful has been added to android since


Solution

  • You need to get the Rotation Matrix and then the product of this matrix and the accelerometer values in onSensorChanged will give you the coordinate in World coordinate system.

    You can download my library at https://github.com/hoananguyen/dsensor and in your code just call DSensorManager.startDSensor(context, DSensor.TYPE_WORLD_ACCELEROMETER, listener). Make sure to call DSensorManager.stopDSensor when the sensor is not needed anymore.