简体   繁体   中英

Android Sensors: Compass

I'm currently working on a little compass app. So far so good, I got the compass working. I'm using the TYPE_ROTATION_VECTOR , since the result is much more stable than using TYPE_ACCELEROMETER combined with TYPE_MAGNETIC_FIELD . That's my code to calculate the azimuth:

SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorEvent.values );
// original values are within [-180,180]
azimuth = (float) (Math.toDegrees(SensorManager.getOrientation(rotationMatrix, orientation)[0]) + 360) % 360;

If the device is laying flat on the table, the results are correct. If I lift my phone, so it's standing (the screen is facing to me now), I get incorrect results. I guess that's because my axes changed. How can i properly detect and handle this?

So as many code snippets and threads here suggests, we need some remapping of axis..

SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remapped);

If I do this, the tilt won't influence the azimuth anymore. BUT, the pitch (lifting left or right side of the phone) will change the azimuth and I can't really figure out why.

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