当手机倾斜时,Android getOrientation 方位角会受到污染 [英] Android getOrientation Azimuth gets polluted when phone is tilted

查看:19
本文介绍了当手机倾斜时,Android getOrientation 方位角会受到污染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常烦人的问题,AR 视图就像指南针一样.因此,当我纵向握住手机时(使屏幕指向我的脸),然后我调用 remapCoordinateSystem 使竖向握持时间距为 0.然后方位角(罗盘功能)是完美的,但是一旦我倾斜手机,方位角就会被破坏,如果我向前弯曲,方位角会增加,如果我向后弯曲,它会减小.

I'm having a really annoying problem with a AR view acting like a compass. So when I hold the phone in portrait (so that the screen is pointing to my face), then I call the remapCoordinateSystem that the pitch is 0 when holding it portrait. Then the azimuth (compass functionality) is perfect, but as soon as I tilt the phone the azimuth gets ruined, if I bend forward the azimuth increases and if I bend backwards it decreases.

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

I use 2 sensors to get the readings, Sensor.TYPE_MAGNETIC_FIELD and Sensor.TYPE_GRAVITY.

我使用了一个非常基本的低通滤波器,它通过一个 alpha 常量实现,并直接用于传感器的读取值.

I use a lowpassfilter which is pretty basic, it's implemented with an alpha constant and is used directly on the read values from the sensors.

这是我的代码:

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);

如您所见,这里没有魔法.当gravitymeterValues和magnetometerValues准备好可以使用时,我调用这段代码.

As you see there is no magic here. I call this piece of code when the gravitymeterValues and the magnetometerValues are ready to be used.

我的问题是如何防止倾斜手机时方位角变得疯狂?

My question is how do I stop the azimuth from going crazy when I tilt the phone?

我在 Google Play 商店查看了一个免费应用 Compass 并没有解决这个问题,但我希望有一个解决方案.

I checked a free app on the Google Play Store, Compass and it hasn't solved this problem, but I hope there is a solution.

我有两个解决方案:

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

  1. Make the AR view only work in very constrainted pitch angles, right now I have something like pitch >= -5 && pitch <= 30. If this isn't fullfilled the user is shown a screen that asks him/her to rotate the phone to portrait.

不知何故使用俯仰来抑制方位角,虽然这似乎是一个非常适合特定设备的解决方案,但当然我愿意接受建议.

Somehow use the pitch to suppress the azimuth, this seems like a pretty device-specific solution though, but of course I'm open for suggestions.

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

I can also add that I've been searching for a couple of hours for a decent solution and I haven't found any that has given me any better solutions than 2) here.

提前致谢!

推荐答案

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

For complete code see https://github.com/hoananguyen/dsensor
Keep a history and average out, I do not know the correct interpretation of pitch and roll so the following code is for azimuth only.

班级成员

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;

onSensorChanged

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;
}

这篇关于当手机倾斜时,Android getOrientation 方位角会受到污染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆