我正在尝试使用磁场和加速度计来计算方向。然而,计算出的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])));
}
}
知道哪里出了问题吗?
最佳答案
试试这个
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));
关于java - Android getRotationMatrix 为所有值提供 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8414254/