Convert Android accelerometer values and get tilt from accel

Quickly share your Android Code Snippets here...

Convert Android accelerometer values and get tilt from accel

This small code converts x, y, z values taken from accelerometer to SunSPOT tri-axis values expressed in G.
It also computes the tilt of the device using the method explained in this reference:

SunSPOT Accelerometer reference

Using java Syntax Highlighting
1.          double accX = -x/SensorManager.GRAVITY_EARTH;
2.          double accY = -y/SensorManager.GRAVITY_EARTH;
3.          double accZ = z/SensorManager.GRAVITY_EARTH;
4.          double totAcc = Math.sqrt((accX*accX)+(accY*accY)+(accZ*accZ));
5.          double tiltX = Math.asin(accX/totAcc);
6.          double tiltY = Math.asin(accY/totAcc);
7.          double tiltZ = Math.asin(accZ/totAcc);
Parsed in 0.013 seconds, using GeSHi 1.0.8.4

If the device is liyng flat on a table with the display on the top it will gives this approx. values:

x = 0
y = 0
z = 0
tilt x = 0
tilt y = 0
tilt z = 1.5

noya
Junior Developer

Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Re: Convert Android accelerometer values and get tilt from a

I just registered to show some love for this code sample, it helped me greatly because I needed to the phone's angle to filter out gravity from the accelerometer and this code helped me do just that.

Specifically using it as a base it can be reconfigured into:

Using java Syntax Highlighting
1.
2.         //put these in variables so they can be adjusted for device calibration
3.         private double gravityConstantX = SensorManager.GRAVITY_EARTH;
4.         private double gravityConstantY = SensorManager.GRAVITY_EARTH;
5.         private double gravityConstantZ = SensorManager.GRAVITY_EARTH;
6.
7.         double accX = -x/gravityConstantX;
8.         double accY = -y/gravityConstantY;
9.         double accZ = -z/gravityConstantZ;
10.
11.         double totAcc = Math.sqrt((accX*accX)+(accY*accY)+(accZ*accZ));
12.         double tiltXInGs = Math.asin(accX/totAcc);
13.         double tiltYInGs = Math.asin(accY/totAcc);
14.         double tiltZInGs = Math.asin(accZ/totAcc);
15.
16.         //convert to rads, or something close, end up with the opposite angle
17.         double tiltX = Math.sin(tiltXInGs);
18.         double tiltY = Math.sin(tiltYInGs);
19.         double tiltZ = Math.sin(tiltZInGs);
20.
21.         //use these angles to calculate gravity  * -1
22.         Double gx = (gravityConstantX  * Math.sin(tiltX));
23.         Double gy = (gravityConstantY * Math.sin(tiltY));
24.         Double gz = (gravityConstantZ * Math.sin(tiltZ));
25.
26.         //add the resulting negative gravity and convert the result  (acceleration of the phone relative to the ground)
27.         // to centimeters a second
28.         Integer resultx = metersToCentimeters(x + gx);
29.         Integer resulty = metersToCentimeters(y + gy);
30.         Integer resultz = metersToCentimeters(z + gz);
31.
32.         //assuming the phone is held on it's back it seems to need this correction
33.         resultz -= 100;
34.
Parsed in 0.011 seconds, using GeSHi 1.0.8.4

1. private Integer metersToRoundedOffCentimeters(Double meters) {
2. Integer centimeters = (int) Math.round(100 * meters);
3. return centimeters;

The math is more advanced then I currently know, so I don't exactly understand how it works. I discovered the "double tiltX = Math.sin(tiltXInGs);" line accidentally while trying to work out how to convert it into angles.
netsplit
Once Poster

Posts: 1
Joined: Sun Nov 14, 2010 2:45 pm

Re: Convert Android accelerometer values and get tilt from a

the device has rotation sensor, accelerometer cannot be a good source for tilt measurement because it needs to be calibrated after a while and errors in accelerometer are much more than rotation sensor, the accelerometer can give you acceleration, speed vectors(one integral), location(two integrals), i think i have the math for doing this but it is not a good source for doing this i assure you, there's a sample with name " IBM eyes" which has code for getting accelerometer and rotation sensor if you want i can send the entire project for you
mshankayi
Freshman

Posts: 5
Joined: Sat Mar 19, 2011 8:09 pm

Re: Convert Android accelerometer values and get tilt from a

thanks for the resources!
Hi I'm Mike. Checkout my source code, projects and work here and let me know what you think.

Feel free to also upload and submit your code/projects there for all to see and find!
Developer

Posts: 26
Joined: Mon Mar 07, 2011 9:19 pm
Location: New York

Re: Convert Android accelerometer values and get tilt from a

mshankayi wrote:the device has rotation sensor, accelerometer cannot be a good source for tilt measurement because it needs to be calibrated after a while and errors in accelerometer are much more than rotation sensor, the accelerometer can give you acceleration, speed vectors(one integral), location(two integrals), i think i have the math for doing this but it is not a good source for doing this i assure you, there's a sample with name " IBM eyes" which has code for getting accelerometer and rotation sensor if you want i can send the entire project for you

Hey
i'm kind of beginner in android programming ... & for an internchip mission i need to develop an android application that's called " paint spray " ... while mooving the android phone ... a paint spray proportional to the movement orientation should be shon on the computer throught a wifi connection ... the iadea is that i get the tilt angle feom the acceleration & then send the angle to the computer where a ( java or .net ) application makes the "paint spray " demonstration based on the angle of the move ... but i'm getting problems in extracting the angle ... do you have any suggetion please ?