user2051823
user2051823

Reputation: 177

get orientation values through accelerometer

I am currently working on how to get orientation values (yaw, pitch, roll) through the accelerometer values. Below is the way I am currently using, but the orientation values seem to be wrong, are there any problems or mistakes I have ignored? Thanks a lot for the help!

if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        accelerometerValues = event.values;
    }

    if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        magneticFieldValues = event.values;
    }

    SensorManager.getRotationMatrix(rotate, null, accelerometerValues,
            magneticFieldValues);
    SensorManager.getOrientation(rotate, rotationValues);

    // change radian to degree
    rotationValues[0] = (float) Math.toDegrees(rotationValues[0]);
    rotationValues[1] = (float) Math.toDegrees(rotationValues[1]);
    rotationValues[2] = (float) Math.toDegrees(rotationValues[2]);

Upvotes: 2

Views: 1402

Answers (2)

Harichandan Pulagam
Harichandan Pulagam

Reputation: 368

The coordinate axes used by getRotationMatrix() and getOrientationMatrix are different. So you need to rotate the rotate matrix code above as follows :

SensorManager.getRotationMatrix(rotate, null, accelerometerValues,
        magneticFieldValues);
SensorManager.remapCoordinateSystem(rotate, SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z, rotate); //Overwriting rotate matrix with the rotated values
SensorManager.getOrientation(rotate, rotationValues);

Upvotes: 1

AlexWien
AlexWien

Reputation: 28727

Orientation related to north pole is rotationValues[2];

So

float course = (float) Math.toDegrees(rotationValues[2]);

should be correct.

Try to calibrate (make a figure eight with the device) and look if other app shows the correct direction. Check with a compass.

Upvotes: 0

Related Questions