yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
MicroROS Robot Expansion Board📖
01. microROS control board development environment
  • 1. Introduction to microROS control board
  • 2. Set up ESP32-IDF development environment
  • 3. ESP32-IDF configuration tool
  • 4. Install ESP32-microros components
  • 5. Install and start microROS agent
  • 6. Flash-tool burning firmware
02. ESP32 basic course
  • 1. Turn on the LED light
  • 2. Button function
  • 3. Drive the buzzer
  • 4. Serial communication
  • 5. Battery voltage detection
  • 6. Drive PWM servo
  • 7. Drive motor
  • 8. Read motor encoder data
  • 9. PID controls car speed
  • 10. Read IMU data
  • 11. Read radar data
  • 12. Flash access data
  • 13. Partition table and memory
  • 14. Bluetooth communication
  • 15. WiFi networking
  • 16. Robot kinematics analysis
  • 17.Drive ESP32 WiFi camera
03. microros basic course
  • 1.Publish topic
  • 2. Subscribe topic
  • 3. Multi-topic subscribe and publish
  • 4. Subscribe buzzer topics
  • 5. Subscribe PWM servo topics
  • 6. Subscribe speed control topics
  • 7. Release speed topic
  • 8. Release IMU data topic
  • 9. Publish lidar data topics
  • 10. Customized transmission method
04.ROS2 basic course
  • 1. Introduction to ROS2
  • 2.ROS2 install Humble
  • 3.ROS2 development environment
  • 4.ROS2 workspace
  • 5.ROS2 function package
  • 6.ROS2 node
  • 7.ROS2 topic communication
  • 8.ROS2 service communication
  • 9.ROS2 action communication
  • 10.ROS2 custom interface message
  • 11.ROS2 parameter service case
  • 12.ROS2 meta-function package
  • 13.ROS2 distributed communication
  • 14.ROS2 DDS
  • 15.ROS2 time related API
  • 16.ROS2 common command tools
  • 17.ROS2 rviz2 use
  • 18.ROS2 rqt toolbox
  • 19.ROS2 Launch startup file configuration
  • 20.ROS2 recording and playback tool
  • 21.ROS2 URDF model
  • 22.ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
05. Robot basic course
4.1 Robot information release
  • Robot information release
4.0 Preparation
  • 1. Write firmware
  • 2. How to install and use VM
  • 3. microROS control board configuration
  • 4. Connect to microROS agent
4.2 Robot keyboard control
  • Robot keyboard control
4.3 Robot handle control
  • Robot handle control
4.4 Robot state estimation
  • Robot state estimation
4.5 Line speed calibration
  • Line speed calibration
4.6 Angular velocity calibration
  • Angular velocity calibration
4.7 Robot URDF model
  • Robot URDF model
Download
  • Firmware And Code
  • VM_File
  • 3D Model File
  • ROS2_Basic_VM
Download ZIP

Welcome to MicroROS Robot Expansion Board repository

7.ROS2 topic communication

7. ROS2 Topic Communication

1. Introduction to Topic Communication

Topic communication is the most frequently used communication method in ROS2. A publisher publishes data on a specified topic, and subscribers who subscribe to that topic receive the data.

Topic communication is based on the publish/subscribe model, as shown in the figure:

image8

Topic data transmission is a process where the data is transmitted from one node to another. The object sending data is called a publisher, and the object receiving data is called a subscriber. Each topic must have a name, and the data transmitted must have a fixed data type.

Next, we will explain how to implement topic communication between nodes using Python.

2. Create a New Package

  • Switch to the src directory of the workspace
  • Create a new pkg_topic package
xxxxxxxxxx
ros2 pkg create pkg_topic --build-type ament_python --dependencies rclpy --node-name publisher_demo

After executing the above command, the pkg_topic package will be created, along with a publisher_demo node and the relevant configuration files.

image-20231023170921392

3. Publisher Implementation

3.1 Create a Publisher

Next, edit [publisher_demo.py] to implement the publisher functionality and add the following code:

xxxxxxxxxx
#Import the rclpy library
import rclpy
from rclpy.node import Node
#Import String messages
from std_msgs.msg import String 
#Create a Topic_Pub node subclass that inherits from the Node base
class Topic_Pub(Node):
    def __init__(self,name):
        super().__init__(name)
        #Create a publisher using the create_publisher function. The passed parameters are:
        #Topic data type, topic name, and queue length for storing messages
        self.pub = self.create_publisher(String,"/topic_demo",1) 
        #Create a timer that enters the interrupt handler every 1 second. The passed parameters are:
        #Interval between interrupt executions, interrupt handler function
        self.timer = self.create_timer(1,self.pub_msg)
    #Define the interrupt handler function
    def pub_msg(self):
        msg = String()  #Create a String variable, msg
        msg.data = "Hi,I send a message." #Assign data to msg
        self.pub.publish(msg) #Publish topic data
        
#Main function
def main():
    rclpy.init() #Initialization
    pub_demo = Topic_Pub("publisher_node") #Create a Topic_Pub class object, passing in the node name as a parameter
    rclpy.spin(pub_demo)     #Execute the rclpy.spin function, passing in the Topic_Pub class object just created as a parameter
    pub_demo.destroy_node()  #Destroy the node object
    rclpy.shutdown()         #Shut down the ROS2 Python interface

 

3.2 Editing the Configuration File

image-20231023173312172

 

3.3 Compiling the Package

  • Compiling the Package
xxxxxxxxxx
colcon build --packages-select pkg_topic
  • Refresh the environment variables in the workspace

image-20250905115606561

3.4 Running the Program

  • After refreshing the environment variables, run the command
xxxxxxxxxx
ros2 run pkg_topic publisher_demo

After the program successfully runs, nothing is printed. We can use the ros2 topic tool to view the data. First, check if there are any topics being published. Open another terminal and enter:

xxxxxxxxxx
ros2 topic list

image-20231023173728019

This topic_demo is the topic data defined in the program. Next, we'll use ros2 topic echo to print this data. In the terminal, enter:

xxxxxxxxxx
ros2 topic echo /topic_demo

image-20231023173821223

As you can see, the output "Hi, I send a message." from the terminal matches the line msg.data = "Hi, I send a message." in our code.

4. Subscriber Implementation

4.1 Creating a Subscriber

Create a new file, [subscriber_demo.py], in the same directory as [publisher_demo.py].

image-20231023174819565

Next, edit [subscriber_demo.py] to implement the subscriber functionality and add the following code:

xxxxxxxxxx
#Import related libraries
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
​
class Topic_Sub(Node):
    def __init__(self,name):
        super().__init__(name)  
        #Create a subscriber using create_subscription. The passed parameters are: topic data type, topic name, callback function name, and queue length.
        self.sub = self.create_subscription(String,"/topic_demo",self.sub_callback,1) 
    #Callback function executes the program: prints the received message.
    def sub_callback(self,msg):
        # print(msg.data,flush=True)
        self.get_logger().info(msg.data)
​
​
​
def main():
    rclpy.init() #Initialize the ROS2 Python interface.
    sub_demo = Topic_Sub("subscriber_node")#Create the object and initialize it.
    rclpy.spin(sub_demo)
    sub_demo.destroy_node()  #Destroy the node object
    rclpy.shutdown()         #Shut down the ROS2 Python interface

 

4.2 Editing the Configuration File

image-20231023175338754

 

4.3 Compile the Workspace

  • Compile the package
xxxxxxxxxx
colcon build --packages-select pkg_topic
  • Refresh the environment variables in the workspace

image-20250905115606561

4.4 Run the Program

Execute the following command in a separate terminal:

xxxxxxxxxx
# Start the publisher node
ros2 run pkg_topic publisher_demo
# Start the subscriber node
ros2 run pkg_topic subscriber_demo

image-20250905120356726

As shown in the figure above, the terminal running the subscriber node will print the information published by the publisher, /topic_demo.

ROS2 node
ROS2 service communication

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文