So we’re trying to use network tables for vision this year. We have an axiscam running on GRIP, and we need to publish data to network tables (like distance, center of a contour, etc.).
Screensteps details how to use Network Tables, but we’re having trouble with EntryListeners (which can update variables for the robot). The problem is that we’re new to this and screensteps only has an example in Java.
We can’t figure out what keys, values, and flags are in C++. Thanks for any help!
Before we get too far along, EntryListeners are used to asynchronously listen for changes on NetworkTables. The callback function is called on a separate thread. Is this what you want to do? It’s more typical to simply read the NetworkTable value directly in your main robot code loop to get the most recent value.
That being said, if you look at the declaration of NetworkTable::AddEntryListener in C++, you’ll see the first parameter is a TableEntryListener, which is defined as follows:
typedef std::function<void(NetworkTable* table, StringRef name,
NetworkTableEntry entry,
std::shared_ptr<Value> value, int flags)>
TableEntryListener;
Thus here’s the equivalent C++ code:
void run() {
auto inst = nt::NetworkTableInstance::GetDefault();
auto table = inst.GetTable("datatable");
inst.StartClientTeam(190);
table->AddEntryListener([] (nt::NetworkTable* table, wpi::StringRef name,
nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value, int flags) {
// I don't try to print value here, I just print the type
wpi::outs() << "Key: " << key
<< " Value type: " << static_cast<int>(value->type())
<< " Flags: " << flags << '\n';
}, NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
std::this_thread::sleep_for(std::chrono::seconds(100));
}