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.
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.
- Gets a
- GetBoolean
- Gets a
bool
value from the table.
- Gets a
- GetString
- Gets a
std::string
value from the table.
- Gets a
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.
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.
- Puts a
- PutBoolean
- Puts a
bool
value from the table.
- Puts a
- PutString
- Puts a
std::string
value from the table.
- Puts a
All the “Put” methods require the key (name) of the value to get, and the value to put.
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.