NetworkTables - hammerheads5000/Team-Documentation GitHub Wiki

We use NetworkTables to transfer data between commands, subsystems, etc., and to and from the computer. See WPILib's NetworkTables Documentation.

Structure

Instance

Instances are full containers for an instance of NetworkTables. We will only ever use the default instance, which shows up in code as NetworkTableInstance inst = NetworkTableInstance.getDefault();

Table

Tables are collections of topics. Think of these as folders of values, which you can access like file paths. One of the default tables is "/SmartDashboard", which puts data on the SmartDashboard interface. An example of a table could be "/ArmData" or "/Swerve/PositionData" (tables can be nested). Usually, it will be better to just use an instance to access topics, but if you want to create a reference to a table in code, it would look like NetworkTable someTable = inst.getTable("tableName");

Topic

Topics are channels that hold a value, which Publishers can write and Subscribers can read. Think of them like variables, which you can also read as a queue (meaning you can get all updates to the value instead of just the most recent). Topics have a set datatype. An example topic of type double could be "/ArmData/EncoderValue". Topics have a set datatype. Access/create it in code by accessing an instance or table. For example, DoubleTopic encoderTopic = inst.getDoubleTopic("/ArmData/EncoderValue"); or DoubleTopic encoderTopic = table.getDoubleTopic("EncoderValue");

Publisher

Publishers can create a topic and publish values to it. Create one from a topic by calling the publish method, for example: DoublePublisher encoderPublisher = encoderTopic.publish();

You can set the default value with setDefault. For example, encoderPublisher.setDefault(0.0);

Set the value with set. For example, encoderPublisher.set(1.3);

Subscriber

Subscribers receive values from a topic. Create one with a topic's subscribe method that takes the default value to return if there is no value available as a parameter. For example, DoubleSubscriber encoderSubscriber = encoderTopic.subscribe(0.0) (default value 0.0).

To get the most recent published value, use get. For example, double encoderVal = encoderSubscriber.get();

If you want the timestamp along with the value, use getAtomic, which returns a TimestampedDouble. For example, TimestampedDouble encoderValTimestamped = encoderSubscriber.getAtomic();

You can also read all values since last getting them with readQueue for timestamped values or readQueueValues which does not. They return an array of TimestampedDoubles and doubles respectively. For example, TimestampedDouble[] tsEncoderValUpdates = encoderSubscriber.readQueue(); or double[] encoderValUpdates = encoderSubscriber.readQueueValues();

Entry

Entries are combined publishers and subscribers, so they can both read and write to a topic. To create one from a topic, use the topic's getEntry method with a default value as a parameter, the same as a subscriber. For example, DoubleEntry encoderEntry = encoderTopic.getEntry(0.0); They support all publisher and subscriber methods, such as get, set, etc.