2022PythonExamples/16 - NetworkTables and Limelight/README.md

5.3 KiB

16 - NetworkTables and Limelight

As we progress through the next several examples, we're going to see some aspects of robot programming that aren't always necessary. Generally speaking most of the things we've covered so far with the exception of the structure and drivetrain code isn't always necessary. It depends a lot on what you're doing. But the next few examples are going to be slightly more "on the fringe" because you may find a reason to use these things, you may not. On top of that, several of the things we're going to talk about have budgetary requirements that your team may not necessarily be able to meet.

None the less, you should know what these things are, just in case you want to use them in the future.

Ok, I'll start with the first thing in the example name, what's a NetworkTable?

NetworkTables is a software mechanism for sending data between systems over a network. Data is formed by a structure of key/value pairs, sort of like a dictionary, where you have a word (the key) and a definition (the value). These key/value pairs are organization into tables, the tables themselves are values associated with keys further up in the hierarchy.

It's basically a bunch of nested Excel spreadsheets, which is a terrible example, but it's the one I'm going with.

To use NetworkTables, you have to have a NetworkTables server, that NetworkTables clients can connect to. Thankfully, the work of creating a NetworkTables server is managed for you. When you run your robot code on the RoboRIO, a NetworkTables server is automatically created, and begins listening for connections from clients.

There are several different forms of clients in FRC. You could use the NetworkTables libraries to create your own client for accessing data or you could use built in tools like Outline Viewer, Shuffleboard, and the LabVIEW Dashboard to begin interacting with the NetworkTables data.

In this example, we'll be interacting with the NetworkTables libraries directly to create a class that manages interaction between our robot and a device called a Limelight.

A second new thing, great, what's a Limelight?

I'm pretty sure I talk a little bit about the Limelight in 8 - Basic Robot Electronics but to recap, a Limelight is a camera with LEDs on it that you use for computer vision tracking. It's based on the ever popular Raspberry Pi small board computers and can be configured and used to provide your robot with real time object tracking for things like field goals and score-able game elements.

The Limelight takes a ton of the work required to create a workable vision tracking algorithm and gives you a simple web based user interface to configure specific settings to tune for what you want to track. Many teams rely on the Limelight over manual vision tracking because in the vast majority of cases it performs well enough for what a team wants to accomplish.

The Limelight communicates with the rest of your robot program over Networktables. The software connects to the NetworkTables server running on your RoboRIO and creates a table with a key called "limelight" (this is actually the default, but you can change the table name if you want). By interacting with this table (whether you're retrieving or setting values), you can manipulate the Limelight and retrive information about what it is tracking based on the configuration you set up using the web user interface prior to putting your robot on the competition field.

The Limelight has several important values you can observe to if it's looking at a target, and if it is, where the target is and roughly how large it is (compared to the overall size of the image). These elements are (by key name):

  • tv - tv will return a decimal (float) with a value of either 0 or 1. It'll be 0 if there's no target currently in view, or 1 if there is
  • tx - tx returns a decimal (float) with a value that is the number of degrees from the left/right center (by default) of the image the target it sees is. The value can be negative or positive up to about 30 degrees (depends on model).
  • ty - ty returns a decimal (float) with a value that is the number of degrees from the up/down center (by default) of the image the target it sees is. This value can be negative or positive up to about 27 degrees (depends on model).
  • ta - ta returns a percentage (float) with a value that is the area that the current target takes up in the image as a whole. If the target covers more than half the image, then it returns 50.

These values can be a pain to work with directly, especially if you have to work with them repeatedly throught different sections of your code. We are going to look at creating a Limelight class that we can use to interact with our Limelight with, and to give better names to the different elements we are working with.

The Limelight also has settings you can change from your robot code, you'll see explanations of these in the comments, more than likely you won't need to interact with them to get what you want out of the Limelight, but they are there for completeness.

What else can I do with NetworkTables?

Interacting directly with NetworkTables? Not a lot at this point, you'd have to have some other goal in mind. Indirectly working with NetworkTables however, we'll see that in 18 - CameraServer and Shuffleboard