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

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

问题描述

如何获得表示设备背面相对于地球坐标的方向的方向向量?

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.

我知道如何根据航向、俯仰和滚转来计算它,只要这些是相对于地球坐标的.在这里要清楚,我不是在寻找角速度,而是在寻找相对于与地球相切的平面的实际当前角度.因此,如果设备垂直放置并面向北,角度alpha"应为 0 或 360,角度beta"应为 90,而gamma"应为 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) {
    // ?    
}

感谢您的任何见解.

推荐答案

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 设备坐标中指向垂直向后远离屏幕的点.如果您将旋转矩阵 R 乘以这个向量,当设备按照您的意愿面朝上放在桌子上时,您将获得世界坐标中的 [0,0,-1].当它直立朝北时,您将得到 [0,-1,0],这表明您选择了一个坐标系,其中 xy 相对于 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 只是否定了 R 的第三列.从中我得到伪代码:

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]

这应该能得到你想要的.

This ought to get what you want.

关于 getRotationMatrix() 正在做什么的其他信息如下.

Additional information on what getRotationMatrix() is doing follows.

您既需要加速度计数据来确定向下"方向,也需要磁力计数据来确定北"方向.您必须假设加速度计仅感应重力(设备静止或以稳定速度移动).然后你需要将磁力计矢量投影到垂直于重力矢量的平面上(因为磁场通常不与地球表面相切).这为您提供了两个轴.第三个是正交的,因此可以通过叉积计算.这为您提供了设备系统中的地球坐标向量.看起来您想要相反的:地球坐标中的设备坐标.为此,只需构造方向余弦矩阵并求逆即可.

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.

我要补充一点,上面的讨论假设磁力计矢量指向北方.我认为(来自高中科学!)它实际上是朝向南磁,但手头没有设备所以不能尝试.当然,根据您在地球上的位置,磁北/南与真实相差 0 到 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天全站免登陆