安卓加速方向 [英] Android acceleration direction

查看:33
本文介绍了安卓加速方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了一段时间后,我没有找到任何将平移加速度方向与旋转隔离的方法.

After searching for a while I did not find any way to isolate translation acceleration direction from rotation.

我正在寻找一种方法来获得与手机旋转方式无关(当然也与手机旋转方式无关)的加速度方向.

I'm looking yo find a way to get acceleration direction independently of how mobile phone is rotated (and of course independently of how it will be rotated).

基本上,这将使我能够区分加速、减速和方向改变.

Basically this would allow me to make the difference among acceleration, deceleration and change of direction.

我尝试用从 getOrientation 获得的角度补偿加速度传感器角度,但我试验了 getOrientation 角度(方位角、俯仰和滚转)在设备有平移运动(加速度).

I tried to compensate acceleration sensor angles with the ones obtained from getOrientation but I experimented that getOrientation angles (Azimut, pitch, and roll) are not the same when the device has translation movement (acceleration).

我需要两者之一:无论设备如何旋转的加速度方向或无论设备如何加速的方向值.

I would need one of both: acceleration direction no matter how the device is rotating or orientation values no matter how the device is being accelerated.

这可能吗?

推荐答案

我们终于找到了!

Android 文档 我们可以得到旋转矩阵(getRotationMatrix)来改变移动的坐标系:旋转矩阵 R 将一个向量从设备坐标系转换为世界坐标系.

After Android documentation we can get rotation Matrix (getRotationMatrix) to change coordinates system of mobile: the rotation matrix R transforms a vector from the device coordinate system to the world's coordinate system.

但在参数文档中说(这里是测验):

But in parameters documentation says (and here is the quiz):

gravity - 是包含重力向量的 3 个浮点数的数组以设备坐标表示.您可以简单地使用这些值由类型为 TYPE_ACCELEROMETER 的 Sensor 的 SensorEvent 返回.

gravity -is an array of 3 floats containing the gravity vector expressed in the device's coordinate. You can simply use the values returned by a SensorEvent of a Sensor of type TYPE_ACCELEROMETER.

那么,如果您使用文档说加速度计值作为旋转矩阵的参数,当然加速度会受到影响,而移动平移和矩阵将毫无用处.

Well then if you use as documentation says the accelerometer values as paremeter on rotation matrix, of course acceleration will be afected while mobile translation and matrix will be all useless.

您必须使用重力传感器值(TYPE_GRAVITY 类型的传感器),而不是使用加速度计传感器值

Instead of using accelerometer sensor values you have to use gravity sensor values (sensor of type TYPE_GRAVITY

由于此传感器不受移动平移旋转矩阵的影响,因此可以在平移移动时使用.

As this sensor does not get affected with mobile translation rotation matrix can be used while mobile is being translated.

最后对我们有用的代码是:

At the end the code that works for us would be:

float[] trueacceleration = new float[4];
float[] R = new float[16];
float[] RINV = new float[16];    

SensorManager.getRotationMatrix(R, I, GRAVITY, geomagnetic);
Matrix.invertM(RINV, 0, R, 0);          
Matrix.multiplyMV(trueAcceleration, 0, RINV, 0, linearAcceleration, 0);

哪里

  • GRAVITY 是带有 TYPE_GRAVITY 传感器值的矢量,
  • geomangetic 是一个向量,其值来自 TYPE_MAGNETIC_FIELD 传感器,并且
  • linearAcceleration 是一个向量,其值来自TYPE_LINEAR_ACCELERATION 传感器

然后在 trueAcceleration 向量中,我们在移动平移中有加速度,无论移动如何移动其方向.

Then in trueAcceleration vector we have acceleration in mobile translation, no matter how mobile moves its orientation.

这篇关于安卓加速方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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