Multi-topic subscribe and publish

1. Experimental purpose

Learn ESP32-microROS components, access the ROS2 environment, subscribe to and publish multiple int32 topics.

 

2. Hardware connection

As shown in the figure below, the microROS control board integrates the ESP32-S3-WROOM core module, which has its own wireless WiFi function. The ESP32-S3 core module needs to be connected to an antenna, and a type-C data cable needs to be connected to the computer and the microROS control board as Burn firmware function.

image-20240112174901940

 

3. Core code analysis

The virtual machine path corresponding to the program source code is as follows

First, get the WiFi name and password to connect from the IDF configuration tool.

The uros_network_interface_initialize function will connect to WiFi hotspots based on the WiFi configuration in IDF.

Then obtain ROS_NAMESPACE, ROS_DOMAIN_ID, ROS_AGENT_IP and ROS_AGENT_PORT from the IDF configuration tool.

Initialize the configuration of microROS, in which ROS_DOMAIN_ID, ROS_AGENT_IP and ROS_AGENT_PORT are modified in the IDF configuration tool according to actual needs.

Try to connect to the proxy. If the connection is successful, go to the next step. If the connection to the proxy is unsuccessful, you will always be in the connected state.

Create the node "esp32_pub_sub", in which ROS_NAMESPACE is empty by default and can be modified in the IDF configuration tool according to actual conditions.

Create three publishers "publisher_1", "publisher_2" and "publisher_3", and specify the ROS topic information as std_msgs/msg/Int32 type.

Create three subscribers "subscriber_1", "subscriber_2" and "subscriber_3", and specify the ROS topic information as std_msgs/msg/Int32 type.

// Set the publishing frequency of the three publishers to 10HZ, or you can set the publishing frequency separately.

Add publisher timers and subscribers to the executor, where the handle_num parameter of the executor represents the number added to the executor.

The timer callback function contents of the three publishers publish topic data.

When microros subscribers receive topic data, the subscriber callback function is triggered and the received data is printed out.

Call rclc_executor_spin_some in the loop to make microros work normally.

 

4. Compile, download and flash firmware

Use a Type-C data cable to connect the virtual machine/computer and the microROS control board. If the system pops up, choose to connect to the virtual machine.

Activate the ESP-IDF development environment. Note that every time you open a new terminal, you need to activate the ESP-IDF development environment before compiling the firmware.

Enter the project directory

Open the ESP-IDF configuration tool.

Open micro-ROS Settings, fill in the IP address of the agent host in micro-ROS Agent IP, and fill in the port number of the agent host in micro-ROS Agent Port.

image-20240116194035834

Open micro-ROS Settings->WiFi Configuration in sequence, and fill in your own WiFi name and password in the WiFi SSID and WiFi Password fields.

image-20240116193354397

Open the micro-ROS example-app settings. The Ros domain id of the micro-ROS defaults to 20. If multiple users are using it at the same time in the LAN, the parameters can be modified to avoid conflicts. Ros namespace of the micro-ROS is empty by default and does not need to be modified under normal circumstances. If non-empty characters (within 10 characters) are modified, the namespace parameter will be added before the node and topic.

image-20240116195328563

After modification, press S to save, and then press Q to exit the configuration tool.

Compile, burn, and open the serial port simulator.

If you need to exit the serial port simulator, press Ctrl+].

 

5. Experimental results

After powering on, ESP32 tries to connect to the WiFi hotspot, and then tries to connect to the proxy IP and port.

If the agent is not turned on in the virtual machine/computer terminal, please enter the following command to turn on the agent. If the agent is already started, there is no need to start the agent again.

image-20240117151035412

After the connection is successful, a node and a subscriber are created.

image-20240117113138240

At this time, you can open another terminal in the virtual machine/computer and view the /esp32_pub_sub node.

image-20240117113725339

Publish information with int data 123 to the /subscriber_1 topic.

Press Ctrl+C to end the command

image-20240117114528218

Publish information with int data 456 to the /subscriber_2 topic.

Press Ctrl+C to end the command

image-20240117115051379

Publish information with int data 789 to the /subscriber_3 topic.

Press Ctrl+C to end the command

image-20240117115127882

You can see the corresponding information printed on the serial port simulator, indicating that the subscription is successful.

image-20240117114915984

View the frequency of /publisher_1, /publisher_2, and /publisher_3 topics

Press Ctrl+C to end the command.

image-20240117123122914

Subscribe to data for /publisher_1, /publisher_2 and /publisher_3 topics

Press Ctrl+C to end the command.

image-20240117141526808

image-20240117141722383

image-20240117141806202