ROS2 Tutorials #4: How to create a ROS2 Package for C++ [NEW]

ROS2 Tutorials - How to create a ROS2 Package for C++

Written by Alberto Ezquerro

19/07/2019

About

In this post, you will learn how to create a simple ROS2 package for C++. You don’t need a ROS2 installation for this, as we will use the ROS Development Studio (ROSDS), an online platform that provides access to ROS (1 or 2) computers and other powerful ROS tools within a browser!

PS: If you have ROS2 installed locally on your machine, you can skip Step 1.

Step 1: Create a Project (ROSject) on ROSDS

Head to http://rosds.online and create a project with a similar configuration as the one shown below. You can change the details as you like, but please ensure you select “Ubuntu 18.04 + ROS2 Crystal” under “Configuration”.

Once done with that, open your ROSject. This might take a few moments, please be patient.

Step 2: Source the ROS2 workspace

Once the ROSject is open, head to the Tools menu and pick the Shell tool (if on your local PC just fire up a terminal) and run the following command to source the workspace:

user:~$ source /opt/ros/crystal/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$

If you get that ROS_DISTRO warning, just ignore it.

Step 3: Create a ROS2 package in your ROS2 workspace

The syntax for creating a ROS2 package is ros2 pkg create <package_name> --build-type <build_type> --dependencies <dependencies_separated_by_single_space>.

In the same terminal as in Step 2, change to your ROS2 workspace src directory and create a package there:

user:~$ cd ros2_ws/src
user:~/ros2_ws/src$ ros2 pkg create ros2_cpp_pkg --build-type ament_cmake --dependencies rclcpp
going to create a new package
package name: ros2_cpp_pkg
destination directory: /home/user/ros2_ws/src
package format: 2
version: 0.0.0
description: TODO: Package description
maintainer: ['user <user@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp']
creating folder ./ros2_cpp_pkg
creating ./ros2_cpp_pkg/package.xml
creating source and include folder
creating folder ./ros2_cpp_pkg/src
creating folder ./ros2_cpp_pkg/include/ros2_cpp_pkg
creating ./ros2_cpp_pkg/CMakeLists.txt
user:~/ros2_ws/src$

Step 4: Create C++ code inside the package

Create a file named ros2_cpp_code.cpp inside the new package:

user:~/ros2_ws/src$ cd ros2_cpp_pkg/src
user:~/ros2_ws/src/ros2_cpp_pkg/src$ touch ros2_cpp_code.cpp
user:~/ros2_ws/src/ros2_cpp_pkg/src$

Pick the IDE from the Tools menu, locate the C++ file and paste in the following code:

#include "rclcpp/rclcpp.hpp"

int main(int argc, char *argv[]) {
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("ObiWan");

  RCLCPP_INFO(node->get_logger(),
              "Help me Obi-Wan Kenobi, you're my only hope");

  rclcpp::shutdown();
  return 0;
}

In the IDE, open up the CMakeLists.txt and paste the following line at the bottom of the file. We are doing this to ensure the C++ file will be properly detected and compiled.

add_executable(cpp_code src/ros2_cpp_code.cpp)
ament_target_dependencies(cpp_code rclcpp)

install(TARGETS
    cpp_code
    DESTINATION lib/${PROJECT_NAME}
)

Step 5: Compile and test-run the package

Now your package is ready to compile…so compile it and source the workspace!

user:~/ros2_ws/src/ros2_cpp_pkg/src$ cd /home/user/ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_cpp_pkg

Finished <<< ros2_cpp_pkg [21.9s]

Summary: 1 package finished [22.0s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
ROS_DISTRO was set to 'crystal' before. Please make sure that the environment does not mix paths from different distributions. ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~/ros2_ws$

Great. Now test-run it.  You should have the string mentioned in the C++ code printed to the terminal output:

user:~/ros2_ws$ ros2 run ros2_cpp_pkg cpp_code
[INFO] [ObiWan]: Help me Obi-Wan Kenobi, you're my only hope
user:~/ros2_ws$

Done!

Extra 1: ROSject link

Get the ROSject containing all code used in the post in the following link: http://www.rosject.io/l/bce4ffd/!

Extra 2: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it 🙂

Topics: ROS Q&A | ros2
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

3 Comments

  1. Anonymous

    C++ code is invalid. main function need to return value

    Reply
  2. Anton

    C++ code `return ;` is invalid. main function need to return value, e.g. `return 0;`

    Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This