Skip to content

Commit

Permalink
Add Subsystem factories
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Aug 29, 2023
1 parent 39188ac commit eb2023e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.util.sendable.SendableRegistry;
import java.util.Set;
import java.util.function.Supplier;

/**
* A robot subsystem. Subsystems are the basic unit of robot organization in the Command-based
Expand Down Expand Up @@ -179,6 +181,18 @@ public Command runEnd(Runnable run, Runnable end) {
return Commands.runEnd(run, end, this);
}

/**
* Constructs a {@link DeferredCommand} with the provided supplier. This subsystem is added as a
* requirement.
*
* @param supplier the command supplier.
* @return the command.
* @see DeferredCommand
*/
public Command defer(Supplier<Command> supplier) {
return Commands.defer(supplier, Set.of(this));
}

/**
* Associates a {@link Sendable} with this Subsystem. Also update the child's name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ CommandPtr Subsystem::RunEnd(std::function<void()> run,
return cmd::RunEnd(std::move(run), std::move(end), {this});
}

CommandPtr Subsystem::Defer(wpi::unique_function<Command*()> supplier) {
return cmd::Defer(std::move(supplier), {this});
}

CommandPtr Subsystem::Defer(wpi::unique_function<CommandPtr()> supplier) {
return cmd::Defer(std::move(supplier), {this});
}

void Subsystem::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Subsystem");
builder.AddBooleanProperty(
Expand Down
18 changes: 18 additions & 0 deletions wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ class Subsystem : public wpi::Sendable, public wpi::SendableHelper<Subsystem> {
[[nodiscard]]
CommandPtr RunEnd(std::function<void()> run, std::function<void()> end);

/**
* Constructs a DeferredCommand with the provided supplier. This subsystem is
* added as a requirement.
*
* @param supplier the command supplier.
* @return the command.
*/
CommandPtr Defer(wpi::unique_function<Command*()> supplier);

/**
* Constructs a DeferredCommand with the provided supplier. This subsystem is
* added as a requirement.
*
* @param supplier the command supplier.
* @return the command.
*/
CommandPtr Defer(wpi::unique_function<CommandPtr()> supplier);

void InitSendable(wpi::SendableBuilder& builder) override;

protected:
Expand Down

0 comments on commit eb2023e

Please sign in to comment.