问题描述
我设计,需要检查设备的方向(方位角,俯仰和横滚)的应用程序。我用下面的code来实现这一点:
I am designing an app that needs to check the device's orientation (Azimuth, Pitch and Roll). I use the following code to achieve this:
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
gravityMatrix = event.values.clone();// Fill gravityMatrix with accelerometer values
else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
geomagneticMatrix = event.values.clone();// Fill geomagneticMatrix with magnetic-field sensor values
if(gravityMatrix != null && geomagneticMatrix != null){
RMatrix = new float[16];
IMatrix = new float[16];
SensorManager.getRotationMatrix(RMatrix, IMatrix, gravityMatrix, geomagneticMatrix);// Retrieve RMatrix, necessary for the getOrientation method
SensorManager.getOrientation(RMatrix, orientation);// Get the current orientation of the device
}
}
现在我能够从方向浮动[]得到方位,俯仰和横滚值。一切顺利的方位和滚动值(它返回正确的角度),但是当我打印间距值(方向[1]),我始终检索PI / 2和-PI / 2之间的角度。我不明白为什么?我无法检索比PI / 2或小于-PI / 2更大的角度。只要我有+的角度 - PI / 2和我一直在转动我的设备(三星Galaxy S2)的角度突然减小它达到了PI / 2值后,
Now I am able to get azimuth, pitch and roll values from the 'orientation' float[]. Everything goes fine for the azimuth and roll values (it returns the correct angle), however when I print the pitch value (orientation[1]), I always retrieve an angle between PI/2 and -PI/2. I don't understand why? I am unable to retrieve an angle greater than PI/2 or less than -PI/2. As soon as I have an angle of +- PI/2 and I keep on rotating my device (Samsung Galaxy S2) the angle suddenly decreases after it reached the PI/2 value.
任何人能解释我为什么俯仰角度的表现如此罕见?
Can anyone explain me why the pitch-angle is behaving so uncommon?
在此先感谢!
推荐答案
间距的计算公式为距=(浮点)Math.asin(-RMatrix [7]);
在 ARCSIN
函数的范围是 [ - PI / 2,π/ 2]
,所以 ASIN
只能取值 -PI / 2
和 PI / 2
。
Pitch is calculated as pitch = (float) Math.asin(-RMatrix[7]);
The range of the arcsin
function is [-PI/2, PI/2]
, so asin
can only take value in between -PI/2
and PI/2
.
这篇关于Android的SensorManager.getOrientation()-PI / 2和PI / 2之间的间距返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!