@The Electrobotz I have studied @jmmckinney 's code for quite a while now and I am trying to understand how it will prevent gyro drift. For that matter, I don't really even understand what exactly the cause of gyro drift is. My understanding is that due to the vibration caused by motors and other factors, the gyro reads extra turning than is actually happening. How would one go about preventing this? Is someone able to explain this to me like I'm a 10 year old who doesn't understand anything.
I understand using filters such as a median filter on potentiometers. I believe the concept is taking several different values of the pot and, sorting those values into an ordered list, and then use the media in whatever control loop is being used. However, I don't see how anything similar to this would help prevent gyro drift. I've even read articles on Kalman filters and several other filter types, and have read several other threads about this, but they all seem to go way past my level.
I would really appreciate a babied down explanation.
Gyro drift is just a side effect of how the sensor operates. The sensor is a mems gyroscope, which measures forces from the Coriolis effect in order to determine the rate at which it is rotating. In order to do this, the sensor actually vibrates itself. You can probably assume that some amount of noise is guaranteed to be introduced from that process. In order to get accurate estimates of angular measurements, some filter(s) are necessary.
You really don't need a complex filter to get good data out of this sensor, just a little bit of understanding of how it works and what you're actually trying to get it to do.
First thing to know: the gyro sensor itself does not directly measure the angle in which it rotates. It measures the rate at which it rotates over time. When you declare a sensor port as a gyro in ROBOTC, it will apply some logic for you in order to transform its raw measurements to a measured angle of rotation. My code replaces the ROBOTC functionality in an attempt to give more accurate measurements, but does not add up the angular rate for you, I assume that the user will be doing that. This allows users to do more filtering and/or estimation or sensor fusion, whether it be a low pass filter, kalman filter, median filter, etc...
The way my code does this filtering is by estimating what the raw value of the sensor is supposed to be at zero movement (which ends up being centered around 1850 +/- a bit), and discarding any values which fall into a specified range of that zero movement value (I assume that the distribution of noise is Gaussian, and discard anything within N standard deviations, with N=5 being the default). Conceptually, the robot is interpreting very slow rotation/movement at no motion, therefore drifting less or not at all, while faster motion is treated normally, which is why turns are still accurate. Think of N as a sensitivity parameter, higher values of N mean that the sensor is less sensitive to movement, while lower values would be more sensitive but subject to drift.
The zero movement value and standard deviation are calculated in a calibration routine that runs when the user calls gyroInit().