Challenge - 5 Problems
ROS2 Subscriber Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat does this ROS subscriber node print when it receives a message?
Consider this ROS subscriber node written in Python. What will it print when a message with data 'Hello ROS' is received?
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(f'Received: {msg.data}') def main(args=None): rclpy.init(args=args) minimal_subscriber = MinimalSubscriber() rclpy.spin(minimal_subscriber) minimal_subscriber.destroy_node() rclpy.shutdown()
Attempts:
2 left
💡 Hint
Look at how the callback uses msg.data inside the info log.
✗ Incorrect
The subscriber callback receives a message object of type String. Accessing msg.data gives the string content. The logger prints 'Received: ' followed by the message data.
📝 Syntax
intermediateWhich option correctly creates a ROS2 subscriber node in Python?
Select the code snippet that correctly creates a subscriber node for topic 'chatter' with message type String.
Attempts:
2 left
💡 Hint
Remember that create_subscription is a method of the Node class and requires the message type, topic name, callback, and queue size.
✗ Incorrect
Option A correctly calls self.create_subscription with the message type String, topic 'chatter', the callback method, and queue size 10. Option A misses self. Option A passes a string instead of the message type. Option A uses callback without self.
❓ state_output
advancedWhat is the value of 'count' after receiving 3 messages in this subscriber node?
Given this subscriber node that counts messages received, what is the value of self.count after 3 messages arrive?
ROS
import rclpy from rclpy.node import Node from std_msgs.msg import String class CounterSubscriber(Node): def __init__(self): super().__init__('counter_subscriber') self.count = 0 self.subscription = self.create_subscription( String, 'count_topic', self.listener_callback, 10) def listener_callback(self, msg): self.count += 1 self.get_logger().info(f'Message count: {self.count}')
Attempts:
2 left
💡 Hint
The count increments by 1 each time the callback runs.
✗ Incorrect
The count starts at 0 and increments by 1 on each message received. After 3 messages, count is 3.
🔧 Debug
advancedWhy does this ROS2 subscriber node fail to receive messages?
Examine this subscriber node code. Why does it not receive any messages?
ROS
import rclpy from rclpy.node import Node from std_msgs.msg import String class SilentSubscriber(Node): def __init__(self): super().__init__('silent_subscriber') self.subscription = self.create_subscription( String, 'silent_topic', self.listener_callback, 10) def listener_callback(self, msg): print(f'Received: {msg.data}') def main(args=None): rclpy.init(args=args) node = SilentSubscriber() # Missing rclpy.spin(node) node.destroy_node() rclpy.shutdown()
Attempts:
2 left
💡 Hint
Think about what keeps the node alive and processing callbacks.
✗ Incorrect
Without calling rclpy.spin(node), the node does not enter the loop to process incoming messages and callbacks are never triggered.
🧠 Conceptual
expertWhich statement best describes the role of QoS (Quality of Service) in ROS2 subscriber nodes?
Choose the most accurate explanation of how QoS settings affect a ROS2 subscriber node.
Attempts:
2 left
💡 Hint
Think about how ROS2 handles message delivery in unreliable networks or when nodes start late.
✗ Incorrect
QoS policies in ROS2 define how messages are delivered, including reliability (best effort or reliable), durability (transient or volatile), and history (keep last or all), which influence subscriber behavior.
