ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
5

ros2: C++ and Python in same package. Is it possible?

asked 2018-07-26 09:35:07 -0600

Anis gravatar image

Hi

In ROS1, it was possible to have Python script in any package that contains C++ or Python, but in ROS2, it seems we need separate packages for Python and C++, which I do not find practical.

Is there any reason of not having the possibility to create one package for both C++ and Python?

edit retag flag offensive close merge delete

Comments

https://answers.ros.org/question/309391 is a follow-up to this question, as OP there didn't find none of the answers in this thread so far makes it clear how to mix C++ and Python.

130s gravatar image 130s  ( 2023-04-07 02:23:36 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted
3

answered 2018-07-26 10:06:53 -0600

sloretz gravatar image

It is possible to create a package having multiple programming languages. rclpy is a ROS 2 package containing both python and C. The same could be done with C++ and python.

In ROS 1 using catkin and calling catkin_python_setup() in a CMakeLists.txt causes a setup.py file to be called, but the CMake code has to be there even if the package has no C or C++ code. In ROS 2 a package can be pure python.

edit flag offensive delete link more

Comments

1

In ROS 2 a CMake project can install Python packages and modules (see https://github.com/ament/ament_cmake/... ) but it does provide a mechanism to invoke a setup.py file.

Dirk Thomas gravatar image Dirk Thomas  ( 2018-07-26 11:36:15 -0600 )edit

While that could be implemented similar as it is in catkin it is fairly complex and the catkin implementation as several severe limitations. Therefore it hasn't been done until now.

Dirk Thomas gravatar image Dirk Thomas  ( 2018-07-26 11:37:03 -0600 )edit

can we build demo_nodes_cpp and demo_nodes_py into one project as demo_nodes_test and run "ros2 run demo_nodes_test listener.py"(run python listener) or " ros2 run demo_nodes_test listener "(run c++ listener) ? I don't know how to install python executable with ament_cmake .

lxbeyond gravatar image lxbeyond  ( 2018-08-09 02:42:17 -0600 )edit

@Dirk Thomas Could you please provide an example in how to invoke a setup.py file in CMake?

andrestoga gravatar image andrestoga  ( 2023-01-03 16:37:34 -0600 )edit
5

answered 2020-12-22 09:18:50 -0600

Andreas Z gravatar image

It is by now easily possible.

You create an cpp package as usual and add the package structure of a normal python package.

The difference is, that you don't need the setup.cfg and setup.py files as this is now all handled by CMakeList.txt.

You have to make small adjustments as adding

find_package(ament_cmake_python REQUIRED)
find_package(rclpy REQUIRED)

and modify the package.xml to support building with python:

<buildtool_depend>ament_cmake_python</buildtool_depend>

For more detailed explanations please look at these ressources:

ros2-package-for-both-python-and-cpp-nodes

creating-a-mixed-cpp-and-python-package

In short your structure has to look like this:

my_example_package/
CMakeLists.txt
package.xml
include/
    my_example_package/
        some_file.hpp
src/
    some_file.cpp
my_example_package/
    __init__.py
    some_module.py
    another_module.py
scripts/
    my_executable

package.xml like this:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>my_cpp_py_pkg</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="your@email.com">Name</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>ament_cmake_python</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>rclpy</depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

and CMakeList.txt like this:

cmake_minimum_required(VERSION 3.5)
project(my_cpp_py_pkg)
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclpy REQUIRED)

# Include Cpp "include" directory
include_directories(include)

# Create Cpp executable
add_executable(cpp_executable src/cpp_node.cpp)
ament_target_dependencies(cpp_executable rclcpp)

# Install Cpp executables
install(TARGETS
  cpp_executable
  DESTINATION lib/${PROJECT_NAME}
)

# Install Python modules
ament_python_install_package(${PROJECT_NAME})

# Install Python executables
install(PROGRAMS
  scripts/py_node.py
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()
edit flag offensive delete link more
2

answered 2018-12-23 23:50:02 -0600

tanyouliang gravatar image

It can be done. I created a sample ROS2 pkg with .cpp and .py script in it, setup.py is not required in this case. Here, the CMakeLists.txt needs to be slightly modified, and an empty __init__.py is used. You can refer to my sample code here:

https://github.com/tanyouliang95/MySa...

edit flag offensive delete link more

Comments

I know that this is an old comment, but I am curious to know how do you execute PayloadController.py. Thank you!

Ana de Sousa gravatar image Ana de Sousa  ( 2020-06-08 15:13:56 -0600 )edit

rosrun ros2_payload PayloadController.py, see https://wiki.ros.org/ROS/Tutorials/Ex...

wiyogo gravatar image wiyogo  ( 2020-10-29 04:22:55 -0600 )edit

Nice! Thank you!

Ana de Sousa gravatar image Ana de Sousa  ( 2020-10-29 08:11:25 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2018-07-26 09:35:07 -0600

Seen: 9,991 times

Last updated: Dec 22 '20