获取Android设备的Euler Yaw角度 [英] Get Euler Yaw angle of an Android Device

查看:96
本文介绍了获取Android设备的Euler Yaw角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图偏航以欧拉角表示的android设备.

I'm trying to get yaw of an android device expressed in euler angles.

要清楚一点,我不希望方位角"(与磁北的夹角),我希望旋转android设备的"z"轴,如下图所示.

To be clear i dont want 'Azimuth' (which is the angle to magnetic north) I want the rotation of the 'z' axis of an android device as shown in this picture.

这个(IMHO)应该是某些汽车游戏中用来确定转向量的轴.

This (IMHO) should be the axis used in some car games to determine the amount of steering.

据我了解,我只能使用加速度计值(没有磁场),但无法获得确定的值. (可能是由于对旋转矩阵的工作原理缺乏了解.)

It is my understanding I could just use the Accelerometer values (without the magnetic fields) but I'm unable to get the determined value. (probably due to a lack of understanding of how rotation matrixes works).

有人可以指出我正确的方向吗?

Can someone point me toward the right direction?

推荐答案

如果要独立于设备位置(平坦或不平坦)计算围绕设备z轴的旋转,则必须跟踪一些东西.如果该应用程序同时支持纵向和横向,那么它会更加复杂,当活动仅限于纵向时,我将给出一个答案.

If you want to calculate the rotation about the device z-axis independent of the device position (that is flat or not flat) then you have to keep track of a few things. If the app support both Portrait and Landscape then it is more complicated, I am going to give an answer when the activity is restricted to Portrait only.

在这里如此平坦意味着间距小于25度或大于155度,否则不是平坦的.
当设备从平面过渡到非平面(反之亦然)时,UI中可能会出现一些不一致的情况.

So flat here means the pitch is less than 25 or more than 155 degrees and not flat otherwise.
When the device is in transition from flat to not flat and vice versa there probably some inconsistent in the UI.

注册TYPE_GRAVITYTYPE_MAGNETIC_FIELD(平面所需)并进行以下计算.

Register for TYPE_GRAVITY and TYPE_MAGNETIC_FIELD (needed for flat) and do the calculation below.

  • getRotationMatrix

a [0] a [1] a [2]
a [3] a [4] a [5]
a [6] a [7] a [8]

a[0] a[1] a[2]
a[3] a[4] a[5]
a[6] a[7] a[8]

计算pitch = Math.asin(-R[7])

如果设备不是平坦的,则旋转为Math.atan2(a [6],a [7])

if the device is not flat the rotation is Math.atan2(a[6], a[7])

如果设备是平坦的,则首先获取初始azymuth = Math.atan2(R[1], R[4]),然后与后续的azymuths求差以旋转.

If the device is flat to start with get the initial azymuth = Math.atan2(R[1], R[4]) and then take the difference with subsequent azymuths to get the rotation.

如果设备从平坦过渡到不平坦,应该没有问题,旋转只是上面不平坦的旋转.

If the device transition from flat to not flat there should be no problem the rotation is just the rotation for not flat above.

如果设备从非平坦过渡到平坦,则需要获取初始的azymuth.可以将初始azymuth设置为后置摄像头方向Math.atan2(-a [2],-a [5]),如果设备是直立的并且仅绕z轴旋转,则该方向不应改变.实际上,它可能会发生几度变化,因此您需要取平均值.

If the device transition from not flat to flat then you need to get the initial azymuth. This initial azymuth can be set to the back camera direction Math.atan2(-a[2], -a[5]), if the device is upright and there is only rotation about the z-axis then this direction should not change. In reallity it will probably change by a few degrees so you will need to take the average.

All the math except for Math.atan2(a[6], a[7]) is explained at Android: Algorithms for SensorManager.getRotationMatrix and SensorManager.getOrientation()
So let me give the explanation for finding the rotation when the device is not flat.

如上面的链接中所述,您必须指定固定矢量和要计算它们之间角度的变化矢量.在这种情况下,变化的矢量是位于设备y轴上的矢量,而固定矢量是重力矢量在设备xy平面上的投影.在最后一行上方的旋转矩阵中是设备坐标中的归一化重力矢量,因此到设备xy平面的投影为(a [6],a [7]).现在可以通过简单的三角函数看出Math.atan2(a [6],a [7])是设备y轴与重力投影之间的角度. (如果看不到,请绘制图片.)

As explained in the link above you have to specify the fixed vector and the vector that varies that you want to calculate the angle between them. In this case the vector that varies is a vector lying in the device y-axis and the fixed vector is the projection of the gravity vector into the device xy-plane. In the rotation matrix above the last row is the normalize gravity vector in device coordinate, thus the projection to the device xy-plane is (a[6], a[7]). Now it is simple trigonometry to see that Math.atan2(a[6], a[7]) is the angle between the device y-axis and the gravity projection. (Draw picture if you do not see it).

这篇关于获取Android设备的Euler Yaw角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆