Skip to content

Commit

Permalink
Translation3d copy hack
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Sep 23, 2024
1 parent 940d2e8 commit 8689761
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
12 changes: 9 additions & 3 deletions photon-lib/py/create_photonlib_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ def write_stubgen():
# f.write(sg.get())


# mess with argv
# mess with argv (and put it back when we're done)
import sys
old_argv = sys.argv
sys.argv = [
"<dummy>",
"--exit-code",
Expand All @@ -29,8 +30,13 @@ def write_stubgen():
"photonlibpy",
]

import pybind11_stubgen
pybind11_stubgen.main()
try:
import pybind11_stubgen
pybind11_stubgen.main()
except Exception as e:
print(e)

sys.argv = old_argv


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion photon-lib/py/photonlibpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ from __future__ import annotations
import hal as hal
import ntcore as ntcore
from photonlibpy._photonlibpy import PhotonPipelineMetadata
from photonlibpy._photonlibpy import Translation3d
from photonlibpy._photonlibpy import print_t
import platform as platform
import robotpy_apriltag as robotpy_apriltag
import wpilib as wpilib
import wpimath as wpimath
import wpinet as wpinet
import wpiutil as wpiutil
from . import _photonlibpy
__all__ = ['PhotonPipelineMetadata', 'hal', 'ntcore', 'platform', 'robotpy_apriltag', 'wpilib', 'wpimath', 'wpinet', 'wpiutil']
__all__ = ['PhotonPipelineMetadata', 'Translation3d', 'hal', 'ntcore', 'platform', 'print_t', 'robotpy_apriltag', 'wpilib', 'wpimath', 'wpinet', 'wpiutil']
11 changes: 10 additions & 1 deletion photon-lib/py/photonlibpy/_photonlibpy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
C++ bindings for photonlib
"""
from __future__ import annotations
__all__ = ['PhotonPipelineMetadata']
__all__ = ['PhotonPipelineMetadata', 'Translation3d', 'print_t']
class PhotonPipelineMetadata:
def __init__(self) -> None:
...
@property
def captureTimestampMicros(self) -> int:
...
Expand All @@ -13,3 +15,10 @@ class PhotonPipelineMetadata:
@property
def sequenceID(self) -> int:
...
class Translation3d:
def __init__(self, arg0: float, arg1: float, arg2: float) -> None:
...
def print_t(arg0: Translation3d) -> None:
"""
Print a frc Translation3d
"""
16 changes: 8 additions & 8 deletions photon-lib/py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ def finalize_options(self):
self.root_is_pure = False


# script_path = os.path.dirname(os.path.realpath(__file__))
# if not os.path.exists(f"{script_path}/photonlibpy/_photonlibpy.pyi"):
# print("Generating typehints")
# # try:
# # from create_photonlib_pyi import write_stubgen
# # write_stubgen()
# # except Exception as e:
# # print(e)
script_path = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(f"{script_path}/photonlibpy/_photonlibpy.pyi"):
print("Generating typehints")
try:
from create_photonlib_pyi import write_stubgen
write_stubgen()
except Exception as e:
print(e)

setup(
name="photonlibpy",
Expand Down
17 changes: 17 additions & 0 deletions photon-lib/src/main/pybindings/cpp/photonlib_pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,34 @@

#include <fmt/format.h>
#include "photon/PhotonCamera.h"
#include <frc/geometry/Translation3d.h>

void print_t(frc::Translation3d t) {
fmt::println("x {} y {} z {}", t.X(), t.Y(), t.Z());
}

// actual nanobind include
#include <pybind11/pybind11.h>


namespace py = pybind11;

PYBIND11_MODULE(_photonlibpy, m) {

m.doc() = "C++ bindings for photonlib";

// hack -- is there any way to pull this from robotpy instead?
py::class_<frc::Translation3d>(m, "Translation3d")
.def(py::init([](double x, double y, double z) { return frc::Translation3d{
units::meter_t{x},
units::meter_t{y},
units::meter_t{z},
}; }));

m.def("print_t", &print_t, "Print a frc Translation3d");

py::class_<photon::PhotonPipelineMetadata>(m, "PhotonPipelineMetadata")
.def(py::init<>())
.def_readonly("sequenceID", &photon::PhotonPipelineMetadata::sequenceID)
.def_readonly("captureTimestampMicros",
&photon::PhotonPipelineMetadata::captureTimestampMicros)
Expand Down

0 comments on commit 8689761

Please sign in to comment.