Android acceleration direction

2019-06-08 19:25发布

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?

1条回答
做个烂人
2楼-- · 2019-06-08 19:52

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.

查看更多
登录 后发表回答