Robot Preferences

In the FIRST Robotics Competition, it may be desirable to a team to have a way to easily alter variables in their code; however, the process of changing the code and re-loading it on a robot is a lengthy one and presents the possibility of breaking the current program. This is why, in the SmartDashboard program made by FIRST, there is a handy way to create and edit variables without having to restart the CRio. Using the Robot Preferences, one can create variables and use them in the program; variables which can be changed on the fly  and without the chance of harming the robot code.

The basic idea behind RobotPreferences is as follows:

  • In your code, you create a variable to store in the Preferences. This variable is defined by a key name and given a value.
  • This value will be saved in a file on the robot.
  • When you call the preference with that key name, it will return the value saved in the file.
  • If you open the Preferences widget in the SmartDashboard, it will load a list of all the variables stored on the robot. From here you can edit them and save new default values for them.

Using Preferences in Your Code
When you want to reference the Preferences file you call the class

However, to simplify things, you may want to assign this to a variable in the class constructor like this
(From now on I will use this variable)
m_pref = Preferences.getInstance();

After this, you can add variables to the file, and save it. Once you save the file, all current name/value pairs will be saved as default. Every time the robot starts, the default values are loaded.
To add a variable, use the one of the “add” methods, depending on what data type it will be. Then, provide the String name and the original default value.

m_pref.addDouble(“firstNumber”, 5.0);;

Make sure you call the save method after creating  all of your variables; this will ensure that they will persist after you reboot the CRio.

Later, you can call this variable with a “get” method.

m_pref.getDouble(“firstNumber”, 0.0);

Notice, I provided, two parameters again. The first is the variable name to call, and the second is a back-up value to return if the variable cannot be loaded.

Using the Preferences Widget

Now that you know how to use this in Java, I’ll show what the widget interface looks like.
This widget found two variables in the Preferences file when it loaded.

From here, you can change the value of current variables, add new variables, or remove variables (NOTE:  Attempting to remove variables in the 2013 version will cause an error on the robot and the CRio will have to be rebooted; the variables will not actually be removed.)
To change a variable, double-click the value, enter a new one, and press enter or click off of the widget. This value will be used by the robot code UNTIL IT IS RESTARTED when the CRio restarts, it loads all of the default values. If you want to save your change as the default value, make sure you press the “save” button before you restart the CRio.

Important Stuff to Remember

  • Whenever you call any of the “add” methods, it will reset the default for that value; to stop this from happening, either take those methods out after the variables have been created, or, to be more elegant, use an “if” statement to check if the variable already exists using the method m_pref.containsKey([Key name]), and only adding the variable if it doesn’t already exist.
  • I recommend creating all of these variables in robotInit() because then they are all in the same place, and they will be created before any other code runs.
  • DO NOT put the method in any of the periodic methods, constantly saving to the CRio(20+ times per second) can damage the flash memory.
  • If you want to delete variables, since the widget doesn’t work, there are three ways that I know of that work.
  • Call m_pref.remove([Key name]) on every variable
  • Call m_pref.getKeys() and use a for loop to remove each key. (Essentially the same as above, but better if there is a large number of variables)
  • FTP the CRio using a command prompt and clear RobotPreferences.ini