Log in

View Full Version : Get SmartDashboard keys?


Toa Circuit
16-02-2015, 14:25
Is there any way to get a list/array of the keys in the SmartDashboard?

legts
16-02-2015, 14:51
It is possible to display values in the SmartDashboard, but I'm not really sure that you mean by "keys"

otherguy
16-02-2015, 16:05
The Network tables viewer likely does what you need:
http://wpilib.screenstepslive.com/s/4485/m/13503/l/144981-2015-frc-software-component-overview

Look at the section titled "Outline Viewer"

Toa Circuit
16-02-2015, 16:14
I should clarify. I need to do it programmatically, in the robot code. (I want to save values to disk so we don't keep losing them) I'd sneak through and grab the underlying hashtable, but it's private in WPILib. :(

When you do SmartDashboard.putString("Key","value");

"Key" is the key. I need to, through code, fetch a list of all keys that are on the SmartDashboard/NetworkTable.

Ozuru
16-02-2015, 16:31
I should clarify. I need to do it programmatically, in the robot code. (I want to save values to disk so we don't keep losing them) I'd sneak through and grab the underlying hashtable, but it's private in WPILib. :(

When you do SmartDashboard.putString("Key","value");

"Key" is the key. I need to, through code, fetch a list of all keys that are on the SmartDashboard/NetworkTable.

With each setter method there is (obviously) a getter -- you could try just doing that. I would recommend put it all into function and then just call the function.

I'm unsure but there might be a way to do a for-each loop in the table -- try seeing what datatypes it returns and try doing a simple for-each loop for that.

fovea1959
17-02-2015, 10:04
Perhaps edu.wpi.first.wpilibj.Preferences would be useful? It takes care of persisting part of the NetworkTables tree to storage on the roboRIO.

There is a GUI element in the SmartDashboard for manipulating RobotPreferences.

If you don't like that GUI, it's not hard to write a custom GUI for the driver's PC for tweaking settings in the Preferences via the NetworkTables, and persisting them in the cRIO. Haven't double checked it for this year yet, but would be very surprised if it's broken.

https://github.com/fovea1959/FRC3620_PreferencesEditor

fovea1959
17-02-2015, 10:09
...and if you really want to iterate over the keys, look at the source for OutlineViewer. I recall that the listener for updates fires off for all existing NetworkTables values when you first connect to the network tables server.

We wrote a server to log data in the network tables to the driver's station disk last year. It was really hard on bandwidth, so we are just logging in the roboRIO this year. The source code for that might help for how to determine what is in the NetworkTables tree.

https://github.com/fovea1959/FRC3620_Telemetry/blob/master/FRC3620TelemetryCollection/src/org/first3620/networktablelogger/DougTableLoggerNoGUI.java

MatthewC529
18-02-2015, 15:35
I should clarify. I need to do it programmatically, in the robot code. (I want to save values to disk so we don't keep losing them) I'd sneak through and grab the underlying hashtable, but it's private in WPILib. :(

When you do SmartDashboard.putString("Key","value");

"Key" is the key. I need to, through code, fetch a list of all keys that are on the SmartDashboard/NetworkTable.

If you REALLY needed to, what says you can't get the private hashtable? We have experimented with grabbing underlying NetworkTable object via reflection but generally this has been fruitless for our needs, requiring more clever solutions.

Java's Field object allows you to set the access flag and it isn't like WPI's launch script executes the program with the default SecurityManager. Just remember Reflection is expensive and there is probably a better way as long as you have the time to look for the better way.

To retrieve a Field, convert it to the right type, and to ensure it is returned to it's original accessibility we use:

private static Object retrieveField(Field field, Object obj) throws IllegalArgumentException, IllegalAccessException {
Object item = null;
final boolean accessible = field.isAccessible();

if (!accessible) {
field.setAccessible(!accessible);
}

item = field.get(obj);

if (!accessible) {
field.setAccessible(accessible);
}

return item;
}

public static <T> T getStaticField(Class<?> klass, String fieldName, Class<T> conversionClass) {
try {
final Field field = klass.getDeclaredField(fieldName);
return conversionClass.cast(retrieveField(field, null));
} catch (final Exception e) {
throw new RobotExecutionException("Failure to reflectively obtain Static Field '" + fieldName + "'!", e);
}
}

public static <T> T getInstanceField(Class<?> instClass, Object instance, String fieldName, Class<T> conversionClass) {
try {
final Field field = instClass.getDeclaredField(fieldName);
return conversionClass.cast(retrieveField(field, instance));
} catch (final Exception e) {
throw new RobotExecutionException("Failure to reflectively obtain Instance Field '" + fieldName + "' from Object '" + instance + "'!", e);
}
}

nfusselman1
19-02-2015, 10:36
What do you have to do to program your robot do all of the smart dashboard stuff like cameras and a gyroscope will be shown in the LabView Default Dashboard.

Lead Programmer (4076)
nfusselman1

fovea1959
19-02-2015, 11:12
@nfusselman: you usually want to start a new thread if the subject changes, that way the title will be something that someone down the line can search for.

Take a look at http://wpilib.screenstepslive.com/s/3120/m/7932/l/90078-getting-started-with-the-smartdashboard and see if that answers your questions...