Skip to content

Commit

Permalink
added After and AfterTime decorator to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-yes-0-fps committed Sep 16, 2024
1 parent 3c21556 commit c6eb4d4
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,56 @@ public ParallelRaceGroup onlyWhile(BooleanSupplier condition) {
return until(() -> !condition.getAsBoolean());
}

/**
* Decorates this command to run once a condition becomes true.
*
* <p>Note: This decorator works by adding this command to a composition. The command the
* decorator was called on cannot be scheduled independently or be added to a different
* composition (namely, decorators), unless it is manually cleared from the list of composed
* commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
* returned from this method can be further decorated without issue.
*
* @param condition the condition to wait for
* @return the decorated command
*/
public SequentialCommandGroup after(BooleanSupplier condition) {
// if we want this to bypass the `WaitUntilCommand` if the condition is already true,
// we can use a conditional command but it proposes some compositional issues
return new SequentialCommandGroup(new WaitUntilCommand(condition), this);
}

/**
* Decorates this command to run after a time delay.
*
* <p>Note: This decorator works by adding this command to a composition. The command the
* decorator was called on cannot be scheduled independently or be added to a different
* composition (namely, decorators), unless it is manually cleared from the list of composed
* commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
* returned from this method can be further decorated without issue.
*
* @param seconds the seconds to wait
* @return the decorated command
*/
public SequentialCommandGroup afterSeconds(double seconds) {
return new SequentialCommandGroup(new WaitCommand(seconds), this);
}

/**
* Decorates this command to run after a time delay.
*
* <p>Note: This decorator works by adding this command to a composition. The command the
* decorator was called on cannot be scheduled independently or be added to a different
* composition (namely, decorators), unless it is manually cleared from the list of composed
* commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
* returned from this method can be further decorated without issue.
*
* @param time the time to wait
* @return the decorated command
*/
public SequentialCommandGroup afterTime(Time time) {
return afterSeconds(time.in(Seconds));
}

/**
* Decorates this command with a runnable to run before this command starts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ CommandPtr Command::OnlyWhile(std::function<bool()> condition) && {
return std::move(*this).ToPtr().OnlyWhile(std::move(condition));
}

CommandPtr After(std::function<bool()> condition) && {
return std::move(*this).ToPtr().After(std::move(condition));
}

CommandPtr AfterTime(units::second_t duration) && {
return std::move(*this).ToPtr().AfterTime(duration);
}

CommandPtr Command::IgnoringDisable(bool doesRunWhenDisabled) && {
return std::move(*this).ToPtr().IgnoringDisable(doesRunWhenDisabled);
}
Expand Down
18 changes: 18 additions & 0 deletions wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ CommandPtr CommandPtr::OnlyIf(std::function<bool()> condition) && {
return std::move(*this).Unless(std::not_fn(std::move(condition)));
}

CommandPtr CommandPtr::After(std::function<bool()> condition) && {
AssertValid();
std::vector<std::unique_ptr<Command>> temp;
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
temp.emplace_back(std::move(m_ptr));
m_ptr = std::make_unique<SequentialCommandGroup>(std::move(temp));
return std::move(*this);
}

CommandPtr CommandPtr::AfterTime(units::second_t duration) && {
AssertValid();
std::vector<std::unique_ptr<Command>> temp;
temp.emplace_back(std::make_unique<WaitCommand>(duration));
temp.emplace_back(std::move(m_ptr));
m_ptr = std::make_unique<SequentialCommandGroup>(std::move(temp));
return std::move(*this);
}

CommandPtr CommandPtr::DeadlineWith(CommandPtr&& parallel) && {
AssertValid();
std::vector<std::unique_ptr<Command>> vec;
Expand Down
18 changes: 18 additions & 0 deletions wpilibNewCommands/src/main/native/include/frc2/command/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
[[nodiscard]]
CommandPtr OnlyWhile(std::function<bool()> condition) &&;

/**
* Decorates this command to run after a condition becomes true.
*
* @param condition the condition to run after
* @return the decorated command
*/
[[nodiscard]]
CommandPtr After(std::function<bool()> condition) &&;

/**
* Decorates this command to run after a specified amount of time.
*
* @param duration the time to wait before running
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AfterTime(units::second_t duration) &&;

/**
* Decorates this command with a runnable to run before this command starts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ class CommandPtr final {
[[nodiscard]]
CommandPtr OnlyIf(std::function<bool()> condition) &&;

/**
* Decorates this command to run after a condition becomes true.
*
* @param condition the condition to run after
* @return the decorated command
*/
[[nodiscard]]
CommandPtr After(std::function<bool()> condition) &&;

/**
* Decorates this command to run after a specified amount of time.
*
* @param duration the time to wait before running
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AfterTime(units::second_t duration) &&;

/**
* Decorates this command with a set of commands to run parallel to it, ending
* when the calling command ends and interrupting all the others. Often more
Expand Down

0 comments on commit c6eb4d4

Please sign in to comment.