From b05f00e874ca87e426b364d3534328ebd776cc25 Mon Sep 17 00:00:00 2001 From: Joe <73498493+jserra99@users.noreply.github.com> Date: Thu, 14 Mar 2024 20:16:31 -0700 Subject: [PATCH] Implemented buildAutoChooser() in Python. (#639) * 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 --- Writerside/topics/pplib-Build-an-Auto.md | 32 ++++++++++++++++++-- pathplannerlib-python/pathplannerlib/auto.py | 31 +++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Writerside/topics/pplib-Build-an-Auto.md b/Writerside/topics/pplib-Build-an-Auto.md index d979519e..0a53d817 100644 --- a/Writerside/topics/pplib-Build-an-Auto.md +++ b/Writerside/topics/pplib-Build-an-Auto.md @@ -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"} @@ -449,6 +449,9 @@ every auto in the project. > {style="warning"} + + + ```Java public class RobotContainer { private final SendableChooser autoChooser; @@ -471,4 +474,29 @@ public class RobotContainer { } ``` - \ No newline at end of file + + + +```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() +``` + + + + + diff --git a/pathplannerlib-python/pathplannerlib/auto.py b/pathplannerlib-python/pathplannerlib/auto.py index bc495d19..8013b5d6 100644 --- a/pathplannerlib-python/pathplannerlib/auto.py +++ b/pathplannerlib-python/pathplannerlib/auto.py @@ -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 @@ -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