For my App I need to calculate the acceleration of my device in reference to true north. My idea was to calculate the device orientation to magnetic north and apply the declination to it to get the orientation to true north. Then I want to calculate the acceleration of the device and reference it to the orientation, but I do not know how I should do this.
I would try to get the device orientation using SensorManager.getRotationMatrix()
and SensorManager.getOrientation()
. Then I get the declination by GeomagneticField.getDeclination()
and apply it on the azimuth of the orientation values from SensorManager.getOrientation()
.
But how do I map the accelerometer values to this orientation? Is it even possible?
The accelerometer sensor returns the acceleration of the device. This is a vector in 3 dimentional space. This vector is returned in the device coordinate system. What you want is the coordinate of this vector in the world coordinate, which is simply
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.
Here is some code to compute it (assumes gravity
and magnetic
contain output from their respective sensors):
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With