Skip to content

Commit

Permalink
Style changes to buildAutoChooser (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhoenixFox authored Oct 13, 2024
1 parent 4e74a20 commit f910b8d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
26 changes: 11 additions & 15 deletions Writerside/topics/pplib-Build-an-Auto.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,33 +540,29 @@ RobotContainer::RobotContainer() {
// Build an auto chooser. This will use frc2::cmd::None() as the default option.
// As an example, this will only show autos that start with "comp" while at
// competition as defined by the programmer
autoChooser = AutoBuilder::buildAutoChooser(
"", // If empty it will choose frc2::cmd::None()
[&isCompetition](const PathPlannerAuto *const autoCommand,
std::filesystem::path autoPath)
autoChooser = AutoBuilder::buildAutoChooserFilter(
[&isCompetition](const PathPlannerAuto& autoCommand)
{
return isCompetition ? autoCommand->GetName().starts_with("comp") : true;
return isCompetition ? autoCommand.GetName().starts_with("comp") : true;
}
);

// Another option that allows you to specify the default auto by its name
/*
autoChooser = AutoBuilder::buildAutoChooser(
"autoDefault", // If filled it will choosen always, regardless of filter
[&isCompetition](const PathPlannerAuto *const autoCommand,
std::filesystem::path autoP)
autoChooser = AutoBuilder::buildAutoChooserFilter(
[&isCompetition](const PathPlannerAuto& autoCommand)
{
return isCompetition ? autoCommand->GetName().starts_with("comp") : true;
}
return isCompetition ? autoCommand.GetName().starts_with("comp") : true;
},
"autoDefault", // If filled it will choosen always, regardless of filter
);
*/

// Another option allows you to filter out current directories relative to pathplanner/auto deploy directory
// Another option allows you to filter out current directories relative to deploy/pathplanner/auto directory
// Allows only autos in directory deploy/pathplanner/autos/comp
/*
autoChooser = AutoBuilder::buildAutoChooser(
"",
[&isCompetition](const PathPlannerAuto *const autoCommand,
autoChooser = AutoBuilder::buildAutoChooserFilterPathFilterPath(
[&isCompetition](const PathPlannerAuto& autoCommand,
std::filesystem::path autoPath)
{
return isCompetition ? autoPath.compare("comp") > 0 : true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,27 @@ void AutoBuilder::regenerateSendableReferences() {
}

frc::SendableChooser<frc2::Command*> AutoBuilder::buildAutoChooser(
std::string defaultAutoName,
std::function<bool(const PathPlannerAuto* const, std::filesystem::path)> filter) {
std::string defaultAutoName) {
return buildAutoChooserFilterPath(
[](const PathPlannerAuto &autoCommand,
std::filesystem::path autoPath) {
return true;
},defaultAutoName);
}

frc::SendableChooser<frc2::Command*> AutoBuilder::buildAutoChooserFilter(
std::function<bool(const PathPlannerAuto&)> filter,
std::string defaultAutoName) {
return buildAutoChooserFilterPath(
[&filter](const PathPlannerAuto &autoCommand,
std::filesystem::path autoPath) {
return filter(autoCommand);
},defaultAutoName);
}

frc::SendableChooser<frc2::Command*> AutoBuilder::buildAutoChooserFilterPath(
std::function<bool(const PathPlannerAuto&, std::filesystem::path)> filter,
std::string defaultAutoName) {
if (!m_configured) {
throw std::runtime_error(
"AutoBuilder was not configured before attempting to build an auto chooser");
Expand All @@ -180,8 +199,7 @@ frc::SendableChooser<frc2::Command*> AutoBuilder::buildAutoChooser(
if (defaultAutoName == autoName) {
sendableChooser.SetDefaultOption(autoName, entry.second.get());
defaultSelected = true;
} else if (filter(
dynamic_cast<const PathPlannerAuto* const >(entry.second.get()),
} else if (filter(*static_cast<PathPlannerAuto*>(entry.second.get()),
entry.first)) {
sendableChooser.AddOption(autoName, entry.second.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,41 +207,68 @@ class AutoBuilder {
/**
* Modifies the existing references that buildAutoChooser returns in SendableChooser to the most recent in the pathplanner/auto deploy directory
*
* Loads PathPlannerAutos from deploy/pathplanner/auto directory (recursively) on every call
* Adds new auto paths from the pathplanner/auto deploy directory however doesn't remove autos already previously loaded
*/

static void regenerateSendableReferences();

/**
* Create and populate a sendable chooser with all PathPlannerAutos in the project in pathplanner/auto deploy directory (recurively)
* Populate a sendable chooser with all loaded PathPlannerAutos in the project in pathplanner/auto deploy directory (recursively)
* Loads PathPlannerAutos from deploy/pathplanner/auto directory (recursively) on first call
*
* @param defaultAutoName The name of the auto that should be the default option. If this is an
* empty string, or if an auto with the given name does not exist, the default option will be
* frc2::cmd::None(), defaultAutoName doesn't get filter out and always is in final sendable chooser
* frc2::cmd::None()
* @return SendableChooser populated with all autos
*/
static frc::SendableChooser<frc2::Command*> buildAutoChooser(
std::string defaultAutoName = "");

/**
* Populate a sendable chooser with all loaded PathPlannerAutos in the project in pathplanner/auto deploy directory (recursively)
* Loads PathPlannerAutos from deploy/pathplanner/auto directory (recursively) on first call
* Filters certain PathPlannerAuto bases on their properties
*
* @param filter Function which filters the auto commands out, returning true allows the command to be uploaded to sendable chooser
* while returning false prevents it from being added.
* First param: autoCommand, pointer to PathPlannerAuto command which was generated
* Second param: autoPath, path to the autoCommand relative to pathplanner/auto deploy directory with extension ".auto"
* autoCommand, const reference to PathPlannerAuto command which was generated
* @param defaultAutoName The name of the auto that should be the default option. If this is an
* empty string, or if an auto with the given name does not exist, the default option will be
* frc2::cmd::None(), defaultAutoName doesn't get filter out and always is in final sendable chooser (if found)
* @return SendableChooser populated with all autos
*/
static frc::SendableChooser<frc2::Command*> buildAutoChooser(
std::string defaultAutoName = "",
std::function<
bool(const PathPlannerAuto* const, std::filesystem::path)> filter =
[](const PathPlannerAuto *const autoCommand,
std::filesystem::path autoPath) {
return true;
});
static frc::SendableChooser<frc2::Command*> buildAutoChooserFilter(
std::function<bool(const PathPlannerAuto&)> filter,
std::string defaultAutoName = "");

/**
* Populate a sendable chooser with all loaded PathPlannerAutos in the project in pathplanner/auto deploy directory (recursively)
* Loads PathPlannerAutos from deploy/pathplanner/auto directory (recursively) on first call
* Filters certain PathPlannerAuto bases on their properties and their filepath
*
* @param filter Function which filters the auto commands out, returning true allows the command to be uploaded to sendable chooser
* while returning false prevents it from being added.
* autoCommand, const reference to PathPlannerAuto command which was generated
* autoPath, path to the autoCommand relative to pathplanner/auto deploy directory with extension ".auto"
* @param defaultAutoName The name of the auto that should be the default option. If this is an
* empty string, or if an auto with the given name does not exist, the default option will be
* frc2::cmd::None(), defaultAutoName doesn't get filter out and always is in final sendable chooser (if found)
* @return SendableChooser populated with all autos
*/
static frc::SendableChooser<frc2::Command*> buildAutoChooserFilterPath(
std::function<bool(const PathPlannerAuto&, std::filesystem::path)> filter,
std::string defaultAutoName = "");

/**
* Get a vector of all auto names in the pathplanner/auto deploy directory (recurively)
* Get a vector of all auto names in the pathplanner/auto deploy directory (recursively)
*
* @return Vector of strings containing all auto names
*/
static std::vector<std::string> getAllAutoNames();

/**
* Get a vector of all auto paths in the pathplanner/auto deploy directory (recurively)
* Get a vector of all auto paths in the pathplanner/auto deploy directory (recursively)
*
* @return Vector of paths relative to autos deploy directory
*/
Expand Down

0 comments on commit f910b8d

Please sign in to comment.