Bird
Raised Fist0
ROSframework~20 mins

Creating a subscriber node in ROS - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ROS2 Subscriber Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What 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()
AIt prints: 'Received: Hello ROS'
BIt prints: 'Hello ROS'
CIt prints: 'Received: std_msgs.msg.String(data=Hello ROS)'
DIt prints nothing because the callback is not called
Attempts:
2 left
💡 Hint
Look at how the callback uses msg.data inside the info log.
📝 Syntax
intermediate
2:00remaining
Which 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.
Aself.subscription = self.create_subscription(String, 'chatter', self.callback, 10)
Bself.subscription = create_subscription(String, 'chatter', self.callback, 10)
Cself.subscription = self.create_subscription('String', 'chatter', self.callback, 10)
Dself.subscription = self.create_subscription(String, 'chatter', callback, 10)
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.
❓ state_output
advanced
2:00remaining
What 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}')
A0
B2
C3
DRaises an AttributeError
Attempts:
2 left
💡 Hint
The count increments by 1 each time the callback runs.
🔧 Debug
advanced
2:00remaining
Why 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()
ABecause the subscription queue size 10 is too small
BBecause the topic name 'silent_topic' is incorrect
CBecause the callback uses print instead of get_logger().info()
DBecause rclpy.spin(node) is missing, so callbacks never run
Attempts:
2 left
💡 Hint
Think about what keeps the node alive and processing callbacks.
🧠 Conceptual
expert
2:00remaining
Which 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.
AQoS is a debugging tool that logs message traffic for subscriber nodes
BQoS controls message delivery reliability, durability, and history, affecting how the subscriber receives messages under different network conditions
CQoS determines the message type the subscriber can accept and automatically converts incompatible messages
DQoS only sets the maximum queue size for incoming messages and does not affect delivery guarantees
Attempts:
2 left
💡 Hint
Think about how ROS2 handles message delivery in unreliable networks or when nodes start late.

Practice

(1/5)
1.

What is the main purpose of a subscriber node in ROS?

easy
A. To send messages to other nodes
B. To listen and react to messages published on a topic
C. To create new topics for communication
D. To store messages permanently

Solution

  1. Step 1: Understand the role of a subscriber node

    A subscriber node listens to messages sent on a specific topic in ROS.
  2. 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.
  3. Final Answer:

    To listen and react to messages published on a topic -> Option B
  4. Quick Check:

    Subscriber node = listens to messages [OK]
Hint: Subscriber nodes always listen to topics, not send [OK]
Common Mistakes:
  • Confusing subscriber with publisher
  • Thinking subscriber creates topics
  • Assuming subscriber stores messages permanently
2.

Which of the following is the correct way to create a subscriber in ROS 2 Python?

node.create_subscription(____, 'chatter', callback, 10)
easy
A. std_msgs.msg.String
B. String
C. std_msgs/String
D. string

Solution

  1. Step 1: Identify the correct message type import

    In ROS 2 Python, message types are imported as modules, e.g., std_msgs.msg.String.
  2. 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.
  3. Final Answer:

    std_msgs.msg.String -> Option A
  4. Quick Check:

    Message type = std_msgs.msg.String [OK]
Hint: Use full message type path with .msg in ROS 2 Python [OK]
Common Mistakes:
  • Using just 'String' without module path
  • Using slash notation like 'std_msgs/String'
  • Using lowercase 'string' instead of class name
3.

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)
medium
A. Received: msg.data
B. Hello
C. Received: Hello
D. msg.data

Solution

  1. Step 1: Understand the callback function

    The callback prints 'Received: ' followed by the message's data attribute.
  2. Step 2: Substitute the message data value

    When the message data is 'Hello', the print outputs 'Received: Hello'.
  3. Final Answer:

    Received: Hello -> Option C
  4. Quick Check:

    Prints 'Received: ' + msg.data [OK]
Hint: Callback prints 'Received: ' plus message data [OK]
Common Mistakes:
  • Printing just the data without prefix
  • Printing the variable name instead of its value
  • Confusing string formatting syntax
4.

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())
medium
A. Missing message type in create_subscription
B. Queue size argument is missing but required
C. Topic name should be a variable, not a string
D. Callback is called immediately instead of passed as a function

Solution

  1. Step 1: Check how callback is passed

    The code uses callback() which calls the function immediately instead of passing the function reference.
  2. Step 2: Understand correct callback usage

    We must pass callback without parentheses so it is called on message arrival.
  3. Final Answer:

    Callback is called immediately instead of passed as a function -> Option D
  4. Quick Check:

    Pass callback function, not call it [OK]
Hint: Pass callback without () to create_subscription [OK]
Common Mistakes:
  • Adding parentheses to callback in subscription
  • Omitting queue size argument (optional in some versions)
  • Using variable instead of string for topic name
5.

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}")
hard
A. msg.data > 10
B. msg.data >= 10
C. msg.data < 10
D. msg.data == 10

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    msg.data > 10 -> Option A
  4. Quick Check:

    Condition for data > 10 is msg.data > 10 [OK]
Hint: Use > 10 to filter messages with data above 10 [OK]
Common Mistakes:
  • Using >= instead of > changes condition
  • Using < or == prints wrong messages
  • Not adding any condition prints all messages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard