Skip to content

Commit

Permalink
Imported upstream version '2.0.0' of 'upstream'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke committed May 25, 2024
1 parent 91bede1 commit d38563c
Show file tree
Hide file tree
Showing 17 changed files with 652 additions and 238 deletions.
75 changes: 75 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
BasedOnStyle: Google
ColumnLimit: 120
MaxEmptyLinesToKeep: 1
SortIncludes: false

Standard: Auto
IndentWidth: 2
TabWidth: 2
UseTab: Never
AccessModifierOffset: -2
ConstructorInitializerIndentWidth: 2
NamespaceIndentation: None
ContinuationIndentWidth: 4
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: false

AlignEscapedNewlinesLeft: false
AlignTrailingComments: true

AllowAllParametersOfDeclarationOnNextLine: false
ExperimentalAutoDetectBinPacking: false
ObjCSpaceBeforeProtocolList: true
Cpp11BracedListStyle: false

AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortCaseLabelsOnASingleLine: false

AlwaysBreakTemplateDeclarations: true
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: true

BinPackParameters: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DerivePointerBinding: false
PointerBindsToType: true

PenaltyExcessCharacter: 50
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 1000
PenaltyBreakFirstLessLess: 10
PenaltyBreakString: 100
PenaltyReturnTypeOnItsOwnLine: 50

SpacesBeforeTrailingComments: 2
SpacesInParentheses: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: false
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true

# Configure each individual brace in BraceWrapping
BreakBeforeBraces: Custom

# Control of individual brace wrapping cases
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
...
69 changes: 69 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# This config uses industrial_ci (https://github.com/ros-industrial/industrial_ci.git).
# For troubleshooting, see readme (https://github.com/ros-industrial/industrial_ci/blob/master/README.rst)

name: CI

on:
workflow_dispatch:
pull_request:
push:

permissions:
contents: read
pages: write
id-token: write

jobs:
ici:
strategy:
fail-fast: false
matrix:
distro: [humble, rolling]
include:
- distro: humble
env:
CLANG_TIDY: true
TARGET_CMAKE_ARGS: >-
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_FLAGS="-Werror -Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls -Wold-style-cast"
env:
ROS_DISTRO: ${{ matrix.distro }}
CLANG_TIDY_ARGS: -quiet -export-fixes ${{ github.workspace }}/.work/clang-tidy-fixes.yaml
CCACHE_DIR: ${{ github.workspace }}/.ccache
BASEDIR: ${{ github.workspace }}/.work
CACHE_PREFIX: "${{ matrix.distro }}${{ contains(matrix.env.TARGET_CMAKE_ARGS, '--coverage') && '-ccov' || '' }}"
# perform full clang-tidy check only on manual trigger (workflow_dispatch), PRs do check changed files, otherwise nothing
CLANG_TIDY_BASE_REF: ${{ github.event_name != 'workflow_dispatch' && (github.base_ref || github.ref) || '' }}
CC: ${{ matrix.env.CLANG_TIDY && 'clang' }}
CXX: ${{ matrix.env.CLANG_TIDY && 'clang++' }}
ADDITIONAL_DEBS: ${{ matrix.env.CLANG_TIDY && 'clang' }}
CLANG_TIDY: ${{ matrix.env.CLANG_TIDY }}
TARGET_CMAKE_ARGS: ${{ matrix.env.TARGET_CMAKE_ARGS }}

name: "${{ matrix.distro }}${{ matrix.env.NAME && ' • ' || ''}}${{ matrix.env.NAME }}${{ matrix.env.CLANG_TIDY && ' • clang-tidy' || '' }}"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Cache ccache
uses: rhaschke/cache@main
with:
path: ${{ env.CCACHE_DIR }}
key: ccache-${{ env.CACHE_PREFIX }}-${{ github.sha }}-${{ github.run_id }}
restore-keys: |
ccache-${{ env.CACHE_PREFIX }}-${{ github.sha }}
ccache-${{ env.CACHE_PREFIX }}
env:
GHA_CACHE_SAVE: always

- id: ici
name: Run industrial_ci
uses: rhaschke/industrial_ci@master

- name: Upload clang-tidy fixes (on failure)
uses: actions/upload-artifact@v4
if: failure() && steps.ici.outputs.clang_tidy_checks
with:
name: clang-tidy-fixes.yaml
path: ${{ env.BASEDIR }}/clang-tidy-fixes.yaml
27 changes: 27 additions & 0 deletions .github/workflows/format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This is a format job. Pre-commit has a first-party GitHub action, so we use
# that: https://github.com/pre-commit/action

name: Format

on:
workflow_dispatch:
pull_request:
push:

jobs:
pre-commit:
name: pre-commit
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install clang-format
run: sudo apt-get install clang-format
- uses: pre-commit/action@v3.0.1
id: precommit
- name: Upload pre-commit changes
if: failure() && steps.precommit.outcome == 'failure'
uses: rhaschke/upload-git-patch-action@main
with:
name: pre-commit
45 changes: 45 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# To use:
#
# pre-commit run -a
#
# Or:
#
# pre-commit install # (runs every time you commit in git)
#
# To update this file:
#
# pre-commit autoupdate
#
# See https://github.com/pre-commit/pre-commit

repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
args: ["--line-length", "100"]

- repo: local
hooks:
- id: clang-format
name: clang-format
description: Format files with ClangFormat.
entry: clang-format
language: system
files: \.(c|cc|cxx|cpp|frag|glsl|h|hpp|hxx|ih|ispc|ipp|java|js|m|proto|vert)$
args: ["-fallback-style=none", "-i"]
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog for package py_binding_tools
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

2.0.0 (2024-05-25)
------------------
* ROS2 migration
* Contributors: Robert Haschke

1.0.0 (2024-03-05)
------------------
* Initial release factored out from https://github.com/ros-planning/moveit/pull/2910
75 changes: 42 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
cmake_minimum_required(VERSION 3.0.2)
cmake_minimum_required(VERSION 3.5)
project(py_binding_tools)

find_package(catkin REQUIRED
COMPONENTS
geometry_msgs
pybind11_catkin
roscpp
)

# catkin_python_setup()

catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS geometry_msgs roscpp
)

include_directories(include ${catkin_INCLUDE_DIRS})
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
find_package(pybind11_vendor REQUIRED)
find_package(pybind11 REQUIRED)

add_library(${PROJECT_NAME}
add_library(${PROJECT_NAME} SHARED
src/ros_msg_typecasters.cpp
src/roscpp_initializer.cpp
src/initializer.cpp
)
add_dependencies(${PROJECT_NAME} ${catkin_EXPORTED_TARGETS})
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>)
ament_target_dependencies(${PROJECT_NAME} rclcpp geometry_msgs pybind11)

ament_export_targets(${PROJECT_NAME}Targets HAS_LIBRARY_TARGET)

install(
TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
)

install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
install(
DIRECTORY include/
DESTINATION include/${PROJECT_NAME}
)

#catkin_lint cannot detect target declarations in functions, here in pybind11_add_module
#catkin_lint: ignore undefined_target
_ament_cmake_python_register_environment_hook()

pybind11_add_module(rclcpp src/rclcpp.cpp)
target_link_libraries(rclcpp PRIVATE ${PROJECT_NAME})
install(TARGETS rclcpp LIBRARY DESTINATION "${PYTHON_INSTALL_DIR}")

if(BUILD_TESTING)
find_package(ament_cmake_pytest REQUIRED)
find_package(std_msgs REQUIRED)

pybind11_add_module(${PROJECT_NAME}_test test/test.cpp)
target_link_libraries(${PROJECT_NAME}_test PRIVATE ${PROJECT_NAME} ${std_msgs_TARGETS})
install(TARGETS ${PROJECT_NAME}_test LIBRARY DESTINATION "${PYTHON_INSTALL_DIR}")

pybind_add_module(${PROJECT_NAME}_module src/${PROJECT_NAME}.cpp)
set_target_properties(${PROJECT_NAME}_module PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME}_module PRIVATE ${PROJECT_NAME})
ament_add_pytest_test(basic test/test_basic.py)
endif()

install(TARGETS ${PROJECT_NAME}_module
LIBRARY DESTINATION ${CATKIN_GLOBAL_PYTHON_DESTINATION})
ament_export_dependencies(rclcpp)
ament_export_dependencies(geometry_msgs)
ament_package()
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ In this case, the string is interpreted as the `header.frame_id` field of the me

### C++ ROS initialization

C++ and Python use their own ROS implementations (`rospy` and `roscpp`).
C++ and Python use their own RCL implementations (`rclpy` and `rclcpp`).
Thus, it is necessary to initialize ROS in the C++ domain additionally to the Python domain before calling any ROS-related functions from wrapped C++ functions or classes.
To this end, the package provides the python function `roscpp_init()` and the C++ class `ROScppInitializer`. The latter is intended to be used as a base class for your python wrapper classes:
To this end, the package provides the python function `rclcpp.init()` and the C++ class `RCLInitializer`. The latter is intended to be used as a base class for your python wrapper classes:

```cpp
class FooWrapper : protected ROScppInitializer, public Foo {
class FooWrapper : protected RCLInitializer, public Foo {
// ...
};
```
to ensure that the ROS infrastructure is initialized before usage in the wrapped C++ class. Ensure to list `ROScppInitializer` as the _first_ base class, if ROS functionality is required in the constructor already!
`ROScppInitializer` registers an anonymous C++ ROS node named `python_wrapper_xxx`. If you need a specific node name or if you want to pass remappings, use the manual initialization function `roscpp_init(name="", remappings={}, options=0)` instead, which effectively calls `ros::init` with the given arguments. Note, that an empty name will map to the above-mentioned node name `python_wrapper_xxx`.
to ensure that the ROS infrastructure is initialized before usage in the wrapped C++ class. Ensure to list `RCLInitializer` as the _first_ base class, if ROS functionality is required in the constructor already!
4 changes: 2 additions & 2 deletions include/py_binding_tools/geometry_msg_typecasters.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace detail
{
/** Convienency type caster, also allowing to initialize PoseStamped from a string */
template <>
struct type_caster<geometry_msgs::PoseStamped> : RosMsgTypeCaster<geometry_msgs::PoseStamped>
struct type_caster<geometry_msgs::msg::PoseStamped> : RosMsgTypeCaster<geometry_msgs::msg::PoseStamped>
{
// Python -> C++
bool load(handle src, bool convert)
Expand All @@ -57,7 +57,7 @@ struct type_caster<geometry_msgs::PoseStamped> : RosMsgTypeCaster<geometry_msgs:
value.pose.orientation.w = 1.0;
return true;
}
return RosMsgTypeCaster<geometry_msgs::PoseStamped>::load(src, convert);
return RosMsgTypeCaster<geometry_msgs::msg::PoseStamped>::load(src, convert);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,22 @@
*********************************************************************/
#pragma once

#include <ros/init.h>
#include <string>
#include <vector>

namespace py_binding_tools
{
/** The constructor of this class ensures that ros::init() has been called.
/** The constructor of this class ensures that rclcpp::init() has been called.
*
* Thread safety and multiple initialization is properly handled.
* The default node name is "python_wrapper".
* If you like to have a custom name, use the roscpp_init() function before. */
class ROScppInitializer
* Thread safety and multiple initialization is properly handled. */
class RCLInitializer
{
public:
ROScppInitializer();
~ROScppInitializer();
RCLInitializer();
~RCLInitializer();
};

void roscpp_init(const std::string& node_name,
const std::map<std::string, std::string>& remappings,
uint32_t options);

void roscpp_shutdown();
void init(const std::vector<std::string>& args);
void shutdown();

} // namespace py_binding_tools
Loading

0 comments on commit d38563c

Please sign in to comment.