Android getRotationMatrix为所有值赋予0

时间:2011-12-07 11:01:48

标签: java android accelerometer android-sensors magnetometer

我正在尝试使用磁场和加速度计来计算方向。但是,计算出的Az,Pitch和Roll为0.这是代码:

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    Log.e(TAG, "Sensor Changed");

    float[] r = new float[9];
    float[] i = new float[9];

    float[] accValues = new float[3];
    float[] geoVallues = new float[3];


    }
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        Log.e(TAG, "Accelerometer Changed");
        accValues = event.values.clone();
    }

    if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        Log.e(TAG, "Magnetif Field Changed");
        geoVallues = event.values.clone();
    }

    if (accValues != null && geoVallues != null) {
        SensorManager.getRotationMatrix(r, i, accValues, geoVallues);
        float[] v = new float[3];
        SensorManager.getOrientation(r, v);
        oriView.setText("Orientation:\nAz=" + Math.toDegrees((v[0])) + "\nPitch=" + Math.toDegrees((v[1])) + "\nRoll=" + Math.toDegrees((v[2])));
    }
}

知道什么是错的吗?

1 个答案:

答案 0 :(得分:2)

试试这个

boolean success = SensorManager.getRotationMatrix(
   matrixR,
   matrixI,
   valuesAccelerometer,
   valuesMagneticField);

if(success){
SensorManager.getOrientation(matrixR, matrixValues);

double azimuth = Math.toDegrees(matrixValues[0]);
double pitch = Math.toDegrees(matrixValues[1]);
double roll = Math.toDegrees(matrixValues[2]);

readingAzimuth.setText("Azimuth: " + String.valueOf(azimuth));
readingPitch.setText("Pitch: " + String.valueOf(pitch));
readingRoll.setText("Roll: "+String.valueOf(roll));