A note before you read on: I made an updated post on the gyro sensor here!

This post deals with interpreting the readings of a gyro sensor.

The Hitechnic gyro that I am using returns integer values between 0 and 1023. The unit being degrees per second. When the gyro is not rotating it returns a value of around 600. This value is called the offset. To know the rate of rotation one has to subtract the offset from the value the gyro returns. Suppose the offset of the sensor is 580 and the value it returns is 670 then the sensor is turning clockwise with about 90 degrees a second. But the gyro is like any other sensor a bit noisy, in reality the rotation could also be 89 or 91 degrees.

The offset is different for every sensor and can change due to external factors as well. For this reason it is important to determine the offset of your gyro before you can use it. This is not so hard to do. You just take the value the sensor returns when it is motionless, this is the offset. But, as said before, there is some noise in gyro readings. To eliminate this noise when finding the offset you shouldn’t rely on just one reading, but you should take the average of a lot of readings. I calculated that 500 readings is enough to get a good offset. As the refresh rate of the gyro is 333 readings a second this will take almost two seconds.

The gyro returns integer values, but in reality the rate of rotation is not an integer, it could well be 5.23 degrees a second. The NXT rounds off the gyro readings when it converts them from an analogue signal to a digital integer value. This means that there is a small error in every reading you get from the gyro. Also, the true offset of a gyro is not an integer value. Therefore one has to store it as a floating point value, this way the value will not be truncated. In the long run it will improve the quality and usability of the gyro information. If one would store the offset as an integer then the offset corrected readings would have an additional error due to truncation of the offset. This error would add to the error that is already in the truncated reading. So it is important to store the offset as a floating value if one needs accurate gyro information. This is especially true when one integrates gyro readings (to calculate the total rotation the sensor has made since starting the measurements).

Until now I talked about the offset as a constant value. In reality this is not true. Factors like temperature or voltage have an effect on the offset. This is sometimes called drift. In the long term this will affect the quality of the gyro information. It would be nice if we could correct for this. There are two techniques to do so. The first one is to update the offset every time the sensor is motionless. One doesn’t need to start all over again with 500 samples. Instead, one can continue to improve the offset that is already calculated. It could look like this:
New offset = (samplesize – 1) * old offset + (1 / samplesize) * gyro reading
Where sample size is a constant, equal the number of readings one used initially to calculate the offset. This method has one drawback, changes in the offset will be effective with some delay.
The second technique to correct the offset is a bit harder to implement. This technique uses knowledge about the effect of the environment ( temperature, voltage) on the offset. If one knows the effect, one can correct for this. Let’s take the influence of power level on the offset as an example. The NXT has a built in voltage sensor. So one can measure both the voltage and the effect this has on the gyro readings. I have done so and I found out that the offset drops about 1 unit with each 0.2 drop in voltage, see the graph for the relation between voltage and offset. This relation is linear, so a drop of 1 volt will cause the offset to drop with 5 degrees. I also found out that the NXT motors do affect the power level substantially, the more power they get, the more the voltage drops. (I use NIMH rechargeble batteries). So if one has a robot with active motors and one needs good gyro information it is useful to correct for this. To do so you would need to define the offset as a function of the voltage level: offset = intersect + slope * voltage. The slope is constant (I think) and is 4.891 in my case. To calibrate the gyro one would just need to determine the intersect using this formula: intersect = gyro reading – 4.891 * voltage. Of course one has to take the average of a lot of readings to eliminate the effect of noise. The formula to get the rate of rotation will then be: Rotation = gyro reading – intersect – 4.891 * voltage.

Using both techniques at the same time I can reduce the cumulative error of the gyro reading down to 0.5 degree a minute. This being tested in a motionless setup. Of course it could well be that there are dynamic errors in the sensor that will decrease the quality of the sensor readings in a real life situation where the sensor is turning, shaking and moving. As a matter of fact, this is what I would expect. Maybe more on this later.

Let me sum up the points I made in this post:
– gyro readings need to be corrected with an offset value
– the offset needs to be calculated
– This offset should be accurate, having enough digits
– the offset should be based on a lot off moles to eliminate noise
– the offset is affected by external factors like power level or temperature
– the offset can be corrected for these external influences
– one technique to correct the offset is to update it’s value over time
– the second technique to correct the offset is to change the offset from a constant to a function of the external factor

A last word of advise. The techniques I showed you not only make things more accurate, they also make things more complex. It is up to you to decide if your project needs this accuracy or that a simple solution will do. If you can keep it simple then you should.