8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[ROS2 foxy] c++とpython共存パッケージのテンプレート

Posted at

ROS2では,c++とpythonのパッケージの書き方は異なる.
実際には,同じパッケージ内で,どちらの言語も使いたいということがある.

表面的にチュートリアルをやっただけだと,共存の方法はわからなかった.

ということで,共存パッケージのテンプレートを作ってみた.
参考にして欲しい.

なお,今回のパッケージは以下のリポジトリにアップしている.

パッケージ構成

一般的なROS2の構成と同様です.
cppのソースコードは,srcフォルダ,
pythonのソースコードは,scriptsフォルダに入れます.

パッケージ構成
src/
    cpp_node.cpp
scripts/
    py_node.py
ros2_base_pkg/
    __init__.py
    import_module.py
include/
CMakeLists.txt
package.xml

import_module.py

import_module.py
#!/usr/bin/env python3

from ros2_base_pkg.import_module import ....

CMakeLists.txt

CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(ros2_base_pkg)

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

#-- Dependencies
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclpy REQUIRED)

#--- for C++
include_directories(include)
add_executable(cpp_node src/cpp_node.cpp)
ament_target_dependencies(cpp_node rclcpp)

install(TARGETS
  cpp_node
  DESTINATION lib/${PROJECT_NAME}
)
#--- end C++

#-- for Python
ament_python_install_package(${PROJECT_NAME})

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

ament_package()

package.xml

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>ros2_base_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>

参考

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?