Search code examples
androidrotationandroid-sensorsquaternions

use of rotation vector sensor


I want to implement rotation vector sensor. I searched as much as I could about this sensor but I confused some points. I understand that I need to use quaternions other than Euler angles for my app to be more stable. I will use this sensor datas for gesture recognition. So I want to use quaternions but I don’t understand how can I convert them to quaternions after using remapCoordinateSystem method? Or am I wrong about what I want to do? Even a little bit help will be very useful for me.

@Override
public void onSensorChanged(SensorEvent event) {

    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, adjustedRotationMatrix);

    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);
    float pitch = orientation[1];
    float roll = orientation[2];
    float yaw = orientation[0];

    yawTextView.setText(getResources().getString(
            R.string.yaw_sensor,yaw));
    pitchTextView.setText(getResources().getString(
            R.string.pitch_sensor,pitch));
    rollTextView.setText(getResources().getString(
            R.string.roll_sensor,roll));

}

Thank you.


Solution

  • See Euler angles to quaternion conversion.

    Or otherwise, if you'd like to calculate quaternion components directly from the rotation matrix, see this page.