安卓:算法SensorManager.getRotationMatrix和SensorManager.getOrientation() [英] Android: Algorithms for SensorManager.getRotationMatrix and SensorManager.getOrientation()

查看:876
本文介绍了安卓:算法SensorManager.getRotationMatrix和SensorManager.getOrientation()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Android的欧拉角(例如,俯仰,滚转,方位)的获得取向为,需要执行如下:

To get orientation as a from of euler angles (e.g., pitch, roll, azimuth) in Android, it is required to execute followings:


  1. SensorManager.getRotationMatrix(浮动[] R,漂浮[]我,浮动[]重力,漂浮[]地磁);

  2. SensorManager.getOrientation(浮动[] R,漂浮[]方向);

在第一个,我意识到,它使用一种算法TRIAD;旋转矩阵(R [])是由重力,地磁点¯x重力,重力X(地磁点¯x重力)--- X是交叉的产物。
见codeS如下:

In the first one, I realize that it uses a kind of TRIAD algorithms; Rotation matrix (R[]) is composed of gravity, geomagnetic X gravity, gravity X (geomagnetic X gravity) --- X is cross product.
See codes below:

    float Ax = gravity[0];
    float Ay = gravity[1];
    float Az = gravity[2];
    final float Ex = geomagnetic[0];
    final float Ey = geomagnetic[1];
    final float Ez = geomagnetic[2];
    float Hx = Ey*Az - Ez*Ay;
    float Hy = Ez*Ax - Ex*Az;
    float Hz = Ex*Ay - Ey*Ax;
    final float normH = (float)Math.sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
    if (normH < 0.1f) {
        // device is close to free fall (or in space?), or close to
        // magnetic north pole. Typical values are  > 100.
        return false;
    }
    final float invH = 1.0f / normH;
    Hx *= invH;
    Hy *= invH;
    Hz *= invH;
    final float invA = 1.0f / (float)Math.sqrt(Ax*Ax + Ay*Ay + Az*Az);
    Ax *= invA;
    Ay *= invA;
    Az *= invA;
    final float Mx = Ay*Hz - Az*Hy;
    final float My = Az*Hx - Ax*Hz;
    final float Mz = Ax*Hy - Ay*Hx;
    if (R != null) {
        if (R.length == 9) {
            R[0] = Hx;     R[1] = Hy;     R[2] = Hz;
            R[3] = Mx;     R[4] = My;     R[5] = Mz;
            R[6] = Ax;     R[7] = Ay;     R[8] = Az;
        } else if (R.length == 16) {
            R[0]  = Hx;    R[1]  = Hy;    R[2]  = Hz;   R[3]  = 0;
            R[4]  = Mx;    R[5]  = My;    R[6]  = Mz;   R[7]  = 0;
            R[8]  = Ax;    R[9]  = Ay;    R[10] = Az;   R[11] = 0;
            R[12] = 0;     R[13] = 0;     R[14] = 0;    R[15] = 1;
        }
    }

不过,我无法理解SensorManager.getOrientation()。

However, I cannot understand SensorManager.getOrientation().

 azimuth = (float)Math.atan2(R[1], R[4]);
 pitch = (float)Math.asin(-R[7]);
 roll = (float)Math.atan2(-R[6], R[8]);

什么是确切的算法得到欧拉角?

What is the exact algorithms to get euler angles?

推荐答案

让我试着解释:
getRotationMatrix重力和megnetic向量的基础上,构成旋转矩阵

Let me try to explain: getRotationMatrix compose rotation matrix on the basis of gravity and megnetic vector.

我们的主在这里的目标是构建 NED框架

Our main goal here is to construct NED frame

我们假定向地球的中心和磁铁北极重力点。但在实际情况下,这些载体非垂直的,这就是为什么我们首先计算向量H是正交的E和A和属于切面。 H是一个跨产品(E X A)和正交到E和A。

We assume that gravity points toward the center of the Earth and magnet to the north Pole. But in real cases these vectors are non-perpendicular, that's why we firstly calculate vector H that is orthogonal to E and A and belong to tangential plane. H is a cross-product (E x A) and is orthogonal to E and A.

float Hx = Ey*Az - Ez*Ay;
float Hy = Ez*Ax - Ex*Az;
float Hz = Ex*Ay - Ey*Ax;
final float normH = (float)Math.sqrt(Hx*Hx + Hy*Hy + Hz*Hz);

正常化加速度和H向量(因为这些载体将构成坐标系统ENU的基础)

normalize acceleration and H vector (because these vectors will compose a basis of ENU coordinate system)

final float invH = 1.0f / normH;
    Hx *= invH;
    Hy *= invH;
    Hz *= invH;
final float invA = 1.0f / (float)Math.sqrt(Ax*Ax + Ay*Ay + Az*Az);
    Ax *= invA;
    Ay *= invA;
    Az *= invA;

查找去年的基础矢量(M)为h的跨产品和A:

Find last basis vector (M) as cross-product of H and A:

double Mx = Ay * Hz - Az * Hy;
double My = Az * Hx - Ax * Hz;
double Mz = Ax * Hy - Ay * Hx;

通过NED在主体框架的前presses任意载体(A)的坐标坐标为= RA'
的R - 变换矩阵的矩阵,其列是新的基向量中的旧基础的坐标

Coordinates of the arbitrary vector (a) in body frame expresses through NED coordinates as a = Ra' R - Transformation matrix matrix, whose columns are the coordinates of the new basis vectors in the old basis

但在NED框架坐标
( - 1)*一个作为'= T ^计算。为正交变换矩阵的逆是等于转置矩阵。因此,我们有:

But coordinates in NED frame are calculated as a' = T^(-1) * a. For orthogonal transformation matrix inverse is equal to transposed matrix. Thus we have:

R[0] = Hx;     R[1] = Hy;     R[2] = Hz;
R[3] = Mx;     R[4] = My;     R[5] = Mz;
R[6] = Ax;     R[7] = Ay;     R[8] = Az;

一旦我们有我们可以把它转换为欧拉旋转矩阵角度重新presentation。皈依的公式依赖从大家使用。
您的公式

Once we have rotation matrix we can convert it to Euler angles representation. Formulas of convertion depend from convention that you use. Your formulas

azimuth = (float)Math.atan2(R[1], R[4]);
pitch = (float)Math.asin(-R[7]);
roll = (float)Math.atan2(-R[6], R[8]);

是正确的Tiat布莱恩与惯例Y-X-Z角度。为了更好地了解旋转矩阵欧拉转换的角度,我建议学习格雷戈里的

这篇关于安卓:算法SensorManager.getRotationMatrix和SensorManager.getOrientation()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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