Using getRotationMatrix and getOrientation in Android 2.1

老子叫甜甜 提交于 2019-12-18 12:32:46

问题


I've been having issues with this for far too long. This code should output dx,dy,dz for the accelerometer, and a running total of the dx. It should also output azimuth, pitch, and roll.

I've used the information given here, but to no avail.

This code does not correctly output pitch, azimuth, or roll. It outputs 0.0, -0.0, -0.0 for the last three textviews, respectively.

switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        accelerometerValues = event.values.clone();
    case Sensor.TYPE_MAGNETIC_FIELD:
        geomagneticMatrix = event.values.clone();
        sensorReady = true;
        break;
    default:
        break;
}   

if (geomagneticMatrix != null && accelerometerValues != null && sensorReady) {
    sensorReady = false;

    float[] R = new float[16];
    float[] I = new float[16];

    SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticMatrix);

    float[] actual_orientation = new float[3];
    SensorManager.getOrientation(R, actual_orientation);

    tvXCoordinate.setText(accelerometerValues[0] + "");
    tvYCoordinate.setText(accelerometerValues[1] + "");
    tvZCoordinate.setText(accelerometerValues[2] + "");

    floatXTotal += accelerometerValues[0];
    tvXTotal.setText(floatXTotal + "");

    tvAzimuth.setText(actual_orientation[0] + "");
    tvPitch.setText(actual_orientation[1] + "");
    tvRoll.setText(actual_orientation[2] + "");
}

回答1:


I might be missing something (and you may have solved this already), but to me it looks like your switch statement is incorrect:

switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            accelerometerValues = event.values.clone();
        case Sensor.TYPE_MAGNETIC_FIELD:
            geomagneticMatrix = event.values.clone();
            sensorReady = true;
            break;
        default:
            break;
    }

If your sensor event is TYPE_ACCELEROMETER the values from the event will be cloned to both accelerometerValues and geomagneticMatrix and sensorReady will be set to true. I think you may need to change the order of this block, or possibly add a break; after your first case.




回答2:


The reason you're getting 0.0, -0.0, -0.0 from getOrientation() is that getRotationMatrix() doesn't always get a valid result. You need to check getRotationMatrix()'s return value, which will be false if the result is invalid, or true if it succeeded.

Added: Actually, that didn't come out right. You're getting an invalid result for the reason aganders pointed out. Checking the return value would simply be an indication that you were in fact getting an invalid result.



来源:https://stackoverflow.com/questions/2963705/using-getrotationmatrix-and-getorientation-in-android-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!