Communication code between robot and computer provided by WPI

Hello Everyone,

There are a lot of people trying to figure out how to do TCP or UDP connections between the robot and a random computer. WPI has already provided the tools to do this, in the form of “NetworkTables”. While the robot has NetworkTable code in C++ and Java, the computer version has only been written in Java. In fact, NetworkTable is what the SmartDashboard uses to communicate with the robot (which is why it can both read data and write data).

Do you know about hash tables? The NetworkTable is a hash table shared between the robot and any other computer (even multiple computers at the same time). Both the robot and the computers can write to the table, and the change will be sent to everyone that is connected.

Let’s say that you wanted to process images on some other computer, and wanted to send data back for the x and y values. You start by coming up with a name for your table, we could call it something clear like “camera”.

Then on the computer, put the NetworkTable.jar jar in your project (you can download the project from the SmartDashboard project, or you can use the .jar in C:\Program Files\SmartDashboard\lib if you installed the SmartDashboard using the installer).

The computer code is very simple. To start, you need to put the line:

NetworkTable.setTeam(3504); // <-- put your own team number here

Then, when you want to send data, you can do this:

NetworkTable.getTable("camera").putInt("x", 50);
NetworkTable.getTable("camera").putInt("y", 60);

and that’s all the network code you need for the computer! It will automatically link up with the robot, sync the data, and reconnect if the robot goes down.

For the robot, you don’t need to specify your team number, just use:

NetworkTable.getTable("camera").getInt("x", 0);  // <-- the second number is the default value you want if their is no number there already
NetworkTable.getTable("camera").getInt("y", 0);

Can you go more in depth instead of just drag and drop i have been trying for a while now…