SensorManager.getOrientation() 的问题
在 android.view.onSensorChanged() 的参考页中,设备的轴被描述为
“X 轴指的是屏幕的水平轴(纵向模式下的小边缘,横向模式下的长边缘)并指向右侧。 Y 轴是指屏幕的垂直轴,指向屏幕顶部(原点位于左下角),当设备背面放在桌子上时,Z 轴指向天空。”
并且,在android.hardware.SensorManager.getOrientation()中,提到该方法将返回设备的方位角、俯仰角和横滚角,逆时针方向为正。但是,当我从代码中调用该函数并打印值时,方位角和俯仰角在顺时针方向上为正,在逆时针方向上为负。如果我所说的是正确的,则通过 android.hardware.SensorManager.getRotationMatrix() 获得的旋转矩阵满足要求,即方位角和俯仰角为顺时针方向的+ve,滚转为逆时针方向的+ve方向。如果我错了,请纠正我。我使用的是 HTC Wildfire S,它运行 Android 2.3.3。
我正在添加代码段,我用它来获取方向值。
sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor gsensor = sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor msensor = sensormanager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensormanager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
sensormanager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
-------------------------------------------------------------------------------
final float rad2deg = (float)(180.0f/Math.PI);
int type = event.sensor.getType();
float[] data;
if (type == Sensor.TYPE_ACCELEROMETER) {
data = gravityValues;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
data = geomagneticValues;
} else {
return;
}
for (int i=0 ; i<3 ; i++)
data[i] = event.values[i];
SensorManager.getRotationMatrix(rValues, null, gravityValues, geomagneticValues);
SensorManager.getOrientation(rValues, mOrientation);
result.setText("Compass yaw: " + (int)(mOrientation[0]*rad2deg) + " pitch: " + (int)(mOrientation[1]*rad2deg) +" roll: " + (int)(mOrientation[2]*rad2deg) );
如果我遗漏了任何必需的信息,请告诉我。提前致谢。
n reference pages of android.view.onSensorChanged() the axes of the device are described as
"The X axis refers to the screen's horizontal axis (the small edge in portrait mode, the long edge in landscape mode) and points to the right. The Y axis refers to the screen's vertical axis and points towards the top of the screen (the origin is in the lower-left corner). The Z axis points toward the sky when the device is lying on its back on a table."
and, in android.hardware.SensorManager.getOrientation(), it is mentioned that, the method will return device's azimuth, pitch and roll, which are positive in counter-clockwise direction. But, when i called the function from my code and printing the values, the azimuth and pitch are positive in clock-wise direction, and negative in counter-clockwise direction. The Rotation Matrix which is obtained by android.hardware.SensorManager.getRotationMatrix() is meeting the requirements, if what i claim is correct, i.e the azimuth and pitch are +ve in clock-wise direction and roll is +ve in counter-clockwise direction. Please correct me if i am wrong. I'm using HTC Wildfire S, which runs on Android 2.3.3.
I'm adding the code segment, which i'm using for obtaining the orientation values.
sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor gsensor = sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor msensor = sensormanager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensormanager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
sensormanager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
-------------------------------------------------------------------------------
final float rad2deg = (float)(180.0f/Math.PI);
int type = event.sensor.getType();
float[] data;
if (type == Sensor.TYPE_ACCELEROMETER) {
data = gravityValues;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
data = geomagneticValues;
} else {
return;
}
for (int i=0 ; i<3 ; i++)
data[i] = event.values[i];
SensorManager.getRotationMatrix(rValues, null, gravityValues, geomagneticValues);
SensorManager.getOrientation(rValues, mOrientation);
result.setText("Compass yaw: " + (int)(mOrientation[0]*rad2deg) + " pitch: " + (int)(mOrientation[1]*rad2deg) +" roll: " + (int)(mOrientation[2]*rad2deg) );
Please let me konw, if i missed mentioning any required information. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档是正确的,getOrientation 调用中的 Z 轴向下指向地球。因此,逆时针是正常意义上的顺时针。
The documentation is correct, the Z-Axis in the getOrientation call is pointing down toward the earth. Thus counterclockwise is clockwise in normal sense.