1

我正在设计一个需要检查设备方向(方位角、俯仰角和滚动角)的应用程序。我使用以下代码来实现这一点:

@Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        gravityMatrix = event.values.clone();// Fill gravityMatrix with accelerometer values
    else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        geomagneticMatrix = event.values.clone();// Fill geomagneticMatrix with magnetic-field sensor values
    if(gravityMatrix != null && geomagneticMatrix != null){
        RMatrix = new float[16];
        IMatrix = new float[16];

        SensorManager.getRotationMatrix(RMatrix, IMatrix, gravityMatrix, geomagneticMatrix);// Retrieve RMatrix, necessary for the getOrientation method
        SensorManager.getOrientation(RMatrix, orientation);// Get the current orientation of the device
    }
}

现在我可以从'orientation' float[] 获得方位角、俯仰角和横滚值。方位角和滚动值一切正常(它返回正确的角度),但是当我打印俯仰值(方向 [1])时,我总是检索 PI/2 和 -PI/2 之间的角度。我不明白为什么?我无法检索大于 PI/2 或小于 -PI/2 的角度。一旦我的角度为 +- PI/2 并且我继续旋转我的设备(Samsung Galaxy S2),角度在达到 PI/2 值后会突然减小。

谁能解释一下为什么俯仰角的行为如此罕见?

提前致谢!

4

1 回答 1

5

音高计算为函数pitch = (float) Math.asin(-RMatrix[7]);的范围是,所以只能取 和 之间的值。arcsin[-PI/2, PI/2]asin-PI/2PI/2

于 2013-07-20T05:41:26.577 回答