Bird
Raised Fist0
ROSframework~20 mins

Publish-subscribe communication model in ROS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ROS Pub-Sub Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a ROS publisher sends a message but no subscribers are connected?
In ROS, if a publisher node sends a message on a topic but no subscriber nodes are currently listening, what is the expected behavior?
AThe message is queued and delivered once a subscriber connects.
BThe message is discarded immediately since no subscribers are connected.
CThe publisher node will throw an error and stop publishing.
DThe message is stored permanently until a subscriber requests it.
Attempts:
2 left
💡 Hint

Think about how ROS handles message delivery when no one is listening.

❓ state_output
intermediate
2:00remaining
What is the output of this ROS subscriber callback when multiple messages arrive quickly?
Consider a ROS subscriber node with a callback that prints the received message data. If messages arrive faster than the callback can process, what will happen to the messages?
ROS
def callback(msg):
    print(f"Received: {msg.data}")

rospy.Subscriber('chatter', String, callback)
rospy.spin()
ASome messages may be dropped if the callback is too slow.
BThe subscriber will crash due to message overload.
CThe subscriber will buffer all messages indefinitely.
DAll messages are printed in order without loss.
Attempts:
2 left
💡 Hint

Consider the subscriber queue size and processing speed.

📝 Syntax
advanced
2:00remaining
Which ROS Python code snippet correctly creates a publisher with a queue size of 10?
Select the code snippet that correctly initializes a ROS publisher with a queue size of 10 in Python.
Apub = rospy.Publisher('topic_name', String, queue_size=10)
Bpub = rospy.Publisher('topic_name', String, 10)
Cpub = rospy.Publisher('topic_name', String, queue=10)
Dpub = rospy.Publisher('topic_name', String, buffer_size=10)
Attempts:
2 left
💡 Hint

Check the parameter name for queue size in rospy.Publisher.

🔧 Debug
advanced
2:00remaining
Why does this ROS subscriber callback never get called?
Given the following ROS Python subscriber code, why does the callback never execute?
ROS
import rospy
from std_msgs.msg import String

def callback(msg):
    print(f"Got: {msg.data}")

rospy.init_node('listener')
rospy.Subscriber('chatter', String, callback)
# Missing rospy.spin() here
AThe topic name 'chatter' is invalid and no messages arrive.
BThe callback function is incorrectly defined and never called.
CBecause rospy.spin() is missing, the node exits immediately before receiving messages.
DThe subscriber is created after rospy.init_node(), which is not allowed.
Attempts:
2 left
💡 Hint

Think about what keeps a ROS node alive to receive messages.

🧠 Conceptual
expert
3:00remaining
In ROS publish-subscribe, what ensures that multiple subscribers receive the same message independently?
Which feature of the ROS publish-subscribe model guarantees that each subscriber gets its own copy of a message published on a topic?
ASubscribers pull messages from the publisher on demand.
BROS sends a single message instance shared among all subscribers.
CMessages are stored in a global buffer accessible by all subscribers.
DROS uses a message queue per subscriber to deliver copies independently.
Attempts:
2 left
💡 Hint

Consider how message delivery isolation is handled in ROS.

Practice

(1/5)
1. What is the main role of a publisher in the ROS publish-subscribe model?
easy
A. To create new topics dynamically
B. To receive messages from a topic
C. To store messages in a database
D. To send messages to a topic

Solution

  1. Step 1: Understand the publish-subscribe roles

    In ROS, publishers send messages to topics, while subscribers receive messages from topics.
  2. Step 2: Identify the publisher's function

    The publisher's job is to send data to a topic so subscribers can receive it.
  3. Final Answer:

    To send messages to a topic -> Option D
  4. Quick Check:

    Publisher = sends messages [OK]
Hint: Publisher sends, subscriber receives messages [OK]
Common Mistakes:
  • Confusing publisher with subscriber
  • Thinking publisher stores messages
  • Assuming publisher creates topics
2. Which of the following is the correct way to create a ROS publisher in Python?
easy
A. pub = rospy.Publisher('topic_name', callback, queue_size=10)
B. pub = rospy.Subscriber('topic_name', String, callback)
C. pub = rospy.Publisher('topic_name', String, queue_size=10)
D. pub = rospy.Subscriber('topic_name', String)

Solution

  1. Step 1: Recall publisher syntax in ROS Python

    The correct syntax uses rospy.Publisher with topic name, message type, and queue size.
  2. Step 2: Check each option

    pub = rospy.Publisher('topic_name', String, queue_size=10) matches the correct syntax. Using Subscriber instead of Publisher is incorrect. pub = rospy.Publisher('topic_name', callback, queue_size=10) incorrectly uses callback instead of message type.
  3. Final Answer:

    pub = rospy.Publisher('topic_name', String, queue_size=10) -> Option C
  4. Quick Check:

    Publisher syntax = rospy.Publisher(topic, type, queue_size) [OK]
Hint: Publisher uses rospy.Publisher(topic, type, queue_size) [OK]
Common Mistakes:
  • Using Subscriber instead of Publisher
  • Passing callback instead of message type
  • Omitting queue_size parameter
3. Given the following ROS Python code snippet, what will be printed when a message with data 'Hello' is published?
import rospy
from std_msgs.msg import String

def callback(msg):
    print(f'Received: {msg.data}')

rospy.init_node('listener')
sub = rospy.Subscriber('chatter', String, callback)
rospy.spin()
medium
A. Received: Hello
B. Hello
C. Received: msg
D. No output

Solution

  1. Step 1: Understand the callback function

    The callback prints 'Received: ' followed by the message's data field.
  2. Step 2: Analyze the message content

    The message data is 'Hello', so the print statement outputs 'Received: Hello'.
  3. Final Answer:

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

    Callback prints 'Received: ' + msg.data [OK]
Hint: Callback prints 'Received: ' plus message data [OK]
Common Mistakes:
  • Printing message object instead of data
  • Expecting just the message data without prefix
  • Thinking no output occurs without explicit print
4. Identify the error in this ROS Python subscriber code:
import rospy
from std_msgs.msg import String

def callback(msg):
    print(msg.data)

rospy.init_node('listener')
sub = rospy.Subscriber('chatter', String)
rospy.spin()
medium
A. Incorrect message type used
B. Missing callback function in Subscriber
C. Node not initialized
D. No import for rospy

Solution

  1. Step 1: Review Subscriber syntax

    Subscriber requires three arguments: topic name, message type, and callback function.
  2. Step 2: Check the given code

    The Subscriber call misses the callback argument, causing an error.
  3. Final Answer:

    Missing callback function in Subscriber -> Option B
  4. Quick Check:

    Subscriber needs callback argument [OK]
Hint: Subscriber must include callback function [OK]
Common Mistakes:
  • Omitting callback in Subscriber
  • Confusing Publisher and Subscriber arguments
  • Forgetting to initialize node
5. You want to create a ROS system where multiple nodes publish sensor data to the same topic, and one node subscribes to process all data. Which feature of the publish-subscribe model makes this possible?
hard
A. Topics allow multiple publishers and subscribers to communicate independently
B. Each topic can have only one publisher and one subscriber
C. Publishers must know all subscribers to send messages
D. Subscribers can only listen to one topic at a time

Solution

  1. Step 1: Understand topic communication in ROS

    Topics support multiple publishers and subscribers simultaneously, enabling flexible communication.
  2. Step 2: Analyze the scenario

    Multiple nodes publishing to the same topic and one subscriber processing all data is possible because topics allow many publishers and subscribers.
  3. Final Answer:

    Topics allow multiple publishers and subscribers to communicate independently -> Option A
  4. Quick Check:

    Topics support many-to-many communication [OK]
Hint: Topics support many publishers and subscribers [OK]
Common Mistakes:
  • Thinking topics allow only one publisher or subscriber
  • Believing publishers must know subscribers
  • Assuming subscribers can listen to only one topic
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