-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[trajoptlib] Add Python bindings using nanobind #848
Draft
spacey-sooty
wants to merge
1
commit into
SleipnirGroup:main
Choose a base branch
from
spacey-sooty:pythontrajoptlib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
for filename in sys.argv[1:]: | ||
src = filename | ||
dest = src.replace("linux_x86_64", "manylinux_2_35_x86_64").replace( | ||
"macosx_14_arm64", "macosx_14_0_arm64" | ||
) | ||
if src != dest: | ||
print(f"{src} -> {dest}") | ||
os.rename(src, dest) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
name: Build Python | ||
|
||
on: [pull_request, push] | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
build-wheel: | ||
timeout-minutes: 10 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- name: Windows x86_64 | ||
os: windows-2022 | ||
version: "3.9" | ||
cmake-env: | ||
- name: Windows x86_64 | ||
os: windows-2022 | ||
version: "3.10" | ||
cmake-env: | ||
- name: Windows x86_64 | ||
os: windows-2022 | ||
version: "3.11" | ||
cmake-env: | ||
- name: Windows x86_64 | ||
os: windows-2022 | ||
version: "3.12" | ||
cmake-env: | ||
- name: Linux x86_64 | ||
os: ubuntu-24.04 | ||
version: "3.9" | ||
cmake-env: | ||
- name: Linux x86_64 | ||
os: ubuntu-24.04 | ||
version: "3.10" | ||
cmake-env: | ||
- name: Linux x86_64 | ||
os: ubuntu-24.04 | ||
version: "3.11" | ||
cmake-env: | ||
- name: Linux x86_64 | ||
os: ubuntu-24.04 | ||
version: "3.12" | ||
cmake-env: | ||
- name: macOS universal | ||
os: macOS-14 | ||
version: "3.10" | ||
cmake-env: CMAKE_OSX_ARCHITECTURES="x86_64;arm64" | ||
- name: macOS universal | ||
os: macOS-14 | ||
version: "3.11" | ||
cmake-env: CMAKE_OSX_ARCHITECTURES="x86_64;arm64" | ||
- name: macOS universal | ||
os: macOS-14 | ||
version: "3.12" | ||
cmake-env: CMAKE_OSX_ARCHITECTURES="x86_64;arm64" | ||
|
||
name: ${{ matrix.version }} ${{ matrix.name }} wheel | ||
runs-on: ${{ matrix.os }} | ||
defaults: | ||
run: | ||
working-directory: ./trajoptlib | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Make GCC 14 the default toolchain (Linux) | ||
if: startsWith(matrix.os, 'ubuntu') | ||
run: | | ||
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 200 | ||
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 200 | ||
|
||
- run: sudo xcode-select -switch /Applications/Xcode_15.3.app | ||
if: startsWith(matrix.os, 'macOS') | ||
|
||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.version }} | ||
|
||
- run: pip install typing_extensions | ||
if: matrix.version == '3.9' || matrix.version == '3.10' | ||
|
||
- run: pip3 install build pytest | ||
- run: ${{ matrix.cmake-env }} python3 -m build --wheel | ||
- run: python3 ../.github/workflows/repair_wheel.py trajoptlib-*.whl | ||
if: startsWith(matrix.os, 'ubuntu') || startsWith(matrix.os, 'macOS') | ||
working-directory: dist | ||
- run: pip3 install dist/trajoptlib-*.whl | ||
shell: bash | ||
- run: pytest | ||
shell: bash | ||
|
||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: ${{ matrix.version }} ${{ matrix.name }} wheel | ||
path: dist | ||
|
||
build-sdist: | ||
timeout-minutes: 10 | ||
|
||
name: sdist | ||
runs-on: ubuntu-24.04 | ||
defaults: | ||
run: | ||
working-directory: ./trajoptlib | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.12 | ||
|
||
- run: pip3 install build | ||
- run: python3 -m build --sdist | ||
|
||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: sdist | ||
path: dist | ||
|
||
pypi-publish: | ||
name: Upload release to PyPI | ||
runs-on: ubuntu-24.04 | ||
defaults: | ||
run: | ||
working-directory: ./trajoptlib | ||
needs: [build-wheel, build-sdist] | ||
if: github.repository_owner == 'SleipnirGroup' && github.ref == 'refs/heads/main' | ||
environment: | ||
name: pypi | ||
url: https://pypi.org/p/trajoptlib | ||
permissions: | ||
id-token: write | ||
steps: | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
path: dist | ||
pattern: '* wheel' | ||
merge-multiple: true | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
path: dist | ||
pattern: 'sdist' | ||
merge-multiple: true | ||
- name: Publish package distributions to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,6 @@ CMakeUserPresets.json | |
|
||
build/ | ||
cli/ | ||
|
||
# python | ||
trajoptlib/.py-build-cmake_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function(pybind11_mkdoc target headers) | ||
find_package(Python3 REQUIRED COMPONENTS Interpreter) | ||
if(UNIX AND NOT APPLE) | ||
set(env_vars LLVM_DIR_PATH=/usr/lib LIBCLANG_PATH=/usr/lib/libclang.so) | ||
endif() | ||
|
||
get_target_property(target_dirs ${target} INCLUDE_DIRECTORIES) | ||
list(TRANSFORM target_dirs PREPEND "-I") | ||
|
||
get_target_property(eigen_dirs Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES) | ||
list(FILTER eigen_dirs INCLUDE REGEX "\\$<BUILD_INTERFACE:.*>") | ||
list(TRANSFORM eigen_dirs PREPEND "-I") | ||
|
||
add_custom_command( | ||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/jormungandr/cpp/Docstrings.hpp | ||
COMMAND | ||
${env_vars} ${Python3_EXECUTABLE} -m pybind11_mkdoc ${headers} -o | ||
${CMAKE_CURRENT_SOURCE_DIR}/jormungandr/cpp/Docstrings.hpp | ||
-I/usr/lib/clang/18/include ${target_dirs} ${eigen_dirs} -std=c++23 | ||
COMMAND | ||
${Python3_EXECUTABLE} | ||
${CMAKE_CURRENT_SOURCE_DIR}/cmake/fix_docstrings.py | ||
${CMAKE_CURRENT_SOURCE_DIR}/jormungandr/cpp/Docstrings.hpp | ||
DEPENDS ${headers} | ||
USES_TERMINAL | ||
) | ||
add_custom_target( | ||
${target}_docstrings | ||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jormungandr/cpp/Docstrings.hpp | ||
) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
A library to generate time optimal trajectories for FRC robots. | ||
""" | ||
|
||
from ._trajoptlib import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) TrajoptLib contributors | ||
|
||
#include <nanobind/nanobind.h> | ||
#include <nanobind/stl/function.h> | ||
|
||
#include <vector> | ||
|
||
#include <sleipnir/optimization/SolverStatus.hpp> | ||
#include <trajopt/SwerveTrajectoryGenerator.hpp> | ||
#include <trajopt/geometry/Translation2.hpp> | ||
|
||
namespace nb = nanobind; | ||
|
||
namespace trajopt { | ||
/* | ||
void BindSwerveDrivetrain(nb::class_<SwerveDrivetrain> cls); | ||
void BindSwerveSolution(nb::class_<SwerveSolution> cls); | ||
void BindSwerveTrajectorySample(nb::class_<SwerveTrajectorySample> cls); | ||
void BindSwerveTrajectory(nb::class_<SwerveTrajectory> cls); | ||
void BindSwervePath(nb::class_<SwervePath> cls); | ||
void BindSwervePathBuilder(nb::class_<SwervePathBuilder> cls); | ||
void BindSwerveTrajectoryGenerator(nb::class_<SwerveTrajectoryGenerator> cls); | ||
*/ | ||
|
||
void BindSwerveDrivetrain(nb::class_<SwerveDrivetrain>& cls) { | ||
using namespace nb::literals; | ||
|
||
cls.def(nb::init<double, double, double, double, double, | ||
std::vector<Translation2d>>(), | ||
"mass"_a, "moi"_a, "wheelRadius"_a, "wheelMaxAngularVelocity"_a, | ||
"wheelMaxTorque"_a, "modules"_a); | ||
cls.def_ro("mass", double, DOC(_trajoptlib, SwerveDrivetrain, double)); | ||
cls.def_ro("moi", double, DOC(_trajoptlib, SwerveDrivetrain, double)); | ||
cls.def_ro("wheelRadius", double, DOC(_trajoptlib, SwerveDrivetrain, double)); | ||
cls.def_ro("wheelMaxAngularVelocity", double, DOC(_trajoptlib, SwerveDrivetrain, double)); | ||
cls.def_ro("wheelMaxTorque", double, DOC(_trajoptlib, SwerveDrivetrain, double)); | ||
cls.def_ro("modules", std::vector<Translation2d>, DOC(_trajoptlib, SwerveDrivetrain, std::vector<Translation2d>)); | ||
} | ||
|
||
// void BindSwerveSolution(nb::class_<SwerveSolution>& cls) { | ||
// using namespace nb::literals; | ||
// | ||
// cls.def(nb::init< | ||
// std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, std::vector<double>, >(), | ||
// "dt"_a, "x"_a, "y"_a, "thetacos"_a, "thetasin"_a, "vx"_a, "vy"_a, | ||
// "omega"_a, "ax"_a, "ay"_a, "alpha"_a, "moduleFX"_a, "moduleFY"_a); | ||
// cls.def_ro("dt", std::vector<double>); | ||
// cls.def_ro("x", std::vector<double>); | ||
// cls.def_ro("y", std::vector<double>); | ||
// cls.def_ro("thetacos", std::vector<double>); | ||
// cls.def_ro("thetasin", std::vector<double>); | ||
// cls.def_ro("vx", std::vector<double>); | ||
// cls.def_ro("vy", std::vector<double>); | ||
// cls.def_ro("omega", std::vector<double>); | ||
// cls.def_ro("ax", std::vector<double>); | ||
// cls.def_ro("ay", std::vector<double>); | ||
// cls.def_ro("alpha", std::vector<double>); | ||
// cls.def_ro("moduleFX", std::vector<double>); | ||
// cls.def_ro("moduleFY", std::vector<double>); | ||
// } | ||
|
||
} // namespace trajopt |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python APIs should use snake case, not mixed case.