Monday, 16 January 2012

How to read the output from accelerometer?

Your sensor may have any kind of output, the one that I know are either I2C or analog.
In the data sheet of your sensor you can find this variable (g / data_output), using this variable is actually waste of time. If you use the method I explained here, you just need to make sure your readings are in same units. Units will cancel each other.
If you want to show the data on your PC screen and in terms of g then do the calculation on your PC not in micro because that will take time form your PC and not your weaker controller on vehicle.
The only thing, I did missed the first time; I was using it was wasting time for finding the readings in g which is not important and keep in mind that I2C and analog outputs are integer and not float so all future calculation becomes much faster for the controller.
You may want to calibrate your accelerometer, this is not actually hard. all you need to do is to rotate the sensor and see the maximum reading in each axis, then multiply the variable  (g / data_output) to your reading and you have to get something around 1. Do not be so picky if your reading is not exactly one since most of the sensors with cheap price are not that accurate and keep in mind that soldering and environment may  change the accuracy of the sensor or the variable  (g / data_output)  slightly.
here I explained how to test your accelerometer. (http://asafdari.blogspot.com/2012/01/how-to-test-gyroscope-accelerometer-and.html)
There will be one other variable that you will need for using in what I explained here (Using three axis accelerometer with three axis gyroscope). This variable is called gravity.
Gravity is basically g in your sensor measurement, without changing units.
Using Pythagorean theorem we know that:
Acceleration_resultant = sqrt ( Acceleration_x^2   +  Acceleration_y^2  +  Acceleration^2 )
Now, consider the situation where the only acceleration applied on sensor is gravity, then:
 Gravity =  = sqrt ( Acceleration_x^2   +  Acceleration_y^2  +  Acceleration^2 )
Finding gravity is necessary and you don't need to measure it every time you turn on your vehicle, you can measure it once and use it as a constant. But it is good to once in a while you measure it again to make sure that your sensor is not affected by environment and even if it is, then just use the new value. I would calculate Gracity 100 times while my sensor is at rest and I would get its average and use it as a constant. Just one more thing, usually the output will be something more than a few hundred (despondent on your sensor and resolution of our reading)  so ignore the decimal point and just round it and keep it as an integer so it will be faster for controller to do calculation with integers.
Usually the vehicles start from rest, so you may calculate Gravity every time you turn on your vehicle. Just make sure you are not relying on one reading.

No comments:

Post a Comment