NetworkTables Tables and Topics
Using the NetworkTable Class
The NetworkTable
(Java, C++, Python
) class is an API abstraction that represents a single « folder » (or « table ») of topics as described in NetworkTables Organization. The NetworkTable class stores the base path to the table and provides functions to get topics within the table, automatically prepending the table path.
Getting a Topic
A Topic
(Java, C++, Python
) object (or NT_Topic
handle) represents a topic. This has a 1:1 correspondence with the topic’s name, and will not change as long as the instance exists. Unlike publishers and subscribers, it is not necessary to store a Topic object.
Having a Topic object or handle does not mean the topic exists or is of the correct type. For convenience when creating publishers and subscribers, there are type-specific Topic classes (e.g. BooleanTopic
: Java, C++, Python
), but there is no check at the Topic level to ensure that the topic’s type actually matches. The preferred method to get a type-specific topic to call the appropriate type-specific getter, but it’s also possible to directly convert a generic Topic into a type-specific Topic class. Note: the handle-based API does not have a concept of type-specific classes.
NetworkTableInstance inst = NetworkTableInstance.getDefault(); NetworkTable table = inst.getTable("datatable"); // get a topic from a NetworkTableInstance // the topic name in this case is the full name DoubleTopic dblTopic = inst.getDoubleTopic("/datatable/X"); // get a topic from a NetworkTable // the topic name in this case is the name within the table; // this line and the one above reference the same topic DoubleTopic dblTopic = table.getDoubleTopic("X"); // get a type-specific topic from a generic Topic Topic genericTopic = inst.getTopic("/datatable/X"); DoubleTopic dblTopic = new DoubleTopic(genericTopic);
nt::NetworkTableInstance inst = nt::NetworkTableInstance::GetDefault(); std::shared_ptr<nt::NetworkTable> table = inst.GetTable("datatable"); // get a topic from a NetworkTableInstance // the topic name in this case is the full name nt::DoubleTopic dblTopic = inst.GetDoubleTopic("/datatable/X"); // get a topic from a NetworkTable // the topic name in this case is the name within the table; // this line and the one above reference the same topic nt::DoubleTopic dblTopic = table->GetDoubleTopic("X"); // get a type-specific topic from a generic Topic nt::Topic genericTopic = inst.GetTopic("/datatable/X"); nt::DoubleTopic dblTopic{genericTopic};
NT_Instance inst = nt::GetDefaultInstance(); // get a topic from a NetworkTableInstance NT_Topic topic = nt::GetTopic(inst, "/datatable/X");
NT_Instance inst = NT_GetDefaultInstance();
// get a topic from a NetworkTableInstance
NT_Topic topic = NT_GetTopic(inst, "/datatable/X");
import ntcore inst = ntcore.NetworkTableInstance.getDefault() table = inst.getTable("datatable") # get a topic from a NetworkTableInstance # the topic name in this case is the full name dblTopic = inst.getDoubleTopic("/datatable/X") # get a topic from a NetworkTable # the topic name in this case is the name within the table; # this line and the one above reference the same topic dblTopic = table.getDoubleTopic("X") # get a type-specific topic from a generic Topic genericTopic = inst.getTopic("/datatable/X") dblTopic = ntcore.DoubleTopic(genericTopic)