I had written an article for getting micro-ROS working with Raspberry Pico a while ago. However, I was not very satisfied with the outcome because the Pico was still tethered to the laptop or if you put it in robot it would be tethered to RPi4. I have been thinking about creating a small and inexpensive robot and completely eliminating the SBC like Raspberry Pi4 but did not have the right resources to figure that out until now.
I recently learned that the micro-ROS on ESP32 now supports WiFi transport over UDP as default. I had actually followed some of the articles in reference to get the micro-ROS working with ESP32 quite a while ago but again at that time I had it working with serial transport with a USB cable tethered to the ESP32 board. So when I heard that now it supports UDP over Wifi I was very excited to give it another try. This article is to document my steps in getting it working over WiFi.
When I got micro-ROS working last time it was setup with my laptop, compiled and built the whole micro-ROS setup. However, I wanted to try the docker route this time around as an added bonus. And at the end I did find that the docker route was a lot easier than setting it all up on laptop.
Here is my hardware setup:
- Laptop with Ubuntu 22.04 Jammy and ROS2 Humble [however you do not need ROS2 installed since we will be using docker for both building the micro-ROS image for ESP32 and a separate docker for running the micro-ROS agent.
- ESP32-WROOM-32D board.
- A power bank battery to power ESP32 on its own disconnected from laptop to test standalone WiFi mode.
It worked just fine with ESP32-WROOM-32D board, however, if you check the micro-ROS official post on ros discourse they recommend using ESP32-WROOM-32E, which is essentially a newer version of 32D with bug fixes is what I found in my searches. I have seen other folks even make it work with ESP32 Firebeetle version as well. So if you have an ESP32 board lying around try it, and it may work.
Here are some pictures of setup for reference along with closeup view of ESP32 where you can see the model.
Here are the steps to get it up and running in probably 30 mins or less including downloading docker images.
- Get github repo for ESP-IDF component downloaded on laptop
# 1. Create a directory for cloning the repo for ESP-IDF component
# 2. Clone the ESP-IDF component repository
git clone https://github.com/micro-ROS/micro_ros_espidf_component.git
# 3. Since we are going to be working with ROS2 Humble checkout humble branch
git checkout -b humble
git status # to make sure we have the right branch
2. Prepare for connecting ESP32 board and get port number
# 4. Before you connect the ESP32 run following command in new terminal window
sudo dmesg --follow
# 5. connect the ESP32 to laptop while the command is running and it will show
# the port number the ESP32 is connected to - for me it was /dev/ttyUSB0
# you can stop the command from running by hitting CTRL+C
# 6. check the permission on that port by running
ls -l /dev/ttyUSB0
# it will show something like this, which means we don't have permissions
# to write to the port yet
crw-rw---- 1 root dialout 188, 0 Aug 28 19:41 /dev/ttyUSB0
# 7. Run following command to grant permission to write, we will need this
# to flash ESP32 with the example program we are going to run which is
sudo chmod 666 /dev/ttyUSB0
3. Build the micro-ROS for ESP32 using docker [assuming you already have docker installed]
docker run -it --rm --user espidf --volume="/etc/timezone:/etc/timezone:ro" -v $(pwd):/micro_ros_espidf_component -v /dev:/dev --privileged --workdir /micro_ros_espidf_component microros/esp-idf-microros:latest /bin/bash -c "cd examples/int32_publisher; idf.py menuconfig build flash monitor"
You will see a lot of output and ultimately you’ll see menuconfig screen where you can set various parameters. We will use this menu to set our wifi SSID, PWD and ip address of our laptop we are going to be running the micro-ROS agent to listen to ROS2 messages from ESP32.
The main menu looks like this:
Navigate to next level menu: micro-ROS Settings → WiFi Configuration
And set correct values for your WiFi SSID and password
Next hit Esc and go one level up to micro-ROS Settings and setup the IP address of micro-ROS Agent machine which in our case will be the IP address of laptop [replace IP address of your laptop where it shows 192.168.1.123]. If you don’t know your laptop IP Address just run the “ip addr” command at terminal to find it.
At this point you can use S to save your changes and then Esc and Q to exit from menu configuration. Good news is, once you save your config, even if you have to re-run the docker command for some reason, those values you setup will still persist.
At this point you should hold down the BOOT button [not the EN button] on the ESP32 board [it should be the one on right of your mini-USB port if you are holding ESP32 board so that usb port is at bottom].
You’ll see a lot of output and ultimately it will show you progress in percentage as it starts to flash the programs to ESP32 board.
If you get error saying port not found or could not write to it — this means you need to run the “sudo chmod 666 /dev/ttyUSB0” command again.
If you get an error saying “Failed to connect to Espressif device: Wrong boot mode detected (0x13)! The chip needs to be in download mode.”, this means you forgot to hold down the BOOT button as it was flashing the board, so you need to start over and try again by running the docker command again.
Once you get the process of flashing complete, you can exit the command by doing CTRL+].
At this point the ESP32 board has the program flashed on it and it knows how to connect to WiFi and which IP address the micro-ROS agent will be running so it can send the messages to it.
Next step is to run the docker command to run the micro-ROS agent.
docker run -it --rm --net=host microros/micro-ros-agent:humble udp4 --port 8888 -v6
You’ll see that the agent starts and is waiting patiently for the ESP32 to start sending messages. So lets disconnect the USB cable from laptop for ESP32 and connect it to the battery power bank as show in picture above.
As soon as you connect the ESP32 to power bank you’ll start seeing messaging coming through over WiFi + UDP, something like this:
At this point you should also be able to examine your ROS2 topics and echo the topic like this:
ros2 topic list
And if you do
ros2 topic echo /freertos_int32_publisher, you’ll see something like this
There you have it, ESP32 publishing ROS2 messages in a completely untethered fashion over WiFi + UDP and micro-ROS agent receiving those messages on your laptop.
This opens up a lot of possibilities to basically run motors, encoders, imu and even lidar at some point on a robot without the need for SBC like RPi4 and it brings down the cost of educational robots significantly. There are already github repositories where people are building code to do this and there is even a fork of the linorobot2 that uses ESP32 and micro-ROS.
Exciting time to be a roboticist. Enjoy!!!
micro-ROS porting to ESP32
The micro-ROS team is glad to announce that the pool of boards officially supported by the project has widened! The…
GitHub - micro-ROS/micro_ros_espidf_component: micro-ROS ESP32 IDF component and sample code
micro-ROS ESP32 IDF component and sample code. Contribute to micro-ROS/micro_ros_espidf_component development by…