Welcome to RDK S600 Developer Kit repository
7.ROS2 topic communication
7. ROS2 Topic Communication
Note: The accompanying reference video uses the ROS Humble version, for reference only
1. Topic Communication Overview
Topic communication is the most frequently used communication method in ROS2. A publisher publishes data on a specified topic, and subscribers who have subscribed to that topic can receive the data.
Topic communication is based on the publish/subscribe model, as shown in the figure:

The characteristics of topic data transmission are: from one node to another. The object sending data is called the publisher, and the object receiving data is called the subscriber. Each topic needs a name, and the transmitted data also needs a fixed data type.
Next, we will explain how to implement topic communication between nodes using the Python language.
2. Create New Package
Navigate to the workspace src directory:
Create the pkg_topic package:
ros2 pkg create pkg_topic --build-type ament_python --dependencies rclpy --node-name publisher_demoAfter executing the above command, the pkg_topic package will be created, along with a publisher_demo node, and the related configuration files will already be set up.

3. Publisher Implementation
3.1 Create Publisher
Next, edit [publisher_demo.py] to implement the publisher functionality. Add the following code:
x# Import rclpy libraryimport rclpyfrom rclpy.node import Node# Import String messagefrom std_msgs.msg import String# Create a Topic_Pub node subclass inheriting from Node base class with a parameter nameclass Topic_Pub(Node): def __init__(self,name): super().__init__(name) # Create a publisher using create_publisher function with parameters: # topic data type, topic name, message queue length self.pub = self.create_publisher(String,"/topic_demo",1) # Create a timer that enters the interrupt handler every 1 second with parameters: # interrupt handler execution interval, interrupt handler function self.timer = self.create_timer(1,self.pub_msg) # Define interrupt handler function def pub_msg(self): msg = String() # Create a String type variable msg msg.data = "Hi,I send a message." # Assign value to msg.data self.pub.publish(msg) # Publish topic data
# Main functiondef main(): rclpy.init() # Initialize pub_demo = Topic_Pub("publisher_node") # Create Topic_Pub class object with node name as parameter rclpy.spin(pub_demo) # Execute rclpy.spin function with Topic_Pub class object as parameter pub_demo.destroy_node() # Destroy node object rclpy.shutdown() # Shutdown ROS2 Python interface
3.2 Edit Configuration File

3.3 Build Package
Build the package:
xxxxxxxxxxcolcon build --packages-select pkg_topicRefresh environment variables in the workspace:

3.4 Run Program
After refreshing environment variables, run the command:
xxxxxxxxxxros2 run pkg_topic publisher_demoWhen the program runs successfully, there is no output. We can use the ros2 topic tool to view data. First, check if there are topics published. Open another terminal and enter:
xxxxxxxxxxros2 topic list
topic_demo is the topic data defined in the program. Next, we use ros2 topic echo to print this data. Enter in the terminal:
xxxxxxxxxxros2 topic echo /topic_demo
As you can see, the terminal prints "Hi,I send a message." which matches msg.data = "Hi,I send a message." in the code.
4. Subscriber Implementation
4.1 Create Subscriber
Create a new file [subscriber_demo.py] in the same directory as [publisher_demo.py]:

Next, edit [subscriber_demo.py] to implement the subscriber functionality. Add the following code:
xxxxxxxxxx# Import related librariesimport rclpyfrom rclpy.node import Nodefrom std_msgs.msg import String
class Topic_Sub(Node): def __init__(self,name): super().__init__(name) # Create subscriber using create_subscription with parameters: # topic data type, topic name, callback function name, queue length self.sub = self.create_subscription(String,"/topic_demo",self.sub_callback,1) # Callback function executes: print received information def sub_callback(self,msg): # print(msg.data,flush=True) self.get_logger().info(msg.data)
def main(): rclpy.init() # ROS2 Python interface initialization sub_demo = Topic_Sub("subscriber_node") # Create object and initialize rclpy.spin(sub_demo) sub_demo.destroy_node() # Destroy node object rclpy.shutdown() # Shutdown ROS2 Python interface
4.2 Edit Configuration File

4.3 Build Workspace
Build the package:
xxxxxxxxxxcolcon build --packages-select pkg_topicRefresh environment variables in the workspace:

4.4 Run Program
Split terminal and execute:
xxxxxxxxxx# Start publisher noderos2 run pkg_topic publisher_demo# Start subscriber noderos2 run pkg_topic subscriber_demo
As shown in the image above, the terminal running the subscriber will print the information published by the publisher on /topic_demo.