-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add Python bindings for human state-related data structures #379
Merged
S-Dafarra
merged 5 commits into
robotology:master
from
carloscp3009:pybindings_humanState
Aug 2, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d217ef9
Writting the initial code to generate the HumanState msg
carloscp3009 fe39c85
Add pybindings for joint positions
carloscp3009 e8db019
Python bindings for HumanState msgs added
carloscp3009 31cb417
Apply suggestions from code review
carloscp3009 f2b1f75
Removing cache files
carloscp3009 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,38 @@ | ||
# This is the CMakeCache file. | ||
# For build in directory: /home/ittbmp014lw003/robotology-superbuild/src/HumanDynamicsEstimation | ||
# It was generated by CMake: /usr/bin/cmake | ||
# You can edit this file to change values found and used by cmake. | ||
# If you do not want to change any of the values, simply exit the editor. | ||
# If you do want to change a value, simply edit, save, and exit the editor. | ||
# The syntax for the file is as follows: | ||
# KEY:TYPE=VALUE | ||
# KEY is the name of a variable in the cache. | ||
# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT TYPE!. | ||
# VALUE is the current value for the KEY. | ||
|
||
######################## | ||
# EXTERNAL cache entries | ||
######################## | ||
|
||
|
||
######################## | ||
# INTERNAL cache entries | ||
######################## | ||
|
||
//This is the directory where this CMakeCache.txt was created | ||
CMAKE_CACHEFILE_DIR:INTERNAL=/home/ittbmp014lw003/robotology-superbuild/src/HumanDynamicsEstimation | ||
//Major version of cmake used to create the current loaded cache | ||
CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3 | ||
//Minor version of cmake used to create the current loaded cache | ||
CMAKE_CACHE_MINOR_VERSION:INTERNAL=22 | ||
//Patch version of cmake used to create the current loaded cache | ||
CMAKE_CACHE_PATCH_VERSION:INTERNAL=1 | ||
//Path to CMake executable. | ||
CMAKE_COMMAND:INTERNAL=/usr/bin/cmake | ||
//Path to cpack program executable. | ||
CMAKE_CPACK_COMMAND:INTERNAL=/usr/bin/cpack | ||
//Path to ctest program executable. | ||
CMAKE_CTEST_COMMAND:INTERNAL=/usr/bin/ctest | ||
//Path to CMake installation. | ||
CMAKE_ROOT:INTERNAL=/usr/share/cmake-3.22 | ||
|
||
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 @@ | ||||||
# This file is generated by cmake for dependency checking of the CMakeCache.txt file | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
As CMakeCache.txt . |
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,69 @@ | ||
# SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
if(HDE_COMPILE_PYTHON_BINDINGS) | ||
|
||
find_package(Python3 3.6 COMPONENTS Interpreter Development REQUIRED) | ||
|
||
find_package(pybind11 2.4.3 CONFIG REQUIRED) | ||
|
||
set(NEW_LINE "\n") | ||
|
||
option(HDE_DETECT_ACTIVE_PYTHON_SITEPACKAGES | ||
"Do you want HDE to detect and use the active site-package directory? (it could be a system dir)" | ||
FALSE) | ||
|
||
# Install the resulting Python package for the active interpreter | ||
if(NOT DEFINED HDE_PYTHON_INSTALL_DIR) | ||
if(HDE_DETECT_ACTIVE_PYTHON_SITEPACKAGES) | ||
set(HDE_PYTHON_INSTALL_DIR ${Python3_SITELIB}) | ||
else() | ||
execute_process(COMMAND ${Python3_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(1,0,prefix=''))" | ||
OUTPUT_VARIABLE _PYTHON_INSTDIR) | ||
string(STRIP ${_PYTHON_INSTDIR} _PYTHON_INSTDIR_CLEAN) | ||
set(HDE_PYTHON_INSTALL_DIR ${_PYTHON_INSTDIR_CLEAN}) | ||
endif() | ||
endif() | ||
set(PYTHON_INSTDIR ${HDE_PYTHON_INSTALL_DIR}/hde) | ||
|
||
# Folder of the Python package within the build tree. | ||
# It is used for the Python tests. | ||
set(HDE_PYTHON_PACKAGE "${CMAKE_BINARY_DIR}/hde") | ||
|
||
# Add the bindings directory | ||
add_subdirectory(python) | ||
|
||
# Create the __init__.py file | ||
file(GENERATE | ||
OUTPUT "${HDE_PYTHON_PACKAGE}/__init__.py" | ||
CONTENT "from .bindings import *${NEW_LINE}") | ||
|
||
# Install the __init__.py file | ||
install(FILES "${HDE_PYTHON_PACKAGE}/__init__.py" | ||
DESTINATION ${PYTHON_INSTDIR}) | ||
|
||
# Install pip metadata files to ensure that HDE installed via CMake is listed by pip list | ||
# See https://packaging.python.org/specifications/recording-installed-packages/ | ||
# and https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata | ||
option(HDE_PYTHON_PIP_METADATA_INSTALL "Use CMake to install Python pip metadata. Set to off if some other tool already installs it." ON) | ||
mark_as_advanced(HDE_PYTHON_PIP_METADATA_INSTALL) | ||
set(HDE_PYTHON_PIP_METADATA_INSTALLER "cmake" CACHE STRING "Specify the string to identify the pip Installer. Default: cmake, change this if you are using another tool.") | ||
mark_as_advanced(HDE_PYTHON_PIP_METADATA_INSTALLER) | ||
if(HDE_PYTHON_PIP_METADATA_INSTALL) | ||
get_filename_component(PYTHON_METADATA_PARENT_DIR ${PYTHON_INSTDIR} DIRECTORY) | ||
if(WIN32) | ||
set(NEW_LINE "\n\r") | ||
else() | ||
set(NEW_LINE "\n") | ||
endif() | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/METADATA "") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Metadata-Version: 2.1${NEW_LINE}") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Name: hde${NEW_LINE}") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Version: ${hde_VERSION}${NEW_LINE}") | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/INSTALLER "${HDE_PYTHON_PIP_METADATA_INSTALLER}${NEW_LINE}") | ||
install( | ||
FILES "${CMAKE_CURRENT_BINARY_DIR}/METADATA" "${CMAKE_CURRENT_BINARY_DIR}/INSTALLER" | ||
DESTINATION ${PYTHON_METADATA_PARENT_DIR}/hde-${hde_VERSION}.dist-info) | ||
endif() | ||
|
||
endif() |
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,34 @@ | ||
# SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
add_subdirectory(msgs) | ||
|
||
get_property(pybind_headers GLOBAL PROPERTY pybind_headers) | ||
get_property(pybind_sources GLOBAL PROPERTY pybind_sources) | ||
get_property(pybind_include_dirs GLOBAL PROPERTY pybind_include_dirs) | ||
get_property(pybind_link_libraries GLOBAL PROPERTY pybind_link_libraries) | ||
|
||
pybind11_add_module(pybind11_hde MODULE | ||
hde.cpp | ||
${pybind_sources} | ||
${pybind_headers} | ||
) | ||
|
||
target_include_directories(pybind11_hde PUBLIC "$<BUILD_INTERFACE:${pybind_include_dirs}>") | ||
|
||
target_link_libraries(pybind11_hde PRIVATE | ||
${pybind_link_libraries}) | ||
|
||
# # The generated Python dynamic module must have the same name as the pybind11 | ||
# # module, i.e. `bindings`. | ||
set_target_properties(pybind11_hde PROPERTIES | ||
LIBRARY_OUTPUT_DIRECTORY "${HDE_PYTHON_PACKAGE}" | ||
OUTPUT_NAME "bindings") | ||
|
||
# Output package is: | ||
# | ||
# bipedal_locomotion | ||
carloscp3009 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# |-- __init__.py (generated from main bindings CMake file) | ||
# `-- bindings.<cpython_extension> | ||
|
||
install(TARGETS pybind11_hde DESTINATION ${PYTHON_INSTDIR}) |
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,18 @@ | ||
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include <hde/bindings/msgs/Module.h> | ||
|
||
// Create the Python module | ||
PYBIND11_MODULE(bindings, m) | ||
{ | ||
namespace py = ::pybind11; | ||
|
||
m.doc() = "Human dynamics estimation bindings"; | ||
|
||
py::module msgModule = m.def_submodule("msg"); | ||
hde::bindings::msgs::CreateModule(msgModule); | ||
} |
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,11 @@ | ||
# SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
|
||
set(H_PREFIX include/hde/bindings/msgs) | ||
|
||
add_hde_python_module( | ||
NAME MsgsBindings | ||
SOURCES src/HumanState.cpp src/Module.cpp | ||
HEADERS ${H_PREFIX}/HumanState.h ${H_PREFIX}/BufferedPort.h ${H_PREFIX}/Module.h | ||
LINK_LIBRARIES HumanDynamicsEstimation::HumanStateMsg YARP::YARP_os) |
44 changes: 44 additions & 0 deletions
44
bindings/python/msgs/include/hde/bindings/msgs/BufferedPort.h
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,44 @@ | ||
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#ifndef HDE_BINDINGS_MSGS_BUFFERED_PORT_H | ||
#define HDE_BINDINGS_MSGS_BUFFERED_PORT_H | ||
|
||
#include <string> | ||
|
||
#include <pybind11/detail/common.h> | ||
#include <pybind11/pybind11.h> | ||
|
||
#include <yarp/os/BufferedPort.h> | ||
|
||
namespace hde { | ||
namespace bindings { | ||
namespace msgs { | ||
|
||
template <typename T> | ||
void CreateBufferedPort(pybind11::module& module, const std::string& name) | ||
{ | ||
namespace py = ::pybind11; | ||
py::class_<::yarp::os::BufferedPort<T>>(module, name.c_str()) | ||
.def(py::init()) | ||
.def("open", | ||
py::overload_cast<const std::string&>(&::yarp::os::BufferedPort<T>::open), | ||
py::arg("name")) | ||
.def("close", &::yarp::os::BufferedPort<T>::close) | ||
.def("isClosed", &::yarp::os::BufferedPort<T>::isClosed) | ||
.def("prepare", | ||
&::yarp::os::BufferedPort<T>::prepare, | ||
py::return_value_policy::reference_internal) | ||
.def("write", | ||
&::yarp::os::BufferedPort<T>::write, | ||
py::arg("forceStrict") = false) | ||
.def("read", | ||
&::yarp::os::BufferedPort<T>::read, | ||
py::arg("shouldWait") = true, | ||
py::return_value_policy::reference_internal); | ||
} | ||
} // namespace msgs | ||
} // namespace bindings | ||
} // namespace hde | ||
|
||
#endif // HDE_BINDINGS_MSGS_BUFFERED_PORT_H |
19 changes: 19 additions & 0 deletions
19
bindings/python/msgs/include/hde/bindings/msgs/HumanState.h
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 @@ | ||
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#ifndef HDE_BINDINGS_MSGS_HUMAN_STATE_H | ||
#define HDE_BINDINGS_MSGS_HUMAN_STATE_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace hde { | ||
namespace bindings { | ||
namespace msgs { | ||
|
||
void CreateHumanState(pybind11::module& module); | ||
|
||
} // namespace msgs | ||
} // namespace bindings | ||
} // namespace hde | ||
|
||
#endif // HDE_BINDINGS_MSGS_HUMAN_STATE_H |
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 @@ | ||
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#ifndef HDE_BINDINGS_MSGS_MODULE_H | ||
#define HDE_BINDINGS_MSGS_MODULE_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace hde { | ||
namespace bindings { | ||
namespace msgs { | ||
|
||
void CreateModule(pybind11::module& module); | ||
|
||
} // namespace msgs | ||
} // namespace bindings | ||
} // namespace hde | ||
|
||
#endif // HDE_BINDINGS_MSGS_MODULE_H |
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,38 @@ | ||
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <string> | ||
#include <hde/msgs/HumanState.h> | ||
|
||
#include <hde/bindings/msgs/BufferedPort.h> | ||
#include <hde/bindings/msgs/HumanState.h> | ||
|
||
namespace hde { | ||
namespace bindings { | ||
namespace msgs { | ||
|
||
void CreateHumanState(pybind11::module& module) | ||
{ | ||
namespace py = ::pybind11; | ||
using namespace ::hde::msgs; | ||
|
||
py::class_<HumanState>(module, "HumanState") | ||
.def(py::init()) | ||
.def_readwrite("jointNames", &HumanState::jointNames) | ||
.def_readwrite("positions", &HumanState::positions) | ||
.def_readwrite("velocities", &HumanState::velocities) | ||
.def_readwrite("baseName", &HumanState::baseName) | ||
.def_readwrite("baseOriginWRTGlobal", &HumanState::baseOriginWRTGlobal) | ||
.def_readwrite("baseOrientationWRTGlobal", &HumanState::baseOrientationWRTGlobal) | ||
.def_readwrite("baseVelocityWRTGlobal", &HumanState::baseVelocityWRTGlobal) | ||
.def_readwrite("CoMPositionWRTGlobal", &HumanState::CoMPositionWRTGlobal) | ||
.def_readwrite("CoMVelocityWRTGlobal", &HumanState::CoMVelocityWRTGlobal); | ||
|
||
CreateBufferedPort<HumanState>(module, "BufferedPortHumanState"); | ||
} | ||
} // namespace msgs | ||
} // namespace bindings | ||
} // namespace hde |
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,20 @@ | ||
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT) | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include <hde/bindings/msgs/HumanState.h> | ||
|
||
namespace hde { | ||
namespace bindings { | ||
namespace msgs { | ||
|
||
void CreateModule(pybind11::module& module) | ||
{ | ||
module.doc() = "YarpUtilities module."; | ||
|
||
CreateHumanState(module); | ||
} | ||
} // namespace msgs | ||
} // namespace bindings | ||
} // namespace hde |
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.
Please remove the
CMakeCache.txt
, it is a file that is supposed to be inside thebuild
directory and contains user-specific folders.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.
The fact that you have this in the root folder indicates that probably you did a in-source build, while we typically always try to do out of source builds, see https://cmake.org/cmake/help/book/mastering-cmake/chapter/Getting%20Started.html#directory-structure :