Welcome to MicroROS V2 Robot Car repository
2. Subscribe To Topics
Subscribing to Topics
1. Experiment Objective
In this experiment, you will learn how to create a ROS2 topic subscriber on the MicroROS-V2 control board using the micro-ROS framework. Through this example, you will master the subscriber initialization method of micro-ROS, the writing of subscription callback functions, and the use of the executor. This example subscribes to Int32 messages on the topic /subscriber and prints the received data to the serial port.
2. Experiment Principle
1. Keyword Explanation
| Keyword | Description |
|---|---|
| micro-ROS Agent | A proxy program running on the ROS2 host side, responsible for bridging communication between the ESP32 firmware and the ROS2 system, connecting with the microcontroller via the UDP protocol |
| subscriber | The subscriber, the message-receiving end in ROS2 communication, subscribes to messages on a specified topic and triggers the callback function to process data when a new message arrives |
| rclc | A ROS2 client library for microcontrollers, providing lightweight node, publisher, subscriber and other APIs, adapted to resource-constrained embedded devices |
| callback (ON_NEW_DATA) | The subscription callback trigger mode, which invokes the callback function only when a new message is received, avoiding wasted CPU resources from empty polling |
| QoS (Best Effort) | The "best effort" mode in the quality-of-service policy, which does not guarantee message delivery and ordering, suitable for scenarios with high real-time requirements but tolerant of a small amount of packet loss |
| executor | The executor, an event-driven scheduler in micro-ROS, responsible for polling subscriber handles and invoking the corresponding callback functions when messages are received |
2. Technical Architecture
This experiment builds a one-way data channel from a ROS2 host to an ESP32 firmware subscriber:
Hardware layer: The MicroROS-V2 control board (ESP32) connects to the local network via Wi-Fi
Transport layer: Uses the UDP protocol, communicating with the ROS2 host through the micro-ROS Agent
Node configuration: Node name
esp32_subscriber, subscribing to topic/subscriberMessage type:
std_msgs/msg/Int32, carrying a 32-bit signed integerQoS mode: Best Effort, adapting to the unreliable characteristics of the Wi-Fi UDP link
Data flow: ROS2 host publishes a message → Agent → UDP → micro-ROS stack → executor polling → subscription callback → serial port printing
3. Core Topic Quick Reference
Firmware Downlink (Control Commands)
| Topic | Message Type | QoS | Field | Range | Direction |
|---|---|---|---|---|---|
/subscriber | std_msgs/msg/Int32 | Best Effort | data (int32) | Any integer value | Input, printed to serial port |
Usage Example:
ros2 topic pub /subscriber std_msgs/msg/Int32 "data: 42"4. Middleware Configuration
micro-ROS uses a static memory model (RCUTILS_AVOID_DYNAMIC_ALLOCATION=ON), all entity memory is pre-allocated at compile time, and runtime dynamic creation is not supported.
Configuration file:
~/esp/extra_components/micro_ros_espidf_component/colcon.meta
| RMW_UXRCE_TRANSPORT | udp | UDP transport (Wi-Fi communication) |
| RMW_UXRCE_MAX_PUBLISHERS | 6 | Maximum number of publishers |
| RMW_UXRCE_MAX_SUBSCRIPTIONS | 6 | Maximum number of subscribers |
| RMW_UXRCE_MAX_NODES | 1 | Maximum number of nodes |
| RMW_UXRCE_MAX_SERVICES | 0 | Services not supported |
| RMW_UXRCE_MAX_CLIENTS | 0 | Clients not supported |
| RMW_UXRCE_MAX_HISTORY | 1 | History depth = 1, no message queuing |
| UCLIENT_UDP_TRANSPORT_MTU | 4096 | Maximum UDP packet size 4 KB |
| UCLIENT_MIN_HEARTBEAT_TIME_INTERVAL | 1 | Agent heartbeat interval 1 ms |
Key Constraints:
MAX_HISTORY=1means there is no message buffer queue, so uplink topics (firmware → host) uniformly use best_effort QoSMAX_SERVICES=0, MAX_CLIENTS=0means only topic communication is supported, not Service/ClientAfter modifying
colcon.meta, the micro-ROS library must be recompiled for the changes to take effect: runidf.py clean-microrosin the project directory to clean and rebuild the micro-ROS library, thenidf.py buildto compile the firmware
3. Experiment Steps
1. Hardware Preparation
| Hardware | Requirement |
|---|---|
| MicroROS-V2 control board | ✅ Required |
| USB Type-C cable | ✅ Required |
| Wi-Fi network (2.4 GHz) | ✅ Required |
2. Prerequisites
The ESP32-IDF development environment has been set up
The MicroROS-V2 control board is connected to the computer via USB
Configure the Wi-Fi SSID, password, Agent IP and port via
idf.py menuconfig(themicro_rosmenu item)xxxxxxxxxxcd ~/esp/microROS_samples/subscribersource ~/esp/esp-idf/export.shidf.py menuconfig
Select example-app setting to set the domain ID to be consistent with the virtual machine

After configuration, press "s" to save and press "q" to exit the configuration interface.