|
| 1 | +Setting Robot Preferences |
| 2 | +========================= |
| 3 | + |
| 4 | +The Robot Preferences (`Java <https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/Preferences.html>`__, `C++ <https://first.wpi.edu/wpilib/allwpilib/docs/release/cpp/classfrc_1_1_preferences.html>`__) class is used to store values in the flash memory on the roboRIO. The values might be for remembering preferences on the robot such as calibration settings for potentiometers, PID values, setpoints, etc. that you would like to change without having to rebuild the program. The values can be viewed on SmartDashboard or Shuffleboard and read and written by the robot program. |
| 5 | + |
| 6 | +This example shows how to utilize Preferences to change the setpoint of a PID controller and the P constant. The code examples are adapted from the Arm Simulation example (`Java <https://github.com/wpilibsuite/allwpilib/blob/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/armsimulation/Robot.java>`__, `C++ <https://github.com/wpilibsuite/allwpilib/blob/main/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/Robot.cpp>`__). You can run the Arm Simulation example in the Robot Simulator to see how to use the preference class and interact with it using the dashboards without needing a robot. |
| 7 | + |
| 8 | + |
| 9 | +Initializing Preferences |
| 10 | +------------------------ |
| 11 | + |
| 12 | +.. tabs:: |
| 13 | + |
| 14 | + .. group-tab:: Java |
| 15 | + |
| 16 | + .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/armsimulation/Robot.java |
| 17 | + :language: java |
| 18 | + :lines: 35-42, 90-91, 98-105 |
| 19 | + |
| 20 | + .. group-tab:: C++ |
| 21 | + |
| 22 | + .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/Robot.cpp |
| 23 | + :language: cpp |
| 24 | + :lines: 37-44, 84-85, 91-99 |
| 25 | + |
| 26 | +Preferences are stored using a name, the key. It's helpful to store the key in a constant, like ``kArmPositionKey`` and ``kArmPKey`` in the code above to avoid typing it multiple times and avoid typos. We also declare variables, ``kArmKp`` and ``armPositionDeg`` to hold the data retrieved from preferences. |
| 27 | + |
| 28 | +In ``robotInit``, each key is checked to see if it already exists in the Preferences database. The ``containsKey`` method takes one parameter, the key to check if data for that key already exists in the preferences database. If it doesn't exist, a default value is written. The ``setDouble`` method takes two parameters, the key to write and the data to write. There are similar methods for other data types like booleans, ints, and strings. |
| 29 | + |
| 30 | +If using the Command Framework, this type of code could be placed in the constructor of a Subsystem or Command. |
| 31 | + |
| 32 | +Reading Preferences |
| 33 | +------------------- |
| 34 | + |
| 35 | +.. tabs:: |
| 36 | + |
| 37 | + .. group-tab:: Java |
| 38 | + |
| 39 | + .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/armsimulation/Robot.java |
| 40 | + :language: java |
| 41 | + :lines: 126-134 |
| 42 | + |
| 43 | + .. group-tab:: C++ |
| 44 | + |
| 45 | + .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/Robot.cpp |
| 46 | + :language: cpp |
| 47 | + :lines: 121-129 |
| 48 | + |
| 49 | +Reading a preference is easy. The ``GetDouble`` method takes two parameters, the key to read, and a default value to use in case the preference doesn't exist. There are similar methods for other data types like booleans, ints, and strings. |
| 50 | + |
| 51 | +Depending on the data that is stored in preferences, you can use it when you read it, such as the proportional constant above. Or you can store it in a variable and use it later, such as the setpoint, which is used in ``telopPeriodic`` below. |
| 52 | + |
| 53 | +.. tabs:: |
| 54 | + |
| 55 | + .. group-tab:: Java |
| 56 | + |
| 57 | + .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/armsimulation/Robot.java |
| 58 | + :language: java |
| 59 | + :lines: 136-147 |
| 60 | + |
| 61 | + .. group-tab:: C++ |
| 62 | + |
| 63 | + .. rli:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.3.1/wpilibcExamples/src/main/cpp/examples/ArmSimulation/cpp/Robot.cpp |
| 64 | + :language: cpp |
| 65 | + :lines: 131-142 |
| 66 | + |
| 67 | +Using Preferences in SmartDashboard |
| 68 | +----------------------------------- |
| 69 | + |
| 70 | +Displaying Preferences in SmartDashboard |
| 71 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 | + |
| 73 | +.. image:: images/robot-preferences/preferences-widget-smartdashboard.png |
| 74 | + :alt: Adding preferences from the Smartdashboard menu |
| 75 | + |
| 76 | +In the SmartDashboard, the Preferences display can be added to the display by selecting :guilabel:`View` then :guilabel:`Add...` then :guilabel:`Robot Preferences`. This reveals the contents of the preferences file stored in the roboRIO flash memory. |
| 77 | + |
| 78 | +Editing Preferences in SmartDashboard |
| 79 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 80 | + |
| 81 | +.. image:: images/robot-preferences/view-edit-preferences-values-smartdashboard.png |
| 82 | + :alt: Editing the robot preferences via the SmartDashboard widget. |
| 83 | + |
| 84 | +The values are shown here with the default values from the code. If the values need to be adjusted they can be edited here and saved. |
| 85 | + |
| 86 | +Using Preferences in Shuffleboard |
| 87 | +--------------------------------- |
| 88 | + |
| 89 | +Displaying Preferences in Shuffleboard |
| 90 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 91 | + |
| 92 | +.. image:: images/robot-preferences/preferences-widget-shuffleboard.png |
| 93 | + :alt: Adding preferences from the sources window in Shuffleboard |
| 94 | + |
| 95 | +In Shuffleboard, the Preferences display can be added to the display by dragging the preferences field from the sources window. This reveals the contents of the preferences file stored in the roboRIO flash memory. |
| 96 | + |
| 97 | +Editing Preferences in Shuffleboard |
| 98 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 99 | + |
| 100 | +.. image:: images/robot-preferences/view-edit-preferences-values-shuffleboard.png |
| 101 | + :alt: Editing the robot preferences via the Shuffleboard widget. |
| 102 | + |
| 103 | +The values are shown here with the default values from the code. If the values need to be adjusted they can be edited here. |
0 commit comments