问题
I'd like to get accelerometers on Android and have them on the earth coordinate system, just like on this topic Acceleration from device's coordinate system into absolute coordinate system or here Transforming accelerometer's data from device's coordinates to real world coordinates but these solutions don't work for me. I'm working on Processing. The project is simply to track the phones accelerations in space, no matter how it is (standing, on the side...). I'm new to Android too. I'd appreciate your help! Thank you.
Edit : I don't need the phones exact position, only accelerations.
回答1:
I finally worked it out! I combined 2 previous answers, here is the code :
monSensorManager.getRotationMatrix(Rotate, I, gravity_values, mag_values);
float[] relativacc = new float[4];
float[] inv = new float[16];
relativacc[0]=lin_values[0];
relativacc[1]=lin_values[1];
relativacc[2]=lin_values[2];
relativacc[3]=0;
android.opengl.Matrix.invertM(inv, 0, Rotate, 0);
android.opengl.Matrix.multiplyMV(earthAcc, 0, inv, 0, relativacc, 0);
1) Get phone unit vector in earth coordinates 2) Invert matrix to get earth unit vector in phone coordinates 3) Multiply phone acceleration by unit vector to transform phone coordinates to earth coordinates. Thank you all for your help!
回答2:
The accelerometers
are given in term of the device coordinate system. To express the accelerometers
in term of the earth coordinate system you need to find the change of basis matrix from the device coordinate system to the earth coordinate system. This is what the rotation matrix
is in getRotationMatrix
method.
Thus you have to register for TYPE_MAGNETIC_FIELD
and filter TYPE_ACCELEROMETER
values or using TYPE_GRAVITY
in order to get the rotation matrix
. Once you get the rotation matrix M
it is simple to convert the accelerometer vector
in the device coordinate system to the earth coodinate system.
A_E = M * A_D
That is
A_E[0] = M[0] * A_D[0] + M[1] * A_D[1] + M[2] * A_D[2];
A_E[1] = M[3] * A_D[0] + M[4] * A_D[1] + M[5] * A_D[2];
A_E[0] = M[6] * A_D[0] + M[7] * A_D[1] + M[8] * A_D[2];
Where A_D
is the accelerometers returned in onSensorChanged
来源:https://stackoverflow.com/questions/23701546/android-get-accelerometers-on-earth-coordinate-system