Getting Started with ROS on Embedded Systems - User Guide - C++ - Publishers and subscribers
Getting Started with ROS on Embedded Systems RidgeRun documentation is currently under development. |
Getting Started with ROS on Embedded Systems |
---|
![]() |
ROS on Embedded Systems Basics |
Getting Started |
C++ User Guide |
Examples |
Basic pipelines |
Performance |
Xavier |
Contact Us |
Introduction
This wiki is a small introduction to the Publishers and Subscriber model that ROS is based upon. ROS2 uses a topic's name and the message type to bind n publishers and n subscribers.
Publishers
In order to publish messages, you will need a node handle, a message type and create a rclcpp::Publisher<TYPE> using the rclcpp::Node::create_publisher function. Like the following example:
rclcpp::Node pub_node = new rclcpp::Node(NONE_NAME); rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; publisher_ = pub_node->create_publisher<TYPE>(TOPIC_NAME, N);
- N is the max message queue.
- TOPIC_NAME is the name of the topic to publish to.
- TYPE is the type of the messages on the topic, like std_msgs::msg::String
- NONE_NAME the name of the node
Subscribers
Similar to publishers, a ROS subscriber needs a node handle and with the subscribe function it will create a new rclcpp::Subscription, we also need a callback method, like the following example:
class MinimalSubscriber : public rclcpp::Node { public: MinimalSubscriber(std::string channel_name) : Node("minimal_subscriber"), channel_name_(channel_name) { subscription_ = this->create_subscription<std_msgs::msg::String>( channel_name_, 10, std::bind(&MinimalSubscriber::topic_callback, this, _1)); } private: void topic_callback(const std_msgs::msg::String msg) const { RCLCPP_INFO(this->get_logger(), "Received message"); } rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; std::string channel_name_; };
The assigned callback will be executed each time there is a new message on the topic.