8

我已经有这个问题太久了。此代码应输出加速度计的 dx,dy,dz 以及 dx 的运行总和。它还应该输出方位角、俯仰角和滚动角。

我已经使用了此处提供的信息,但无济于事。

此代码无法正确输出俯仰、方位角或滚动。它分别为最后三个文本视图输出 0.0、-0.0、-0.0。

switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        accelerometerValues = event.values.clone();
    case Sensor.TYPE_MAGNETIC_FIELD:
        geomagneticMatrix = event.values.clone();
        sensorReady = true;
        break;
    default:
        break;
}   

if (geomagneticMatrix != null && accelerometerValues != null && sensorReady) {
    sensorReady = false;

    float[] R = new float[16];
    float[] I = new float[16];

    SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticMatrix);

    float[] actual_orientation = new float[3];
    SensorManager.getOrientation(R, actual_orientation);

    tvXCoordinate.setText(accelerometerValues[0] + "");
    tvYCoordinate.setText(accelerometerValues[1] + "");
    tvZCoordinate.setText(accelerometerValues[2] + "");

    floatXTotal += accelerometerValues[0];
    tvXTotal.setText(floatXTotal + "");

    tvAzimuth.setText(actual_orientation[0] + "");
    tvPitch.setText(actual_orientation[1] + "");
    tvRoll.setText(actual_orientation[2] + "");
}
4

2 回答 2

6

我可能遗漏了一些东西(您可能已经解决了这个问题),但在我看来,您的 switch 语句不正确:

switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            accelerometerValues = event.values.clone();
        case Sensor.TYPE_MAGNETIC_FIELD:
            geomagneticMatrix = event.values.clone();
            sensorReady = true;
            break;
        default:
            break;
    }

如果您的传感器事件是TYPE_ACCELEROMETER来自事件的值将被克隆到两者accelerometerValuesgeomagneticMatrix并且sensorReady将设置为 true。我认为您可能需要更改此块的顺序,或者可能break;在您的第一个案例之后添加一个。

于 2010-06-29T16:01:30.567 回答
2

The reason you're getting 0.0, -0.0, -0.0 from getOrientation() is that getRotationMatrix() doesn't always get a valid result. You need to check getRotationMatrix()'s return value, which will be false if the result is invalid, or true if it succeeded.

Added: Actually, that didn't come out right. You're getting an invalid result for the reason aganders pointed out. Checking the return value would simply be an indication that you were in fact getting an invalid result.

于 2010-09-10T04:37:57.223 回答