从陀螺仪安卓传感器获得旋转度

时间:2012-11-12 19:58:08

标签: android android-sensors gyroscope

到目前为止,我已经在互联网上搜索过了。 我正在尝试开发一个问题,我需要从手机起点的旋转度。要知道用户上下移动手机我是否正在使用加速度计,这在开始时有点不稳定,但是我设法使其稳定。 我现在需要绕自己旋转的程度。像方向传感器一样,不推荐使用。然后我尝试使用Magnometer,但我是不稳定的。 当我使用示例代码时,我确定我想尝试使用陀螺仪:

 // 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;

此代码取自他们的文档,无论如何我可以将其转换为360度?或者我可以得到一些价值,比如手机从起点转过多少度?

提前致谢。

1 个答案:

答案 0 :(得分:1)

在感兴趣的两个时间点获取3x3轮换矩阵R1R2getRotationMatrix()。您想知道angle的{​​{1}} RR1R2对齐。

首先计算R

R = R1 * transpose(R2)

然后计算这个旋转的角度:

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

就是这样。