Get rotation and display in degrees

I needed something very simple, but I could not find a suitable example to study. My only goal is to:


Since the device is placed on the table (tightly) on the table, it should show 0 (or close to 0) for the X and Y axis. When I lift it from the top (where the speaker is), and the bottom (where the microphone is) remains depressed - She should show me how many degrees the phone is tilted. It is mathematically described - to show in degrees the angle between the back of the phone and the table for one of the axes. When I lift the lower part (and the upper part remains depressed), then show minus degrees.

The same applies to the other axis - turning the phone around its long sides.


I tried to build an application from various examples using a gyroscope or accelerometer or rotation vector vectors, but could not come up with something working properly.

Can someone give me an example of the onSensorChanged function (since all the work is going on here) and just tell me which sensor is being used, so I know what to register?

+8
android rotation accelerometer motion-detection gyroscope
source share
2 answers

There are several examples and tutorials on the Internet, but be careful. Sensor.TYPE_ORIENTATION deprecated. You must calculate the rotations by listening to the two sensors Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_MAGNETIC_FIELD .

The challenge after registering to receive notifications from these sensors is to figure out how to process the data received from them. The key part is the following:

 public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values; if (mGravity != null && mGeomagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll pitch = orientation[1]; roll = orientation[2]; } } } 

Here is how you should calculate the azimuth, step, roll of your device in onSensorChanged(SensorEvent event) . Keep in mind that "All three angles are higher in radians and are positive in the counterclockwise direction." You can simply convert them to degrees using Math.toDegrees()

As Louis CAD pointed out in the comments, it would be nice to override the initialization of I, R arrays and orientations from the onSensorChanged callback, as it is often called. Creating and leaving them for the GC has a bad effect on the performance of your applications. I left it there for the sake of simplicity.

Depending on how your device rotates, you may need to reassign the coordinates to get the desired result. You can read more about remapCoordinateSystem and also about getRotationMatrix and getOrientation in the Android documentation

Example code: http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

+14
source share

Create a layouts.xml file in the res/values-land and res/values-port folders with the following contents:

RES / value-earth / layouts.xml:

 <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="is_landscape">true</bool> </resources> 

RES / port values ​​/layouts.xml:

 <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="is_landscape">false</bool> </resources> 

In the source code, you can now access the current orientation as follows:

 context.getResources().getBoolean(R.bool.is_landscape) 
0
source share

All Articles