Our C++ robot code reads settings via the WPILib Preferences class, but we want to edit these values on the smart dashboard.
We added the Robot Preferences widget on the dashboard. None of the robot-defined preference names are seen there, even though the dashboard shows other NetworkTable status data from the robot.
We pressed SAVE on the preferences widget, then rebooted robot and dashboard and when we restart both and connect, the dashboard preferences widget still shows no robot-defined preference values.
Does anyone know how we can edit the robots preferences from the Smartdashboard’s preferences widget?
To get preferences to work to/from the smartdashboard here is what I had to do in java. C++ will be similar.
On the robot:
// declare variable
NetworkTable preferencesTable;
// in robotInit get or create singleton instance (getTable will get instance if it exists or create one if it does not.
preferencesTable = NetworkTable.getTable("Preferences");
// write some test data
preferencesTable.putString("pref1", "998899");
preferencesTable.putString("pref2", "77777");
// in teleopPeriodic for test purposes get the data
System.out.println( "pref1 = " + preferencesTable.getString("pref1", "Empty"));
System.out.println( "pref2 = " + preferencesTable.getString("pref2", "Empty"));
Start running smartdashboard on the DS with the Preferences grid open.
Start program on robot with above code included. The two initial values for pref1 and pref2 should show on the grid. Enable teleop and you should see the two values on the console. Now edit the values in the preferences grid on the smartdashboard, the values should change on the console.
One caveat, use putString and getString not one of the other methods such as getDouble, getInt, etc. In my first test I used putInt to set the initial values and getInt to retrieve them. When I changed the values on the smartdashboard, they stopped displaying in the console. Smartdashboard sends everything as a string. You will need to parse to a number on the cRio.
The save button is not necessary unless you want to save the table values to flash memory on the crio. They could then be read in at startup. There is a cautionary comment that too much saving can damage the flash memory of the crio. Don’t do it in your periodic or continuous methods, only when necessary.
Thank you so much for your timely posting. I’ve been trying to use the SmartDashboard for Driver/Operator input to set values. Your example and point about using String was the key piece.
One of the students explained that the Tab key can be used to “enter” a new value since the Enter key will disable the robot.
One question. I accidentally used the Save button and now the table has a variable that I no longer use. How can I clear the saved entries from the cRIO flash memory?
update: Perhaps the way to remove bad entries is with the Preferences.remove() method?