Skip to content

Commit 8ec3858

Browse files
authored
Rewrite Preferences article (#1716)
Move to basic programming from Smartdashboard Show more complete code examples using RLIs Show Shuffleboard usage Fixes #1628
1 parent 0633fd1 commit 8ec3858

File tree

11 files changed

+105
-56
lines changed

11 files changed

+105
-56
lines changed
48.3 KB
Loading
15.4 KB
Loading
29.1 KB
Loading
11.2 KB
Loading

source/docs/software/basic-programming/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ Basic Programming
77
git-getting-started.rst
88
cpp-units
99
joystick
10+
robot-preferences
1011
using-test-mode
1112
reading-stacktraces
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.

source/docs/software/dashboards/smartdashboard/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ SmartDashboard
1010
changing-display-widget-type
1111
choosing-an-autonomous-program-from-smartdashboard
1212
displaying-status-of-commands-and-subsystems
13-
setting-robot-preferences-from-smartdashboard
1413
verifying-smartdashboard-is-working
1514
smartdashboard-namespace
1615
test-mode-and-live-window/index

source/docs/software/dashboards/smartdashboard/setting-robot-preferences-from-smartdashboard.rst

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)