Last post I showed that Sidbot is capable of attitude calculation. It is however prone to drift. After a while the attitude calculated by the NXT drifts from its real attitude. There are two factors contributing to this. The first is offset drift in the gyro. I dealt with this, this post describes how. The second factor is random errors. I’ll deal with that later.

The offset of a gyro is the value it returns when being inert. The offset has to be distracted from the gyro readings to get the rate of turn. It is however not a constant, it’s value changes over time. One has to find a way to update its value, even when the sensor is not inert. You can do so by first recognizing that in the long run the robot is standing still (on average). If you average the readings over some time then the result is equal to the offset. This is exactly how you update the offset, you calculate the average reading over some time. In code it could look like this
Offset=((N-1)*offset)+gyroValue)/N
Where N represents an amount of time.
N is subject to tweaking. If its value is to low the offset will not be stable as it has a lot of “movement” in it. When it is too high the calculated offset cannot keep track of real offset changes. It then has too much “history” in it. I used Excel to experiment with different values of N. To do so I had to export raw gyro readings to excel and add the formula above. For the moment I have chosen a value of 200 while the sample rate is 50 hertz. To make the effect of the formula constant with different sample rates I define N as 4*sample_rate.