The publish-subscribe model helps different parts of a robot system talk to each other without being directly connected. It makes communication easy and flexible.
Publish-subscribe communication model in ROS
import rospy from std_msgs.msg import String # Publisher node rospy.init_node('talker') publisher = rospy.Publisher('chatter', String, queue_size=10) rate = rospy.Rate(10) # 10 Hz while not rospy.is_shutdown(): message = "hello world" publisher.publish(message) rate.sleep() # Subscriber node def callback(data): rospy.loginfo(f"I heard {data.data}") rospy.init_node('listener') subscriber = rospy.Subscriber('chatter', String, callback) rospy.spin()
The publisher sends messages to a named topic.
The subscriber listens to that topic and runs a callback when a message arrives.
# Example: Empty message import rospy from std_msgs.msg import String rospy.init_node('listener') subscriber = rospy.Subscriber('chatter', String, lambda data: rospy.loginfo(f"Received: '{data.data}'")) rospy.spin()
# Example: Single message publish import rospy from std_msgs.msg import String rospy.init_node('talker') publisher = rospy.Publisher('chatter', String, queue_size=10) rospy.sleep(1) # Wait for connections publisher.publish("single message")
# Example: Subscriber with multiple callbacks import rospy from std_msgs.msg import String def callback_one(data): rospy.loginfo(f"Callback one got: {data.data}") def callback_two(data): rospy.loginfo(f"Callback two got: {data.data}") rospy.init_node('listener') subscriber = rospy.Subscriber('chatter', String, callback_one) subscriber2 = rospy.Subscriber('chatter', String, callback_two) rospy.spin()
This program creates a publisher that sends three messages, and a subscriber that listens and prints them. It shows how publish-subscribe works in ROS.
import rospy from std_msgs.msg import String # Publisher node class Talker: def __init__(self): self.publisher = rospy.Publisher('chatter', String, queue_size=10) self.rate = rospy.Rate(1) # 1 Hz def start(self): count = 0 while not rospy.is_shutdown() and count < 3: message = f"hello {count}" rospy.loginfo(f"Publishing: {message}") self.publisher.publish(message) self.rate.sleep() count += 1 # Subscriber node def callback(data): rospy.loginfo(f"I heard: {data.data}") if __name__ == '__main__': try: # Start subscriber node rospy.init_node('listener') subscriber = rospy.Subscriber('chatter', String, callback) # Start publisher talker = Talker() talker.start() rospy.spin() except rospy.ROSInterruptException: pass
Publishing and subscribing happen asynchronously, so messages may arrive with some delay.
Use queue_size to control how many messages are buffered if the subscriber is slow.
Common mistake: forgetting to call rospy.spin() in subscriber nodes, which stops callbacks from running.
Use publish-subscribe when you want loose coupling between parts of your robot system.
Publish-subscribe lets parts send and receive messages without knowing about each other.
Publishers send messages to topics; subscribers listen to those topics.
This model helps build flexible and scalable robot software.
Practice
publisher in the ROS publish-subscribe model?Solution
Step 1: Understand the publish-subscribe roles
In ROS, publishers send messages to topics, while subscribers receive messages from topics.Step 2: Identify the publisher's function
The publisher's job is to send data to a topic so subscribers can receive it.Final Answer:
To send messages to a topic -> Option DQuick Check:
Publisher = sends messages [OK]
- Confusing publisher with subscriber
- Thinking publisher stores messages
- Assuming publisher creates topics
Solution
Step 1: Recall publisher syntax in ROS Python
The correct syntax uses rospy.Publisher with topic name, message type, and queue size.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.Final Answer:
pub = rospy.Publisher('topic_name', String, queue_size=10) -> Option CQuick Check:
Publisher syntax = rospy.Publisher(topic, type, queue_size) [OK]
- Using Subscriber instead of Publisher
- Passing callback instead of message type
- Omitting queue_size parameter
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()Solution
Step 1: Understand the callback function
The callback prints 'Received: ' followed by the message's data field.Step 2: Analyze the message content
The message data is 'Hello', so the print statement outputs 'Received: Hello'.Final Answer:
Received: Hello -> Option AQuick Check:
Callback prints 'Received: ' + msg.data [OK]
- Printing message object instead of data
- Expecting just the message data without prefix
- Thinking no output occurs without explicit print
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()Solution
Step 1: Review Subscriber syntax
Subscriber requires three arguments: topic name, message type, and callback function.Step 2: Check the given code
The Subscriber call misses the callback argument, causing an error.Final Answer:
Missing callback function in Subscriber -> Option BQuick Check:
Subscriber needs callback argument [OK]
- Omitting callback in Subscriber
- Confusing Publisher and Subscriber arguments
- Forgetting to initialize node
Solution
Step 1: Understand topic communication in ROS
Topics support multiple publishers and subscribers simultaneously, enabling flexible communication.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.Final Answer:
Topics allow multiple publishers and subscribers to communicate independently -> Option AQuick Check:
Topics support many-to-many communication [OK]
- Thinking topics allow only one publisher or subscriber
- Believing publishers must know subscribers
- Assuming subscribers can listen to only one topic
Ready to prove it?
Pass the test and this pattern moves unknown
