Skip to content

Commit

Permalink
whoops
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Sep 26, 2024
1 parent cddd3c6 commit 8e0c0e9
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 116 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: "photonlibpy-stubgen"
path: ./photon-lib/py/photonlibpy/*.pyi
path: ./photon-lib/py/photonlibpy/lib/*.pyi


build-host:
Expand All @@ -76,11 +76,8 @@ jobs:
# Reference: https://github.com/wpilibsuite/wpilib-tool-plugin/blob/main/src/main/java/edu/wpi/first/tools/NativePlatforms.java#L23
os:
- name: "ubuntu-22.04"
native_build_task: "Linuxx86-64"
- name: "macos-12"
native_build_task: "Osxuniversal"
- name: "windows-2022"
native_build_task: "Windowsx86-64"
python_version:
# - '3.8'
# - '3.9'
Expand Down Expand Up @@ -114,7 +111,7 @@ jobs:
- run: ./gradlew photon-lib:tasks -i -PpythonExecutable=python${{ matrix.python_version }}
- run: |
chmod +x gradlew
./gradlew photon-lib:installPhotonlibpyNative -PpythonExecutable=python${{ matrix.python_version }} -PnativeBuildPlatform=${{ matrix.os.native_build_task }}
./gradlew photon-lib:installPhotonlibpyNative -PpythonExecutable=python${{ matrix.python_version }}
- name: Install dependencies
working-directory: ./photon-lib/py
Expand All @@ -126,7 +123,7 @@ jobs:
- uses: actions/download-artifact@v4
with:
name: "photonlibpy-stubgen"
path: ./photon-lib/py/photonlibpy/*.pyi
path: ./photon-lib/py/photonlibpy/lib/*.pyi

- name: Build wheel
working-directory: ./photon-lib/py
Expand Down Expand Up @@ -168,7 +165,7 @@ jobs:
name: roborio
build-options: "-PArchOverride=linuxathena"
# gradle needs first letter uppercase
arch: "Linuxathena"
# arch: "Linuxathena"

# - container: wpilib/raspbian-cross-ubuntu:bullseye-22.04-py38
# name: raspbian-py38
Expand All @@ -188,7 +185,7 @@ jobs:
- container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py310
name: raspbian-aarch64-py310
build-options: "-PArchOverride=linuxarm64"
arch: "Linuxarm64"
# arch: "Linuxarm64"
# - container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py311
# name: raspbian-aarch64-py311
# - container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py312
Expand Down Expand Up @@ -243,7 +240,7 @@ jobs:
- uses: actions/download-artifact@v4
with:
name: "photonlibpy-stubgen"
path: ./photon-lib/py/photonlibpy/*.pyi
path: ./photon-lib/py/photonlibpy/lib/*.pyi

- name: Build wheel
working-directory: ./photon-lib/py
Expand Down
2 changes: 1 addition & 1 deletion photon-lib/py/create_photonlib_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def write_stubgen():
# From nanobind==2.1.0
from nanobind.stubgen import StubGen

# this is legal since we import everything from this shared library in __init__.py
# this is legal since we import everything from this shared library in __init__.py
# but still isn't /great/, lol
from photonlibpy.lib import _photonlibpy as _pp

Expand Down
1 change: 1 addition & 0 deletions photon-lib/py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def finalize_options(self):
print("Generating typehints")
try:
from create_photonlib_pyi import write_stubgen

write_stubgen()
except Exception as e:
print(e)
Expand Down
104 changes: 1 addition & 103 deletions photon-lib/src/main/pybindings/cpp/photonlib_nanobind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,110 +22,8 @@
* SOFTWARE.
*/

#include <fmt/format.h>

#include <memory>

#include "photon/PhotonCamera.h"
#include "photon/simulation/VisionSystemSim.h"

// actual nanobind include
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/optional.h>

namespace nb = nanobind;
using namespace nb::literals;

// repr helper
template <> struct fmt::formatter<photon::PhotonTrackedTarget> : formatter<string_view> {
auto format(photon::PhotonTrackedTarget const &c, format_context &ctx) const {
return fmt::format_to(ctx.out(), "PhotonTrackedTarget<yaw={},pitch={}>", c.yaw, c.pitch);
}
};
template <> struct fmt::formatter<photon::PhotonPipelineMetadata> : formatter<string_view> {
auto format(photon::PhotonPipelineMetadata const &c, format_context &ctx) const {
return fmt::format_to(ctx.out(), "PhotonPipelineMetadata<sequenceID={}>", c.sequenceID);
}
};

frc::Pose3d makePose(double x, double y, double z, double W, double X, double Y,
double Z) {
return frc::Pose3d{frc::Translation3d{units::meter_t{x}, units::meter_t{y},
units::meter_t{z}},
frc::Rotation3d{frc::Quaternion{W, X, Y, Z}}};
}

void wrap_geom(nb::module_ m) {
using namespace frc;
nb::class_<Transform3d>(m, "Transform3d").def(nb::init<>());
nb::class_<Pose3d>(m, "Pose3d")
.def(nb::init<>())
.def(nb::new_(&makePose),
"Create a Pose3d from translation/rotation components");
}

void wrap_photon(nb::module_ m) {
nb::class_<photon::PhotonPipelineMetadata>(m, "PhotonPipelineMetadata")
.def_ro("sequenceID", &photon::PhotonPipelineMetadata::sequenceID)
.def_ro("captureTimestampMicros",
&photon::PhotonPipelineMetadata::captureTimestampMicros)
.def_ro("publishTimestampMicros",
&photon::PhotonPipelineMetadata::publishTimestampMicros);

nb::class_<photon::PhotonTrackedTarget>(m, "PhotonTrackedTarget")
.def_ro("yaw", &photon::PhotonTrackedTarget::yaw)
.def_ro("pitch", &photon::PhotonTrackedTarget::pitch)
// String representation
.def("__repr__", [](const photon::PhotonTrackedTarget& t) {
std::string s;
fmt::format_to(std::back_inserter(s),
"{}>", t);
return s;
});
nb::class_<photon::MultiTargetPNPResult>(m, "MultiTargetPNPResult")
.def_ro("fiducialIDsUsed",
&photon::MultiTargetPNPResult::fiducialIDsUsed);

nb::class_<photon::PhotonPipelineResult>(m, "PhotonPipelineResult")
.def_ro("metadata", &photon::PhotonPipelineResult::metadata)
.def_ro("targets", &photon::PhotonPipelineResult::targets)
.def_ro("multitagResult", &photon::PhotonPipelineResult::multitagResult)
.def("__repr__", [](const photon::PhotonPipelineResult& t) {
std::string s;
fmt::format_to(std::back_inserter(s),
"PhotonPipelineResult<metadata={},targets=[{}]>", t.metadata, fmt::join(t.targets, ", "));
return s;
});

nb::class_<photon::PhotonCamera>(m, "PhotonCamera")
.def(nb::init<const std::string&>())
.def("GetDriverMode", &photon::PhotonCamera::GetDriverMode)
.def("GetLatestResult", &photon::PhotonCamera::GetLatestResult);
}

void wrap_photon_sim(nb::module_ m) {
using namespace photon;

nb::class_<VisionSystemSim>(m, "VisionSystemSim")
.def(nb::init<const std::string&>())
.def("AddCamera", &VisionSystemSim::AddCamera)
.def("AddVisionTargets",
nb::overload_cast<std::string, const std::vector<VisionTargetSim>&>(
&VisionSystemSim::AddVisionTargets))
.def("Update",
nb::overload_cast<const frc::Pose3d&>(&VisionSystemSim::Update));

nb::class_<PhotonCameraSim>(m, "PhotonCameraSim")
.def(nb::init<PhotonCamera*>());

nb::class_<VisionTargetSim>(m, "VisionTargetSim")
.def(nb::init<frc::Pose3d, TargetModel>(), "pose"_a, "model"_a, "Create a simulated target at a given pose")
.def(nb::init<frc::Pose3d, TargetModel, int>(), "pose"_a, "model"_a, "fiducial_id"_a, "Create a simulated AprilTag at a given pose");

nb::class_<TargetModel>(m, "TargetModel").def(nb::init<units::meter_t, units::meter_t>());
}
#include "photonlib_nanobind.hpp"

NB_MODULE(_photonlibpy, m) {
m.doc() = "C++ bindings for photonlib";
Expand Down
29 changes: 29 additions & 0 deletions photon-lib/src/main/pybindings/cpp/photonlib_nanobind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) PhotonVision
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <nanobind/nanobind.h>

void wrap_geom(nanobind::module_ m);
void wrap_photon(nanobind::module_ m);
void wrap_photon_sim(nanobind::module_ m);
56 changes: 56 additions & 0 deletions photon-lib/src/main/pybindings/cpp/wrap_geom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* MIT License
*
* Copyright (c) PhotonVision
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <fmt/format.h>

#include <memory>

#include "photon/PhotonCamera.h"
#include "photon/simulation/VisionSystemSim.h"
#include "photonlib_nanobind.hpp"

// actual nanobind include
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/optional.h>

namespace nb = nanobind;
using namespace nb::literals;

frc::Pose3d makePose(double x, double y, double z, double W, double X, double Y,
double Z) {
return frc::Pose3d{frc::Translation3d{units::meter_t{x}, units::meter_t{y},
units::meter_t{z}},
frc::Rotation3d{frc::Quaternion{W, X, Y, Z}}};
}

void wrap_geom(nb::module_ m) {
using namespace frc;
nb::class_<Transform3d>(m, "Transform3d").def(nb::init<>());
nb::class_<Pose3d>(m, "Pose3d")
.def(nb::init<>())
.def(nb::new_(&makePose),
"Create a Pose3d from translation/rotation components");
}
91 changes: 91 additions & 0 deletions photon-lib/src/main/pybindings/cpp/wrap_photonlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* MIT License
*
* Copyright (c) PhotonVision
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <fmt/format.h>

#include <memory>

#include "photon/PhotonCamera.h"
#include "photon/simulation/VisionSystemSim.h"
#include "photonlib_nanobind.hpp"

// actual nanobind include
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/optional.h>

namespace nb = nanobind;
using namespace nb::literals;

// repr helper
template <> struct fmt::formatter<photon::PhotonTrackedTarget> : formatter<string_view> {
auto format(photon::PhotonTrackedTarget const &c, format_context &ctx) const {
return fmt::format_to(ctx.out(), "PhotonTrackedTarget<yaw={},pitch={}>", c.yaw, c.pitch);
}
};
template <> struct fmt::formatter<photon::PhotonPipelineMetadata> : formatter<string_view> {
auto format(photon::PhotonPipelineMetadata const &c, format_context &ctx) const {
return fmt::format_to(ctx.out(), "PhotonPipelineMetadata<sequenceID={}>", c.sequenceID);
}
};

void wrap_photon(nb::module_ m) {
nb::class_<photon::PhotonPipelineMetadata>(m, "PhotonPipelineMetadata")
.def_ro("sequenceID", &photon::PhotonPipelineMetadata::sequenceID)
.def_ro("captureTimestampMicros",
&photon::PhotonPipelineMetadata::captureTimestampMicros)
.def_ro("publishTimestampMicros",
&photon::PhotonPipelineMetadata::publishTimestampMicros);

nb::class_<photon::PhotonTrackedTarget>(m, "PhotonTrackedTarget")
.def_ro("yaw", &photon::PhotonTrackedTarget::yaw)
.def_ro("pitch", &photon::PhotonTrackedTarget::pitch)
// String representation
.def("__repr__", [](const photon::PhotonTrackedTarget& t) {
std::string s;
fmt::format_to(std::back_inserter(s),
"{}>", t);
return s;
});
nb::class_<photon::MultiTargetPNPResult>(m, "MultiTargetPNPResult")
.def_ro("fiducialIDsUsed",
&photon::MultiTargetPNPResult::fiducialIDsUsed);

nb::class_<photon::PhotonPipelineResult>(m, "PhotonPipelineResult")
.def_ro("metadata", &photon::PhotonPipelineResult::metadata)
.def_ro("targets", &photon::PhotonPipelineResult::targets)
.def_ro("multitagResult", &photon::PhotonPipelineResult::multitagResult)
.def("__repr__", [](const photon::PhotonPipelineResult& t) {
std::string s;
fmt::format_to(std::back_inserter(s),
"PhotonPipelineResult<metadata={},targets=[{}]>", t.metadata, fmt::join(t.targets, ", "));
return s;
});

nb::class_<photon::PhotonCamera>(m, "PhotonCamera")
.def(nb::init<const std::string&>())
.def("GetDriverMode", &photon::PhotonCamera::GetDriverMode)
.def("GetLatestResult", &photon::PhotonCamera::GetLatestResult);
}
Loading

0 comments on commit 8e0c0e9

Please sign in to comment.