简体   繁体   English

加速度计和磁力计的Android Tilt问题

[英]Android Tilt issue with Accelerometer and Magnetometer

I am trying to detect Yaw, Pitch & roll using the following code: 我正在尝试使用以下代码检测偏航,俯仰和滚动:

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor == mAccelerometer) {
            System.arraycopy(event.values.clone(), 0, mLastAccelerometer, 0, event.values.length);
            mLastAccelerometerSet = true;
        } else if (event.sensor == mMagnetometer) {
            System.arraycopy(event.values.clone(), 0, mLastMagnetometer, 0, event.values.length);
            mLastMagnetometerSet = true;
        }
        if (mLastAccelerometerSet && mLastMagnetometerSet) {
            SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
            SensorManager.getOrientation(mR, mOrientation);

            mOrientation[0] = (float) Math.toDegrees(mOrientation[0]);
            mOrientation[1] = (float) Math.toDegrees(mOrientation[1]);
            mOrientation[2] = (float) Math.toDegrees(mOrientation[2]);

            mLastAccelerometerSet = false;
            mLastMagnetometerSet = false;
            ManageSensorChanges();
        }
    }

This works fine apart from one issue; 除了一个问题之外,这还行得通。

When the phone is up-side-down or starts to go up-side-down (even if tilted a little forward in the portrait mode), the angles go haywire ... spitting out random angles! 当手机上下颠倒或开始上下颠倒时(即使在纵向模式下稍微向前倾斜),角度也会变得一团糟...散开随机角度!

Why is this happening - and - Any solution to this? 为什么会发生-和-任何解决方案?

In case some one is stuck with this issue: 如果有人卡在这个问题上:

Strange behavior with android orientation sensor Android方向感应器的奇怪行为

In summary, the trouble is with the use of Eulers angle - so there is little that can be done to sort the above issue at extreme angles of the device ... apart from using Quaternions (which is suitable for OpenGL or 3D projections). 总而言之,麻烦在于使用Eulers角-因此,除了使用四元数(适用于OpenGL或3D投影)之外,在设备的极端角度下几乎无法解决上述问题……

I am using it for 2D drawing on my app (to create a parallax effect), so I just ended up using the Accelerometer values (range is obviously -9.8 to 9.8) ... It works perfectly and an annoyingly easy solution for my crude needs. 我将其用于我的应用程序上的2D绘图(以创建视差效果),所以我最终使用了加速度计值(范围显然为-9.8到9.8)...对于我的原油而言,它完美且令人讨厌的简单解决方案需要。 This technique is not acceptable for 3d projection... if needing precise measurements, see https://bitbucket.org/apacha/sensor-fusion-demo . 此技术不适用于3d投影...如果需要精确的测量,请参阅https://bitbucket.org/apacha/sensor-fusion-demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android加速度计和磁力计同时读取 - Android Accelerometer and Magnetometer readings simultaneously 如何在Android中获取原始加速度计和磁力计数据? - How to get raw accelerometer and Magnetometer data in Android? 如何从Android中的加速度计和磁力计中检索位置 - how to retrieve location from accelerometer and magnetometer in android 从android加速度计获取倾斜角度 - Get tilt angle from the android accelerometer Android上的Accelerometer,Gyro和Magnetometer之间的样本数量不均匀 - Uneven number of samples between Accelerometer, Gyro and Magnetometer on Android 如何从加速度计和磁力计传感器计算android中的旋转矩阵 - How to calculate Rotation Matrix in android from accelerometer and magnetometer sensor 如何使用Android中的加速度计测量手机在XY平面中的倾斜度 - How to measure the tilt of the phone in XY plane using accelerometer in Android 如何检测安装有加速度计的Android设备的左右倾斜? - How to detect left and right tilt of an android device mounted with an accelerometer? 过滤掉来自Android倾斜(加速度计和磁场)传感器的噪声? - Filtering out noise from Android tilt (accelerometer and magnetic field) sensors? 不同的传感器(磁力计,加速度计)如何工作以制作指南针-Android(Java) - How does different sensor(magnetometer, accelerometer) work to make compass - Android (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM