Connect ESP32 to ROS2 Foxy

In the era of UAVs, IOT devices, and my favorite Google Assistant :P, everything is oriented to collect sensor data and using it to get our task done.

Some of us find it interesting working in simulated environments like Gazebo and others with physical hardware. But how can we connect real devices to simulated environments? Here ROS comes in action,

The Robot Operating System (ROS) is a set of software libraries and tools for building robot applications. From drivers to state-of-the-art algorithms, and with powerful developer tools, ROS has what you need for your next robotics project. And it’s all open source.https://docs.ros.org/en/foxy/index.html

Yeah That sounds good, but isn't it for microprocessor based systems and not microcontroller?

They created several packages to bridge the gap between resources constrained devices (ESP32) and higher computing devices (Computers, Raspberry PI). This makes sensors as smart devices to monitor physical world and make it available for research applications or simulations.

That's a lot for theory, Lets Start

In this post, I am using ESP32 Wroom32 Board with ROS2 foxy running on Virtual Machine. The connection to ROS is via wifi. But will be using USB / UART to upload code.

Installation

Step 1. Basic Installations

Step 1.1. ROS 2 foxy Installation

Step 1.2. ESP IDF Installation

But for a quick start, I would suggest using VScode and ESP IDF extension for complete installation as it does everything and meanwhile you can have a cup of tea☕

https://github.com/espressif/vscode-esp-idf-extension/blob/master/docs/tutorial/install.md

Step 2. Micro-ROS Installation

# Source the ROS 2 installation
source /opt/ros/$ROS_DISTRO/setup.bash
# Create a workspace and download the micro-ROS tools
mkdir microros_ws
cd microros_ws
git clone -b $ROS_DISTRO https://github.com/micro-ROS/micro_ros_setup.git src/micro_ros_setup
# Update dependencies using rosdep
sudo apt update && rosdep update
rosdep install --from-path src --ignore-src -y
# Install pip
sudo apt-get install python3-pip
# Build micro-ROS tools and source them
colcon build
source install/local_setup.bash

Step 3. Creating a new firmware workspace for ESP32

ros2 run micro_ros_setup create_firmware_ws.sh freertos esp32

Step 4. Configuring created firmware

Project name is the folder in which you add your required code. Here we use a demo code available int32_publisher. Other demo codes can be found here, https://github.com/micro-ROS/freertos_apps/tree/foxy/apps

IP address can be found using “ ifconfig ” without quotation marks, for Unix based systems (UBUNTU).

ros2 run micro_ros_setup configure_firmware.sh [PROJECT NAME] -t udp -i [LOCAL MACHINE IP ADDRESS] -p 8888
Configuring firmware. Here PROJECT as int32_publisher and ID ADDRESS as 192.168.0.192

Then use the following command to set your wifi credentials for ESP32 to connect to your network.

ros2 run micro_ros_setup build_firmware.sh menuconfig
Menuconfig

which opens a windows as below.

Menuconfig Window

Now go to the micro-ROS Transport Settings → WiFi Configuration menu and fill your WiFi SSID and password. Save your changes, exit the window.

Step 5. Build firmware

ros2 run micro_ros_setup build_firmware.sh
Build firmware

At the end of build, you should get an error free output as

Build firmware

Step 6. Flash firmware

ros2 run micro_ros_setup flash_firmware.sh
Flash firmware

This will also take time and output should look like this

Flash firmware

Step 7. Creating the micro-ROS agent

# Download micro-ROS-Agent packages
ros2 run micro_ros_setup create_agent_ws.sh

Now, let’s build the agent packages and, when this is done, source the installation:

# Build step
ros2 run micro_ros_setup build_agent.sh
source install/local_setup.bash

Step 8. Running the micro-ROS app

To give micro-ROS access to the ROS 2 dataspace, run the agent:

# Run a micro-ROS agent
ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888
Run agent

Once the connection is established, it looks like this

Run agent

Step 9. Testing the micro-ROS app

# list ros2 topics
ros2 topic list
Topic list

Here we see an additional topic as /freertos_int32_publisher. Let’s subscribe to it and check the content.

# Subscribe to micro-ROS ping topic
ros2 topic echo /freertos_int32_publisher
Topic echo

We got the output. 🥳

There is a small video too for the demo. But it uses Hello World and rest follows the same. You can find the code here, https://github.com/sam-tj/freertos_apps/tree/foxy/apps/string_publisher

I am working with IMU and ESP32 to make something exciting. So be connected and hope you found this post interesting.

Bis später.

I am easily satisfied with the very best... Find more about me on sam-tj.github.io/