Since your title does not match your questions, I try to answer as many as possible.
Gyroscopes do not give absolute orientation (like ROTATION_VECTOR ), but only rotational speeds around this axis are built so as to "rotate" around. This is due to the design and construction of the gyroscope . Imagine the design below. The golden thing rotates, and thanks to the laws of physics, it does not want to change its rotation. Now you can rotate the frame and measure these rotations.

Now, if you want to get something like the βcurrent rotational stateβ from the gyroscope, you will have to start with the initial rotation, call it q0
and constantly add those tiny small rotational differences that the gyroscope measures around the axis to it: q1 = q0 + gyro0
, q2 = q1 + gyro1
, ...
In other words: The gyroscope gives you the difference that it rotated around the three constructed axes, so you are not making up absolute values, but a small delta.
Now this is very general and leaves a couple of questions unanswered:
- Where do I get the starting position? Answer: look at the Vector Vector Rotation Sensor - you can use the Quaternion received from there as initialization
- How to "sum" q and a gyroscope?
Depending on the current rotation representation: if you use a rotation matrix, a simple matrix multiplication should complete the task, as suggested in the comments (note that this implementation of the matrix multiplication is not effective!):
public float[] naivMatrixMultiply(float[] B, float[] A) { int mA, nA, mB, nB; mA = nA = (int) Math.sqrt(A.length); mB = nB = (int) Math.sqrt(B.length); if (nA != mB) throw new RuntimeException("Illegal matrix dimensions."); float[] C = new float[mA * nB]; for (int i = 0; i < mA; i++) for (int j = 0; j < nB; j++) for (int k = 0; k < nA; k++) C[i + nA * j] += (A[i + nA * k] * B[k + nB * j]); return C; }
To use this method, imagine that mRotationMatrix
contains the current state, these two lines do the job:
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector); mRotationMatrix = naivMatrixMultiply(mRotationMatrix, deltaRotationMatrix); // Apply rotation matrix in OpenGL gl.glMultMatrixf(mRotationMatrix, 0);
If you decide to use Quaternions, imagine again that mQuaternion
contains the current state:
// Perform Quaternion multiplication mQuaternion.multiplyByQuat(deltaRotationVector); // Apply Quaternion in OpenGL gl.glRotatef((float) (2.0f * Math.acos(mQuaternion.getW()) * 180.0f / Math.PI),mQuaternion.getX(),mQuaternion.getY(), mQuaternion.getZ());
Multiplication of quaternions is described here - equation (23) . Make sure you apply the multiplication correctly, as it is not commutative!
If you just want to know the rotation of your device (I assume that this is what you ultimately want), I highly recommend the ROTATION_VECTOR-Sensor. Gyroscopes, on the other hand, are quite accurate for measuring rotational speed and have a very good dynamic response, but suffer from drift and do not give you absolute orientation (toward magnetic north or gravity).
UPDATE If you want to see the full example, you can download the source code for a simple demo-app from https://bitbucket.org/apacha/sensor-fusion-demo .