5

Ive searched the internet thin so far. I am trying to develop a problem where i need the degrees of rotation from the phones starting point. To know if the users it moving the phone up or down i am using the accelerometer, which in start was a bit unstable, but however i managed to make it stable. I now need the the degree of rotation around it self. Like the orientation sensor, which is deprecated. I then tryid using the Magnometer, but i was way to unstable. I then determed that i wanted to try using the gyroscope, when i use the sample code:

 // This timestep's delta rotation to be multiplied by the current rotation
 // after computing it from the gyro sample data.
 if (timestamp != 0) {
     final float dT = (event.timestamp - timestamp) * NS2S;
     // Axis of the rotation sample, not normalized yet.
     float axisX = event.values[0];
     float axisY = event.values[1];
     float axisZ = event.values[2];

     // Calculate the angular speed of the sample
     float omegaMagnitude = (float) Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);

     // Normalize the rotation vector if it's big enough to get the axis
     if (omegaMagnitude > EPSILON) {
         axisX /= omegaMagnitude;
         axisY /= omegaMagnitude;
         axisZ /= omegaMagnitude;
     }

     // Integrate around this axis with the angular speed by the timestep
     // in order to get a delta rotation from this sample over the timestep
     // We will convert this axis-angle representation of the delta rotation
     // into a quaternion before turning it into the rotation matrix.
     float thetaOverTwo = omegaMagnitude * dT / 2.0f;
     float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
     float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
     deltaRotationVector[0] = sinThetaOverTwo * axisX;
     deltaRotationVector[1] = sinThetaOverTwo * axisY;
     deltaRotationVector[2] = sinThetaOverTwo * axisZ;
     deltaRotationVector[3] = cosThetaOverTwo;
 }
 timestamp = event.timestamp;
 float[] deltaRotationMatrix = new float[9];
 SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
 // User code should concatenate the delta rotation we computed with the current rotation
 // in order to get the updated rotation.
 // rotationCurrent = rotationCurrent * deltaRotationMatrix;

This code is taking from their documentation, is there anyway i could convert this to 360 degrees? or i could get values like how many degrees the phone was turned away from the starting point?

Thanks in advance.

user1819127
  • 53
  • 1
  • 1
  • 5
  • pls check this thread http://stackoverflow.com/questions/18587262/get-quaternion-from-android-gyroscope – M.Hefny Sep 01 '14 at 10:50

1 Answers1

2

Get the 3x3 rotation matrices, R1 and R2, with getRotationMatrix() at the two timepoints of interest. You would like to know the angle of the rotation R that brings R1 to align with R2.

First calculate R:

R = R1 * transpose(R2)

Then calculate the angle of this rotation:

angle = acos((trace(R)-1)/2)

That is all.

Ali
  • 56,466
  • 29
  • 168
  • 265
  • Thank you very much for taking time to try answering this question. Im not a shark at mathematics. The RotationMatrix returned is not 2D, how to a transpose that? I made my own transpose method: public float[] transposeMatrix(float[] m){ float[] res = new float[9]; res[0] = m[0]; res[1] = m[3]; res[2] = m[6]; res[3] = m[1]; res[4] = m[4]; res[5] = m[7]; res[6] = m[2]; res[7] = m[5]; res[8] = m[8]; return m; } – user1819127 Nov 14 '12 at 20:16
  • But however, im not able to * R1 and R2 in java, it says that the operater is undefined. Any way you could give me a bit more help. Thanks in advance! – user1819127 Nov 14 '12 at 20:17
  • Wikipedia is your friend: [transpose](http://en.wikipedia.org/wiki/Transpose#Examples) and [matrix multiplication](http://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29). – Ali Nov 14 '12 at 20:20