Android 从陀螺仪传感器获取旋转角度

Android 从陀螺仪传感器获取旋转角度,android,android-sensors,gyroscope,Android,Android Sensors,Gyroscope,到目前为止,我在网上搜索得很少。 我试图发展一个问题,我需要从手机的起点旋转的角度。为了知道用户是否会向上或向下移动手机,我使用的是加速计,一开始有点不稳定,但我设法使其稳定。 我现在需要它自身的旋转度。像方位传感器一样,它已被弃用。然后我试着用磁强计,但我太不稳定了。 然后,当我使用示例代码时,我决定尝试使用陀螺仪: // This timestep's delta rotation to be multiplied by the current rotation // after com

到目前为止,我在网上搜索得很少。 我试图发展一个问题,我需要从手机的起点旋转的角度。为了知道用户是否会向上或向下移动手机,我使用的是加速计,一开始有点不稳定,但我设法使其稳定。 我现在需要它自身的旋转度。像方位传感器一样,它已被弃用。然后我试着用磁强计,但我太不稳定了。 然后,当我使用示例代码时,我决定尝试使用陀螺仪:

 // 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度?或者我可以得到一些数值,比如手机从起始点转了多少度


提前谢谢

获取3x3旋转矩阵,
R1
R2
,在两个感兴趣的时间点。您想知道使
R1
R2
对齐的旋转
角度

首先计算
R

R = R1 * transpose(R2)
然后计算此旋转的角度:

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

就这些。

非常感谢您抽出时间尝试回答这个问题。我不擅长数学。返回的RotationMatrix不是2D,如何转置?我创建了自己的转置方法:public float[]transposeSmatrix(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[2];res[7]=m[5];res[8]=m[8];返回m;}但是,我无法在java中使用*R1和R2,它说运算符是未定义的。不管怎样,你都可以给我多一点帮助。提前谢谢!维基百科是你的朋友:还有。请检查这个帖子