Cara menggunakan ros python node cmakelists

Cara menggunakan ros python node cmakelists

Now that we’ve created our two nodes, we need to build them. Building ROS nodes means getting them in a format that enables ROS to use them.

The first thing we need to do is to edit the CMakeLists.txt file inside the noetic_basics_part_1 package. 

The CMakeLists.txt file is the file inside the noetic_basics_part_1 package that was automatically generated when we created the package. CMakeLists.txt says how to build the code we wrote (i.e. the nodes we just created).

Let’s edit the CMakeLists.txt file for the noetic_basics_part_1 package. Open a new terminal window, and type this command:

roscd noetic_basics_part_1 
gedit CMakeLists.txt

You should see a file that looks like this:

Cara menggunakan ros python node cmakelists

Now add these lines to the bottom of the CMakeLists.txt file:

add_executable(simple_publisher_node src/simple_publisher_node.cpp)
target_link_libraries(simple_publisher_node ${catkin_LIBRARIES})

add_executable(simple_subscriber_node src/simple_subscriber_node.cpp)
target_link_libraries(simple_subscriber_node ${catkin_LIBRARIES})

In the code above, we have done the following:

  • Create two executables, simple_publisher_node and simple_subscriber_node, which will by default go into the ~/catkin_ws/devel/lib/noetic_basics_part_1 folder.
  • Make sure the nodes get linked to the catkin libraries (i.e. ROS-related libraries).

Now save and close the CMakeLists.txt file.

Open a new terminal window, and type the following commands to build all the nodes in the noetic_basics_part_1 package:

cd ~/catkin_ws
catkin_make 

That command above will build all nodes inside all the packages in your catkin workspace.

Cara menggunakan ros python node cmakelists

Now, open a new terminal window and go to the catkin_ws/devel/lib/noetic_basics_part_1/ folder.

cd ~/catkin_ws/devel/lib/noetic_basics_part_1/

Type dir to see the files listed. You will see the publisher and subscriber executables. Feel free to close the terminal window now.

Cara menggunakan ros python node cmakelists

How to Build a Python Node

Navigate to the scripts folder inside your package. 

roscd noetic_basics_part_1/scripts

Make the files executable. Type these commands, pressing Enter after each.

chmod +x simple_python_publisher.py
chmod +x simple_python_subscriber.py

Open a new terminal window, and type the following commands to build all the nodes in the noetic_basics_part_1 package:

cd ~/catkin_ws
catkin_make 

Post navigation

In ROS2, when you create a package you have to select a build type: either ament_cmake or ament_python. This will determine whether your package is a Cpp package, or a Python package.

But… How to do if you want to create a ROS2 package containing both Python nodes and Cpp nodes?

Good news for you: it’s possible, and in this tutorial I’ll show you how to create and configure your Python and Cpp nodes in the same package.

First things first, make sure you already know how to setup a standard ROS2 Python package, and a ROS2 Cpp package.

And now, let’s dive in!

  • How we’ll manage to setup Python and Cpp nodes in the same ROS2 package
  • Setup your ROS2 Cpp and Python package
    • Create a standard Cpp package
    • Add a Cpp node + header
    • Add a Python node + module to import
    • ROS2 Package architecture with both Python and Cpp nodes – final
  • Configure your ROS2 package for both Cpp and Python
    • package.xml
    • CMakeLists.txt
    • Compile and run your ROS2 Cpp and Python nodes
  • Going further

How we’ll manage to setup Python and Cpp nodes in the same ROS2 package

First we’ll create a ROS2 Cpp package, which contains a package.xml and CMakeLists.txt. For Cpp related stuff, nothing will be too different from what you’re already used to do.


You are learning ROS2...

As a complete beginner? Check out ROS2 For Beginners and learn ROS2 in 1 week.

As a ROS1 developer? Check out Learn ROS2 as a ROS1 Developer and Migrate Your ROS Projects.


For Python, we’ll add some folders and files so you an add your Py nodes inside the package. And we’ll configure the Python stuff… in the CMakeLists.txt. This is one of the main difference you’ll have from a standard Python package. No more setup.py and setup.cfg, everything will be done in the CMakeLists.txt.

Note: the method I propose here is not necessarily the only one. You have many different ways to achieve that. First follow this tutorial to get something working, and then modify the architecture/configuration as you need.

Setup your ROS2 Cpp and Python package

The name of the package for this tutorial will be “my_cpp_py_pkg”.

Create a standard Cpp package

First create the package with the ament_cmake build type .

$ cd ~/ros2_ws/src/
$ ros2 pkg create my_cpp_py_pkg --build-type ament_cmake

For now the package contains those files:

my_cpp_py_pkg/
├── CMakeLists.txt
├── include
│   └── my_cpp_py_pkg
├── package.xml
└── src

This is the base for a Cpp package. Later on, from that base we’ll add necessary files and configuration for Python.

Add a Cpp node + header

Let’s add a .cpp file in the src/ directory, and an .hpp header file in the include/my_cpp_py_pkg/ directory. No need to create new folders here.

$ cd my_cpp_py_pkg/
$ touch src/cpp_node.cpp
$ touch include/my_cpp_py_pkg/cpp_header.hpp

Note: if your cpp_node.cpp file needs to include the cpp_header.hpp file you’ll have to write #include "my_cpp_py_pkg/cpp_header.hpp".

Note 2: you have to have a main() in your cpp_node otherwise the compilation step won’t succeed. You can just add a minimal  C++ node if you’re following this tutorial by the letter.

Add a Python node + module to import

Let’s setup the package so it can also have Python nodes.

$ mkdir my_cpp_py_pkg
$ touch my_cpp_py_pkg/__init__.py
$ mkdir scripts

The first folder we create has the same name as the package. Inside this folder we create an empty __init__.py file. This is something that is already present when you create a Python package.

This folder will host any library and module we want to use or export.

Then we create a scripts/ folder. In there we’ll place our executables (and nodes).

Now that we have the structure, let’s add one Python module and one node.

$ touch my_cpp_py_pkg/module_to_import.py
$ touch scripts/py_node.py

Important: you have to add a shebang line first thing in the py_node.py file:

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

This is something you don’t have to do with a standard ROS2 Python package (it’s managed for you), but with this setup if you don’t add this line, you will get an error when you try to start the node with ros2 run or from a launch file.

Also (and we’ll come back to it later), if you want to be able to modify your code and re-run it without recompiling every time, or if you simply want to start your node by launching your script directly, then make the script executable: chmod +x scripts/py_node.py.

Note: if you want to import the module_to_import.py file from your py_node.py file (or from any other file from other packages), you’ll have to write from my_cpp_py_pkg.module_to_import import ....

ROS2 Package architecture with both Python and Cpp nodes – final

By now your package should look like this:

my_cpp_py_pkg/
# --> package info, configuration, and compilation
├── CMakeLists.txt
├── package.xml
# --> Python stuff
├── my_cpp_py_pkg
│   ├── __init__.py
│   └── module_to_import.py
├── scripts
│   └── py_node.py
# --> Cpp stuff
├── include
│   └── my_cpp_py_pkg
│       └── cpp_header.hpp
└── src
    └── cpp_node.cpp

The CMakeLists.txt and package.xml will be used for both Python and Cpp nodes. For the rest, you can see that Cpp stuff is clearly separated from Python stuff.

Now, don’t forget to write something to run in the Python and Cpp files, and after that let’s configure the package!

Configure your ROS2 package for both Cpp and Python

package.xml



  my_cpp_py_pkg
  0.0.0
  TODO: Package description
  Name
  TODO: License declaration

  ament_cmake
  ament_cmake_python

  rclcpp
  rclpy

  ament_lint_auto
  ament_lint_common

  
    ament_cmake
  

First of all, if you ever need to share your package with other people, or publish it on the Internet, don’t forget to modify the version, description, maintainer (+ author if needed), and license tags.

Here is what we added:

  ament_cmake
  ament_cmake_python

By default you should already have a buildtool_depend tag for ament_cmake, since that’s what we asked when creating the package from command line.

Here we add another buildtool_depend tag: ament_cmake_python.

Note: in a standard Python package, you’d have ament_python, not ament_cmake_python. Make sure not to mix those 2. Using ament_cmake_python means that we’ll be able to setup our Python stuff with cmake, so, from the CMakeLists.txt file.

  rclcpp
  rclpy

We add a dependency for the ROS2 Cpp library (rclcpp) as well as the ROS2 Python library (rclpy).

That’s it for package.xml.

CMakeLists.txt

Here’s the complete CMakeLists.txt to install both Cpp and Python nodes. I have removed some stuff we don’t use here (“Default to C99”, and the test section).

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

Basically, we can split this config into 3 parts: dependencies, Cpp part, and Python part.

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

We import external dependencies for both Cpp and Python at the same time.

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

This is the Cpp part.

First we include the “include” directory so the cpp_header.hpp file can be found.

Then it’s business as usual: you create an executable, link with dependencies, and install the executable in the lib/ folder of your package (inside the install/ folder of your ROS2 workspace).

# Install Python modules
ament_python_install_package(${PROJECT_NAME})

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

And this is the Python part, which should be new to you if you’ve always installed ROS2 Python nodes from setup.py (which does not exist here).

First, use ament_python_install_package(${PROJECT_NAME}) to install any Python module (in this example: files under my_cpp_py_pkg/ folder), so you can find them from this – or another – package.

Then, we install the scripts/py_node.py file. We put this file in the install lib/ folder, which is the same folder as for ROS2 Cpp nodes. Thus all Cpp/Python executables will be at the same place.

For any new Python script you need to install, just add a new line here. After being copied, the “installed” file will be automatically made executable.

Compile and run your ROS2 Cpp and Python nodes

Let’s compile our ROS2 Python/Cpp package.

$ cd ~/ros2_ws/
$ colcon build --packages-select my_cpp_py_pkg

Open 2 new terminals, source the ROS2 environment, and you can start both Cpp and Python nodes.

$ ros2 run my_cpp_py_pkg cpp_executable
$ ros2 run my_cpp_py_pkg py_node.py

Important: if you wish to compile with the --symlink-install option (so you can modify and re-run a script without having to re-compile), you’ll have to make your scripts executable with chmod +x. Otherwise when you try to run your node you’ll get this error :”No executable found”.

Going further

In this tutorial you’ve seen how to create and setup a ROS2 package for both Python and Cpp nodes.

As you can see it’s not that difficult, and as long as you clearly separate your Python/Cpp files, as well as the Python/Cpp configuration, nothing should go wrong.

If you wish to install other things in this package, such as launch files, YAML config files, etc., then you just need to install them as you would in a standard Cpp package.