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

Combined package for cpp and python in ROS2 foxy

asked 2021-11-18 00:35:14 -0500

Paulidrobo gravatar image

updated 2021-12-17 16:44:56 -0500

osilva gravatar image

Hello, I want to create python and cpp package for my workspace and found relevant details from here.

https://roboticsbackend.com/ros2-package-for-both-python-and-cpp-nodes/ In the tutorial the user creates module in the my_cpp_py_pkg subfodler and import it to the script py_node.py. Why do we need to create __init__.py , Can we have our python script in same subfolder rather than creating sctra folder and name in script.

However I am encountering these errors.

ros2 run combined_py_cpp py_node.py 
Traceback (most recent call last):
  File "/opt/ros/foxy/bin/ros2", line 11, in <module>
    load_entry_point('ros2cli==0.9.10', 'console_scripts', 'ros2')()
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2cli/cli.py", line 67, in main
    rc = extension.main(parser=parser, args=args)
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2run/command/run.py", line 70, in main
    return run_executable(path=path, argv=args.argv, prefix=prefix)
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2run/api/__init__.py", line 61, in run_executable
    process = subprocess.Popen(cmd)
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/piyush/model_ws/install/combined_py_cpp/lib/combined_py_cpp/py_node.py'

My cmake file:

cmake_minimum_required(VERSION 3.5)
project(combined_py_cpp)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# 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)
# uncomment the following section in order to fill in
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclpy REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
include_directories(include)

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}
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

My package.xml:

<?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>combined_py_cpp</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="piyush@todo.todo">piyush</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>

My pythonnode is example for publisher script in ros2 tutorial.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-18 05:18:15 -0500

ljaniec gravatar image

updated 2021-12-19 15:17:04 -0500

Fix your question in edits please, right now it is hard to read. Code and terminal info should be formatted. Use preview under the editor.

  • Why do we need to create "__init__.py" , Can we have our python script in same subfolder rather than creating sctra folder and name in script.

Through the official documentation for Python ROS2 package:

<package_name>- a directory with the same name as your package, used by ROS 2 tools to find your package, contains __init__.py

So it is necessary because of this point. You can have your scripts in <your_pkg_name>/<your_pkg_name> too - just remember to define correct entry_points in your setup.py (e.g.

entry_points={ "console_scripts": ["your_pkg_name = your_pkg_name.your_pkg_name:main"], } ) for easier run.

  • OSError: [Errno 8] Exec format error: '/home/piyush/model_ws/install/combined_py_cpp/lib/combined_py_cpp/py_node.py' - this suggests problem in your py_node.py, citation from there:

The Exec format error is typically seen if you try to start a script (with bash or another shell) and that doesn't specify which interpreter should be used

Did you add #! /usr/bin/env python3 at the top of node?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-11-18 00:35:14 -0500

Seen: 250 times

Last updated: Dec 19 '21