# Using itg-3200 gyro to measure angular position

By lex lee

August 20, 2011

August 20, 2011

Hi all,

I have the sparkfun itg-3200 gyro connected to an arduino Uno. The example code works but it only indicates angular velocity.

Can this be used to measure angular position? Or do I need to throw in a accelerometer as well?

This gyro is supposed to be integrated with a pan-tilt servo kit, thus relaying it's orientation back to the uno.

## A gyroscopic sensor by

A gyroscopic sensor by nature can only detect angular velocity - the usual idea is to combine a gyro and an accelerometer to form an IMU (inertial measurement unit).

The accelerometer gives you a good reading for 'down', but doesn't give you the best data during quick movements. The gyro complements this by being good at responding quickly to rotational motion, but it cannot directly tell you where you've ended up.

The only way to use a typical gyro to get angular position is with a sort of 'dead reckoning'. If you know that you've started at a reference angle (let's call it 0 degrees), you can calculate how far you've rotated from the position using the gyro readings.

If you start from 0 degrees, and rotate at an average of +10 degrees per second for a duration of 2 seconds, then the angular position must be: 0 deg + (+10deg/s)*(2s) = 20 degrees.

Next, you rotate at an average of -15 degrees per second for 3 seconds, so the new angular position must be: 20 deg + (-15deg/s)*(3s) = -25 degrees.

You must have a known starting position for this to work, but the more common problem is the accumulated error. Every time you update the current angular position value, you'll have at least a small amount of error. Over time the error will creep up to the point where you can't rely on the angular position value any more. Using an accelerometer allows you to recalibrate every now and then, which removes any error you've built up.

## I've set up the gyro using

I've set up the gyro using the "calculation method". The sampling rate is about 12 ms and the max angular velocity @ 300 °/s.

By constantly updating the position value, the soon falls out of the servo range, the error is just too high.

How does the accelerometer recalibrate the gyro readings, especially the yaw reading.

A friend actually suggested integrating the velocity to get positional values. Can this be done with the arduino?

## If you're using the method

If you're using the method of taking the angular velocity and multiplying it by the sample time to get the change in angular position, then you're actually performing a type of numerical integration already =)

There are many different ways you can use the accelerometer and gyro together, but indeed as eric suggests the Kalman filter is extremely popular, and for good reason. It's a powerful approach that I've seen used effectively in a number of projects, most of them involving balancing or flying where stability is required.

If you don't want to jump straight into using something like one of the Kalman filter options, try this for a simple example:

Whenever your gyro reading is close to zero, the accelerometer reading should be pretty reliable, so grab a sample of the current values. With a bit of trig, depending on how many axes your accelerometer has and the sensor orientation, you can work out which way gravity is pointing relative to your machine. From that, you can update your angular position.

If your system is naturally unstable, or is being pushed around, it won't be long before you start falling one way or another. When this happens you can use the gyro readings to quickly respond and balance the machine. Once things have settled down the accelerometer is used to again update the angular position, which will erase any error you might've accumulated from using the gyro.

## If I recall correctly from

If I recall correctly from previous research the idea behind using both an accelerometer and a gyroscope is that you can average the values together to help to eliminate error. What you're looking for is a Kalman filter, which essentially (this is a huge oversimplification) combines and averages the values from both the gyro and accelerometer. I'm sure someone more knowledgeable than me will follow up with more information than that.