Skip to content

Commit 3613421

Browse files
authored
Python for glass docs (#2487)
1 parent 22b0fd5 commit 3613421

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

source/docs/software/dashboards/glass/command-based-widgets.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ The :guilabel:`Command Selector` widget allows you to start and cancel a specif
2323
MyCommand command{...};
2424
frc::SmartDashboard::PutData("My Command", &command);
2525

26+
.. code-block:: python
27+
28+
from wpilib import SmartDashboard
29+
30+
command = MyCommand(...)
31+
SmartDashboard.putData("My Command", command)
32+
2633
.. note:: The ``MyCommand`` instance can also be sent using a lower-level NetworkTables API or using the :ref:`Shuffleboard API <docs/software/dashboards/shuffleboard/getting-started/shuffleboard-displaying-data:Displaying data from your robot>`. In this case, the ``SmartDashboard`` API was used, meaning that the :guilabel:`Command Selector` widget will appear under the ``SmartDashboard`` table name.
2734

2835
.. image:: images/command-selector.png

source/docs/software/dashboards/glass/field2d-widget.rst

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@ To send your robot's position (usually obtained by :ref:`odometry <docs/software
1313
1414
private final Field2d m_field = new Field2d();
1515
16-
public Drivetrain() {
17-
...
18-
SmartDashboard.putData("Field", m_field);
19-
}
16+
// Do this in either robot or subsystem init
17+
SmartDashboard.putData("Field", m_field);
2018
21-
...
22-
23-
public void periodic() {
24-
...
25-
m_field.setRobotPose(m_odometry.getPoseMeters());
26-
}
19+
// Do this in either robot periodic or subsystem periodic
20+
m_field.setRobotPose(m_odometry.getPoseMeters());
2721
2822
.. code-block:: c++
2923

@@ -32,17 +26,23 @@ To send your robot's position (usually obtained by :ref:`odometry <docs/software
3226

3327
frc::Field2d m_field;
3428

35-
Drivetrain() {
36-
...
37-
frc::SmartDashboard::PutData("Field", &m_field);
38-
}
29+
// Do this in either robot or subsystem init
30+
frc::SmartDashboard::PutData("Field", &m_field);
31+
32+
// Do this in either robot periodic or subsystem periodic
33+
m_field.SetRobotPose(m_odometry.GetPose());
34+
35+
.. code-block:: python
3936
40-
...
37+
from wpilib import SmartDashboard, Field2d
4138
42-
void Periodic() {
43-
...
44-
m_field.SetRobotPose(m_odometry.GetPose());
45-
}
39+
self.field = Field2d()
40+
41+
# Do this in either robot or subsystem init
42+
SmartDashboard.putData("Field", self.field)
43+
44+
# Do this in either robot periodic or subsystem periodic
45+
self.field.setRobotPose(self.odometry.getPose())
4646
4747
.. note:: The ``Field2d`` instance can also be sent using a lower-level NetworkTables API or using the :ref:`Shuffleboard API <docs/software/dashboards/shuffleboard/getting-started/shuffleboard-displaying-data:Displaying data from your robot>`. In this case, the ``SmartDashboard`` API was used, meaning that the :guilabel:`Field2d` widget will appear under the ``SmartDashboard`` table name.
4848

@@ -65,6 +65,10 @@ Visualizing your trajectory is a great debugging step for verifying that your tr
6565
:linenos:
6666
:lineno-start: 18
6767

68+
.. rli:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/RamseteController/robot.py
69+
:language: python
70+
:lines: 19,26-39,46-53
71+
6872
Viewing Trajectories with Glass
6973
-------------------------------
7074

@@ -73,7 +77,7 @@ The sent trajectory can be viewed with :ref:`Glass <docs/software/dashboards/gla
7377
.. image:: images/sent-trajectory.png
7478
:alt: Picture containing Field2d and the generated trajectory
7579

76-
.. note:: The above example which uses the `RamseteController (Java) <https://github.com/wpilibsuite/allwpilib/blob/a610379965680a8f9214d5f0db3a8e1bc20d4712/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/ramsetecontroller/Robot.java>`__/`RamseteController (C++) <https://github.com/wpilibsuite/allwpilib/blob/a610379965680a8f9214d5f0db3a8e1bc20d4712/wpilibcExamples/src/main/cpp/examples/RamseteController/cpp/Robot.cpp>`__ will not show the sent trajectory until autonomous is enabled at least once.
80+
.. note:: The above example which uses the RamseteController (`Java <https://github.com/wpilibsuite/allwpilib/blob/a610379965680a8f9214d5f0db3a8e1bc20d4712/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/ramsetecontroller/Robot.java>`__ / `C++ <https://github.com/wpilibsuite/allwpilib/blob/a610379965680a8f9214d5f0db3a8e1bc20d4712/wpilibcExamples/src/main/cpp/examples/RamseteController/cpp/Robot.cpp>`__ / `Python <https://github.com/robotpy/examples/tree/2024.0.0b4/RamseteController>`__) will not show the sent trajectory until autonomous is enabled at least once.
7781

7882
Viewing the Robot Pose in Glass
7983
-------------------------------

source/docs/software/dashboards/glass/mech2d-widget.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ In the examples below, an elevator is drawn, with a rotational wrist on top of t
2424
:linenos:
2525
:lineno-start: 59
2626

27+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/Mechanism2d/robot.py
28+
:language: python
29+
:lines: 32-35
30+
:linenos:
31+
:lineno-start: 32
32+
2733

2834
Each ``MechanismLigament2d`` object represents a stage of the mechanism. It has a three required parameters, a name, an initial length to draw (relative to the size of the ``Mechanism2d`` object), and an initial angle to draw the ligament in degrees. Ligament angles are relative to the parent ligament, and follow math notation - the same as :ref:`Rotation2d <docs/software/advanced-controls/geometry/pose:Rotation>` (counterclockwise-positive). A ligament based on the root with an angle of zero will point right. Two optional parameters let you change the width (also relative to the size of the Mechanism2d object) and the color. Call ``append()``/``Append()`` on a root node or ligament node to add another node to the figure. In Java, pass a constructed ``MechanismLigament2d`` object to add it. In C++, pass the construction parameters in order to construct and add a ligament.
2935

@@ -42,6 +48,12 @@ Each ``MechanismLigament2d`` object represents a stage of the mechanism. It has
4248
:linenos:
4349
:lineno-start: 63
4450

51+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/Mechanism2d/robot.py
52+
:language: python
53+
:lines: 37-44
54+
:linenos:
55+
:lineno-start: 37
56+
4557
Then, publish the ``Mechanism2d`` object to NetworkTables:
4658

4759
.. tab-set-code::
@@ -58,6 +70,12 @@ Then, publish the ``Mechanism2d`` object to NetworkTables:
5870
:linenos:
5971
:lineno-start: 36
6072

73+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/Mechanism2d/robot.py
74+
:language: python
75+
:lines: 46-47
76+
:linenos:
77+
:lineno-start: 46
78+
6179
.. note:: The ``Mechanism2d`` instance can also be sent using a lower-level NetworkTables API or using the :ref:`Shuffleboard API <docs/software/dashboards/shuffleboard/getting-started/shuffleboard-displaying-data:Displaying data from your robot>`. In this case, the ``SmartDashboard`` API was used, meaning that the :guilabel:`Mechanism2d` widget will appear under the ``SmartDashboard`` table name.
6280

6381
To manipulate a ligament angle or length, call ``setLength()`` or ``setAngle()`` on the ``MechanismLigament2d`` object. When manipulating ligament length based off of sensor measurements, make sure to add the minimum length to prevent 0-length (and therefore invisible) ligaments.
@@ -76,6 +94,12 @@ To manipulate a ligament angle or length, call ``setLength()`` or ``setAngle()``
7694
:linenos:
7795
:lineno-start: 40
7896

97+
.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/2024.0.0b4/Mechanism2d/robot.py
98+
:language: python
99+
:lines: 49-54
100+
:linenos:
101+
:lineno-start: 49
102+
79103
Viewing the Mechanism2d in Glass
80104
--------------------------------
81105

@@ -99,4 +123,4 @@ Viewing the Mechanism2d in AdvantageScope
99123
Next Steps
100124
----------
101125

102-
As mentioned above, the Mechanism2d visualization can be combined with :doc:`Physics Simulation </docs/software/wpilib-tools/robot-simulation/physics-sim>` to help you program mechanisms before your robot is built. The ArmSimulation (`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>`__) and ElevatorSimulation (`Java <https://github.com/wpilibsuite/allwpilib/blob/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/elevatorsimulation/Robot.java>`__ / `C++ <https://github.com/wpilibsuite/allwpilib/blob/main/wpilibcExamples/src/main/cpp/examples/ElevatorSimulation/cpp/Robot.cpp>`__) examples combine physics simulation and Mechanism2d visualization so that you can practice programming a single jointed arm and elevator without a robot.
126+
As mentioned above, the Mechanism2d visualization can be combined with :doc:`Physics Simulation </docs/software/wpilib-tools/robot-simulation/physics-sim>` to help you program mechanisms before your robot is built. The ArmSimulation (`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>`__ / `Python <https://github.com/robotpy/examples/blob/2024.0.0b4/ArmSimulation/robot.py>`__) and ElevatorSimulation (`Java <https://github.com/wpilibsuite/allwpilib/blob/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/elevatorsimulation/Robot.java>`__ / `C++ <https://github.com/wpilibsuite/allwpilib/blob/main/wpilibcExamples/src/main/cpp/examples/ElevatorSimulation/cpp/Robot.cpp>`__ / `Python <https://github.com/robotpy/examples/blob/2024.0.0b4/ElevatorSimulation/robot.py>`__) examples combine physics simulation and Mechanism2d visualization so that you can practice programming a single jointed arm and elevator without a robot.

source/docs/software/dashboards/glass/widgets.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ The :guilabel:`Sendable Chooser` widget represents a ``SendableChooser`` instanc
4040

4141
frc::SmartDashboard::PutData("Auto Selector", &m_selector);
4242

43+
.. code-block:: python
44+
45+
from wpilib import SmartDashboard
46+
47+
SmartDashboard.putData("Auto Selector", selector)
48+
4349
.. note:: For more information on creating a ``SendableChooser``, please see :ref:`this document <docs/software/dashboards/smartdashboard/choosing-an-autonomous-program-from-smartdashboard:Setting up SendableChooser>`.
4450

4551
The :guilabel:`Sendable Chooser` widget will appear in the :guilabel:`NetworkTables` menu and underneath the main table name that the instance was sent over. From the example above, the main table name would be :guilabel:`SmartDashboard`.
@@ -60,6 +66,12 @@ The :guilabel:`PID Controller` widget allows you to quickly tune PID values for
6066

6167
frc::SmartDashboard::PutData("Elevator PID Controller", &m_elevatorPIDController);
6268

69+
.. code-block:: python
70+
71+
from wpilib import SmartDashboard
72+
73+
SmartDashboard.putData("Elevator PID Controller", elevatorPIDController)
74+
6375
This allows you to quickly tune P, I, and D values for various setpoints.
6476

6577
.. image:: images/pid.png

0 commit comments

Comments
 (0)