Use this file to discover all available pages before exploring further.
NetworkTables Core (ntcore) provides a high-performance, real-time publish-subscribe protocol for communication between robot components, driver stations, and coprocessors.
// Create subscriber with default valueDoubleSubscriber sub = topic.subscribe(0.0);// Subscribe with optionsPubSubOption[] options = { PubSubOption.pollStorage(10), // Buffer size PubSubOption.periodic(0.02) // Update rate};DoubleSubscriber sub2 = topic.subscribe(0.0, options);// Get latest valuedouble value = sub.get();// Get with default if no valuedouble value2 = sub.get(99.9);// Check if value existsboolean exists = sub.exists();// Get timestamped valueTimestampedDouble tValue = sub.getAtomic();double val = tValue.value;long timestamp = tValue.timestamp;long serverTime = tValue.serverTime;// Close subscribersub.close();
// Get all value changes since last readTimestampedDouble[] values = sub.readQueue();for (TimestampedDouble val : values) { System.out.println(val.value + " at " + val.timestamp);}
Entries combine publisher and subscriber functionality.
// Create entryDoubleEntry entry = topic.getEntry(0.0);// Read valuedouble value = entry.get();// Write value entry.set(2.5);// Set default (published if topic doesn't exist)entry.setDefault(1.0);// Atomic get and setentry.set(entry.get() + 1.0);
NetworkTableInstance inst = NetworkTableInstance.getDefault();// Start as client (coprocessor, dashboard)inst.startClient4("myclient");inst.setServerTeam(1234); // Team number// orinst.setServer("10.12.34.2"); // IP address// Start as server (robot)inst.startServer();// Stopinst.stopClient();inst.stopServer();
nt::NetworkTableInstance inst = nt::NetworkTableInstance::GetDefault();// Start as clientinst.StartClient4("myclient");inst.SetServerTeam(1234);// orinst.SetServer("10.12.34.2");// Start as serverinst.StartServer();
int listenerId = inst.addConnectionListener(true, event -> { if (event.is(NetworkTableEvent.Kind.kConnected)) { System.out.println("Connected to server"); } else if (event.is(NetworkTableEvent.Kind.kDisconnected)) { System.out.println("Disconnected from server"); }});// Remove listenerinst.removeListener(listenerId);
// Get all topicsTopicInfo[] topics = inst.getTopics();for (TopicInfo info : topics) { System.out.println(info.name + ": " + info.type);}// Get topics by prefixTopicInfo[] visionTopics = inst.getTopics("/vision/");// Get topic infoTopic topic = inst.getTopic("/mytable/value");TopicInfo info = topic.getInfo();String name = info.name;String type = info.type;boolean retained = info.retained;
// Set properties when publishingPubSubOption[] options = { PubSubOption.retained(true), // Retain value across connections PubSubOption.persistent(true) // Save to persistent storage};DoublePublisher pub = topic.publish(options);// Set properties on topictopic.setProperty("units", "meters");topic.setProperty("min", -10.0);topic.setProperty("max", 10.0);// Get propertiesString units = topic.getProperty("units");
// Cache publishers/subscribersprivate final DoublePublisher velocityPub = table.getDoubleTopic("velocity").publish();// Don't create publishers in loopspublic void periodic() { velocityPub.set(getVelocity()); // Good // table.getDoubleTopic("velocity").publish().set(getVelocity()); // Bad!}// Use struct topics for complex data instead of multiple topicsStructPublisher<Pose2d> posePub = inst.getStructTopic("pose", Pose2d.struct).publish();posePub.set(pose); // One publish instead of 3
// Close publishers/subscribers when donepublic void close() { velocityPub.close(); poseSub.close();}// Or use try-with-resourcestry (DoublePublisher pub = topic.publish()) { pub.set(1.0);}