Subscribe buzzer topics

1. Experimental purpose

Learn ESP32-microROS components, access the ROS2 environment, and subscribe to the topic of controlling the buzzer switch.

 

2. Hardware connection

As shown in the figure below, the microROS control board integrates an active buzzer and 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. The computer and microROS control board function as firmware burning.

image-20240117162943243

 

3. Core code analysis

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

Since an active buzzer is used this time, and the components of the active buzzer have been made in the previous routine, the components of the active buzzer need to be copied to the components directory of the project. Call Beep_Init at the beginning of the program to initialize the buzzer.

image-20240117143730788

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 "beep_subscriber", in which ROS_NAMESPACE is empty by default and can be modified in the IDF configuration tool according to actual conditions.

To create subscriber "beep", you need to specify the ROS topic information as std_msgs/msg/UInt16 type.

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

When microros subscribers receive topic data, the beep_callback callback function is triggered and the active buzzer is controlled based on the received value.

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, flash, 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 on the virtual machine/computer and view the /beep_subscriber node.

image-20240117152943906

Publish data to the /beep topic and control the buzzer to sound continuously.

Publish data to the /beep topic and control the buzzer to turn off.

Publish data to the /beep topic and control the buzzer to sound for 300 milliseconds and then turn off automatically.

image-20240117153526094

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

image-20240117153538129