我正在尝试为我正在处理的项目制作一个指南针应用程序,该应用程序可以在崎岖地形上工作。我已经使用标准TYPE_ACCELEROMETER
和TYPE_MAGNETIC_FIELD
传感器,它似乎给出了相当准确的读数。但是,当我围绕其y轴和z轴倾斜手机时,即使手机指向相同方向(恒定z轴),标题读数也会改变。Android倾斜补偿指南针
有谁知道如何弥补这个?示例代码将不胜感激。
这是我目前使用的代码:
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]);
}
}
}
感谢您的帮助,
亚当
您的意思是说,它在旋转时被某种设备固定在适当位置时会稍微变化?或者它比微小的漂移更明显? – 2013-04-30 18:21:44
当围绕y轴旋转时,即使手机指向相同方向,返回的值也会从160 *到200 *。除了轻微的漂移,Defo更重要。 – 2013-04-30 18:32:03
手机是平躺还是垂直? – 2013-04-30 19:59:54