Android Tilt补偿指南针

时间:2013-04-30 18:17:33

标签: android android-sensors

我正在尝试为我正在进行的项目制作一个指南针应用程序,它可以在崎岖的地形上工作。我使用了标准TYPE_ACCELEROMETERTYPE_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]);
        }
      }
}

感谢您的帮助,

亚当

2 个答案:

答案 0 :(得分:2)

使用TYPE_GRAVITY代替,如果不可用,则使用低通滤波器或卡尔曼滤波器对加速度计进行滤波。 TYPE_GRAVITY可提高低通滤波器的精度,最高可达10度。

答案 1 :(得分:0)

试过这个Android getOrientation() method returns bad results,它似乎有效。

将滤波器应用于建议的方向结果,以及原始加速度计阵列。