Skip to content

Commit

Permalink
Implemented buildAutoChooser() in Python. (#639)
Browse files Browse the repository at this point in the history
* Implemented the method 'buildAutoChooser' for the Autobuilder class in the python lib.

* Added Python Sendablechooser Usage Docs.

* fixed a folder path bug

* Added tabs to autochooser PP docs
  • Loading branch information
jserra7d5 authored Mar 15, 2024
1 parent 5637a14 commit b05f00e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
32 changes: 30 additions & 2 deletions Writerside/topics/pplib-Build-an-Auto.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class RobotContainer:

> **Note**
>
> This feature is only available in the Java version of PathPlannerLib
> This feature is only available in the Java and Python versions of PathPlannerLib
>
{style="note"}

Expand All @@ -449,6 +449,9 @@ every auto in the project.
>
{style="warning"}

<tabs group="pplib-language">
<tab title="Java" group-key="java">

```Java
public class RobotContainer {
private final SendableChooser<Command> autoChooser;
Expand All @@ -471,4 +474,29 @@ public class RobotContainer {
}
```

</snippet>
</tab>
<tab title="Python" group-key="python">

```Python
from pathplannerlib.auto import AutoBuilder

class RobotContainer:

def __init__():

# Build an auto chooser. This will use Commands.none() as the default option.
self.autoChooser = AutoBuilder.buildAutoChooser()

# Another option that allows you to specify the default auto by its name
# self.autoChooser = AutoBuilder.buildAutoChooser("My Default Auto")

SmartDashboard.putData("Auto Chooser", self.autoChooser)

def getAutonomousCommand():
return self.autoChooser.getSelected()
```

</tab>
</tabs>

</snippet>
31 changes: 29 additions & 2 deletions pathplannerlib-python/pathplannerlib/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PathfindThenFollowPathLTV
from .geometry_util import flipFieldPose
import os
from wpilib import getDeployDirectory, reportError, reportWarning
from wpilib import getDeployDirectory, reportError, reportWarning, SendableChooser
import json
from commands2.command import Command
from commands2.subsystem import Subsystem
Expand Down Expand Up @@ -499,7 +499,34 @@ def buildAuto(auto_name: str) -> Command:
with open(filePath, 'r') as f:
auto_json = json.loads(f.read())
return AutoBuilder.getAutoCommandFromJson(auto_json)


@staticmethod
def buildAutoChooser(default_auto_name: str = "") -> SendableChooser:
"""
Create and populate a sendable chooser with all PathPlannerAutos in the project and the default auto name selected.
:param default_auto_name: the name of the default auto to be selected in the chooser
:return: a sendable chooser object populated with all of PathPlannerAutos in the project
"""
if not AutoBuilder.isConfigured():
raise RuntimeError('AutoBuilder was not configured before attempting to build an auto chooser')
auto_folder_path = os.path.join(getDeployDirectory(), 'pathplanner', 'autos')
auto_list = os.listdir(auto_folder_path)

chooser = SendableChooser()
default_auto_added = False

for auto in auto_list:
auto = auto.removesuffix(".auto")
if auto == default_auto_name:
default_auto_added = True
chooser.setDefaultOption(auto, AutoBuilder.buildAuto(auto))
else:
chooser.addOption(auto, AutoBuilder.buildAuto(auto))
if not default_auto_added:
chooser.setDefaultOption("None", cmd.none())
return chooser


class PathPlannerAuto(Command):
_autoCommand: Command
Expand Down

0 comments on commit b05f00e

Please sign in to comment.