Networking

Description

Networking on the robot is fun! Not only that, but it is important to feedback diagnostic information to the driver station to help drivers know the robot’s state during a match and help programmers debug issues with the robot.

Network Tables

WPILib provides the NetworkTables library for communicating data between devices on the robot and the driver station. NetworkTables is a key-value store, meaning that data is stored in a table with a key (name) and a value. The value can be strings, booleans, doubles, etc.

There are several different “Tables” on the robot, each with a different purpose.

  • SmartDashboard
    • Used to communicate information to the dashboard on the driver station computer.
  • FMSInfo
    • Used to communicate match information from the field management system to the robot.
  • Limelight (May be named differently, ex. “limelight-homer”)
    • Used to communicate information from the Limelight sensor to the roboRIO and the driver station.

Initialization

To get a NetworkTable, you first have to get the nt::NetworkTableInstance object using the nt::NetworkTableInstance::GetDefault method. Then, using that NetworkTableInstance object, you can get a nt::NetworkTable using the nt::NetworkTableInstance::GetTable method.

#include <networktables/NetworkTableInstance.h>
#include <networktables/NetworkTable.h>

// Get the default NetworkTableInstance object
nt::NetworkTableInstance ntinst = nt::NetworkTableInstance::GetDefault();

// Get the SmartDashboard table
std::shared_ptr<nt::NetworkTable> sd = ntinst.GetTable("SmartDashboard");

Reading Data

To read data, you can use the “Get” methods of the nt::NetworkTable object, such as

  • GetNumber
    • Gets a double value from the table.
  • GetBoolean
    • Gets a bool value from the table.
  • GetString
    • Gets a std::string value from the table.

All the “Get” methods require the key (name) of the value to get, and a fallback value to return if the key does not exist in the table.

double double_value = sd->GetNumber("my_number", 0.0);
bool bool_value = sd->GetBoolean("my_bool", false);
std::string str_value = sd->GetString("my_string", "");

Writing Data

To write data, you can use the “Put” methods of the nt::NetworkTable object, such as

  • PutNumber
    • Puts a double value from the table.
  • PutBoolean
    • Puts a bool value from the table.
  • PutString
    • Puts a std::string value from the table.

All the “Put” methods require the key (name) of the value to get, and the value to put.

sd->PutNumber("my_number", 4.2);
sd->PutBoolean("my_bool", true);
sd->PutString("my_string", "Hi!");

SmartDashboard

Instead of using the low-level NetworkTables API, you can use the SmartDashboard class, which provides a simpler API for reading and writing data to the SmartDashboard table.

#include <frc/smartdashboard/SmartDashboard.h>

// Writing data
frc::SmartDashboard::PutNumber("my_number", 4.2);
frc::SmartDashboard::PutBoolean("my_bool", true);
frc::SmartDashboard::PutString("my_string", "Hi!");

// Reading data
double double_value = frc::SmartDashboard::GetNumber("my_number", 0.0);
bool bool_value = frc::SmartDashboard::GetBoolean("my_bool", false);
std::string str_value = frc::SmartDashboard::GetString("my_string", "");

Rolling Thunder Programming Subteam - The Greatest Subteam on 1511