1. Home
  2. /
  3. Ros-ros2
  4. /
  5. ROS2 Subscriber Node (C++)

ROS2 Subscriber Node (C++) - Ros-ros2 Typing CST Test

Loading…

ROS2 Subscriber Node (C++) — Ros-ros2 Code

A simple ROS2 subscriber node that listens to messages from a topic.

#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>

class MinimalSubscriber : public rclcpp::Node {
public:
	MinimalSubscriber() : Node("minimal_subscriber") {
		subscription_ = this->create_subscription<std_msgs::msg::String>(
		"topic", 10,
		std::bind(&MinimalSubscriber::topic_callback, this, std::placeholders::_1));
	}

private:
	void topic_callback(const std_msgs::msg::String::SharedPtr msg) const {
		RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
	}

	rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char * argv[]) {
	rclcpp::init(argc, argv);
	rclcpp::spin(std::make_shared<MinimalSubscriber>());
	rclcpp::shutdown();
	return 0;
}

Ros-ros2 Language Guide

ROS (Robot Operating System) and ROS2 are open-source frameworks for robot software development. They provide libraries, tools, and conventions to simplify programming, communication, and control in robotics, supporting modularity, real-time systems, and hardware abstraction.

Primary Use Cases

  • ▸Robot perception, navigation, and control
  • ▸Multi-robot coordination and communication
  • ▸Simulation and testing of robotic systems
  • ▸Integration with sensors, actuators, and middleware
  • ▸Development of autonomous systems and AI robotics

Notable Features

  • ▸Node-based modular architecture
  • ▸Communication via topics, services, and actions
  • ▸Hardware abstraction for diverse robot platforms
  • ▸Simulation and visualization integration
  • ▸Cross-platform support in ROS2 (Linux, Windows, macOS)

Origin & Creator

ROS was created by the Stanford AI Lab and Willow Garage around 2007-2009. ROS2 development started by Open Robotics in 2014 to address ROS1 limitations in real-time, security, and cross-platform support.

Industrial Note

ROS2 is increasingly adopted in industrial automation, autonomous vehicles, and robotics middleware due to its support for real-time deterministic systems and secure communication.

More Ros-ros2 Typing Exercises

ROS2 Publisher Node (Python)ROS2 Service Node (Python)

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher