Hello ROS2 enthusiasts!
Following up on our journey through ROS2 Humble with Turtlesim, it’s time to elevate our skills. Today, we’re diving into the heart of ROS2 communication by creating a custom C++ node designed to publish data within the ROS2 ecosystem. This guide provides a detailed, step-by-step walkthrough, enabling you to replicate the process on your own system and solidify your understanding of ROS2 development.
Let’s get started!
Step 1: Laying the Foundation – Creating a New ROS2 Package
Every ROS2 project begins with a package. This command establishes the necessary structure for our C++ node.
First, navigate to your workspace’s source directory:
cd ~/ros2_ws/src
Now, create your new package named pkg_2 using ament_cmake as the build type:
ros2 pkg create --build-type ament_cmake pkg_2
What this command accomplishes:
* It generates a new directory: pkg_2.
* Within this directory, it sets up essential files like CMakeLists.txt and package.xml, along with include/ and src/ folders.
* Crucially, it prepares a standard C++ ROS2 package, ready to host your custom node.
Your project structure should now resemble this:
pkg_2/
├── CMakeLists.txt
├── include/pkg_2/
├── package.xml
└── src/
Step 2: Crafting Your C++ Node
With the package in place, it’s time to write the C++ code for our node. This initial version will simply run, confirming its existence within the ROS2 graph, even before it starts publishing.
Move into the src directory of your new package:
cd pkg_2/src
Create a new file named my_node.cpp:
nano my_node.cpp
#include "rclcpp/rclcpp.hpp"
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("my_cpp_node");
RCLCPP_INFO(node->get_logger(), "Hello guys this is nagu !");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
This node, once compiled, will be alive and operational, ready for future communication extensions.
Step 3: Configuring CMakeLists.txt
The CMakeLists.txt file is vital. It instructs ROS2 how to compile your C++ node, linking it correctly with the rclcpp library – the core client library for C++ in ROS2. You’ll need to add lines to specify your executable and link against rclcpp.
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(pkg_2)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
add_executable(my_node src/my_node.cpp)
ament_target_dependencies(my_node rclcpp)
install(TARGETS
my_node
DESTINATION lib/${PROJECT_NAME}
)
ament_package()
Step 4: Declaring Dependencies in package.xml
ROS2 packages manage their dependencies through package.xml. We need to explicitly state that our package relies on rclcpp.
Open your package.xml file:
nano ../package.xml
Locate the <dependencies> section and add the following line:
<depend>rclcpp</depend>
Step 5: Building Your ROS2 Package
With all the files prepared and configured, it’s time to compile everything.
Return to the root of your ROS2 workspace:
cd ~/ros2_ws
Execute the build command. colcon is the primary build tool, responsible for discovering packages, determining their build order, and orchestrating the compilation process.
colcon build
source install/setup.bash
Understanding colcon:
* Discovery: colcon scans your src/ directory, reading package.xml files to identify all packages.
* Build Orchestration: It intelligently determines the correct build order based on package dependencies.
* Tool Execution: It then invokes the appropriate build tool (like ament_cmake for C++ packages) for each package.
* Output: The compiled artifacts are organized into build/, install/, and log/ directories.
* Sourcing Setup: source install/setup.bash is crucial! It updates your environment variables, allowing your shell to locate and execute your newly built ROS2 nodes and packages.
Step 6: Running Your First C++ Node
Now for the moment of truth! Let’s execute our new node.
Run your node using the ros2 run command:
ros2 run pkg_2 my_node
If everything is set up correctly, you should see output similar to this:
[INFO] [my_cpp_node]: Hello guys this is nagu !
🎉 Congratulations! You have successfully created and run your very first C++ ROS2 node!
Common Issues and Troubleshooting Tips
While building, you might encounter some typical errors. Here’s a quick guide to understanding and resolving them:
Package 'pkg_2' not found:
This error almost always means you forgot tosource install/setup.bashafter building, or you built your package under a different ROS2 overlay that isn’t sourced.- Linker errors (e.g.,
undefined references):
This indicates that your executable isn’t correctly linked to required libraries. Double-check yourCMakeLists.txtto ensure you haveament_target_dependencies(my_node rclcpp)andfind_package(rclcpp). - Runtime errors about DDS:
Problems withrclcpp::initoften stem from missingrmwimplementations, incorrect environment variables, or even firewall settings blocking DDS (Data Distribution Service) discovery. colcon command is not currently installed:
Ifcolconisn’t found, you need to install it. Use these commands:
plaintext
sudo apt update
sudo apt install python3-colcon-common-extensions -y
What’s Next on Our ROS2 Journey?
This node is just the beginning! In our next blog post, we’ll enhance this node to regularly publish messages and introduce a subscriber node to receive them. This is where the true power of ROS2 communication protocols will come alive.
Stay tuned for more! Keep experimenting and continue exploring the vast potential of ROS2!
💡 Bonus Tips for ROS2 Development
- To see all active ROS2 nodes:
plaintext
ros2 node list - To monitor system logs via
/rosouttopic:
plaintext
ros2 topic echo /rosout
Author’s Note:
Written by NARASIMHA ALIAS Nagu. “Don’t prove, just improve.” Follow for more insights into robotics and ROS2.