Hi Everyone,
I'm currently working on a robot that has to climb a 'hill' with an unknown inclination angle between 0-50 degrees. We have to display the instantaneous angle of the robot at all times while climbing up and back down the hill. We bought an accelerometer (the mma7361 sparkfun breakout) after reading online that it's possible to get an angle using that, but we've had no luck.
The code that we're using is here: #include <AcceleroMMA7361.h>AcceleroMMA7361 accelero;int x;int y;int z - Pastebin.com
We're also using the library for the MMA7361 found here: https://code.google.com/p/mma7361-library/
After reading online for like a solid week and I can't seem to figure out why it's not working. Here's a paste of some output values. Laying flat on the table I get:
x: 3.50 y: 0.00
x: 2.89 y: -0.58
x: 3.47 y: 0.00
x: 4.09 y: 0.00
x: 4.04 y: 0.00
x: 4.09 y: 0.00
Holding it straight up I get:
x: -86.78 y: 2.68
x: -88.24 y: 1.76
x: -88.26 y: 1.74
x: -87.40 y: 2.60
x: -86.43 y: 3.47
x: -85.79 y: 3.36
x: -86.48 y: 3.42
x: -86.72 y: 1.82
If you guys have any advice I would GLADLY love to hear it!
Sorry I should have been more clear. It's horribly inaccurate between 0 and 90 degrees. like what I think is 45 degrees reads as 15 degrees. and it doesn't work past 90 degrees, and only works on the xaxis.
Before you can calculate tilt angles, you need to make certain that the accelerometer is as accurately calibrated as possible. There is a separate gain and offset, at minimum, to be determined for each axis.
an accelerometer measures changes in speed := accelaration
to measure an angle you will have to monitor the sensor a lot and then make lots of calcs to get position anfle can not be measured with this only x and y movement.
If this is for measuring the angle while going up an incline you have to disentangle the reading due to the movement from the reading due to the inclination. I am not sure this can be done with any accuracy if at all.
I took a look at the library for the MMA7361, linked in the first post, and it is nearly worthless. Arbitrary constants pop up everywhere, especially in the mysterious routine called "calibration". These routines were obviously intended for some special purpose, are keyed to the authors particular device and are not general at all. The documentation is quite poor.
Keeping in mind the warnings others have given about making sure that the device is held still while making measurements of the gravity vector, you definitely will need to write your own calibration routines, and make use of them, before you can expect to make any sense at all of the readings.
That said, there are a number of possible improvements to be made in your main program. It is better to use the atan2 function to calculate angles, as it keeps proper track of quadrants and pow(x,2) is a poor way of squaring x.