[英]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.