2022-10-23 22:08:22 +03:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/commands2/Commands.hpp"
|
2022-10-23 22:08:22 +03:00
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/FunctionExtras.hpp"
|
|
|
|
|
#include "wpi/util/deprecated.hpp"
|
|
|
|
|
|
|
|
|
|
#include "wpi/commands2/ConditionalCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/DeferredCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/FunctionalCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/InstantCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/ParallelCommandGroup.hpp"
|
|
|
|
|
#include "wpi/commands2/ParallelDeadlineGroup.hpp"
|
|
|
|
|
#include "wpi/commands2/ParallelRaceGroup.hpp"
|
|
|
|
|
#include "wpi/commands2/PrintCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/ProxyCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/RunCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/SequentialCommandGroup.hpp"
|
|
|
|
|
#include "wpi/commands2/WaitCommand.hpp"
|
|
|
|
|
#include "wpi/commands2/WaitUntilCommand.hpp"
|
2022-10-23 22:08:22 +03:00
|
|
|
|
|
|
|
|
using namespace frc2;
|
|
|
|
|
|
|
|
|
|
// Factories
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::None() {
|
|
|
|
|
return InstantCommand().ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 22:52:48 -04:00
|
|
|
CommandPtr cmd::Idle(Requirements requirements) {
|
|
|
|
|
return Run([] {}, requirements);
|
2023-08-29 23:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-23 22:08:22 +03:00
|
|
|
CommandPtr cmd::RunOnce(std::function<void()> action,
|
2023-09-17 20:48:39 -07:00
|
|
|
Requirements requirements) {
|
2022-10-23 22:08:22 +03:00
|
|
|
return InstantCommand(std::move(action), requirements).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 20:48:39 -07:00
|
|
|
CommandPtr cmd::Run(std::function<void()> action, Requirements requirements) {
|
2022-10-23 22:08:22 +03:00
|
|
|
return RunCommand(std::move(action), requirements).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::StartEnd(std::function<void()> start, std::function<void()> end,
|
2023-09-17 20:48:39 -07:00
|
|
|
Requirements requirements) {
|
2022-10-23 22:08:22 +03:00
|
|
|
return FunctionalCommand(
|
|
|
|
|
std::move(start), [] {},
|
|
|
|
|
[end = std::move(end)](bool interrupted) { end(); },
|
|
|
|
|
[] { return false; }, requirements)
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::RunEnd(std::function<void()> run, std::function<void()> end,
|
2023-09-17 20:48:39 -07:00
|
|
|
Requirements requirements) {
|
2022-10-23 22:08:22 +03:00
|
|
|
return FunctionalCommand([] {}, std::move(run),
|
|
|
|
|
[end = std::move(end)](bool interrupted) { end(); },
|
|
|
|
|
[] { return false; }, requirements)
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 15:40:13 -04:00
|
|
|
CommandPtr cmd::StartRun(std::function<void()> start, std::function<void()> run,
|
|
|
|
|
Requirements requirements) {
|
|
|
|
|
return FunctionalCommand(
|
|
|
|
|
std::move(start), std::move(run), [](bool interrupted) {},
|
|
|
|
|
[] { return false; }, requirements)
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 22:08:22 +03:00
|
|
|
CommandPtr cmd::Print(std::string_view msg) {
|
|
|
|
|
return PrintCommand(msg).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 23:49:02 -04:00
|
|
|
CommandPtr cmd::DeferredProxy(wpi::unique_function<Command*()> supplier) {
|
2025-01-02 07:11:39 +08:00
|
|
|
return Defer(
|
|
|
|
|
[supplier = std::move(supplier)]() mutable {
|
|
|
|
|
// There is no non-owning version of AsProxy(), so use the non-owning
|
|
|
|
|
// ProxyCommand constructor instead.
|
|
|
|
|
return ProxyCommand{supplier()}.ToPtr();
|
|
|
|
|
},
|
|
|
|
|
{});
|
2023-09-17 23:49:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::DeferredProxy(wpi::unique_function<CommandPtr()> supplier) {
|
2025-01-02 07:11:39 +08:00
|
|
|
return Defer([supplier = std::move(
|
|
|
|
|
supplier)]() mutable { return supplier().AsProxy(); },
|
|
|
|
|
{});
|
2023-09-17 23:49:02 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-23 22:08:22 +03:00
|
|
|
CommandPtr cmd::Wait(units::second_t duration) {
|
|
|
|
|
return WaitCommand(duration).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::WaitUntil(std::function<bool()> condition) {
|
|
|
|
|
return WaitUntilCommand(condition).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::Either(CommandPtr&& onTrue, CommandPtr&& onFalse,
|
|
|
|
|
std::function<bool()> selector) {
|
|
|
|
|
return ConditionalCommand(std::move(onTrue).Unwrap(),
|
|
|
|
|
std::move(onFalse).Unwrap(), std::move(selector))
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 22:16:33 -04:00
|
|
|
CommandPtr cmd::Defer(wpi::unique_function<CommandPtr()> supplier,
|
|
|
|
|
Requirements requirements) {
|
|
|
|
|
return DeferredCommand(std::move(supplier), requirements).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 22:08:22 +03:00
|
|
|
CommandPtr cmd::Sequence(std::vector<CommandPtr>&& commands) {
|
|
|
|
|
return SequentialCommandGroup(CommandPtr::UnwrapVector(std::move(commands)))
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::RepeatingSequence(std::vector<CommandPtr>&& commands) {
|
|
|
|
|
return Sequence(std::move(commands)).Repeatedly();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::Parallel(std::vector<CommandPtr>&& commands) {
|
|
|
|
|
return ParallelCommandGroup(CommandPtr::UnwrapVector(std::move(commands)))
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::Race(std::vector<CommandPtr>&& commands) {
|
|
|
|
|
return ParallelRaceGroup(CommandPtr::UnwrapVector(std::move(commands)))
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr cmd::Deadline(CommandPtr&& deadline,
|
|
|
|
|
std::vector<CommandPtr>&& others) {
|
|
|
|
|
return ParallelDeadlineGroup(std::move(deadline).Unwrap(),
|
|
|
|
|
CommandPtr::UnwrapVector(std::move(others)))
|
|
|
|
|
.ToPtr();
|
|
|
|
|
}
|