android - 引用真北计算加速度

标签 android orientation accelerometer sensors

对于我的应用程序,我需要引用真北来计算我的设备的加速度。我的想法是计算设备对磁北的方向并将偏角应用于它以获得真正的北方方向。然后我想计算设备的加速度并将其作为方向的引用,但我不知道该怎么做。

我会尝试使用 SensorManager.getRotationMatrix()SensorManager.getOrientation() 获取设备方向。然后,我通过 GeomagneticField.getDeclination() 获取偏角,并将其应用于来自 SensorManager.getOrientation() 的方向值的方位角。

但是如何将加速度计值映射到这个方向?有可能吗?

最佳答案

加速计传感器返回设备的加速度。这是 3 维空间中的向量。该向量在设备坐标系中返回。你要的是这个向量在世界坐标中的坐标,简单点就是

R = rotation matrix obtained by calling getRotationMatrix
A_D = accelerator vector return by sensor ( A_D = event.values.clone )
A_W = R * A_D is the same acceleration vector in the world coordinate system.

A_W is an array of dimention 3 
A_W[0] is acceleration due east.
A_W[1] is acceleration due north.

下面是一些计算它的代码(假设 gravitymagnetic 包含它们各自传感器的输出):

            float[] R = new float[9];
            float[] I = new float[9];
            SensorManager.getRotationMatrix(R, I, gravity, magnetic);
            float [] A_D = values.clone();
            float [] A_W = new float[3];
            A_W[0] = R[0] * A_D[0] + R[1] * A_D[1] + R[2] * A_D[2];
            A_W[1] = R[3] * A_D[0] + R[4] * A_D[1] + R[5] * A_D[2];
            A_W[2] = R[6] * A_D[0] + R[7] * A_D[1] + R[8] * A_D[2];

关于android - 引用真北计算加速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14963190/

相关文章:

ios - 如果我的应用程序中的方向受限,如何手动确定设备方向?

python - 减少 iPad 加速度计中的噪音

iphone - iPhone 加速度计究竟测量什么?

android - smsManager.sendTextMessage 不工作

android - 旋转设备时如何防止布局发生任何变化?

安卓应用打包

java - 如果以编程方式在 LandScape 中更改方向,则 OverridePendingTransition 不起作用

html - Web Worker 中的传感器读取

android - 如何在Android中实现推送通知

android - 使用 SimpleDateFormat 格式化日期时出现 NullPointerException