Android加速方向 [英] Android acceleration direction

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

问题描述

搜索了一会儿之后,我没有找到任何方法来将平移加速度方向与旋转隔离。



我在寻找一种独立于如何获得加速度方向的方法



基本上,这可以使我在加速,减速和方向改变之间有所区别。



我试图用从getOrientation获得的加速度传感器角度来补偿加速度传感器的角度,但我尝试了当设备平移时getOrientation角度(方位角,俯仰和滚动)不相同的情况运动(加速度)。



我需要两者之一:无论设备如何旋转,加速度方向还是无论设备如何加速度,方向值。



那有可能吗?

解决方案

最后我们找到了它!



Android文档,我们可以获得旋转矩阵(getRotationMatrix)来更改手机的坐标系:旋转矩阵R将矢量从设备坐标系转换为世界坐标系。



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


gravity -is一个由3个浮点组成的数组,其中包含以设备坐标表示的重力矢量
。您可以简单地使用TYPE_ACCELEROMETER类型的Sensor的SensorEvent返回的值


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



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



传感器不受移动翻译的影响,可以在移动翻译时使用旋转矩阵。



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

  float [] trueacceleration =新的float [4]; 
float [] R =新的float [16];
float [] RINV =新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向量中,无论移动如何移动其方向,我们都可以在移动翻译中获得加速。


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

    Im 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.

    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 device has translation movement (acceleration).

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

    Is that even possible?

    解决方案

    Finally we found it!

    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 -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.

    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);
    

    Where

    • GRAVITY is vector with values from TYPE_GRAVITY sensor,
    • geomangetic is a vector with values from TYPE_MAGNETIC_FIELD sensor, and
    • linearAcceleration is a vector with values from TYPE_LINEAR_ACCELERATION sensor

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

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

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