1

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

Community
  • 1
  • 1
Simon
  • 9,762
  • 15
  • 62
  • 119

1 Answers1

0

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.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • In your code, could you point me to where you're getting the rotation matrix and doing the multiplication? I'd like to figure out how to implement this myself without relying on another library – Simon Feb 29 '16 at 18:54
  • Also, how is the rotation matrix different from the other ones I listed above? – Simon Feb 29 '16 at 18:54
  • 1
    The types you listed above give you a vector not a matrix. You can get the rotation matrix from a few methods in the SensorManager class. In my code look at the method processDataWithRotationMatrix in the class DSensorProcessor. – Hoan Nguyen Feb 29 '16 at 19:08
  • So to clarify, you use `SensorManager.getRotationMatrix()` into which you pass the gravity and magnetic field sensor values. 2 other things passed into that method are the rotation matrix R and matrix I. Calling that method will then store the rotation matrix values into the passed in matrix R? Then you use R to multiply by the accelerometer values? – Simon Feb 29 '16 at 19:40
  • Do you apply some sort of filter/smoothing to the resulting signal? Because what I get is very rough and sensitive to slight movements of the device – Simon Mar 01 '16 at 17:44
  • You need to average out the result which I did in my library. For any movement however slight you will see the result changes as the coordinate will change. Even when the device is still you will see change. You will need to set a threshold for changes before updating the UI if do not want to see any small change. – Hoan Nguyen Mar 01 '16 at 20:02
  • Thanks. Could you point me to the code section in your library where you apply the moving average? – Simon Mar 01 '16 at 20:06
  • If you look further down the processDataWithRotationMatrix you will see the code to translate accelerometer from device coordinate to world coordinate which involve the class WordHistory. All the code to save the history and averaging is in that class. – Hoan Nguyen Mar 01 '16 at 20:24