Log in

View Full Version : How do I use NetworkTables' SubTables?


Aero
13-02-2014, 16:19
Hey! I'm trying to get NetworkTables working, and I have a list of things I want to send to the driver station. So I'm putting values into a Subtable on the robot, and trying to read them on the DS.

Is there a way to loop through every value in an ITable (which NetworkTables extends)? I assume there is, because the TableViewer program can do it (I compiled it but can't figure it out)

Here's the code I use for putting the info into the NetworkTable.

public void sendSubsystemInfo() {
Vector list = Warp7Robot.subsystem.subsystemList;
NetworkTable subtable = (NetworkTable) table.getSubTable("Subsystems");

for (int i = 0; i < list.size(); i++) {
Subsystem s = (Subsystem) list.elementAt(i);
subtable.putBoolean(s.getName(), s.isEnabled());
}
}

irvingc
13-02-2014, 19:48
The TableViewer application does not use NetworkTable objects exclusively, there's quite a bit of back-end.

You know the names of your subsystems; is there a reason you can't just use ITable.getBoolean("NAME_HERE")?

Aero
13-02-2014, 21:47
The TableViewer application does not use NetworkTable objects exclusively, there's quite a bit of back-end.

You know the names of your subsystems; is there a reason you can't just use ITable.getBoolean("NAME_HERE")?
I could, but it's kind of annoying to have to edit the driver station program every time I add or remove a subsystem. I'd much prefer dynamicism

fovea1959
14-02-2014, 09:55
Once you connect, you can call NetworkTableClient.getEntryStore().list() to get a list of all the existing key names in your client's copy of the table.

A listener will give you updated keys and values as they come in, so even if you don't get all the existing key names when you first connect, you'll be able to build a complete list of keys pretty quickly if you are sending them out in periodic().

fovea1959
14-02-2014, 09:57
I just reread your post, and something hit me...

Where are you trying to see what values are in the table? The FRC Driver Station software, the dashboard, or another team-written program running on the driver station PC?

fovea1959
14-02-2014, 10:03
...and I just checked, and dumping the keys at the beginning is unnecessary. When you connect, it appears you get update events for every existing value in the table, so you can just set up a listener and watch the names and values stream in...

Aero
14-02-2014, 10:25
...and I just checked, and dumping the keys at the beginning is unnecessary. When you connect, it appears you get update events for every existing value in the table, so you can just set up a listener and watch the names and values stream in...

Awesome! We're using a custom piece of software for the client-side, as the SmartDashboard didn't really offer what we were looking for. Is there any example code for adding an ITableListener (I think that's what I want, not sure)? I can probably figure it out tonight, but I'm just wondering.
Thanks again!

fovea1959
14-02-2014, 11:06
super straightforward: I implemented ITableListener, then called networkTableClient.addTableListener (this, true).

If you need to see connections and disconnections, implement IRemoteConnectionListener, and call networkTableClient.addTableListener(this,true).

of course, you can have different objects for your listener (so you wouldn't use this), but hopefully you'll get the gist of it.

your valueChanged() can be a little tricky: you could get back an array or just a single variable of unknown type, and you don't know ahead of time what it will be. I had a few instance operators in there to sort stuff out. For display purposes, this worked nice:


if (o instanceof Object[]) {
for (Object o1 : (Object[]) o) {
sb.append("\t");
sb.append(o1.toString());
} else {
sb.append("\t");
sb.append(o.toString());
}


You *could* also get the type by going back to the client.getEntryStore().getEntry(name).getType(), but I didn't need it. I did grab client.getEntryStore().getEntry(name).getSequenceN umber() so I could see if we had updates that didn't get to the client (this is from our telemetry receiver).