Skip to content

Commit b72bddf

Browse files
authored
TimedRobot SendableChooser Tutorial (#1802)
* Add TimedRobot explanation for autonomous choosers Resolves #1736 by creating separate sections for TimedRobot and Command-Based Robots. * Fix duplicate titles
1 parent 93e7151 commit b72bddf

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

source/docs/software/dashboards/smartdashboard/choosing-an-autonomous-program-from-smartdashboard.rst

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,78 @@ Choosing an Autonomous Program
33

44
Often teams have more than one autonomous program, either for competitive reasons or for testing new software. Programs often vary by adding things like time delays, different strategies, etc. The methods to choose the strategy to run usually involves switches, joystick buttons, knobs or other hardware based inputs.
55

6-
With the SmartDashboard you can simply display a widget on the screen to choose the autonomous program that you would like to run. And with command based programs, that program is encapsulated in one of several commands. This article shows how to select an autonomous program with only a few lines of code and a nice looking user interface.
6+
With the SmartDashboard you can simply display a widget on the screen to choose the autonomous program that you would like to run. And with command based programs, that program is encapsulated in one of several commands. This article shows how to select an autonomous program with only a few lines of code and a nice looking user interface, with examples for both TimedRobot and Command-Based Robots.
7+
8+
TimedRobot
9+
----------
10+
11+
.. note:: The code snippets shown below are part of the TimedRobot template (`Java <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timed>`__, `C++ <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibcExamples/src/main/cpp/templates/timed>`__):
12+
13+
Creating SendableChooser Object
14+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
In ``Robot.java`` / ``Robot.h``, create a variable to hold a reference to a ``SendableChooser`` object. Two or more auto modes can be added by creating strings to send to the chooser. Using the ``SendableChooser``, one can choose between them. In this example, ``Default`` and ``My Auto`` are shown as options. You will also need a variable to store which auto has been chosen, ``m_autoSelected``.
17+
18+
.. tabs::
19+
20+
.. group-tab:: Java
21+
22+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timed/Robot.java
23+
:language: java
24+
:lines: 18-21
25+
26+
.. group-tab:: C++
27+
28+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/templates/timed/include/Robot.h
29+
:language: c++
30+
:lines: 28-31
31+
32+
Setting Up Options
33+
^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
35+
The chooser allows you to pick from a list of defined elements, in this case the strings we defined above. In ``robotInit``, add your options created as strings above using ``setDefaultOption`` or ``addOption``. ``setDefaultOption`` will be the one selected by default when the dashboard starts. The ``putData`` function will push it to the dashboard on your driver station computer.
36+
37+
.. tabs::
38+
39+
.. group-tab:: Java
40+
41+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timed/Robot.java
42+
:language: java
43+
:lines: 28-32
44+
45+
.. group-tab:: C++
46+
47+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/templates/timed/cpp/Robot.cpp
48+
:language: c++
49+
:lines: 11-15
50+
51+
Running Autonomous Code
52+
^^^^^^^^^^^^^^^^^^^^^^^
53+
54+
Now, in ``autonomousInit`` and ``autonomousPeriodic``, you can use the ``m_autoSelected`` variable to read which option was chosen, and change what happens during the autonomous period.
55+
56+
.. tabs::
57+
58+
.. group-tab:: Java
59+
60+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timed/Robot.java
61+
:language: java
62+
:lines: 54-73
63+
64+
.. group-tab:: C++
65+
66+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/templates/timed/cpp/Robot.cpp
67+
:language: c++
68+
:lines: 38-57
69+
70+
71+
Command-Based
72+
-------------
773

874
.. note:: The code snippets shown below are part of the HatchbotTraditional example project (`Java <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hatchbottraditional>`__, `C++ <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional>`__):
975

1076
Creating the SendableChooser Object
11-
-----------------------------------
77+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1278

1379
In ``RobotContainer``, create a variable to hold a reference to a ``SendableChooser`` object. Two or more commands can be created and stored in new variables. Using the ``SendableChooser``, one can choose between them. In this example, ``SimpleAuto`` and ``ComplexAuto`` are shown as options.
1480

@@ -20,14 +86,14 @@ In ``RobotContainer``, create a variable to hold a reference to a ``SendableChoo
2086
:language: java
2187
:lines: 37-47
2288

23-
.. group-tab:: C++ (Header)
89+
.. group-tab:: C++
2490

2591
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2022.4.1/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional/include/RobotContainer.h
2692
:language: c++
2793
:lines: 38-44
2894

2995
Setting up SendableChooser
30-
--------------------------
96+
^^^^^^^^^^^^^^^^^^^^^^^^^^
3197

3298
Imagine that you have two autonomous programs to choose between and they are encapsulated in commands ``SimpleAuto`` and ``ComplexAuto``. To choose between them:
3399

@@ -54,7 +120,7 @@ In ``RobotContainer``, create a ``SendableChooser`` object and add instances of
54120
frc::SmartDashboard::PutData(&m_chooser);
55121

56122
Starting an Autonomous Command
57-
------------------------------
123+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58124

59125
In ``Robot.java``, when the autonomous period starts, the ``SendableChooser`` object is polled to get the selected command and that command must be scheduled.
60126

@@ -81,7 +147,7 @@ In ``Robot.java``, when the autonomous period starts, the ``SendableChooser`` ob
81147
:lines: 37-44
82148

83149
Running the Scheduler during Autonomous
84-
---------------------------------------
150+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85151

86152
In ``Robot.java``, this will run the scheduler every driver station update period (about every 20ms) and cause the selected autonomous command to run.
87153

@@ -106,7 +172,7 @@ In ``Robot.java``, this will run the scheduler every driver station update perio
106172
:lineno-start: 20
107173

108174
Canceling the Autonomous Command
109-
---------------------------------
175+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110176

111177
In ``Robot.java``, when the teleop period begins, the autonomous command will be canceled.
112178

@@ -127,7 +193,7 @@ In ``Robot.java``, when the teleop period begins, the autonomous command will be
127193
:lines: 47-56
128194

129195
SmartDashboard Display
130-
----------------------
196+
^^^^^^^^^^^^^^^^^^^^^^
131197

132198
.. image:: images/choosing-an-autonomous-program-from-smartdashboard/smartdashboard-display.png
133199
:alt: SendableChooser shows two selectable autos: Simple Auto and Complex Auto.

0 commit comments

Comments
 (0)