2020-12-26 14:12:05 -08: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.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-10-18 10:57:43 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4521)
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
#include <memory>
|
2022-12-16 04:28:52 +02:00
|
|
|
#include <string>
|
2019-11-20 22:44:18 -08:00
|
|
|
#include <type_traits>
|
2019-08-25 23:55:59 -04:00
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#include "frc2/command/CommandBase.h"
|
|
|
|
|
#include "frc2/command/PrintCommand.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* A command composition that runs one of a selection of commands, either using
|
|
|
|
|
* a selector and a key to command mapping, or a supplier that returns the
|
|
|
|
|
* command directly at runtime.
|
2019-08-25 23:55:59 -04:00
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* <p>The rules for command compositions apply: command instances that are
|
|
|
|
|
* passed to it are owned by the composition and cannot be added to any other
|
|
|
|
|
* composition or scheduled individually, and the composition requires all
|
|
|
|
|
* subsystems its components require.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* This class is provided by the NewCommands VendorDep
|
2019-08-25 23:55:59 -04:00
|
|
|
*/
|
2019-11-05 20:52:49 -08:00
|
|
|
template <typename Key>
|
2019-08-25 23:55:59 -04:00
|
|
|
class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new selectcommand.
|
|
|
|
|
*
|
|
|
|
|
* @param commands the map of commands to choose from
|
|
|
|
|
* @param selector the selector to determine which command to run
|
|
|
|
|
*/
|
|
|
|
|
template <class... Types,
|
|
|
|
|
typename = std::enable_if_t<std::conjunction_v<
|
|
|
|
|
std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
|
2020-12-28 11:18:07 -08:00
|
|
|
explicit SelectCommand(std::function<Key()> selector,
|
|
|
|
|
std::pair<Key, Types>... commands)
|
2019-08-25 23:55:59 -04:00
|
|
|
: m_selector{std::move(selector)} {
|
|
|
|
|
std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
|
|
|
|
|
|
|
|
|
|
((void)foo.emplace_back(commands.first,
|
|
|
|
|
std::make_unique<std::remove_reference_t<Types>>(
|
|
|
|
|
std::move(commands.second))),
|
|
|
|
|
...);
|
|
|
|
|
|
|
|
|
|
for (auto&& command : foo) {
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto&& command : foo) {
|
|
|
|
|
this->AddRequirements(command.second->GetRequirements());
|
|
|
|
|
m_runsWhenDisabled &= command.second->RunsWhenDisabled();
|
2022-11-28 02:23:56 +02:00
|
|
|
if (command.second->GetInterruptionBehavior() ==
|
|
|
|
|
Command::InterruptionBehavior::kCancelSelf) {
|
|
|
|
|
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
m_commands.emplace(std::move(command.first), std::move(command.second));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SelectCommand(
|
|
|
|
|
std::function<Key()> selector,
|
|
|
|
|
std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
|
|
|
|
|
: m_selector{std::move(selector)} {
|
|
|
|
|
for (auto&& command : commands) {
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto&& command : commands) {
|
|
|
|
|
this->AddRequirements(command.second->GetRequirements());
|
|
|
|
|
m_runsWhenDisabled &= command.second->RunsWhenDisabled();
|
2022-11-28 02:23:56 +02:00
|
|
|
if (command.second->GetInterruptionBehavior() ==
|
|
|
|
|
Command::InterruptionBehavior::kCancelSelf) {
|
|
|
|
|
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
m_commands.emplace(std::move(command.first), std::move(command.second));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No copy constructors for command groups
|
|
|
|
|
SelectCommand(const SelectCommand& other) = delete;
|
|
|
|
|
|
2019-10-18 10:57:43 -04:00
|
|
|
// Prevent template expansion from emulating copy ctor
|
|
|
|
|
SelectCommand(SelectCommand&) = delete;
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
/**
|
|
|
|
|
* Creates a new selectcommand.
|
|
|
|
|
*
|
|
|
|
|
* @param toRun a supplier providing the command to run
|
2022-11-29 00:43:10 +02:00
|
|
|
* @deprecated Replace with {@link ProxyCommand},
|
|
|
|
|
* composing multiple of them in a {@link ParallelRaceGroup} if needed.
|
2019-08-25 23:55:59 -04:00
|
|
|
*/
|
2022-11-29 00:43:10 +02:00
|
|
|
WPI_DEPRECATED("Replace with ProxyCommand")
|
2020-12-28 10:12:52 -08:00
|
|
|
explicit SelectCommand(std::function<Command*()> toRun)
|
|
|
|
|
: m_toRun{std::move(toRun)} {}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
SelectCommand(SelectCommand&& other) = default;
|
|
|
|
|
|
|
|
|
|
void Initialize() override;
|
|
|
|
|
|
|
|
|
|
void Execute() override { m_selectedCommand->Execute(); }
|
|
|
|
|
|
|
|
|
|
void End(bool interrupted) override {
|
|
|
|
|
return m_selectedCommand->End(interrupted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsFinished() override { return m_selectedCommand->IsFinished(); }
|
|
|
|
|
|
|
|
|
|
bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
|
|
|
|
|
|
2022-11-28 02:23:56 +02:00
|
|
|
Command::InterruptionBehavior GetInterruptionBehavior() const override {
|
|
|
|
|
return m_interruptBehavior;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
void InitSendable(wpi::SendableBuilder& builder) override {
|
|
|
|
|
CommandBase::InitSendable(builder);
|
|
|
|
|
|
|
|
|
|
builder.AddStringProperty(
|
|
|
|
|
"selected",
|
|
|
|
|
[this] {
|
|
|
|
|
if (m_selectedCommand) {
|
|
|
|
|
return m_selectedCommand->GetName();
|
|
|
|
|
} else {
|
|
|
|
|
return std::string{"null"};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
protected:
|
|
|
|
|
std::unique_ptr<Command> TransferOwnership() && override {
|
|
|
|
|
return std::make_unique<SelectCommand>(std::move(*this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
|
|
|
|
|
std::function<Key()> m_selector;
|
|
|
|
|
std::function<Command*()> m_toRun;
|
|
|
|
|
Command* m_selectedCommand;
|
|
|
|
|
bool m_runsWhenDisabled = true;
|
2022-11-28 02:23:56 +02:00
|
|
|
Command::InterruptionBehavior m_interruptBehavior{
|
|
|
|
|
Command::InterruptionBehavior::kCancelIncoming};
|
2019-08-25 23:55:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
void SelectCommand<T>::Initialize() {
|
|
|
|
|
if (m_selector) {
|
|
|
|
|
auto find = m_commands.find(m_selector());
|
|
|
|
|
if (find == m_commands.end()) {
|
|
|
|
|
m_selectedCommand = new PrintCommand(
|
|
|
|
|
"SelectCommand selector value does not correspond to any command!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_selectedCommand = find->second.get();
|
|
|
|
|
} else {
|
|
|
|
|
m_selectedCommand = m_toRun();
|
|
|
|
|
}
|
|
|
|
|
m_selectedCommand->Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace frc2
|
2019-10-18 10:57:43 -04:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif
|