2014 FRC Control SystemWPILib programming documentationWPILib programmingRobot to driver station networkingWriting a simple NetworkTables program in C++ and Java with a Java client (PC side)

Writing a simple NetworkTables program in C++ and Java with a Java client (PC side)

NetworkTables is an implementation of a distributed "dictionary". That is named values are created either on the robot, driver station, or potentially an attached coprocessor, and the values are automatically distributed to all the other participants. For example, a driver station laptop might receive camera images over the network, perform some vision processing algorithm, and come up with some values to sent back to the robot. The values might be an X, Y, and Distance. By writing these results to NetworkTable values called "X", "Y", and "Distance" they can be read by the robot shortly after being written. Then the robot can act upon them.

NetworkTables can be used by programs on the robot in either C++, Java or LabVIEW and is built into each version of WPILib.

Using NetworkTables from a Java robot program

Using NetworkTables from a Java robot program

NetworkTables programs on the robot are easiest to write. The program simply reads or writes values from within the program. The instance of NetworkTables is automatically created by the WPILib runtime system. This example is the simplest robot program that can be written that continuously writes pairs of values (X, and Y) to a table called "datatable". Whenever these values are written on the robot, they can be read shortly after on the desktop client.

  1. The variable "table" is of type NetworkTable. NetworkTables are hierarchical, that is tables can be nested by using their names for representing the position in the hierarchy.
  2. The table is associated with values within the hierarchy, in this case the path to the data is /datatable/X and /datatable/Y.
  3. Values are written to the "datatable" NetworkTable. Each value will automatically be replicated between all the NetworkTable programs running on the network.

When this program is run on the robot and enabled in Teleop mode, it will start writing incrementing X and Y values continuously, updating them 4 times per second (every 0.25 seconds).

Using Network Tables from a C++ robot program

Using Network Tables from a C++ robot program

NetworkTables programs on the robot are easiest to write. The program simply reads or writes values from within the program. The instance of NetworkTables is automatically created by the WPILib runtime system. This example is the simplest robot program that can be written that continuously writes pairs of values (X, and Y) to a table called "datatable". Whenever these values are written on the robot, they can be read shortly after on the desktop client.

  1. The variable "table" is of type NetworkTable. NetworkTables are hierarchical, that is tables can be nested by using their names for representing the position in the hierarchy.
  2. The table is associated with values within the hierarchy, in this case the path to the data is /datatable/X and /datatable/Y.
  3. Values are written to the "datatable" NetworkTable. Each value will automatically be replicated between all the NetworkTable programs running on the network.

When this program is run on the robot and enabled in Teleop mode, it will start writing incrementing X and Y values continuously, updating them 4 times per second (every 0.25 seconds).

Using the client version of NetworkTables on a desktop computer

Using the client version of NetworkTables on a desktop computer

The NetworkTables libraries are built into all versions of robot-side WPILib. You can set values from the robot in C++, Java or LabVIEW with simple put and get methods. To use it on a laptop (usually the driver station computer), there are several options:

  1. a client library that you can reference from Java programs that you write.
  2. from plugins that you write for the SmartDashboard (it's included there)

The Java library is part of the NetBeans Java plugin installation and can be found in the <user-directory>/sunspotfrcsdk/desktop-lib directory as shown here.

For C++ WindRiver installations the .jar files are located in the C:\WindRiver\WPILib\desktop-lib directory.

Setting up NetBeans to create the client-side (laptop/desktop computer) program

Setting up NetBeans to create the client-side (laptop/desktop computer) program

To write a program that runs on your PC that uses NetworkTables the Java project must reference the JAR file from the NetBeans installation shown above. The project has to reference the networktables-desktop.jar file. This is an example of doing it with NetBeans but any IDE will have a way of adding .JAR files to a project. In this example the .jar file was added to the project properties.

Note: this is not necessary for a robot program since NetworkTables is built into WPILib. You simply have to add the necessary java import statements or C++ #includes for the NetworkTable classes that are used in the program.

The client (laptop) side of the program

The client (laptop) side of the program

This program is the simplest program that you can write on a PC to use NetworkTables. It continuously reads the values from robot example in the previous step.

  1. Set NetworkTables to client mode (not on the robot) and specifiy the IP address of the robot.
  2. Create a NetworkTable variable ("table") that is associated with the "datatable" NetworkTable.
  3. Loop continuously and sleep for 1 second each time through the loop.
  4. Read the X and Y values from the /datatable NetworkTable that was written on the robot in the previous program and print the values. The program output is shown below.

Program output from the simple client example

Program output from the simple client example

This output is from the NetBeans "output" window. This is the results from the System.out.println() method from the previous program that is running on a desktop computer retrieving values written on the robot from the earlier Robot program.

Viewing the NetworkTables variables in TableViewer

Viewing the NetworkTables variables in TableViewer

There is a diagnostic tool called TableViewer that will display the current state of the NetworkTables table. In this case, running it will show the current values of all the variables in the variables created in this example are shown in the red box above. TableViewer is located in the sunspotfrcsdk folder for NetBeans intstalls or in the C:\WindRiver\Workbench\WPILib folder for C++ installs.

Receiving notifications of changes to a NetworkTable

Receiving notifications of changes to a NetworkTable

A PC or Robot program can receive notifications of changes to a NetworkTable. This example is a client-side (PC) program, but the same concepts will work on a robot program. These notifications are received asynchonously as the new values are received by the NetworkTable library.

  1. Connect to the NetworkTable server using the same technique as in the previous example.
  2. Register this class as a ITableListener. Changes to the "datatable" will be reported to this class through the "valueChanged" callback method (below)
  3. Sleep for 100 seconds while values are reported. The program could do anything here, but in this simple example, it only waits for 100 seconds while waiting for values to arrive.
  4. This valueChanged method is called whenever there are changes or additions to the NetworkTable "datatable". The boolean value bln will be true if this is a new value or false if it is just an update to a previously reported variable. The Object is the new value that has been received. The output from this program is shown in the next step.

Results of running the client-side (PC) TableListener example

Results of running the client-side (PC) TableListener example

In this screen image the values returned from the TableListener example are shown. Notice that at the top of the output X and Y are returned with their respective values and "true" for the boolean value. This indicates that they are new values. In all the other cases, the boolean value is "false" indicating that it is just an update to a previously reported value.

Using NetworkTables with RoboRealm

Using NetworkTables with RoboRealm

RoboRealm is a program that does client-side (PC) vision processing. RoboRealm can connect to a camera on a robot and do real-time tracking of field targets and sending the results back to the robot. In the past this required writing custom networking code for the PC to robot communications. RoboRealm now has a built-in NetworkTables client and this allows the RoboRealm program to send values directly back to the robot via some shared variables.

For further information see: http://www.roborealm.com/help/Network_Tables.php