获取方向向量在Android中 [英] Getting Direction Vector in Android

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

问题描述

我怎样才能得到一个方向的矢量再presenting设备背面指向相对于地球坐标方向?

How can I get a direction vector representing the direction the back of the device is pointing relative to earth coordinates?

例如,如果发生倒在桌子上(屏幕朝上),它应该读作[0,0,-1]如果垂直拿着朝北应该读作[1,0,0]等。

For example, if place down on a desk (screen facing up) it should read [0,0,-1] and if held vertically facing north it should read [1,0,0], etc.

我知道如何从航向,俯仰和滚动计算的话,只要这些是相对于地球坐标。需要明确的是,我在这里不是找角速度,但实际电流的角度相对于平面相切的地球。因此,如果该设备垂直放置,并朝北,角度阿尔法应改为0或360,角度试用应改为90,和伽马应改为0。我​​无法弄清楚如何获得这些价值无论是。

I know how to calculate it from heading, pitch, and roll, as long as those are relative to earth coordinates. To be clear here I am not looking for angular velocity, but the actual current angle relative to the plane tangent to the earth. So if the device is held vertically and facing north, the angle "alpha" should read 0 or 360, the angle "beta" should read 90, and "gamma" should read 0. I can't figure out how to get these values either.

我一直在阅读的API了一整天,我仍无法找到如何获得任何这些东西。

I've been reading the API all day and I still cannot find how to get either of these things.

public void onSensorChanged(SensorEvent event) {
    // ?    
}

感谢您的任何见解。

Thanks for any insights.

推荐答案

SensorManager.getRotationMatrix()做以下清单所列,写之前,我发现了这一点。因为如果你想纠正磁性与真北,你仍然需要它的区别,我会离开加解释。

SensorManager.getRotationMatrix() does what's outlined below, written before I found this out. I'll leave the added explanation because if you want to correct for the difference between magnetic and true north you'll still need it.

粗糙的算法来获得旋转矩阵,向量乘 [0,0,-1] 通过它,然后调整到您的坐标系。为什么? Android的文档给协调器和世界体系

The rough algorithm is to get the rotation matrix, multiply vector [0,0,-1] by it, then adjust this to your coordinate system. Why? Android docs give the coordinate systems for device and world

世界

请注意 [0,0,-1] 在Android设备COORDS垂直向下远离屏幕点。如果通过这个矢量乘以旋转矩阵R,你会得到 [0,0,-1] 世界COORDS当设备面朝上你的愿望表。当它直立朝北,你会得到 [0,-1,0] ,这表明你选择了一个坐标系,其中 X 和互换相对于Android系统,但是这只是惯例的改变。

Note [0,0,-1] in Android device coords points perpendicular backward away from the screen. If you multiply rotation matrix R by this vector, you'll get [0,0,-1] in world coords when the device is on the table facing up as you desire. When it's upright facing north, you'll get [0,-1,0], which indicates that you've chosen a coordinate system where x and y are swapped with respect to the Android system, but that's simply a change of conventions.

请注意 R * [0,0,-1] ^ T 研究的只是第三列否定。由此我得到的伪code:

Note R * [0,0,-1]^T is just the third column of R negated. From this I get the pseudocode:

getRotationMatrix(R);
Let v = first three elements of third column of R.
swap v[0] and v[1]

这应该得到你想要的。

这是什么 getRotationMatrix()做如下的其他信息。

Additional information on what getRotationMatrix() is doing follows.

您既需要accerometer数据建立的方向向下和磁力数据来确定方向北。你必须承担的加速度计仅检测重力(该设备是固定的,或在一个稳定的速度运动)。然后,你需要投影的磁力载体上的平面垂直于重力矢量(因为磁场是不是一般正切于地球表面)。这给你两个轴。第三个是正交的,所以可以通过叉积来计算。这给了你地球在设备系​​统坐标向量。它看起来像你想的相反:在大地坐标系设备坐标。对于这个刚刚建立的方向余弦和反转的矩阵。

You need both accerometer data to establish the direction "down" and magnetometer data to determine the direction "north." You'll have to assume the accelerometers are sensing only gravity (the device is stationary or moving at a steady velocity). Then you need to project the magnetometer vector onto the plane perpendicular to the gravity vector (because the magnetic field is not generally tangent to the earth's surface). This gives you two axes. The third is orthogonal, so can be computed by cross product. This gives you the earth coordinate vectors in the device system. It looks like you want the inverse: device coordinates in earth coordinates. For this just construct the matrix of direction cosines and invert.

我要补充的是,上述的讨论假设磁矢量指向北方。我认为(从高中科学!)它实际上是对磁南,但没有设备在手,以便能不试试呢。当然,磁北/南是根据从零到180度,真正的不同,你是地球上。您可以检索GPS坐标并计算实际的偏移量。

I will add that the above discussion assumes that the magnetometer vector points north. I think (from high school science!) it's actually toward magnetic south, but have no device at hand so can't try it. Of course magnetic north/south is different from true by zero to 180 degrees depending where you are on the earth. You can retrieve GPS coordinates and compute an actual offset.

如果您不熟悉需要做这些数学题,我可以进一步解释,但它必须要晚。

If you are not familiar with the math needed to do these, I can explain further, but it will have to be later.

这篇关于获取方向向量在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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