Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
Add bindings for Mechanism2d et al
Browse files Browse the repository at this point in the history
- Fixes #695
  • Loading branch information
virtuald committed Jan 19, 2022
1 parent 1127ec8 commit f863c91
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
6 changes: 6 additions & 0 deletions gen/Mechanism2d.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
---

extra_includes:
- wpi/sendable/SendableBuilder.h

classes:
Mechanism2d:
ignored_bases:
- wpi::SendableHelper<Mechanism2d>
methods:
Mechanism2d:
GetRoot:
return_value_policy: reference_internal
SetBackgroundColor:
InitSendable:
1 change: 1 addition & 0 deletions gen/MechanismLigament2d.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ classes:
MechanismLigament2d:
methods:
MechanismLigament2d:
ignore: true
SetColor:
SetLength:
GetLength:
Expand Down
22 changes: 22 additions & 0 deletions gen/MechanismObject2d.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
---

extra_includes:
- frc/smartdashboard/MechanismLigament2d.h

classes:
MechanismObject2d:
force_type_casters:
- units::degree_t
attributes:
m_mutex:
ignore: true
methods:
GetName:
Append:
ignore: true
MechanismObject2d:
ignore: true
UpdateEntries:

# keep this in sync with MechanismRoot2d.yml
inline_code: |
cls_MechanismObject2d
.def("appendLigament", [](MechanismObject2d *self,
std::string_view name, double length, units::degree_t angle,
double lineWidth, const frc::Color8Bit& color) {
return self->Append<MechanismLigament2d>(name, length, angle, lineWidth, color);
},
py::arg("name"), py::arg("length"), py::arg("angle"),
py::arg("lineWidth") = 6, py::arg("color") = frc::Color8Bit{235, 137, 52},
"Append a ligament node",
py::return_value_policy::reference_internal)
;
21 changes: 21 additions & 0 deletions gen/MechanismRoot2d.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
---

extra_includes:
- frc/smartdashboard/MechanismLigament2d.h

classes:
MechanismRoot2d:
force_type_casters:
- units::degree_t
methods:
MechanismRoot2d:
ignore: true
SetPosition:

# keep this in sync with MechanismRoot2d.yml
inline_code: |
cls_MechanismRoot2d
.def("getName", [](MechanismRoot2d *self) { return self->GetName(); }, release_gil())
.def("appendLigament", [](MechanismRoot2d *self,
std::string_view name, double length, units::degree_t angle,
double lineWidth, const frc::Color8Bit& color) {
return self->Append<MechanismLigament2d>(name, length, angle, lineWidth, color);
},
py::arg("name"), py::arg("length"), py::arg("angle"),
py::arg("lineWidth") = 6, py::arg("color") = frc::Color8Bit{235, 137, 52},
release_gil(), "Append a ligament node",
py::return_value_policy::reference_internal)
;
9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,10 @@ VictorSP = "frc/motorcontrol/VictorSP.h"
Field2d = "frc/smartdashboard/Field2d.h"
FieldObject2d = "frc/smartdashboard/FieldObject2d.h"
# ListenerExecutor = "frc/smartdashboard/ListenerExecutor.h" # internal detail
# TODO: needs pybind magic
# Mechanism2d = "frc/smartdashboard/Mechanism2d.h"
# MechanismLigament2d = "frc/smartdashboard/MechanismLigament2d.h"
# MechanismObject2d = "frc/smartdashboard/MechanismObject2d.h"
# MechanismRoot2d = "frc/smartdashboard/MechanismRoot2d.h"
Mechanism2d = "frc/smartdashboard/Mechanism2d.h"
MechanismLigament2d = "frc/smartdashboard/MechanismLigament2d.h"
MechanismObject2d = "frc/smartdashboard/MechanismObject2d.h"
MechanismRoot2d = "frc/smartdashboard/MechanismRoot2d.h"
SendableBuilderImpl = "frc/smartdashboard/SendableBuilderImpl.h"
SendableChooser = "frc/smartdashboard/SendableChooser.h"
SendableChooserBase = "frc/smartdashboard/SendableChooserBase.h"
Expand Down
11 changes: 11 additions & 0 deletions tests/test_mechanism2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from wpilib import Mechanism2d


def test_create_mechanism():
m = Mechanism2d(100, 100)
r1 = m.getRoot("r1", 10, 10)
l1 = r1.appendLigament("l1", 4, 3)
l2 = l1.appendLigament("l2", 4, 3)
assert l2 is not None

# TODO... check that they do something?
8 changes: 8 additions & 0 deletions wpilib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
Jaguar,
Joystick,
LiveWindow,
Mechanism2d,
MechanismLigament2d,
MechanismObject2d,
MechanismRoot2d,
MotorControllerGroup,
MotorSafety,
NidecBrushless,
Expand Down Expand Up @@ -144,6 +148,10 @@
"Jaguar",
"Joystick",
"LiveWindow",
"Mechanism2d",
"MechanismLigament2d",
"MechanismObject2d",
"MechanismRoot2d",
"MotorControllerGroup",
"MotorSafety",
"NidecBrushless",
Expand Down

0 comments on commit f863c91

Please sign in to comment.