Android 倾斜补偿指南针

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

谢谢你的帮助,

亚当

最佳答案

改用 TYPE_GRAVITY,或者如果不可用,请使用低通滤波器或卡尔曼滤波器过滤加速度计。 TYPE_GRAVITY 将低通滤波器的精度提高了 10 度。

关于Android 倾斜补偿指南针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306445/

相关文章:

java - 随机变化的变量值。在简单的安卓游戏中

java - 为什么空项目有很大的内存分配?

android - 如何在android中获取平滑的方向数据

java - 仅在用户移动时运行服务以收集传感器数据

java - 检测口袋中的设备是否存在

android - 如何在Android中启用接近传感器

android - 从 assets 文件夹复制 SQLite 数据库

android - Android 模拟器的三星系统镜像?

android - Android 上的设备倾斜度

android - 从 android 上的 ksoap2 得到错误的响应