I have an android smartphone that I use to collect data from accelerometer sensor while driving a car. The mobile is placed on a holder inside the vehicle so that, it is nearly tilted about the Z-axis. I want to reorient the readings gathered from the accelerometer to the global frame of reference. I have found that there are two functions in android documentation that are used for that specific purpose: getRotationMatrixFromVector followed by getOrientation to calculate the three euler angles. The two sensors I use are TYPE_ACCELEROMETER and TYPE_ROTATION_VECTOR. The following is the code I used:
public void reorient(float accX, float accY, float accZ, float rot1, float rot2, float rot3, float rot4) {
//rotation matrix to be used later as a return from getRotationMatrix()
float[] rotation = new float[9];
float[] acc = {accX,accY,accZ};
float[] rotation_vector = {rot1,rot2,rot3,rot4};
//get rotation matrix from phone to global coordinate system
SensorManager.getRotationMatrixFromVector(rotation, rotation_vector);
float[] orientation = new float[3];
SensorManager.getOrientation(rotation,orientation);
//project mobile to global coordinates
float globAccX = rotation[0]*acc[0] + rotation[1]*acc[1] + rotation[2]*acc[2];
float globAccY = rotation[3]*acc[0] + rotation[4]*acc[1] + rotation[5]*acc[2];
float globAccZ = rotation[6]*acc[0] + rotation[7]*acc[1] + rotation[8]*acc[2];
}
I could achieve good results when the mobile phone is on a horizonatl plane. But when the mobile phone is placed vertically for example on a holder, results were so bad. My question is why such behaviour occurs?