Android SensorManager.getOrientation() returns pit

2019-02-20 16:15发布

I am designing an app that needs to check the device's orientation (Azimuth, Pitch and Roll). I use the following code to achieve this:

@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
    }
}

Now I am able to get azimuth, pitch and roll values from the 'orientation' float[]. Everything goes fine for the azimuth and roll values (it returns the correct angle), however when I print the pitch value (orientation[1]), I always retrieve an angle between PI/2 and -PI/2. I don't understand why? I am unable to retrieve an angle greater than PI/2 or less than -PI/2. As soon as I have an angle of +- PI/2 and I keep on rotating my device (Samsung Galaxy S2) the angle suddenly decreases after it reached the PI/2 value.

Can anyone explain me why the pitch-angle is behaving so uncommon?

Thanks in advance!

1条回答
▲ chillily
2楼-- · 2019-02-20 16:23

Pitch is calculated as pitch = (float) Math.asin(-RMatrix[7]); The range of the arcsin function is [-PI/2, PI/2], so asin can only take value in between -PI/2 and PI/2.

查看更多
登录 后发表回答