简体   繁体   中英

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]);

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM