我正在尝试为我正在进行的项目制作一个指南针应用程序,该应用程序可以在崎岖地形上运行。我使用了标准的 TYPE_ACCELEROMETER
和 TYPE_MAGNETIC_FIELD
传感器,它似乎提供了相当准确的读数。但是,当我围绕其 y 轴和 z 轴倾斜手机时,即使手机指向同一方向(恒定 z 轴),航向读数也会发生变化。
有谁知道如何补偿这个?示例代码将不胜感激。
这是我目前使用的代码:
private void updateDirection() {
float[] R = new float[16];
float[] orientationValues = new float[3];
if(SensorManager.getRotationMatrix (R, null, accelerometerValues, magneticValues)){
SensorManager.getOrientation (R, orientationValues);
orientationValues[0] = (float)Math.toDegrees (orientationValues[0]);
orientationValues[1] = (float)Math.toDegrees (orientationValues[1]);
orientationValues[2] = (float)Math.toDegrees (orientationValues[2]);
if(orientationValues[0] < 0){
orientationValues[0] = 360 + orientationValues[0];
}
final int trueNorthHeading = NavigationUtils.getTrueNorthBearing(currentLocation, orientationValues[0]);
if(trueNorthHeading == currentHeading) {
return;
}
int accuracy = 3;
if(NavigationUtils.isSignificantHeadingChange(currentHeading, trueNorthHeading, accuracy, SIGNIFICANT_CHANGE_LIMIT)){
int headingChangeDegrees = NavigationUtils.getStearageDegree(currentHeading, trueNorthHeading);
currentHeading = trueNorthHeading;
navigationManager.headingUpdate(trueNorthHeading, Math.round(orientationValues[0]), headingChangeDegrees);
Log.d("COMPASS", "North: values[0]: " + (int)orientationValues[0]);
}
}
}
谢谢你的帮助,
亚当
最佳答案
改用 TYPE_GRAVITY,或者如果不可用,请使用低通滤波器或卡尔曼滤波器过滤加速度计。 TYPE_GRAVITY 将低通滤波器的精度提高了 10 度。
关于Android 倾斜补偿指南针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306445/