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.
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.
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
Nordic Storm is FIRST Robotics Competition (FRC) team 3018 from the St. Peter, Mankato, and Lake Crystal area.
We can be reached at firstname.lastname@example.org
Gustavus Adolphus College, Beck Academic Hall, Room 019
800 West College Avenue,
St. Peter, MN 56082 Map