Skip to content

Commit 7717de9

Browse files
authored
Add Python to SmartDashboard Articles (#2486)
1 parent 3613421 commit 7717de9

File tree

5 files changed

+233
-72
lines changed

5 files changed

+233
-72
lines changed

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

Lines changed: 104 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,27 @@ In ``Robot.java`` / ``Robot.h``, create a variable to hold a reference to a ``Se
2121
:sync: Java
2222

2323
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timed/Robot.java
24-
:language: java
24+
:language: Java
2525
:lines: 18-21
2626

2727
.. tab-item:: C++
2828
:sync: C++
2929

3030
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibcExamples/src/main/cpp/templates/timed/include/Robot.h
31-
:language: c++
31+
:language: C++
3232
:lines: 28-31
3333

34+
.. tab-item:: Python
35+
:sync: Python
36+
37+
.. code-block:: python
38+
39+
import wpilib
40+
41+
self.defaultAuto = "Default"
42+
self.customAuto = "My Auto";
43+
self.chooser = wpilib.SendableChooser()
44+
3445
Setting Up Options
3546
^^^^^^^^^^^^^^^^^^
3647

@@ -52,6 +63,17 @@ The chooser allows you to pick from a list of defined elements, in this case the
5263
:language: c++
5364
:lines: 10-14
5465

66+
.. tab-item:: Python
67+
:sync: Python
68+
69+
.. code-block:: python
70+
71+
from wpilib import SmartDashboard
72+
73+
self.chooser.setDefaultOption("Default Auto", self.defaultAuto)
74+
self.chooser.addOption("My Auto", self.customAuto)
75+
SmartDashboard.putData("Auto choices", self.chooser)
76+
5577
Running Autonomous Code
5678
^^^^^^^^^^^^^^^^^^^^^^^
5779

@@ -63,20 +85,36 @@ Now, in ``autonomousInit`` and ``autonomousPeriodic``, you can use the ``m_autoS
6385
:sync: Java
6486

6587
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timed/Robot.java
66-
:language: java
88+
:language: Java
6789
:lines: 54-56, 58-73
6890

6991
.. tab-item:: C++
7092
:sync: C++
7193

7294
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibcExamples/src/main/cpp/templates/timed/cpp/Robot.cpp
73-
:language: c++
95+
:language: C++
7496
:lines: 37-38, 41-57
7597

98+
.. tab-item:: Python
99+
:sync: Python
100+
101+
.. code-block:: python
102+
103+
def autonomousInit(self):
104+
self.autoSelected = self.chooser.getSelected()
105+
print("Auto selected: " + self.autoSelected)
106+
107+
def autonomousPeriodic(self):
108+
match self.autoSelected:
109+
case self.customAuto:
110+
# Put custom auto code here
111+
case _:
112+
# Put default auto code here
113+
76114
Command-Based
77115
-------------
78116

79-
.. 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>`__):
117+
.. 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>`__, `Python <https://github.com/robotpy/examples/tree/main/HatchbotTraditional>`__):
80118

81119
Creating the SendableChooser Object
82120
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -89,23 +127,30 @@ In ``RobotContainer``, create a variable to hold a reference to a ``SendableChoo
89127
:sync: Java
90128

91129
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hatchbottraditional/RobotContainer.java
92-
:language: java
130+
:language: Java
93131
:lines: 40-49
94132

95133
.. tab-item:: C++ (using raw pointers)
96134
:sync: C++ (using raw pointers)
97135

98136
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional/include/RobotContainer.h
99-
:language: c++
137+
:language: C++
100138
:lines: 38-44
101139

102140
.. tab-item:: C++ (using ``CommandPtr``)
103141
:sync: C++ (using ``CommandPtr``)
104142

105143
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.1.1-beta-4/wpilibcExamples/src/main/cpp/examples/HatchbotInlined/include/RobotContainer.h
106-
:language: c++
144+
:language: C++
107145
:lines: 45-50
108146

147+
.. tab-item:: Python
148+
:sync: Python
149+
150+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/HatchbotTraditional/robotcontainer.py
151+
:language: Python
152+
:lines: 45-54
153+
109154
Setting up SendableChooser
110155
^^^^^^^^^^^^^^^^^^^^^^^^^^
111156

@@ -136,19 +181,42 @@ In ``RobotContainer``, create a ``SendableChooser`` object and add instances of
136181
:language: c++
137182
:lines: 12-15
138183

184+
.. tab-item:: Python
185+
:sync: Python
186+
187+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/HatchbotTraditional/robotcontainer.py
188+
:language: Python
189+
:lines: 56-58
190+
139191
Then, publish the chooser to the dashboard:
140192

141-
.. tab-set-code::
193+
.. tab-set::
194+
195+
.. tab-item:: Java
196+
:sync: Java
197+
198+
.. code-block:: java
199+
200+
// Put the chooser on the dashboard
201+
SmartDashboard.putData(m_chooser);
202+
203+
.. tab-item:: C++
204+
:sync: C++
205+
206+
.. code-block:: c++
142207

143-
.. code-block:: java
208+
// Put the chooser on the dashboard
209+
frc::SmartDashboard::PutData(&m_chooser);
144210

145-
// Put the chooser on the dashboard
146-
SmartDashboard.putData(m_chooser);
211+
.. tab-item:: Python
212+
:sync: Python
147213

148-
.. code-block:: c++
214+
.. code-block:: python
149215
150-
// Put the chooser on the dashboard
151-
frc::SmartDashboard::PutData(&m_chooser);
216+
from wpilib import SmartDashboard
217+
218+
# Put the chooser on the dashboard
219+
SmartDashboard.putData(chooser)
152220
153221
Starting an Autonomous Command
154222
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -179,10 +247,21 @@ In ``Robot.java``, when the autonomous period starts, the ``SendableChooser`` ob
179247
:language: c++
180248
:lines: 46-52
181249

250+
.. tab-item:: Python
251+
:sync: Python
252+
253+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/HatchbotTraditional/robotcontainer.py
254+
:language: Python
255+
:lines: 93-94
256+
257+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/HatchbotTraditional/robot.py
258+
:language: Python
259+
:lines: 41-46
260+
182261
Running the Scheduler during Autonomous
183262
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
184263

185-
In ``Robot.java``, this will run the scheduler every driver station update period (about every 20ms) and cause the selected autonomous command to run.
264+
In ``Robot.java``, this will run the scheduler every driver station update period (about every 20ms) and cause the selected autonomous command to run. In Python the scheduler runs automatically when ``TimedCommandRobot`` is used.
186265

187266
.. note:: Running the scheduler can occur in the ``autonomousPeriodic()`` function or ``robotPeriodic()``, both will function similarly in autonomous mode.
188267

@@ -231,6 +310,15 @@ In ``Robot.java``, when the teleop period begins, the autonomous command will be
231310
:linenos:
232311
:lineno-start: 56
233312

313+
.. tab-item:: Python
314+
:sync: Python
315+
316+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/HatchbotTraditional/robot.py
317+
:language: Python
318+
:lines: 51-57
319+
:linenos:
320+
:lineno-start: 51
321+
234322
SmartDashboard Display
235323
^^^^^^^^^^^^^^^^^^^^^^
236324

source/docs/software/dashboards/smartdashboard/displaying-status-of-commands-and-subsystems.rst

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ Displaying the Scheduler Status
2323

2424
.. tab-set-code::
2525

26-
.. code-block:: java
26+
.. code-block:: java
2727
28-
SmartDashboard.putData(CommandScheduler.getInstance());
28+
SmartDashboard.putData(CommandScheduler.getInstance());
2929
30-
.. code-block:: c++
30+
.. code-block:: c++
3131

32-
frc::SmartDashboard::PutData(frc2::CommandScheduler::GetInstance());
32+
frc::SmartDashboard::PutData(frc2::CommandScheduler::GetInstance());
33+
34+
.. code-block:: python
35+
36+
from wpilib import SmartDashboard
37+
from commands2 import CommandScheduler
38+
39+
SmartDashboard.putData(CommandScheduler.getInstance())
3340
3441
You can display the status of the Scheduler (the code that schedules your commands to run). This is easily done by adding a single line to the ``RobotInit`` method in your RobotProgram as shown here. In this example the Scheduler instance is written using the ``putData`` method to SmartDashboard. This line of code produces the display in the previous image.
3542

@@ -43,13 +50,19 @@ Displaying Subsystem Status
4350

4451
.. tab-set-code::
4552

46-
.. code-block:: java
53+
.. code-block:: java
54+
55+
SmartDashboard.putData(exampleSubsystem);
56+
57+
.. code-block:: c++
4758

48-
SmartDashboard.putData(exampleSubsystem);
59+
frc::SmartDashboard::PutData(&exampleSubsystem);
4960

50-
.. code-block:: c++
61+
.. code-block:: python
5162
52-
frc::SmartDashboard::PutData(&exampleSubsystem);
63+
from wpilib import SmartDashboard
64+
65+
SmartDashboard.putData(exampleSubsystem)
5366
5467
In this example we are writing the command instance, ``exampleSubsystem`` and instance of the ``ExampleSubsystem`` class to the SmartDashboard. This causes the display shown in the previous image. The text field will either contain a few dashes, ``---`` indicating that no command is current using this subsystem, or the name of the command currently using this subsystem.
5568

@@ -63,13 +76,19 @@ Activating Commands with a Button
6376

6477
.. tab-set-code::
6578

66-
.. code-block:: java
79+
.. code-block:: java
80+
81+
SmartDashboard.putData("Autonomous Command", exampleCommand);
82+
83+
.. code-block:: c++
84+
85+
frc::SmartDashboard::PutData("Autonomous Command", &exampleCommand);
6786

68-
SmartDashboard.putData("Autonomous Command", exampleCommand);
87+
.. code-block:: python
6988
70-
.. code-block:: c++
89+
from wpilib import SmartDashboard
7190
72-
frc::SmartDashboard::PutData("Autonomous Command", &exampleCommand);
91+
SmartDashboard.putData("Autonomous Command", exampleCommand)
7392
7493
This is the code required to create a button for the command on SmartDashboard. Pressing the button will schedule the command. While the command is running, the button label changes from ``start`` to ``cancel`` and pressing the button will cancel the command.
7594

source/docs/software/dashboards/smartdashboard/test-mode-and-live-window/displaying-LiveWindow-values.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ For each sensor or actuator that is created, set the subsystem name and display
3232
frc::Victor wrist{2};
3333
SendableRegistry::SetName(wrist, "Arm", "Wrist");
3434

35+
.. code-block:: python
36+
37+
from wpilib import Jaguar, Ultrasonic, Victor
38+
from wpiutil import SendableRegistry
39+
40+
ultrasonic = Ultrasonic(1, 2)
41+
SendableRegistry.setName(ultrasonic, "Arm", "Ultrasonic")
42+
43+
elbow = Jaguar(1)
44+
SendableRegistry.setName(elbow, "Arm", "Elbow")
45+
46+
wrist = Victor(2)
47+
SendableRegistry.setName(wrist, "Arm", "Wrist")
48+
3549
If your objects are in a ``Subsystem``, this can be simplified using the addChild method of ``SubsystemBase``
3650

3751
.. tab-set-code::
@@ -58,6 +72,20 @@ If your objects are in a ``Subsystem``, this can be simplified using the addChil
5872
frc::Victor wrist{2};
5973
AddChild("Wrist", wrist);
6074

75+
.. code-block:: python
76+
77+
from wpilib import Jaguar, Ultrasonic, Victor
78+
from commands2 import SubsystemBase
79+
80+
ultrasonic = Ultrasonic(1, 2)
81+
SubsystemBase.addChild("Ultrasonic", ultrasonic)
82+
83+
elbow = Jaguar(1)
84+
SubsystemBase.addChild("Elbow", elbow)
85+
86+
wrist = Victor(2)
87+
SubsystemBase.addChild("Wrist", wrist)
88+
6189
Viewing the Display in SmartDashboard
6290
-----------------------------------------
6391

0 commit comments

Comments
 (0)