Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify RLIs with :lineno-match: #2851

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions source/docs/contributing/frc-docs/style-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,17 @@ When possible, instead of using code blocks, an RLI should be used. This pulls
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/gettingstarted/Robot.java
:language: java
:lines: 19-25
:linenos:
:lineno-start: 19
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/GettingStarted/cpp/Robot.cpp
:language: c++
:lines: 50-60
:linenos:
:lineno-start: 50
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/c6d0540b01e138725fad7366ff4e317e9994b78b/GettingStarted/robot.py
:language: python
:linenos:
:lines: 12-29
:lineno-start: 12
:lineno-match:
```

Make sure to link to the raw version of the file on GitHub. There is a handy ``Raw`` button in the top right corner of the page.
Expand All @@ -132,8 +129,7 @@ To create code tabs in an article, you can use the ``.. tab-set-code::`` directi
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/gettingstarted/Robot.java
:language: java
:lines: 19-25
:linenos:
:lineno-start: 19
:lineno-match:

```c++
// Start the timer.
Expand Down
3 changes: 1 addition & 2 deletions source/docs/romi-robot/programming-romi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ To run a Romi program, first, ensure that your Romi is powered on. Next, connect
.. rli:: https://raw.githubusercontent.com/wpilibsuite/vscode-wpilib/v2025.1.1-beta-1/vscode-wpilib/resources/gradle/javaromi/build.gradle
:language: groovy
:lines: 44-47
:linenos:
:lineno-start: 44
:lineno-match:
:emphasize-lines: 2

Now to start your Romi robot code, open the WPILib Command Palette (type :kbd:`Ctrl+Shift+P`) and select "Simulate Robot Code", or press :kbd:`F5`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,14 @@ A more complete example of ``ProfiledPIDController`` usage is provided in the El
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/elevatorprofiledpid/Robot.java
:language: java
:lines: 5-
:linenos:
:lineno-start: 5
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/ElevatorProfiledPID/cpp/Robot.cpp
:language: c++
:lines: 5-
:linenos:
:lineno-start: 5
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/ElevatorProfiledPID/robot.py
:language: python
:lines: 8-
:linenos:
:lineno-start: 8
:lineno-match:
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,14 @@ A more complete example of ``TrapezoidProfile`` usage is provided in the Elevato
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/elevatortrapezoidprofile/Robot.java
:language: java
:lines: 5-
:linenos:
:lineno-start: 5
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/ElevatorTrapezoidProfile/cpp/Robot.cpp
:language: c++
:lines: 5-
:linenos:
:lineno-start: 5
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/ElevatorTrapezoidProfile/robot.py
:language: python
:lines: 8-
:linenos:
:lineno-start: 8
:lineno-match:
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ The ``LinearSystem`` class contains methods for easily creating state-space syst
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheelsysid/Robot.java
:language: java
:lines: 32-46
:linenos:
:lineno-start: 33
:lineno-match:


.. tab-item:: C++
Expand All @@ -78,28 +77,24 @@ The ``LinearSystem`` class contains methods for easily creating state-space syst
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheelSysId/cpp/Robot.cpp
:language: c++
:lines: 17
:linenos:
:lineno-start: 17
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheelSysId/cpp/Robot.cpp
:language: c++
:lines: 30-46
:linenos:
:lineno-start: 30
:lineno-match:

.. tab-item:: Python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheelSysId/robot.py
:language: python
:lines: 23-27
:linenos:
:lineno-start: 23
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheelSysId/robot.py
:language: python
:lines: 37-48
:linenos:
:lineno-start: 37
:lineno-match:


### Modeling Using Flywheel Moment of Inertia and Gearing
Expand All @@ -120,38 +115,33 @@ The ``LinearSystem`` class contains methods to easily create a model of a flywhe
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheel/Robot.java
:language: java
:lines: 33-46
:linenos:
:lineno-start: 33
:lineno-match:

.. tab-item:: C++
:sync: c++

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 17-17
:linenos:
:lineno-start: 17
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 17,31-46
:linenos:
:lineno-start: 31
:lines: 31-46
:lineno-match:

.. tab-item:: Python
:sync: python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 21-25
:linenos:
:lineno-start: 21
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 37-46
:linenos:
:lineno-start: 37
:lineno-match:

## Kalman Filters: Observing Flywheel State

Expand All @@ -174,32 +164,28 @@ Because the feedback controller computes error using the :term:`x-hat` estimated
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheel/Robot.java
:language: java
:lines: 48-58
:linenos:
:lineno-start: 48
:lineno-match:

.. tab-item:: C++
:sync: c++

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheelSysId/cpp/Robot.cpp
:language: c++
:lines: 13-13
:linenos:
:lineno-start: 13
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 48-53
:linenos:
:lineno-start: 48
:lineno-match:

.. tab-item:: Python
:sync: python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 48-54
:linenos:
:lineno-start: 48
:lineno-match:

Because Kalman filters use our state-space model in the :ref:`docs/software/advanced-controls/state-space/state-space-observers:Predict step`, it is important that our model is as accurate as possible. One way to verify this is to record a flywheel's input voltage and velocity over time, and replay this data by calling only ``predict`` on the Kalman filter. Then, the kV and kA gains (or moment of inertia and other constants) can be adjusted until the model closely matches the recorded data.

Expand All @@ -219,32 +205,28 @@ Much like ``SimpleMotorFeedforward`` can be used to generate feedforward voltage
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheel/Robot.java
:language: java
:lines: 59-70
:linenos:
:lineno-start: 59
:lineno-match:

.. tab-item:: C++
:sync: c++

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheelSysId/cpp/Robot.cpp
:language: c++
:lines: 11
:linenos:
:lineno-start: 11
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 55-75
:linenos:
:lineno-start: 54
:lineno-match:

.. tab-item:: Python
:sync: python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 56-66
:linenos:
:lineno-start: 56
:lineno-match:

## Bringing it All Together: LinearSystemLoop

Expand All @@ -258,32 +240,28 @@ LinearSystemLoop combines our system, controller, and observer that we created e
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheel/Robot.java
:language: java
:lines: 72-74
:linenos:
:lineno-start: 72
:lineno-match:

.. tab-item:: C++
:sync: c++

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheelSysId/cpp/Robot.cpp
:language: c++
:lines: 15-15
:linenos:
:lineno-start: 15
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 71-74
:linenos:
:lineno-start: 71
:lineno-match:

.. tab-item:: Python
:sync: python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 68-71
:linenos:
:lineno-start: 68
:lineno-match:

Once we have our ``LinearSystemLoop``, the only thing left to do is actually run it. To do that, we'll periodically update our Kalman filter with our new encoder velocity measurements and apply new voltage commands to it. To do that, we first set the :term:`reference`, then ``correct`` with the current flywheel speed, ``predict`` the Kalman filter into the next timestep, and apply the inputs generated using ``getU``.

Expand All @@ -295,32 +273,28 @@ Once we have our ``LinearSystemLoop``, the only thing left to do is actually run
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheel/Robot.java
:language: java
:lines: 94-119
:linenos:
:lineno-start: 94
:lineno-match:

.. tab-item:: C++
:sync: c++

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheelSysId/cpp/Robot.cpp
:language: c++
:lines: 5-17
:linenos:
:lineno-start: 5
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 92-114
:linenos:
:lineno-start: 92
:lineno-match:

.. tab-item:: Python
:sync: python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 87-109
:linenos:
:lineno-start: 87
:lineno-match:

## Angle Wrap with LQR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,26 @@ WPILib's Kalman Filter classes' constructors take a linear system, a vector of p
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/statespaceflywheel/Robot.java
:language: java
:lines: 48-57
:linenos:
:lineno-start: 48
:lineno-match:

.. tab-item:: C++

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 5-18
:linenos:
:lineno-start: 5
:lineno-match:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2025.1.1-beta-1/wpilibcExamples/src/main/cpp/examples/StateSpaceFlywheel/cpp/Robot.cpp
:language: c++
:lines: 48-53
:linenos:
:lineno-start: 48
:lineno-match:

.. tab-item:: Python

.. remoteliteralinclude:: https://raw.githubusercontent.com/robotpy/examples/d89b0587a1e1111239728140466c7dc4324d4005/StateSpaceFlywheel/robot.py
:language: python
:lines: 48-54
:linenos:
:lineno-start: 48
:lineno-match:

## Footnotes

Expand Down
Loading