【问题标题】:Calculation of proper Rotation Values from Gyroscope Sensor in AndroidAndroid中陀螺仪传感器正确旋转值的计算
【发布时间】:2011-12-16 07:44:25
【问题描述】:

我正在使用 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;

我得到了一些输出,比如 轴X:0.69363713 轴Y:0.18359372 轴Z:0.0228636 旋转角度:36.7191 并且由于轴值的原因,当设备放在桌子上时,输出矩形看起来会发生调整。

上面的代码有问题吗?

【问题讨论】:

  • 我的猜测是它的单位是 rad/s。那么有这么小的值是正常的。
  • 好的...谢谢。现在,我转换为度数。
  • 你能解释一下你做了什么来解决这个问题吗?您是否在处理之前将 X、Y 和 Z 值转换为度数?

标签: android gyroscope


【解决方案1】:

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

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

某些具有早期 Android 版本的设备以度/秒为单位返回(返回)值,但这只是其中的一小部分。例如,带有 android 2.2 的 LG Optimus Black (P970) 是这些返回 deg/s 的设备之一,但这并不常见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多