Skip to content

Commit 526697f

Browse files
committed
Initial python wrapper implementation
1 parent 89e3b98 commit 526697f

Some content is hidden

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

53 files changed

+1686
-0
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
*.py[ciod]
3+
*.egg-info
4+
5+
*.dll
6+
*.so
7+
*.dylib
8+
py.typed
9+
10+
/build
11+
/dist
12+
13+
/.vscode
14+
15+
/wpilib/buttons/rpy-include
16+
/wpilib/buttons/_init_commands_v1_buttons.py
17+
/wpilib/buttons/pkgcfg.py
18+
19+
/commands2/_init_impl.py
20+
/commands2/include
21+
/commands2/lib
22+
/commands2/rpy-include
23+
/commands2/pkgcfg.py
24+
/commands2/version.py

commands2/__init__.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from . import _init_impl
2+
3+
from .version import version as __version__
4+
5+
# autogenerated by 'robotpy-build create-imports commands2 commands2._impl'
6+
from ._impl import (
7+
Command,
8+
CommandBase,
9+
CommandGroupBase,
10+
CommandScheduler,
11+
CommandState,
12+
ConditionalCommand,
13+
FunctionalCommand,
14+
InstantCommand,
15+
MecanumControllerCommand,
16+
NotifierCommand,
17+
PIDCommand,
18+
PIDSubsystem,
19+
ParallelCommandGroup,
20+
ParallelDeadlineGroup,
21+
ParallelRaceGroup,
22+
PerpetualCommand,
23+
PrintCommand,
24+
ProfiledPIDCommand,
25+
ProfiledPIDSubsystem,
26+
ProxyScheduleCommand,
27+
RamseteCommand,
28+
RunCommand,
29+
ScheduleCommand,
30+
SequentialCommandGroup,
31+
StartEndCommand,
32+
Subsystem,
33+
SubsystemBase,
34+
Swerve2ControllerCommand,
35+
Swerve3ControllerCommand,
36+
Swerve4ControllerCommand,
37+
Swerve6ControllerCommand,
38+
TimedCommandRobot,
39+
TrapezoidProfileCommand,
40+
TrapezoidProfileCommandRadians,
41+
TrapezoidProfileSubsystem,
42+
TrapezoidProfileSubsystemRadians,
43+
Trigger,
44+
WaitCommand,
45+
WaitUntilCommand,
46+
# button,
47+
requirementsDisjoint,
48+
)
49+
50+
__all__ = [
51+
"Command",
52+
"CommandBase",
53+
"CommandGroupBase",
54+
"CommandScheduler",
55+
"CommandState",
56+
"ConditionalCommand",
57+
"FunctionalCommand",
58+
"InstantCommand",
59+
"MecanumControllerCommand",
60+
"NotifierCommand",
61+
"PIDCommand",
62+
"PIDSubsystem",
63+
"ParallelCommandGroup",
64+
"ParallelDeadlineGroup",
65+
"ParallelRaceGroup",
66+
"PerpetualCommand",
67+
"PrintCommand",
68+
"ProfiledPIDCommand",
69+
"ProfiledPIDSubsystem",
70+
"ProxyScheduleCommand",
71+
"RamseteCommand",
72+
"RunCommand",
73+
"ScheduleCommand",
74+
"SequentialCommandGroup",
75+
"StartEndCommand",
76+
"Subsystem",
77+
"SubsystemBase",
78+
"Swerve2ControllerCommand",
79+
"Swerve3ControllerCommand",
80+
"Swerve4ControllerCommand",
81+
"Swerve6ControllerCommand",
82+
"TimedCommandRobot",
83+
"TrapezoidProfileCommand",
84+
"TrapezoidProfileCommandRadians",
85+
"TrapezoidProfileSubsystem",
86+
"TrapezoidProfileSubsystemRadians",
87+
"Trigger",
88+
"WaitCommand",
89+
"WaitUntilCommand",
90+
# "button",
91+
"requirementsDisjoint",
92+
]

commands2/button/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# autogenerated by 'robotpy-build create-imports commands2.button commands2._impl.button'
2+
from .._impl.button import Button, JoystickButton, NetworkButton, POVButton
3+
4+
__all__ = ["Button", "JoystickButton", "NetworkButton", "POVButton"]

commands2/src/Command.cpp.inl

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
// Decorators taken from Java
3+
4+
#define DECORATOR_NOTE \
5+
"\n" \
6+
"Note: This decorator works by composing this command within a CommandGroup. The command\n" \
7+
"cannot be used independently after being decorated, or be re-decorated with a different\n" \
8+
"decorator, unless it is manually cleared from the list of grouped commands with Command.setGrouped(False)\n" \
9+
"The decorated command can, however, be further decorated without issue\n"
10+
11+
12+
cls_Command
13+
.def("andThen",
14+
[](Command* self, py::args cmds) {
15+
std::vector<std::shared_ptr<Command>> commands;
16+
commands.emplace_back(self->shared_from_this());
17+
for (auto cmd : cmds) {
18+
auto cmdptr = py::cast<std::shared_ptr<Command>>(cmd);
19+
commands.emplace_back(cmdptr);
20+
}
21+
return std::make_shared<SequentialCommandGroup>(std::move(commands));
22+
},
23+
"Decorates this command with a set of commands to run after it in sequence. Often more\n"
24+
"convenient/less-verbose than constructing a new {@link SequentialCommandGroup} explicitly.\n"
25+
DECORATOR_NOTE)
26+
.def("alongWith",
27+
[](Command* self, py::args cmds) {
28+
std::vector<std::shared_ptr<Command>> commands;
29+
commands.emplace_back(self->shared_from_this());
30+
for (auto cmd : cmds) {
31+
auto cmdptr = py::cast<std::shared_ptr<Command>>(cmd);
32+
commands.emplace_back(cmdptr);
33+
}
34+
return std::make_shared<ParallelCommandGroup>(std::move(commands));
35+
},
36+
"Decorates this command with a set of commands to run parallel to it, "
37+
"ending when the last\n"
38+
"command ends. Often more convenient/less-verbose than constructing a new\n"
39+
"ParallelCommandGroup explicitly.\n"
40+
DECORATOR_NOTE)
41+
.def("deadlineWith",
42+
[](Command* self, py::args cmds) {
43+
return std::make_shared<ParallelDeadlineGroup>(
44+
self->shared_from_this(), std::move(pyargs2cmdList(cmds)));
45+
},
46+
"Decorates this command with a set of commands to run parallel to it, ending when the calling\n"
47+
"command ends and interrupting all the others. Often more convenient/less-verbose than\n"
48+
"constructing a new {@link ParallelDeadlineGroup} explicitly.\n"
49+
DECORATOR_NOTE)
50+
.def("raceWith",
51+
[](Command* self, py::args cmds) {
52+
std::vector<std::shared_ptr<Command>> commands;
53+
commands.emplace_back(self->shared_from_this());
54+
for (auto cmd : cmds) {
55+
auto cmdptr = py::cast<std::shared_ptr<Command>>(cmd);
56+
commands.emplace_back(cmdptr);
57+
}
58+
return std::make_shared<ParallelRaceGroup>(std::move(commands));
59+
},
60+
"Decorates this command with a set of commands to run parallel to it, ending when the first\n"
61+
"command ends. Often more convenient/less-verbose than constructing a new\n"
62+
"ParallelRaceGroup explicitly.\n"
63+
DECORATOR_NOTE)
64+
;
65+

commands2/src/TimedCommandRobot.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#pragma once
3+
4+
#include <frc/TimedRobot.h>
5+
#include <frc2/command/CommandScheduler.h>
6+
7+
namespace frc2 {
8+
9+
/**
10+
* TimedCommandRobot implements the IterativeRobotBase robot program framework.
11+
*
12+
* The TimedCommandRobot class is intended to be subclassed by a user creating a
13+
* command-based robot program. This python-specific class calls the
14+
* CommandScheduler run method in robotPeriodic for you.
15+
*/
16+
class TimedCommandRobot : public frc::TimedRobot {
17+
public:
18+
/** Ensures commands are run */
19+
void RobotPeriodic() override { CommandScheduler::GetInstance().Run(); }
20+
};
21+
22+
} // namespace frc2

commands2/src/Trigger.cpp.inl

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cls_Trigger
2+
.def("and_", [](Trigger * self, Trigger * other) {
3+
return *self && *other;
4+
}, py::arg("other"),
5+
"Composes this trigger with another trigger, returning a new trigger that is active when both\n"
6+
"triggers are active.\n"
7+
"\n"
8+
":param trigger: the trigger to compose with\n"
9+
"\n"
10+
":returns: the trigger that is active when both triggers are active\n")
11+
.def("or_", [](Trigger * self, Trigger * other) {
12+
return *self || *other;
13+
}, py::arg("other"),
14+
"Composes this trigger with another trigger, returning a new trigger that is active when either\n"
15+
"triggers are active.\n"
16+
"\n"
17+
":param trigger: the trigger to compose with\n"
18+
"\n"
19+
":returns: the trigger that is active when both triggers are active\n")
20+
.def("not_", [](Trigger * self) {
21+
return !*self;
22+
},
23+
"Creates a new trigger that is active when this trigger is inactive, i.e. that acts as the\n"
24+
"negation of this trigger.\n"
25+
"\n"
26+
":param trigger: the trigger to compose with\n"
27+
"\n"
28+
":returns: the trigger that is active when both triggers are active\n");

commands2/src/cpp/frc2/command/CommandScheduler.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ CommandScheduler& CommandScheduler::GetInstance() {
9595
return scheduler;
9696
}
9797

98+
void CommandScheduler::ResetInstance() {
99+
CommandScheduler& instance = GetInstance();
100+
std::make_unique<Impl>().swap(instance.m_impl);
101+
}
102+
98103
void CommandScheduler::SetPeriod(units::second_t period) {
99104
m_watchdog.SetTimeout(period);
100105
}

commands2/src/helpers.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#include "helpers.h"
3+
4+
std::vector<std::shared_ptr<frc2::Command>> pyargs2cmdList(py::args cmds) {
5+
std::vector<std::shared_ptr<frc2::Command>> commands;
6+
for (auto cmd : cmds) {
7+
commands.emplace_back(py::cast<std::shared_ptr<frc2::Command>>(cmd));
8+
}
9+
return commands;
10+
}
11+
12+
std::vector<std::shared_ptr<frc2::Subsystem>> pyargs2SharedSubsystemList(py::args subs) {
13+
std::vector<std::shared_ptr<frc2::Subsystem>> subsystems;
14+
for (auto sub : subs) {
15+
subsystems.emplace_back(py::cast<std::shared_ptr<frc2::Subsystem>>(sub));
16+
}
17+
return subsystems;
18+
}
19+
20+
std::vector<frc2::Subsystem*> pyargs2SubsystemList(py::args subs) {
21+
std::vector<frc2::Subsystem*> subsystems;
22+
for (auto sub : subs) {
23+
subsystems.emplace_back(py::cast<frc2::Subsystem*>(sub));
24+
}
25+
return subsystems;
26+
}

commands2/src/helpers.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#pragma once
3+
#include <robotpy_build.h>
4+
#include <frc2/command/Command.h>
5+
#include <frc2/command/Subsystem.h>
6+
7+
std::vector<std::shared_ptr<frc2::Command>> pyargs2cmdList(py::args cmds);
8+
9+
std::vector<std::shared_ptr<frc2::Subsystem>> pyargs2SharedSubsystemList(py::args subs);
10+
11+
std::vector<frc2::Subsystem*> pyargs2SubsystemList(py::args subs);

commands2/src/include/frc2/command/CommandScheduler.h

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class CommandScheduler final : public frc::Sendable,
3939
*/
4040
static CommandScheduler& GetInstance();
4141

42+
/**
43+
* python-specific: clear the global command scheduler instance
44+
*/
45+
static void ResetInstance();
46+
4247
~CommandScheduler() override;
4348
CommandScheduler(const CommandScheduler&) = delete;
4449
CommandScheduler& operator=(const CommandScheduler&) = delete;

commands2/src/main.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "rpygen_wrapper.hpp"
2+
3+
RPYBUILD_PYBIND11_MODULE(m)
4+
{
5+
initWrapper(m);
6+
}

gen/Button.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
3+
extra_includes:
4+
- frc2/command/Command.h
5+
- frc2/command/Subsystem.h
6+
7+
classes:
8+
Button:
9+
shared_ptr: true
10+
force_no_trampoline: true
11+
subpackage: button
12+
methods:
13+
Button:
14+
overloads:
15+
std::function<bool ( )>:
16+
"":
17+
WhenPressed:
18+
overloads:
19+
std::shared_ptr<Command>, bool:
20+
T&&, bool:
21+
ignore: true
22+
std::function<void ( )>, std::initializer_list<std::shared_ptr<Subsystem>>:
23+
ignore: true
24+
std::function<void ( )>, wpi::ArrayRef<std::shared_ptr<Subsystem>>:
25+
WhileHeld:
26+
overloads:
27+
std::shared_ptr<Command>, bool:
28+
T&&, bool:
29+
ignore: true
30+
std::function<void ( )>, std::initializer_list<std::shared_ptr<Subsystem>>:
31+
ignore: true
32+
std::function<void ( )>, wpi::ArrayRef<std::shared_ptr<Subsystem>>:
33+
WhenHeld:
34+
overloads:
35+
std::shared_ptr<Command>, bool:
36+
T&&, bool:
37+
ignore: true
38+
WhenReleased:
39+
overloads:
40+
std::shared_ptr<Command>, bool:
41+
T&&, bool:
42+
ignore: true
43+
std::function<void ( )>, std::initializer_list<std::shared_ptr<Subsystem>>:
44+
ignore: true
45+
std::function<void ( )>, wpi::ArrayRef<std::shared_ptr<Subsystem>>:
46+
ToggleWhenPressed:
47+
overloads:
48+
std::shared_ptr<Command>, bool:
49+
T&&, bool:
50+
ignore: true
51+
CancelWhenPressed:

0 commit comments

Comments
 (0)