DEV Community

naguballa77
naguballa77

Posted on

ROS2 Publisher Node.

Hey everyone πŸ‘‹
In my previous blog, I shared how I started exploring ROS2 Humble using Turtlesim.
Now, I’ve moved one step ahead β€” by creating my own C++ node that publishes data inside ROS2.

This post will walk you through everything I did β€” step by step β€” so you can do the same on your system.

βš™οΈ Step 1:

Creating a New Package

  • Inside your workspace:
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake pkg_2

Enter fullscreen mode Exit fullscreen mode

What this does:

  • Creates a folder named pkg_2
  • Generates the necessary files: CMakeLists.txt, package.xml, include/, and src/
  • Sets up a C++ ROS2 package ready for your node

Your structure should look like:

pkg_2/
β”œβ”€β”€ CMakeLists.txt
β”œβ”€β”€ include/pkg_2/
β”œβ”€β”€ package.xml
└── src/

Enter fullscreen mode Exit fullscreen mode

🧩 Step 2:

Writing the Node

  • Create a file:
cd pkg_2/src
nano my_node.cpp

Enter fullscreen mode Exit fullscreen mode

This node doesn’t publish yet β€” but it’s alive and running, ready to communicate.
πŸ“„ Step 3:

Editing

CMakeLists.txt

This tells ROS2 how to compile your C++ node and link it with the rclcpp library
πŸ“¦ Step 4:

Updating package.xml

Open:

nano ../package.xml

Enter fullscreen mode Exit fullscreen mode

And add your dependency to xml file:

<depend>rclcpp</depend>

Enter fullscreen mode Exit fullscreen mode

πŸ—οΈ Step 5:

Building the Package

  • Go back to your workspace:
cd ~/ros2_ws
colcon build
source install/setup.bash

Enter fullscreen mode Exit fullscreen mode

colcon is the build tool front-end. It discovers packages in src/, figures out build order from package manifests, and runs the configured build tool (eg. ament_cmake) for each package. Results go into build/, install/, and log/.

Discovery

: colcon scans src/ for packages (reads package.xml).
▢️ Step 6:

Running the Node

Run it with:

ros2 run pkg_2 my_node

Enter fullscreen mode Exit fullscreen mode

Output:

[INFO] [my_cpp_node]: Hello guys this is nagu !

Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Congratulations β€” you’ve just created your first C++ ROS2 node!


Typical errors you might encounter and what they mean

Package 'pkg_2' not found:
You didn’t source install/setup.bashof your workspace (or you built under a different overlay).

Linker errors (undefined references):
you forgot to ament_target_dependencies(... rclcpp)or find_package(rclcpp).

Runtime errors about DDS:
rclcpp::initcan fail if the rmw implementation is missing or environment variables are wrong. Also firewall settings can block DDS discovery.

It appears that the colcon command is not currently installed on your system. This tool is required to build ROS2 workspaces. You may install it using the following commands:

sudo apt update
sudo apt install python3-colcon-common-extensions -y
Enter fullscreen mode Exit fullscreen mode

πŸ’¬ What’s Next?

In my next blog, I’ll extend this node to publish messages at regular intervals and create a subscriber node to receive them β€” this is where true ROS2 communication begins.

Stay tuned πŸš€
Until then, keep experimenting and exploring the power of ROS2!


🧩 Bonus Tip:

To see all running nodes:

ros2 node list

Enter fullscreen mode Exit fullscreen mode

To check logs:

ros2 topic echo /rosout

Enter fullscreen mode Exit fullscreen mode

πŸͺΆ >Written by NARASIMHA ALIAS Nagu
πŸ’‘ "Don't prove, just improve."
follow me for more knowledge

Top comments (2)

Collapse
 
naguballa77 profile image
naguballa77

this is good work

Collapse
 
naguballa77 profile image
naguballa77

hi