Strange behavior with android orientation sensor

2019-01-01 16:32发布

问题:

Currently, I\'m trying to rotate 3D Cube using orientation sensor values, using getRotation() method. Some unexpected behaviors are observed when the android device is rotated above some bounds. For instance, if I make the device \'stand up\', the value of the \'roll\' just becomes crazy.

Also I\'m experiencing the phenomenon similar to so-called gimbal-lock. The only difference is I\'m experiencing the very problem even before applying the sensor values to the 3D rotation. When I try to change the \'pitch\' value by rotating the device around only \'pitch\' axis, the \'yaw\' value also changes according to the rotation of the pitch. It seems completely unreasonable to me.

Could somebody help me?? I\'m stuck in this problem for a month.

回答1:

This is a common problem with yaw, pitch and roll. You cannot get rid of it as long as you are using yaw, pitch and roll (Euler angles). This video explains why.

I use rotation matrices instead of Euler angles in my motion sensing application. For an introduction to rotation matrices I recommend:

Direction Cosine Matrix IMU: Theory

Rotation matrices work like a charm.

Quaternions are also very popular and said to be the most stable.

[This answer was copied from here.]



回答2:

Using quaternions to compute YPR won\'t do much to solve any problem. The problem of gimbal lock (which near pitch of +/-90 can drive yaw and roll -- actually yaw-roll at the north pole -- to go crazy under slight changes/noise in the underlying quaternion).

However, if you use Yaw Pitch and Roll values to perform a rotation of a 3D object shouldn\'t exhibit any odd behavior near the gimbal lock position. It\'s just that an amibguity in yaw and roll arise and large variations in yaw and roll do not imply the actual orientation is going crazy -- just that the orientation is insensitive to large changes in yaw-roll near pitch of 90.

BUT, also note that phones and browsers for HTML5 do not properly implement yaw, pitch and roll per conventions for Android. Here is a good blog for reference:

http://www.sensorplatforms.com/understanding-orientation-conventions-mobile-platforms/



回答3:

Here is a basic example, this will return the vector of gravity. Note that you can change the sensor type and the speed of sampling, more details here

SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

sensorManager.registerListener(new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        double total = Math.sqrt(x * x + y * y + z * z);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

}, sensor, SensorManager.SENSOR_DELAY_FASTEST);