문제

I'm trying to use Magnetic Field and Accelerometer to calculate Orientation. However, the calculated Az, Pitch and Roll are 0. Here's the code:

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

Any idea what's wrong?

도움이 되었습니까?

해결책

try this

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));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top