Skip to content

Commit 6c12ac9

Browse files
authored
[commands] C++: Add CommandPtr overload for SetDefaultCommand (#4488)
1 parent bab2af0 commit 6c12ac9

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

cpp/frc2/command/CommandScheduler.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ void CommandScheduler::UnregisterSubsystem(
296296
}
297297
}
298298

299+
void CommandScheduler::SetDefaultCommand(Subsystem* subsystem,
300+
CommandPtr&& defaultCommand) {
301+
if (!defaultCommand.get()->HasRequirement(subsystem)) {
302+
throw FRC_MakeError(frc::err::CommandIllegalUse, "{}",
303+
"Default commands must require their subsystem!");
304+
}
305+
if (defaultCommand.get()->IsFinished()) {
306+
throw FRC_MakeError(frc::err::CommandIllegalUse, "{}",
307+
"Default commands should not end!");
308+
}
309+
310+
SetDefaultCommandImpl(subsystem, std::move(defaultCommand).Unwrap());
311+
}
312+
299313
Command* CommandScheduler::GetDefaultCommand(const Subsystem* subsystem) const {
300314
auto&& find = m_impl->subsystems.find(subsystem);
301315
if (find != m_impl->subsystems.end()) {

cpp/frc2/command/Subsystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "frc2/command/Subsystem.h"
66

7+
#include "frc2/command/CommandPtr.h"
8+
79
using namespace frc2;
810
Subsystem::~Subsystem() {
911
CommandScheduler::GetInstance().UnregisterSubsystem(this);
@@ -13,6 +15,11 @@ void Subsystem::Periodic() {}
1315

1416
void Subsystem::SimulationPeriodic() {}
1517

18+
void Subsystem::SetDefaultCommand(CommandPtr&& defaultCommand) {
19+
CommandScheduler::GetInstance().SetDefaultCommand(this,
20+
std::move(defaultCommand));
21+
}
22+
1623
Command* Subsystem::GetDefaultCommand() const {
1724
return CommandScheduler::GetInstance().GetDefaultCommand(this);
1825
}

include/frc2/command/CommandScheduler.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ class CommandScheduler final : public nt::NTSendable,
191191
std::forward<T>(defaultCommand)));
192192
}
193193

194+
/**
195+
* Sets the default command for a subsystem. Registers that subsystem if it
196+
* is not already registered. Default commands will run whenever there is no
197+
* other command currently scheduled that requires the subsystem. Default
198+
* commands should be written to never end (i.e. their IsFinished() method
199+
* should return false), as they would simply be re-scheduled if they do.
200+
* Default commands must also require their subsystem.
201+
*
202+
* @param subsystem the subsystem whose default command will be set
203+
* @param defaultCommand the default command to associate with the subsystem
204+
*/
205+
void SetDefaultCommand(Subsystem* subsystem, CommandPtr&& defaultCommand);
206+
194207
/**
195208
* Gets the default command associated with this subsystem. Null if this
196209
* subsystem has no default command associated with it.

include/frc2/command/Subsystem.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace frc2 {
1313
class Command;
14+
class CommandPtr;
1415
/**
1516
* A robot subsystem. Subsystems are the basic unit of robot organization in
1617
* the Command-based framework; they encapsulate low-level hardware objects
@@ -71,6 +72,17 @@ class Subsystem {
7172
this, std::forward<T>(defaultCommand));
7273
}
7374

75+
/**
76+
* Sets the default Command of the subsystem. The default command will be
77+
* automatically scheduled when no other commands are scheduled that require
78+
* the subsystem. Default commands should generally not end on their own, i.e.
79+
* their IsFinished() method should always return false. Will automatically
80+
* register this subsystem with the CommandScheduler.
81+
*
82+
* @param defaultCommand the default command to associate with this subsystem
83+
*/
84+
void SetDefaultCommand(CommandPtr&& defaultCommand);
85+
7486
/**
7587
* Gets the default command for this subsystem. Returns null if no default
7688
* command is currently associated with the subsystem.

0 commit comments

Comments
 (0)