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

Implemented buildAutoChooser() in Python. #639

Merged
merged 5 commits into from
Mar 15, 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
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>

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is a stray closing tag

Suggested change
</snippet>

Copy link
Contributor Author

@jserra7d5 jserra7d5 Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it? That was there before, I simply moved it down. The md originally opened with <snippet id="build-an-auto"> and ended with </snippet> immediately after the java example code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right. Forgot that file is different

</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
Loading