2022PythonExamples/14 - Gyroscopes/README.md

22 lines
3.1 KiB
Markdown

# 14 - Gyroscopes
## What is a Gyroscope?
This is covered in slightly more detail in [8 - Basic Robot Electronics](../8%20-%20Basic%20Robot%20Electronics/), but to reiterate, a Gyroscope is a device that tells you the (near) instantaneous acceleration at which your robot is turning (often in 3 dimensions). Special software is used to calculate from the acceleration data the rate your turning, and the angle you've turned relative to a starting point (that you can control).
Gyroscopes can take you from saying "turn left for .75 seconds" to "turn to -45 degrees"
## How does a gyroscope know how fast or far it's gone?
It depends a lot on the type of gyroscope, but generally the ones you'll see in FRC use a trick of electrical capacitance (not capacity, two different things).
If you're really interested, you can [learn more about capacitive gyroscope sensing here](https://www.analog.com/en/technical-articles/mems-gyroscope-provides-precision-inertial-sensing.html). Be warned though, it's a dense read, and is far outside the scope of what you need to know if you actually just want to use the gyroscope.
## Another solution that seems better than timers, but I'm guessing there's a catch?
Yep. Definitely still a better solution than timers in many cases, however, gyroscopes alone can't help you determine the speed or distance you have traveled, just how much you've rotated.
Similar to Encoders, this example I've provided here also has the same overshoot problem. We'll see how to fix this next example, but just keep in mind that the way this example is written, you may ask for 45 degrees, and get 46, or 50, or maybe even 55 depending on the conditions around your robot.
Gyroscopes also have another issue, drift. Because of how the rotation is sensed, and the math involved to go from acceleration to rate and angle, a certain amount of drift (that is, a certain amount of rotation is preceived by the underlying code that isn't actually happening) is accumulated over time. It varies from sensor to sensor, even if there the same part from the same manufacturer.
Calling .calibrate() on your Gyro can help mitigate some of this, and for simple to moderately complex processes, this would be enough (and in general is going to be enough for most situations you might want to write code for). But some teams (and perhaps you in the future) will want to do something more complex that requires <i>very</i> accurate gyroscope readings. This is where filters come in. Filters help to wash away the noise while still allowing the actual activity to pass through appropriately.
Filters could be an example all there own, and in the Java training, we covered them very little, because setting up filters isn't usually necessary for beginner to intermediate work. If you're interested in reading more about filters, [refer to the WPILib documentation](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/filters/index.html) for more information. They provide a significant amount of detailed information, as well as graphs made with sample data to give a rough idea about the behavior of each filter type.