Android getOrientation Azimuth在手机倾斜时会受到污染

Joh*_*n S 9 math android augmented-reality compass-geolocation

AR视图就像一个指南针,我有一个非常讨厌的问题.因此,当我以纵向握住手机(以便屏幕指向我的脸部)时,我remapCoordinateSystem会在按住它时将其调用为0.然后方位角(罗盘功能)是完美的,但是一旦我倾斜手机,方位角就会被毁坏,如果我向前弯曲,方位角会增加,如果我向后弯曲,它会减小.

我使用2个传感器来获取读数,Sensor.TYPE_MAGNETIC_FIELDSensor.TYPE_GRAVITY.

我使用的是一个非常基本的lowpassfilter,它是用alpha常量实现的,直接用于传感器的读取值.

这是我的代码:

float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrix(rotationMatrix, null, gravitymeterValues,
    magnetometerValues);

float[] remappedRotationMatrix = new float[9];

SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X,
    SensorManager.AXIS_Z, remappedRotationMatrix);

float results[] = new float[3];
SensorManager.getOrientation(remappedRotationMatrix, results);

float azimuth = (float) (results[0] * 180 / Math.PI);
if (azimuth < 0) {
    azimuth += 360;
}

float pitch = (float) (results[1] * 180 / Math.PI);
float roll = (float) (results[2] * 180 / Math.PI);
Run Code Online (Sandbox Code Playgroud)

如你所见,这里没有魔力.当gravitymeterValues和magnetometerValues准备好使用时,我称这段代码.

我的问题是当我倾斜手机时如何阻止方位角疯狂?

我在Google Play商店,Compass上查了一个免费的应用程序并且它还没有解决这个问题,但我希望有一个解决方案.

我有两个解决方案:

  1. 使AR视图仅在非常有限的俯仰角度下工作,现在我有类似的东西pitch >= -5 && pitch <= 30.如果没有填满,则会向用户显示一个屏幕,要求他/她将手机旋转为纵向.

  2. 不知怎的,使用音调抑制方位角,这似乎是一个非常特定于设备的解决方案,但当然我愿意接受建议.

我还可以补充一点,我一直在寻找一个合适的解决方案几个小时,我没有找到任何比这更好的解决方案.

提前致谢!

Hoa*_*yen 14

有关完整代码,请参阅https://github.com/hoananguyen/dsensor
保留历史记录和平均值,我不知道对俯仰和滚动的正确解释,因此以下代码仅用于方位角.

班级成员

private List<float[]> mRotHist = new ArrayList<float[]>();
private int mRotHistIndex;
// Change the value so that the azimuth is stable and fit your requirement
private int mHistoryMaxLength = 40;
float[] mGravity;
float[] mMagnetic;
float[] mRotationMatrix = new float[9];
// the direction of the back camera, only valid if the device is tilted up by
// at least 25 degrees.
private float mFacing = Float.NAN;

public static final float TWENTY_FIVE_DEGREE_IN_RADIAN = 0.436332313f;
public static final float ONE_FIFTY_FIVE_DEGREE_IN_RADIAN = 2.7052603f;
Run Code Online (Sandbox Code Playgroud)

onSensorChanged

@Override
public void onSensorChanged(SensorEvent event)
{
     if (event.sensor.getType() == Sensor.TYPE_GRAVITY)
     {
         mGravity = event.values.clone();
     }
     else
     {
        mMagnetic = event.values.clone();
     }

     if (mGravity != null && mMagnetic != null)
     {
          if (SensorManager.getRotationMatrix(mRotationMatrix, null, mGravity, mMagnetic))
          {
              // inclination is the degree of tilt by the device independent of orientation (portrait or landscape)
              // if less than 25 or more than 155 degrees the device is considered lying flat
              float inclination = (float) Math.acos(mRotationMatrix[8]);
              if (inclination < TWENTY_FIVE_DEGREE_IN_RADIAN 
                      || inclination > ONE_FIFTY_FIVE_DEGREE_IN_RADIAN)
              {
                  // mFacing is undefined, so we need to clear the history
                  clearRotHist();
                  mFacing = Float.NaN;
              }
              else
              {
                  setRotHist();
                  // mFacing = azimuth is in radian
                  mFacing = findFacing(); 
              }
          }
     }
}

private void clearRotHist()
{
    if (DEBUG) {Log.d(TAG, "clearRotHist()");}
    mRotHist.clear();
    mRotHistIndex = 0;
}

private void setRotHist()
{
    if (DEBUG) {Log.d(TAG, "setRotHist()");}
    float[] hist = mRotationMatrix.clone();
    if (mRotHist.size() == mHistoryMaxLength)
    {
        mRotHist.remove(mRotHistIndex);
    }   
    mRotHist.add(mRotHistIndex++, hist);
    mRotHistIndex %= mHistoryMaxLength;
}

private float findFacing()
{
    if (DEBUG) {Log.d(TAG, "findFacing()");}
    float[] averageRotHist = average(mRotHist);
    return (float) Math.atan2(-averageRotHist[2], -averageRotHist[5]);
}

public float[] average(List<float[]> values)
{
    float[] result = new float[9];
    for (float[] value : values)
    {
        for (int i = 0; i < 9; i++)
        {
            result[i] += value[i];
        }
    }

    for (int i = 0; i < 9; i++)
    {
        result[i] = result[i] / values.size();
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

  • 我使用Normal,这对我的目的来说已经足够了. (2认同)
  • 我正在尝试编写一个基于位置的AR库.我没有完成它,因为我想要包括海拔高度,并找出测试GPS返回的高度是非常不准确的. (2认同)