Skip to content

Commit ea8234e

Browse files
committed
Merge branch 'upstream' into main
2 parents 3e20921 + 5665862 commit ea8234e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+7037
-0
lines changed
+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include "frc2/command/Command.h"
6+
7+
#include "frc2/command/CommandScheduler.h"
8+
#include "frc2/command/InstantCommand.h"
9+
#include "frc2/command/ParallelCommandGroup.h"
10+
#include "frc2/command/ParallelDeadlineGroup.h"
11+
#include "frc2/command/ParallelRaceGroup.h"
12+
#include "frc2/command/PerpetualCommand.h"
13+
#include "frc2/command/ProxyScheduleCommand.h"
14+
#include "frc2/command/SequentialCommandGroup.h"
15+
#include "frc2/command/WaitCommand.h"
16+
#include "frc2/command/WaitUntilCommand.h"
17+
18+
using namespace frc2;
19+
20+
Command::~Command() {
21+
CommandScheduler::GetInstance().Cancel(this);
22+
}
23+
24+
Command::Command(const Command& rhs) : ErrorBase(rhs) {}
25+
26+
Command& Command::operator=(const Command& rhs) {
27+
ErrorBase::operator=(rhs);
28+
m_isGrouped = false;
29+
return *this;
30+
}
31+
32+
void Command::Initialize() {}
33+
void Command::Execute() {}
34+
void Command::End(bool interrupted) {}
35+
36+
ParallelRaceGroup Command::WithTimeout(units::second_t duration) && {
37+
std::vector<std::unique_ptr<Command>> temp;
38+
temp.emplace_back(std::make_unique<WaitCommand>(duration));
39+
temp.emplace_back(std::move(*this).TransferOwnership());
40+
return ParallelRaceGroup(std::move(temp));
41+
}
42+
43+
ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && {
44+
std::vector<std::unique_ptr<Command>> temp;
45+
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
46+
temp.emplace_back(std::move(*this).TransferOwnership());
47+
return ParallelRaceGroup(std::move(temp));
48+
}
49+
50+
SequentialCommandGroup Command::BeforeStarting(
51+
std::function<void()> toRun,
52+
std::initializer_list<Subsystem*> requirements) && {
53+
return std::move(*this).BeforeStarting(
54+
std::move(toRun),
55+
wpi::makeArrayRef(requirements.begin(), requirements.end()));
56+
}
57+
58+
SequentialCommandGroup Command::BeforeStarting(
59+
std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && {
60+
std::vector<std::unique_ptr<Command>> temp;
61+
temp.emplace_back(
62+
std::make_unique<InstantCommand>(std::move(toRun), requirements));
63+
temp.emplace_back(std::move(*this).TransferOwnership());
64+
return SequentialCommandGroup(std::move(temp));
65+
}
66+
67+
SequentialCommandGroup Command::AndThen(
68+
std::function<void()> toRun,
69+
std::initializer_list<Subsystem*> requirements) && {
70+
return std::move(*this).AndThen(
71+
std::move(toRun),
72+
wpi::makeArrayRef(requirements.begin(), requirements.end()));
73+
}
74+
75+
SequentialCommandGroup Command::AndThen(
76+
std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && {
77+
std::vector<std::unique_ptr<Command>> temp;
78+
temp.emplace_back(std::move(*this).TransferOwnership());
79+
temp.emplace_back(
80+
std::make_unique<InstantCommand>(std::move(toRun), requirements));
81+
return SequentialCommandGroup(std::move(temp));
82+
}
83+
84+
PerpetualCommand Command::Perpetually() && {
85+
return PerpetualCommand(std::move(*this).TransferOwnership());
86+
}
87+
88+
ProxyScheduleCommand Command::AsProxy() {
89+
return ProxyScheduleCommand(this);
90+
}
91+
92+
void Command::Schedule(bool interruptible) {
93+
CommandScheduler::GetInstance().Schedule(interruptible, this);
94+
}
95+
96+
void Command::Cancel() {
97+
CommandScheduler::GetInstance().Cancel(this);
98+
}
99+
100+
bool Command::IsScheduled() const {
101+
return CommandScheduler::GetInstance().IsScheduled(this);
102+
}
103+
104+
bool Command::HasRequirement(Subsystem* requirement) const {
105+
bool hasRequirement = false;
106+
for (auto&& subsystem : GetRequirements()) {
107+
hasRequirement |= requirement == subsystem;
108+
}
109+
return hasRequirement;
110+
}
111+
112+
std::string Command::GetName() const {
113+
return GetTypeName(*this);
114+
}
115+
116+
bool Command::IsGrouped() const {
117+
return m_isGrouped;
118+
}
119+
120+
void Command::SetGrouped(bool grouped) {
121+
m_isGrouped = grouped;
122+
}
123+
124+
namespace frc2 {
125+
bool RequirementsDisjoint(Command* first, Command* second) {
126+
bool disjoint = true;
127+
auto&& requirements = second->GetRequirements();
128+
for (auto&& requirement : first->GetRequirements()) {
129+
disjoint &= requirements.find(requirement) == requirements.end();
130+
}
131+
return disjoint;
132+
}
133+
} // namespace frc2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include "frc2/command/CommandBase.h"
6+
7+
#include <frc/smartdashboard/SendableBuilder.h>
8+
#include <frc/smartdashboard/SendableRegistry.h>
9+
10+
using namespace frc2;
11+
12+
CommandBase::CommandBase() {
13+
frc::SendableRegistry::GetInstance().Add(this, GetTypeName(*this));
14+
}
15+
16+
void CommandBase::AddRequirements(
17+
std::initializer_list<Subsystem*> requirements) {
18+
m_requirements.insert(requirements.begin(), requirements.end());
19+
}
20+
21+
void CommandBase::AddRequirements(wpi::ArrayRef<Subsystem*> requirements) {
22+
m_requirements.insert(requirements.begin(), requirements.end());
23+
}
24+
25+
void CommandBase::AddRequirements(wpi::SmallSet<Subsystem*, 4> requirements) {
26+
m_requirements.insert(requirements.begin(), requirements.end());
27+
}
28+
29+
wpi::SmallSet<Subsystem*, 4> CommandBase::GetRequirements() const {
30+
return m_requirements;
31+
}
32+
33+
void CommandBase::SetName(const wpi::Twine& name) {
34+
frc::SendableRegistry::GetInstance().SetName(this, name);
35+
}
36+
37+
std::string CommandBase::GetName() const {
38+
return frc::SendableRegistry::GetInstance().GetName(this);
39+
}
40+
41+
std::string CommandBase::GetSubsystem() const {
42+
return frc::SendableRegistry::GetInstance().GetSubsystem(this);
43+
}
44+
45+
void CommandBase::SetSubsystem(const wpi::Twine& subsystem) {
46+
frc::SendableRegistry::GetInstance().SetSubsystem(this, subsystem);
47+
}
48+
49+
void CommandBase::InitSendable(frc::SendableBuilder& builder) {
50+
builder.SetSmartDashboardType("Command");
51+
builder.AddStringProperty(
52+
".name", [this] { return GetName(); }, nullptr);
53+
builder.AddBooleanProperty(
54+
"running", [this] { return IsScheduled(); },
55+
[this](bool value) {
56+
bool isScheduled = IsScheduled();
57+
if (value && !isScheduled) {
58+
Schedule();
59+
} else if (!value && isScheduled) {
60+
Cancel();
61+
}
62+
});
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include "frc2/command/CommandGroupBase.h"
6+
7+
#include <frc/WPIErrors.h>
8+
9+
using namespace frc2;
10+
11+
bool CommandGroupBase::RequireUngrouped(Command& command) {
12+
if (command.IsGrouped()) {
13+
wpi_setGlobalWPIErrorWithContext(
14+
CommandIllegalUse,
15+
"Commands cannot be added to more than one CommandGroup");
16+
return false;
17+
} else {
18+
return true;
19+
}
20+
}
21+
22+
bool CommandGroupBase::RequireUngrouped(
23+
wpi::ArrayRef<std::unique_ptr<Command>> commands) {
24+
bool allUngrouped = true;
25+
for (auto&& command : commands) {
26+
allUngrouped &= !command.get()->IsGrouped();
27+
}
28+
if (!allUngrouped) {
29+
wpi_setGlobalWPIErrorWithContext(
30+
CommandIllegalUse,
31+
"Commands cannot be added to more than one CommandGroup");
32+
}
33+
return allUngrouped;
34+
}
35+
36+
bool CommandGroupBase::RequireUngrouped(
37+
std::initializer_list<Command*> commands) {
38+
bool allUngrouped = true;
39+
for (auto&& command : commands) {
40+
allUngrouped &= !command->IsGrouped();
41+
}
42+
if (!allUngrouped) {
43+
wpi_setGlobalWPIErrorWithContext(
44+
CommandIllegalUse,
45+
"Commands cannot be added to more than one CommandGroup");
46+
}
47+
return allUngrouped;
48+
}

0 commit comments

Comments
 (0)