-
Notifications
You must be signed in to change notification settings - Fork 611
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
[cmd] Add Commands.choose (aka SendableChooserCommand) #6355
base: main
Are you sure you want to change the base?
[cmd] Add Commands.choose (aka SendableChooserCommand) #6355
Conversation
I implemented something similar recently. The Command chooser = Commands.choose(
chooser -> SmartDashboard.putData("Drive Command Chooser", chooser),
tankDrive, arcadeDrive, curvatureDrive
);
SmartDashboard.putData("Drive Command", chooser); I wonder if this change could increase in scope and if var chooser = new SendableChooserCommand(tankDrive, arcadeDrive, curvatureDrive);
// Does nothing unless sent to a Dashboard
SmartDashboard.putData("Drive Command", chooser); |
Why do you need to publish the command to the dashboard? |
Dashboards support starting and stopping commands that you upload. https://docs.wpilib.org/en/stable/docs/software/dashboards/glass/command-based-widgets.html |
Sure. That's separate functionality, and isn't needed for SendableChooserCommand. The primary use case for SendableChooser is selecting a command for auto, and the SendableChooser needs to be published for selections to be possible; there's no need (and practically no benefit) to put the command on the dashboard as well. |
c85ea89
to
11e62a9
Compare
wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SendableChooserCommandTest.java
Outdated
Show resolved
Hide resolved
#7340 might be resolved by this, though the discussion of the impl in that issue is different from the impl in this PR. (Hence why I'm avoiding the auto-linking words for now) |
Wow I forgot I wrote this. This PR does what I was thinking of in that issue. Is this missing something? |
I think there should be an overload that accepts a string and a command vararg in addition to the sendable chooser lambda overload. |
What would that string be used for, and what's the motivation for that overload? |
|
It shouldn't be coupled to SmartDashboard. Accepting an absolute NT key would be good, though. |
There doesn't appear to be a standard way to add a sendable to an arbitrary NT key, unfortunately. Maybe fix the merge conflicts for this PR and make an issue to add that capacity later? |
2976c3d
to
a125981
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The c++ version needs a test- it doesn't compile currently.
wpilibNewCommands/src/main/native/include/frc2/command/Commands.h
Outdated
Show resolved
Hide resolved
template <std::convertible_to<CommandPtr>... CommandPtrs> | ||
[[nodiscard]] | ||
CommandPtr Choose(std::function<void(wpi::Sendable*)> publish, | ||
CommandPtrs&&... commands) { | ||
frc::SendableChooser<std::string_view> chooser; | ||
((void)chooser.AddOption(commands.GetName(), commands.GetName()), ...); | ||
publish(&chooser); | ||
return Select( | ||
[sendableChooser = std::move(chooser)]() mutable { | ||
return sendableChooser.GetSelected(); | ||
}, | ||
(std::pair{commands.GetName(), std::move(commands)}, ...)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't compile
0d2b280
to
8157c91
Compare
8157c91
to
c2294d5
Compare
Tried fixing this locally, and while some issues could be fixed, a fundamental issue is that |
Thanks for looking into this! |
Here's a patch that makes tests pass: diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h b/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h
index 7dea6f2c9..787497590 100644
--- a/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h
+++ b/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h
@@ -183,14 +183,14 @@ template <std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr Choose(std::function<void(wpi::Sendable*)> publish,
CommandPtrs&&... commands) {
- frc::SendableChooser<std::string_view> chooser;
- ((void)chooser.AddOption(commands.GetName(), commands.GetName()), ...);
- publish(&chooser);
- return Select(
- [sendableChooser = std::move(chooser)]() mutable {
- return sendableChooser.GetSelected();
+ auto chooser = std::make_shared<frc::SendableChooser<std::string_view>>();
+ ((void)chooser->AddOption(commands.get()->GetName(), commands.get()->GetName()), ...);
+ publish(chooser.get());
+ return Select<std::string_view>(
+ [sendableChooser = chooser]() mutable {
+ return sendableChooser->GetSelected();
},
- (std::pair{commands.GetName(), std::move(commands)}, ...));
+ std::pair{std::string_view{commands.get()->GetName()}, std::move(commands)}...);
}
/**
diff --git a/wpilibNewCommands/src/test/native/cpp/frc2/command/SendableChooserCommandTest.cpp b/wpilibNewCommands/src/test/native/cpp/frc2/command/SendableChooserCommandTest.cpp
index 0d419d855..25e67eb7a 100644
--- a/wpilibNewCommands/src/test/native/cpp/frc2/command/SendableChooserCommandTest.cpp
+++ b/wpilibNewCommands/src/test/native/cpp/frc2/command/SendableChooserCommandTest.cpp
@@ -23,7 +23,7 @@ using namespace frc2;
using SendableChooserTestPublisherFunc = std::function<void(wpi::Sendable*)>;
using SendableChooserTestArgs =
std::pair<std::function<frc2::CommandPtr(SendableChooserTestPublisherFunc)>,
- std::vector<std::string_view>>;
+ std::vector<std::string>>;
class SendableChooserCommandTest
: public CommandTestBaseWithParam<SendableChooserTestArgs> {
@@ -50,7 +50,7 @@ TEST_P(SendableChooserCommandTest, OptionsAreCorrect) {
}
namespace utils {
-static const frc2::CommandPtr CommandNamed(std::string_view name) {
+static frc2::CommandPtr CommandNamed(std::string_view name) {
return frc2::cmd::Print(name).WithName(name);
}
@@ -58,17 +58,17 @@ static const std::vector<SendableChooserTestArgs> OptionsAreCorrectParams() {
SendableChooserTestArgs empty{[](SendableChooserTestPublisherFunc func) {
return frc2::cmd::Choose(func);
},
- std::vector<std::string_view>()};
+ std::vector<std::string>()};
SendableChooserTestArgs duplicateName{
[](SendableChooserTestPublisherFunc func) {
return frc2::cmd::Choose(func, CommandNamed("a"), CommandNamed("b"), CommandNamed("a"));
- }, make_vector<std::string_view>("a", "b")};
+ }, make_vector<std::string>("a", "b")};
SendableChooserTestArgs happyPath{
[](SendableChooserTestPublisherFunc func) {
return frc2::cmd::Choose(func, CommandNamed("a"), CommandNamed("b"), CommandNamed("c"));
- }, make_vector<std::string_view>("a", "b", "c")};
+ }, make_vector<std::string>("a", "b", "c")};
return make_vector<SendableChooserTestArgs>(empty, duplicateName, happyPath);
} Good thinking about the
So yeah, some of the errors were poorly wording/misleading, others just were a bit tricky to figure out because there were so many. |
Wow -- thank you very much! |
Co-authored-by: Joseph Eng <[email protected]>
No description provided.