A subscriber node listens to messages from other parts of a robot system. It helps your program react when new data arrives.
Creating a subscriber node in ROS
import rclpy from rclpy.node import Node from std_msgs.msg import String class MinimalSubscriber(Node): def __init__(self): super().__init__('minimal_subscriber') self.subscription = self.create_subscription( String, 'topic', self.listener_callback, 10) self.subscription # prevent unused variable warning def listener_callback(self, msg): self.get_logger().info('I heard: "%s"' % msg.data) def main(args=None): rclpy.init(args=args) minimal_subscriber = MinimalSubscriber() rclpy.spin(minimal_subscriber) minimal_subscriber.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()
The create_subscription method sets up the subscriber.
The callback function runs every time a new message arrives.
self.subscription = self.create_subscription(String, 'chatter', self.listener_callback, 10)
def listener_callback(self, msg): self.get_logger().info(f'Received: {msg.data}')
This program creates a subscriber node named 'minimal_subscriber'. It listens to the 'greetings' topic for String messages. When a message arrives, it prints the message content.
import rclpy from rclpy.node import Node from std_msgs.msg import String class MinimalSubscriber(Node): def __init__(self): super().__init__('minimal_subscriber') self.subscription = self.create_subscription( String, 'greetings', self.listener_callback, 10) self.subscription # prevent unused variable warning def listener_callback(self, msg): self.get_logger().info(f'I heard: "{msg.data}"') def main(args=None): rclpy.init(args=args) minimal_subscriber = MinimalSubscriber() rclpy.spin(minimal_subscriber) minimal_subscriber.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()
Make sure the topic name matches the publisher's topic exactly.
The queue size (here 10) controls how many messages to store if they arrive faster than processed.
Use rclpy.spin() to keep the node alive and listening.
A subscriber node listens to messages on a topic.
Use create_subscription with message type, topic name, callback, and queue size.
The callback runs each time a new message arrives.
Practice
What is the main purpose of a subscriber node in ROS?
Solution
Step 1: Understand the role of a subscriber node
A subscriber node listens to messages sent on a specific topic in ROS.Step 2: Compare options with the subscriber role
Only To listen and react to messages published on a topic describes listening and reacting to messages, which matches the subscriber's purpose.Final Answer:
To listen and react to messages published on a topic -> Option BQuick Check:
Subscriber node = listens to messages [OK]
- Confusing subscriber with publisher
- Thinking subscriber creates topics
- Assuming subscriber stores messages permanently
Which of the following is the correct way to create a subscriber in ROS 2 Python?
node.create_subscription(____, 'chatter', callback, 10)Solution
Step 1: Identify the correct message type import
In ROS 2 Python, message types are imported as modules, e.g., std_msgs.msg.String.Step 2: Match the correct syntax for create_subscription
The first argument must be the full message type class, so std_msgs.msg.String is correct.Final Answer:
std_msgs.msg.String -> Option AQuick Check:
Message type = std_msgs.msg.String [OK]
- Using just 'String' without module path
- Using slash notation like 'std_msgs/String'
- Using lowercase 'string' instead of class name
Given this ROS 2 Python subscriber code snippet, what will be printed when a message with data 'Hello' arrives?
def callback(msg):
print(f'Received: {msg.data}')
node.create_subscription(std_msgs.msg.String, 'chatter', callback, 10)Solution
Step 1: Understand the callback function
The callback prints 'Received: ' followed by the message's data attribute.Step 2: Substitute the message data value
When the message data is 'Hello', the print outputs 'Received: Hello'.Final Answer:
Received: Hello -> Option CQuick Check:
Prints 'Received: ' + msg.data [OK]
- Printing just the data without prefix
- Printing the variable name instead of its value
- Confusing string formatting syntax
What is wrong with this ROS 2 Python subscriber code?
def callback(msg):
print(msg.data)
node.create_subscription(std_msgs.msg.String, 'chatter', callback())Solution
Step 1: Check how callback is passed
The code uses callback() which calls the function immediately instead of passing the function reference.Step 2: Understand correct callback usage
We must pass callback without parentheses so it is called on message arrival.Final Answer:
Callback is called immediately instead of passed as a function -> Option DQuick Check:
Pass callback function, not call it [OK]
- Adding parentheses to callback in subscription
- Omitting queue size argument (optional in some versions)
- Using variable instead of string for topic name
You want to create a subscriber node that listens to the topic 'sensor_data' and prints only messages where the data is greater than 10. Which callback function correctly implements this?
def callback(msg):
# Fill in the condition
if ____:
print(f"Data: {msg.data}")Solution
Step 1: Understand the requirement
We want to print messages only if data is greater than 10, so the condition must check msg.data > 10.Step 2: Compare options with condition
msg.data > 10 matches exactly 'greater than 10'. Others check equal or less, which do not meet the requirement.Final Answer:
msg.data > 10 -> Option AQuick Check:
Condition for data > 10 is msg.data > 10 [OK]
- Using >= instead of > changes condition
- Using < or == prints wrong messages
- Not adding any condition prints all messages
Ready to prove it?
Pass the test and this pattern moves unknown
