📖 Scenario: You are building a simple ROS node that sends messages to other nodes. This is like a radio station broadcasting news to listeners. Your node will publish a greeting message repeatedly.
🎯 Goal: Create a ROS publisher node in Python that sends the message "Hello ROS" on the topic chatter at 1 message per second.
📋 What You'll Learn
Create a ROS node named talker
Create a publisher for the topic chatter with message type String
Set the publishing rate to 1 Hz
Publish the message "Hello ROS" repeatedly
💡 Why This Matters
🌍 Real World
Publishing messages is how ROS nodes communicate sensor data, commands, or status updates in robots.
💼 Career
Understanding how to create publisher nodes is essential for robotics software engineers working with ROS to build modular and interactive robot systems.
Progress0 / 4 steps
1
Setup ROS node and import dependencies
Write the first lines to import rclpy and String from std_msgs.msg. Then initialize the ROS client library with rclpy.init() and create a node named talker using rclpy.create_node('talker').
ROS
Hint
Remember to import rclpy and the message type String. Then initialize ROS and create the node with the exact name 'talker'.
2
Create a publisher and set the publishing rate
Add a publisher to the node named publisher that publishes String messages on the topic chatter. Then create a rate variable set to 1.0 to set the publishing frequency to 1 Hz.
ROS
Hint
Use node.create_publisher(String, 'chatter') to create the publisher. Use rate = 1.0 to set 1 message per second.
3
Publish the message in a loop
Write a while rclpy.ok() loop that creates a String message with data 'Hello ROS' and publishes it using publisher.publish(). Then call rclpy.spin_once(node, timeout_sec=rate) inside the loop to wait for 1 second between messages and process ROS events.
ROS
Hint
Use a while rclpy.ok() loop to keep publishing. Create a String message, set data to 'Hello ROS', publish it, then rclpy.spin_once(node, timeout_sec=rate).
4
Shutdown the ROS node cleanly
After the loop, call node.destroy_node() to clean up the node, then call rclpy.shutdown() to shut down the ROS client library.
ROS
Hint
Call node.destroy_node() and then rclpy.shutdown() after the publishing loop to clean up.
Practice
(1/5)
1. What is the main purpose of a publisher node in ROS?
easy
A. To store data in a database
B. To receive messages from other nodes
C. To compile the ROS package
D. To send messages on a topic to share information
Solution
Step 1: Understand the role of a publisher node
A publisher node sends messages to other nodes by publishing on a topic.
Step 2: Compare options with the role
Only To send messages on a topic to share information describes sending messages on a topic, which matches the publisher's role.
Final Answer:
To send messages on a topic to share information -> Option D
Quick Check:
Publisher node = sends messages [OK]
Hint: Publisher nodes send messages, subscribers receive them [OK]
Common Mistakes:
Confusing publisher with subscriber
Thinking publisher stores data
Assuming publisher compiles code
2. Which of the following is the correct way to create a publisher in a ROS2 Python node?
easy
A. self.createPublisher(String, 'topic_name', 10)
B. self.create_publisher(String, 'topic_name', 10)
C. create_publisher(String, 'topic_name', 10)
D. self.create_publisher('topic_name', String, 10)
Solution
Step 1: Recall ROS2 Python publisher syntax
The method is called on the node instance as self.create_publisher, with message type first, then topic name, then queue size.
Step 2: Check each option's syntax
self.create_publisher(String, 'topic_name', 10) matches the correct method name and argument order. self.createPublisher(String, 'topic_name', 10) has wrong method name casing. create_publisher(String, 'topic_name', 10) misses self. self.create_publisher('topic_name', String, 10) swaps argument order.
Final Answer:
self.create_publisher(String, 'topic_name', 10) -> Option B
Quick Check:
Correct method and argument order = self.create_publisher(String, 'topic_name', 10) [OK]
Hint: Use self.create_publisher with message type first [OK]
Common Mistakes:
Wrong method name casing
Missing self when calling method
Swapping topic name and message type
3. Given this ROS2 Python publisher code snippet, what will be printed when the timer callback runs?
B. create_publisher is called with wrong arguments
C. Timer is created outside the class constructor
D. msg.data is assigned incorrectly
Solution
Step 1: Check code structure and indentation
The self.timer = self.create_timer(1.0, timer_callback) line lacks indentation and appears outside __init__ at class level, where 'self' is undefined, causing NameError.
Step 2: Verify other options
A: timer_callback includes 'self'. B: create_publisher args are correct (type, topic, qos). D: msg.data = 'Hello' is valid.
Final Answer:
Timer is created outside the class constructor -> Option C
Quick Check:
self assignments must be inside instance methods like __init__ [OK]
Hint: Create timers inside __init__ with self.create_timer(1.0, self.timer_callback) [OK]
Common Mistakes:
Omitting 'self' in method parameters
Misordering create_publisher arguments
Creating timer outside class context
5. You want to create a ROS2 publisher node that publishes integers incrementing every second starting from 0. Which code snippet correctly implements this behavior?