C++ Advanced · Lesson 10 of 23
ROS 2 nodes in C++
rclcpp::init, rclcpp::Node, spin and shutdown.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain ros 2 nodes in c++ in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A ROS 2 C++ node is a class derived from rclcpp::Node. main() calls rclcpp::init, creates the node with std::make_shared, runs rclcpp::spin (the event loop from Phase 6), and calls rclcpp::shutdown. The base class constructor takes the node's name.
Source /opt/ros/lyrical/setup.bash first, and use RCLCPP_INFO(this->get_logger(), ...) for logging. Ctrl+C makes spin return so the destructors (RAII) run and shut everything down cleanly.
Guided lesson
Work through these steps
- 1.
Read the ros 2 nodes in c++ example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
#include "rclcpp/rclcpp.hpp"
// The main rclcpp header: Node, init, spin, logging macros.
class HelloNode : public rclcpp::Node {
// Our node IS-A rclcpp::Node.
public:
// Public interface.
HelloNode() : Node("hello_node") {
// Call the base constructor with the node's name.
RCLCPP_INFO(get_logger(), "hello from C++17");
// Log an info message through ROS 2's logger.
}
// End of the constructor.
};
// End of the class.
int main(int argc, char** argv) {
// Program entry point; ROS reads its arguments from argc/argv.
rclcpp::init(argc, argv);
// Initialize the ROS 2 client library.
rclcpp::spin(std::make_shared<HelloNode>());
// Create the node and process its events until Ctrl+C.
rclcpp::shutdown();
// Release ROS 2 resources.
return 0;
// Exit with success.
}
// End of main.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Messages between nodes
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for ROS 2 nodes in C++. Expected result: [INFO] [hello_node]: hello from C++17
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
// src/hello_node.cpp build: colcon build --packages-select robot_nodes run: ros2 run robot_nodes hello_node
#include "rclcpp/rclcpp.hpp"
class HelloNode : public rclcpp::Node {
public:
// TODO: call Node("hello_node") in the constructor and log "hello from C++17" with RCLCPP_INFO
};
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
// TODO: spin a HelloNode created with std::make_shared, then shut ROS down
return 0;
}Expected output
[INFO] [hello_node]: hello from C++17Stuck? Show the full solution (try it yourself first)
// src/hello_node.cpp build: colcon build --packages-select robot_nodes run: ros2 run robot_nodes hello_node
#include "rclcpp/rclcpp.hpp"
class HelloNode : public rclcpp::Node {
public:
HelloNode() : Node("hello_node") { RCLCPP_INFO(get_logger(), "hello from C++17"); }
};
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<HelloNode>());
rclcpp::shutdown();
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Create a package with ros2 pkg create --build-type ament_cmake, add HelloNode, build with colcon, run it with ros2 run, and confirm with ros2 node list from a second terminal. Press Ctrl+C and verify a clean exit.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.