The gyroscope value continuously increases/decreases, even when the brick is not rotating! What could be done in order to avoid, mitigate or at least reduce the impact of this behaviour?
1 Answer
This is a pretty common issue called "gyro drift". If the gyroscope is rotating when plugged in and/or when the brick starts up, that constant rotation will be seen as "resting", and when the gyroscope is still, it believes it is rotating.
There are two main fixes to this
Hardware fix
In this, you simply unplug the gyroscope, then plug it back in while holding the robot very still. Any rotation can drift the gyroscope, however, it is pretty simple for most robots.
Alternatively, completely restart the robot while keeping it still. Most of the time this is easier to do, though it takes quite a while.
Software fix
In the software, you can account for the gyroscope drift. Instead of measuring the gyroscope directly, create a variable called gyro
. At the start of the program, measure the gyroscope speed multiple times (about 10 should be good for most applications), waiting a small amount of time (try 0.05 seconds) between each, and take the average of the measurements. Then, every second, change the gyro
variable by the gyro speed MINUS the averaged gyroscope drift. My preferred method for doing this is to broadcast a message, and have a forever loop in the broadcast receiver, updating gyro
and waiting one second in the loop. This keeps your main forever loop free to run quickly.