Android中陀螺仪传感器适当旋转值的计算

时间:2011-12-16 07:44:25

标签: android gyroscope

我正在使用Android 3.1设备中陀螺仪传感器的方向值旋转矩形。

我必须非常快速地旋转我的设备才能获得1.0或更高的值。

这是代码

final float currentRotVector[] =  { 1, 0, 0, 0 };
if (timestamp != 0)
{
    final float dT = (event.timestamp - timestamp) * NS2S;
    // Axis of the rotation sample, not normalized yet.

    // Calculate the angular speed of the sample
    float omegaMagnitude = (float) Math.sqrt(X * X + Y * Y + Z * Z);

    // Normalize the rotation vector if it's big enough to get the axis
    if (omegaMagnitude > EPSILON)
    {
    X /= omegaMagnitude;
    Y /= omegaMagnitude;
    Z /= 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 = dT * omegaMagnitude / 2.0f;
    float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
    float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
    deltaRotationVector[0] = cosThetaOverTwo;
    deltaRotationVector[1] = sinThetaOverTwo * X;
    deltaRotationVector[2] = sinThetaOverTwo * Y;
    deltaRotationVector[3] = sinThetaOverTwo * Z;

    /* quaternion multiplication 
        Reference: http://www.cprogramming.com/tutorial/3d/quaternions.html
    */

    currentRotVector[0] = deltaRotationVector[0] * currentRotVector[0] - 
                          deltaRotationVector[1] * currentRotVector[1] - 
                          deltaRotationVector[2] * currentRotVector[2] - 
                          deltaRotationVector[3] * currentRotVector[3];

    currentRotVector[1] = deltaRotationVector[0] * currentRotVector[1] + 
                          deltaRotationVector[1] * currentRotVector[0] + 
                          deltaRotationVector[2] * currentRotVector[3] - 
                          deltaRotationVector[3] * currentRotVector[2];

    currentRotVector[2] = deltaRotationVector[0] * currentRotVector[2] - 
                          deltaRotationVector[1] * currentRotVector[3] + 
                          deltaRotationVector[2] * currentRotVector[0] + 
                          deltaRotationVector[3] * currentRotVector[1];

    currentRotVector[3] = deltaRotationVector[0] * currentRotVector[3] + 
                          deltaRotationVector[1] * currentRotVector[2] - 
                          deltaRotationVector[2] * currentRotVector[1] + 
                          deltaRotationVector[3] * currentRotVector[0];
    final float rad2deg = (float) (180.0f / Math.PI);
    RotAngle = currentRotVector[0] * rad2deg;
    axisX = currentRotVector[1];
    axisY = currentRotVector[2];
    axisZ = currentRotVector[3];

    Log.i("Sensor Orientation GyroScope", "axisX: " + axisX + //
        " axisY: " + axisY + //
                    " axisZ: " + axisZ + //
        " RotAngle: " + RotAngle);
}

timestamp = event.timestamp;

我得到了一些输出 axisX:0.69363713 axisY:0.18359372 axisZ:0.0228636 RotAngle:36.7191 并且由于轴值,当设备放在桌子上时,输出矩形看起来被调整。

上面的代码有问题吗?

1 个答案:

答案 0 :(得分:2)

这些值以rad / s为单位。这已从Android 2.3开始标准化

要获得大约1.0的值,你必须以接近60度/秒的速度转动

以前Android版本的某些设备以度/秒返回(返回)值,但这些只是少数几个。例如,带有android 2.2的LG Optimus Black(P970)是返回deg / s的这些设备之一,但这不是常见的情况。