This topic comes up from time to time, and if you've ever gone down the road of reading a Gyro or Accelerometer, you know the difficulties in determining a reliable position.

Here is a method that I have used that works well. Keep in mind however A Gyro by-itself will have drift as would an Accelerometer. To get the most accuracy, it would be ideal to combine the efforts of different types of sensors.

Tilt Orientation is also critical for a Gyro... Take this for example: If you orient a Gyro such that the rotational axis is horizontal and rotate it vertically (Perpendicular to the axis) you would expect Zero amount of influence and this would normally be true. However it's nearly impossible to rotate it exactly perpendicular to the axis, thus there would be a slight amount of 'drift' in the rotational reading. The opposite is true if you don't rotate the Gyro exactly on the intended rotational axis, you can also have a slight 'drift'.

Code:Theory of Operation: There are a few things that need to happen to the RAW data in order to determine a relative Deg value. As it is, the RAW data has a value associated to it of Deg/sec but there is no real weight or meaning to that value if you don't integrate time with the reading. It's just an arbitrary unit of measurement without time associated to it. We need to integrate time into the equation in order to get a valid Deg value. The easy way to do this is to clear an accumulator, take as many RAW readings as you can in a fixed or known amount of time and add those readings to the accumulator. An initial reading of the accumulator is used as a Reference to 'normalize' the remainder of the data. This isn't the best approach, but it will work for this demonstration. It's basically a single value that represents several data samples and could be looked at as though it were an average although we aren't dividing it over a number of samples like you would with an average. Ideally this value would be dynamic and track over time instead of an initial reading up front. So, the remaining values from the accumulator are subtracted from the Reference value, and added to a Rotation accumulator. The number of Samples taken within the fixed amount of time can be calculated (or even directly counted) by dividing the initial accumulator value by 512 (<- the mid position of the 10-Bit ADC representing Vdd/2) In the program Example the 'fixed time' is 1/50th of a second and the calculations are basically as follows... Hope it helps.

## Bookmarks