Android 传感器 : getRotationMatrix() returns wrong values, 为什么?

标签 android math matrix coordinates sensors

我开始使用这个功能已经过去几天了,还没有成功获得有效结果。

我想要的是基本上将加速度矢量从设备坐标系转换为真实世界坐标。我知道这是可能的,因为我在相对坐标中有加速度,而且我知道设备在现实世界系统中的方向。

Reading Android developers似乎使用 getRotationMatrix() 我得到 R = 旋转矩阵。

因此,如果我想从 A'(电话系统中的加速度矢量)获得 A(世界系统中的加速度矢量),我必须简单地做:

  A=R*A'

但我不明白为什么向量 A 的第一个和第二个分量总是零(例如:+0,00;-0,00;+6,43)

我目前的代码是这样的:

  public void onSensorChanged(SensorEvent event) {
     synchronized (this) {   
        switch(event.sensor.getType()){
        case Sensor.TYPE_ACCELEROMETER:
            accelerometervalues = event.values.clone();
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            geomagneticmatrix =event.values.clone();
            break;
        }
        if (geomagneticmatrix != null && accelerometervalues != null) {
            float[] Rs = new float[16];
            float[] I = new float[16];
            SensorManager.getRotationMatrix(Rs, I, accelerometervalues, geomagneticmatrix);
            float resultVec[] = new float[4];
            float relativacc[]=new float [4];
                relativacc[0]=accelerationvalues[0];
                relativacc[1]=accelerationvalues[1];
                relativacc[2]=accelerationvalues[2];
                relativacc[3]=0;
            Matrix.multiplyMV(resultVec, 0, Rs, 0, relativacc, 0);

            //resultVec[] is the vector acceleration relative to world coordinates system..but doesn't WORK!!!!!

        }
      }
   }

这个问题与这个问题非常相似Transforming accelerometer's data from device's coordinates to real world coordinates但是我找不到解决方案...我已经尝试了所有方法..

请帮助我,我需要帮助!!!

更新:

现在我的代码在下面,我试图解释矩阵乘积,但没有任何改变:

            float[] Rs = new float[9];
            float[] I = new float[9];
            SensorManager.getRotationMatrix(Rs, I, accelerationvalues, geomagneticmatrix);
            float resultVec[] = new float[4];

            resultVec[0]=Rs[0]*accelerationvalues[0]+Rs[1]*accelerationvalues[1]+Rs[2]*accelerationvalues[2];
            resultVec[1]=Rs[3]*accelerationvalues[0]+Rs[4]*accelerationvalues[1]+Rs[5]*accelerationvalues[2];
            resultVec[2]=Rs[6]*accelerationvalues[0]+Rs[7]*accelerationvalues[1]+Rs[8]*accelerationvalues[2];

这里是一些数据读取和结果的例子:

Rs separated by " " Rs[0] Rs[1]....Rs[8]
Av separated by " " accelerationvalues[0] ...accelerationvalues[2]
rV separated by " " resultVec[0] ...resultVec[2]

Data from CAT manager

如您所见,即使您快速移动手机,现实世界中 x 轴和 y 轴上的分量也为零(大约)。相反,相对加速度矢量可以正确检测每个运动!!!

解决方案 编号器中的错误与浮点变量乘法有关,它与双倍乘法不同。 这总结了一个事实,即如果手机旋转矩阵不是恒定的,即使方向相同,也在加速。 所以在运动过程中不可能将加速度矢量转换为绝对坐标...... 这很难,但这是现实。

最佳答案

最后我找到了答案:

数字中的错误与浮点变量乘法有关,它与双倍乘法不同。 Here there is the solution. 这总结了这样一个事实:即使手机在加速时,旋转矩阵也不是恒定的。所以不可能在运动过程中将加速度矢量转换为绝对坐标...这很难,但这是现实。

仅供引用,方向矢量由磁体数据和重力矢量组成。这导致了一个常见的问题:转换相对 acc 需要方向需要磁场和重力,但只有当手机被相对 acc 停止时我们才知道重力..所以我们回到开始。

这在 Android Developers where is explained 中得到确认只有当手机没有加速(例如,他们谈论自由落体,实际上不应该有重力测量)或当它不在非常规磁场中时,旋转矩阵才会给出真实结果。

The matrices returned by this function are meaningful only when the device is not free-falling and it is not close to the magnetic north. If the device is accelerating, or placed into a strong magnetic field, the returned matrices may be inaccurate.

在其他世界,完全没有用...... 你可以相信这个东西在 table 上用 Android Senor 或类似的东西做简单的实验..

关于Android 传感器 : getRotationMatrix() returns wrong values, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8027536/

相关文章:

java - 将矩阵格式化为从左到右读取

android - Firebase 无法与 Firebase 服务器通信

android - 如何在 Braintree Dropin 中显示 "Google Pay"选项?

php - 我如何在数据库中插入数学二次方程/公式?

math - 聪明的数学标签舍入

javascript - 旋转 Canvas 的一部分

android - 多 dex 的问题

android - 如何填充 Canvas 中两个圆圈之间的区域?

java - 如何检查轮廓是否相同?

Matlab:如何重新排序(重新组织)矩阵