Android AR方向

时间:2015-06-07 18:05:25

标签: android orientation augmented-reality

我正在制作程序,用于在相机上显示来自地图的对象,这几乎可以很好地工作,除了从垂直方向左右几度(如80-110和260-280度)。在其他+ -320度,它运作良好。我已经尝试使用TYPE_ROTATION_VECTOR和带磁力计的加速度计,它们具有相同的结果。有人知道任何解决方案吗?

使用TYPE_ROTATION_VECTOR:

if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
        {
      float[] roationV = new float[16];
            SensorManager.getRotationMatrixFromVector(roationV, event.values);

            float[] orientationValuesV = new float[3];
            SensorManager.getOrientation(roationV, orientationValuesV);

            tvHeading.setText(String.format(
                    "Coordinates: lat = %1$.2f, lon = %2$.2f, time = %3$.2f",
                    orientationValuesV[0], orientationValuesV[1], orientationValuesV[2]));

            float[] rotationMatrix=new float[16];
            mSensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
            float[] orientationValues = new float[3];
            SensorManager.getOrientation(rotationMatrix, orientationValues);
            double azimuth = Math.toDegrees(orientationValues[0]);
            double pitch = Math.toDegrees(orientationValues[1]);
            double roll = Math.toDegrees(orientationValues[2]);

            tvOrientation.setText(String.format(
                    "Coordinates: lat = %1$.2f, lon = %2$.2f, time = %3$.2f",
                    azimuth,pitch,roll));


        }

带加速度计+磁力计

if (event.sensor == mAccelerometer) {
            System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
            mLastAccelerometer = meanFilterAccelSmoothing
                    .addSamples(mLastAccelerometer);
            mLastAccelerometer = medianFilterAccelSmoothing
                    .addSamples(mLastAccelerometer);
            for (int i = 0; i < mLastAccelerometer.length; i++) {
                mLastAccelerometer[i] = (float) Math.floor(mLastAccelerometer[i] * 1000) / 1000;
            }
 mLastAccelerometerSet = true;
        }
        if (event.sensor == mMagnetometer) {
            System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
            mLastMagnetometer = meanFilterMagneticSmoothing.addSamples(mLastMagnetometer);
            mLastMagnetometer = medianFilterMagneticSmoothing.addSamples(mLastMagnetometer);
            for (int i = 0; i < mLastMagnetometer.length; i++) {
                mLastMagnetometer[i] = (float) Math.floor(mLastMagnetometer[i] * 1000) / 1000;
            }
            mLastMagnetometerSet = true;
        }

 if (mLastAccelerometerSet && mLastMagnetometerSet) {

            SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
            SensorManager.getOrientation(mR, mOrientation);
            if (angeles.size() > 0) {
                for (int i = 0; i < mapObjects.size(); i++) {
                    compassFunc(i, mOrientation[0], mOrientation[1], mOrientation[2]);

                }
            }

private void compassFunc(int number, float... values) {

        double angularXSpeed = Math.floor(values[0] * 180 / Math.PI * 100) / 100;
        double angularYSpeed = Math.floor(values[1] * 180 / Math.PI * 100) / 100;
        double angularZSpeed = Math.floor(values[2] * 180 / Math.PI * 100) / 100;

tvOrientation.setText(String.format(
                "Screen: lt= %1$.2f : %2$.2f,rt= %3$.2f : %4$.2f,lb= %5$.2f : %6$.2f,rb= %7$.2f : %8$.2f",
                xLeftTop,  yLeftTop, xRightTop,yRightTop,xLeftBottom,yLeftBottom,xRightBottom,yRightBottom));
}

1 个答案:

答案 0 :(得分:0)

这听起来像Gimbal Lock的典型案例。当另一个轴旋转到+ -90度时,您对围绕一个轴旋转的描述表明情况确实如此。

这是具有欧拉角(Yaw / Azimuth,Pitch,Roll)的fundamental problem,这就是为什么大多数这样的计算是使用旋转矩阵或四元数完成的,而欧拉角通常仅在特定方向时使用将被展示给人类(人类通常不善于解释旋转矩阵和四元数)。

ROTATION_VECTOR传感器以四元数格式(source)输出数据,虽然重新排列了值,getRotationMatrixFromVector()方法将其转换为旋转矩阵。我建议您使用其中一个描述进行内部计算。

this similar question的答案为如何解决问题提供了一些具体的建议。