Search code examples
javaandroidorientation

Android getOrientation issue - mirrored values along axis


I've got some troubles with getOrientation. I'm running my application in landscape mode and I have troubles with one axis. There's more explanation in code, some images:

enter image description here enter image description here

    valuesMagnet      = new float[3];
    valuesAccel       = new float[3];
    valuesOrientation = new float[3];
    rotationMatrix    = new float[9];
    rotationMatrixTemp= new float[9];

....


@Override
public void onSensorChanged(SensorEvent event) {


    switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            System.arraycopy(event.values, 0, valuesAccel, 0, 3);
            break;

        case Sensor.TYPE_MAGNETIC_FIELD:
            System.arraycopy(event.values, 0, valuesMagnet, 0, 3);
            break;
    }

    if (SensorManager.getRotationMatrix(rotationMatrixTemp, null, valuesAccel, valuesMagnet)) {
        //SensorManager.remapCoordinateSystem(rotationMatrixTemp, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix);
        SensorManager.getOrientation(rotationMatrix, valuesOrientation);

        for (int i = 0; i < valuesOrientation.length; i++) {
            valuesOrientation[i] /= Math.PI;
            valuesOrientation[i] *= 100;
            valuesOrientation[i] = (int)valuesOrientation[i];
            valuesOrientation[i] /= 100;
        }

        updateOrientationBuffer(valuesOrientation);

        quadcopter.onEvent(new Event(Event.Codes.ORIENTATION_CHANGED, calculateSmoothedOrientation()));
    }
    else {
        Log.d("App", "Matrix rotate error");
    }
}

I want orientation[1] axis to give full spectrum of values between -1 and 1 while rotating phone by 180 degrees. Also, target device as no gyroscope.


Solution

  • @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                System.arraycopy(event.values, 0, valuesAccel, 0, 3);
                break;
    
            case Sensor.TYPE_MAGNETIC_FIELD:
                System.arraycopy(event.values, 0, valuesMagnet, 0, 3);
                break;
    
            case Sensor.TYPE_GRAVITY:
                System.arraycopy(event.values, 0, valuesGravity, 0, 3);
                break;
    
            default:
                break;
        }
    
        if (SensorManager.getRotationMatrix(rotationMatrixTemp, null, valuesAccel, valuesMagnet)) {
            SensorManager.getOrientation(rotationMatrixTemp, valuesOrientation);
    
            if(valuesGravity[2] < 0)                                     {
                if (valuesOrientation[1] > 0) {
                    valuesOrientation[1] = (float) (Math.PI - valuesOrientation[1]);
                }
                else {
                    valuesOrientation[1] = (float) (-Math.PI - valuesOrientation[1]);
                }
            }
    
            for (int i = 0; i < valuesOrientation.length; i++) {
                valuesOrientation[i] /= Math.PI;
                valuesOrientation[i] *= 100;
                valuesOrientation[i] = (int)valuesOrientation[i];
                valuesOrientation[i] /= 100;
            }
    
    
            updateOrientationBuffer(valuesOrientation);
    
            quadcopter.onEvent(new Event(Event.Codes.ORIENTATION_CHANGED, calculateSmoothedOrientation()));
        }
        else {
            Log.d("Quadcopter-SM", "Matrix rotate error");
        }
    }