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://www.ros.org/
Yeah That sounds good, but isn't it for microprocessor based systems and not microcontroller?
And the answer YES, it is. But we all know, there are some developers working hard to find some cool solution to make things easy and so happened at “micro-ROS”.
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 (tested on foxy and humble) running on Virtual Machine and native installation. The connection to ROS is via wifi. But will be using USB / UART to upload code.
Installation
All parts of this post are taken from original docs published by micro ros team at https://micro.ros.org/ and this post it to making it handy.
Step 1. Basic Installations
Step 1.1. ROS 2 Installation
ROS2 documentation is really helpful and up to date. Thus it’s better to follow it directly from their webpage. In this we will be using Ubuntu as OS and debian package method to keep everything simple. Follow the documentation till section “Try some examples” https://docs.ros.org/en/foxy/Installation/Ubuntu-Install-Debians.html
Step 1.2. ESP IDF Installation
The same with ESP IDF. Their documentation is way enough to explain everything. https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/
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
Once everything is set with ROS and the example talker listener.
works, we can start micro-ros installation. Follow these steps to install the micro-ROS build system:
# 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
This step creates firmware workspace that targets all the required code and tools. Once the command is executed, a folder named firmware
must be present in your workspace.
ros2 run micro_ros_setup create_firmware_ws.sh freertos esp32
Step 4. Configuring created firmware
This step helps to set the configuration files for the board. Here you have to enter 2 things: PROJECT NAME and LOCAL MACHINE IP ADDRESS (computer, laptop or other dev board).
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 (do check the branch according to your installation)
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
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
which opens a windows as below.
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
Nothing to add. Just Copy and Paste. It takes a white to complete.
ros2 run micro_ros_setup build_firmware.sh
At the end of build, you should get an error free output as
Step 6. Flash firmware
Connect your ESP32 to the computer with a micro-USB cable, and run,
ros2 run micro_ros_setup flash_firmware.sh
This will also take time and output should look like this
Step 7. Creating the micro-ROS agent
The micro-ROS app is now ready to be connected to a micro-ROS agent to start talking with the rest of the ROS 2 world. To do that, let’s first of all create a 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
At this point, you have both the client and the agent correctly installed in your host machine.
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
Once the connection is established, it looks like this
Step 9. Testing the micro-ROS app
Now is time to test the code. For this, we check the topic list and subscribe to it.
# list ros2 topics
ros2 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
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 (would work on other versions of ROS too)
You can even test this with a hardware connected with esp32. An example of joystick and Turtlesim is available here, https://github.com/sam-tj/freertos_apps/tree/hardware_example/apps/twist_joystick
I am working with IMU and ESP32 to make something exciting. So be connected and hope you found this post interesting.
Bis später.