Android – Compute rotation matrix using the accelerometer

accelerometerandroidiphonematrixrotation

I'm currently facing a more mathematical problem. I'm developing an application which is interested in acceleration in X and Y axis. In other words I want to track acceleration which goes left or right and forth or back.

If the device is lying on a table facing up all accelerations in the needed direction are visible in the acceleration values of these axis. A problem occurs is the device not placed in such a position and has a certain rotation around the X or Y axis (pitch, roll).

I need a rotation matrix (Rm) that I can multiply to a measures acceleration (x) vector to gain a new calibrated vector (x') other classes can work with: x' = Rm * x. This should be calculated out of the reference vector (0,0,-1) and the current gravity vector of the device.

I know it has something to do with Euler angles but I can not figure out how I can calculate those and create my rotation matrix with these. I also know that there is a rotation matrix in the CMAttitude class but I would like to have more insight in how these matrix is computed.

Example:

Imagine you want to messure how hard you a braking on your bike. If your iPhone is mounted on the bike with the display pointing straight up you can read the acceleration in the y value of the acceleration vector. Other classes in your project assume that the acceleration when braking can always be seen in this value.

The problem is when the iPhone is placed in portrait view the display pointing to you. Then braking would not increase the y value but the z value instead. So you have to rotate your acceleration value for -90 degree around the x axis.

I need this rotation matrix for arbitrary rotation of the device. I know that it is not possible to calculate the rotation around the z axis from gravity vectors but as long x and y rotations are negated I'm fine.

Thanks a lot for your help

Best Answer

i think that the following link it should be helpful Transforming iPhone Accelerometer Data to Automobile Axes pay attention there is a error in the rotation matrix, the element rm11 it should be cos"Beta"*cos"gamma" and not cos"alpha"*cos"gamma"