简体   繁体   中英

How to map device co-ordinate system to world co-ordinate system in android

I get the vector (Ax,Ay,Az) from the accelerometer. I know my phone co-ordinate system and world co-ordinate system matches when my phone is lying on table with top facing the north. But when i change the position of the phone the co-ordinate system is not same and needs a remap. For that purpose i need a rotation matrix to fix things. But cannot determine the rotation matrix. I tried like following tread and no luck

Not able to translate device co-ordinate system to world co-ordinate system on android

So is there any way i can get the rotation matrix and perform the mapping in android?

I tried multiplying the vectors with three axes's rotation matrix. But it didn't give any satisfying result. As the angle determined from accelerometer is not correct. Also i think the rotation matrix is not serving the purpose.

rotation matrix

For these purpose android provides SensorManager given the gravity sensor data and magnetic sensor data. At first the gravity sensor(accelerometer) readings and magnetic sensor readings. Declare arrays and store data to them. The rotation array will be used for storing rotation matrix data. temp[] for storing data temporarily.

private static final float rotation[] = new float[9];
private static final float temp[] = new float[9];
private static final float grav[] = new float[3];
private static final float mag[] = new float[3];

Then get the rotation matrix by following code. Here temp is temporary matrix to store rotation info

SensorManager.getRotationMatrix(temp, null, grav, mag);

Remap the coordinate by using below method

SensorManager.remapCoordinateSystem(temp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM