Seamless Robotics Integration: Setting Up micro-ROS on ESP32
Tags: ros, microros, esp32, robotics
Published: 2023-06-12 18:18:34

Let's get the job done. Setting up micro-ROS on resource-constrained edge computing systems like ESP32 was a tedious job for me, which made me think of writing a blog to make it easy. So in this article, we will be mainly discussing some basics of ROS, micro-ROS and how to get it working within a robotic system.

ROS(Robot Operating System) or presently its new version ROS 2 is an open-source framework that eases the process of building a robot. It provides libraries, packages, and message-passing systems which act as an abstraction above underlying operating systems like Linux or Windows (currently ROS is not supported on Mac).

Simulations always go along with ROS and but how we can connect real-world sensors to these simulated environments, there come micro ROS. Most edge computing systems are highly resource constrained, thus we need some highly resource-optimized library that bridges the gap between high computing devices and these MCUs.

micro-ROS is a simple miniaturized version of ROS client API that runs on MCU, over an RTOS (Real Time Operating System) and supports all of the ROS concepts. It helps in the easy integration of microcontrollers with the robotics system. Now, let's get into the installation.

Installation

ROS 2 Humble Installation :

ROS 2 humble has nice documentation, which will help you install the same with simple steps. You can follow the documentation over here. I am using Ubuntu 22.04 LTS version for this installation. You can try out some examples to get familiarised with different ROS concepts.

ESP IDF Installation :

It is the official development framework for the ESP32 and ESP32-s series of SoCs, and the installation is fairly simple following their official documentation over here.

We can also install it as a simple VScode extension, which I will recommend since it is fairly simple and less time-consuming.

micro-ROS installation :

Build system installation :

MicroROS build system can be installed easily by following the below steps;

```
# 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
```

Creating a firmware workspace :

We need to create a firmware workspace, to proceed with the installation. this can be easily created using the micro_ros_setup tool package that we installed in the above step. On executing the below-given command a new folder named firmware is automatically created inside the microros_ws.

```
# Create step
ros2 run micro_ros_setup create_firmware_ws.sh freertos esp32
```

Micro-ROS currently supports three different RTOS, namely NuttX, FreeRTOS, and Zephyr. We can use any of these according to our needs, which can be found in the reference section.

Configure firmware :

We use the below command to configure the firmware we just created.

```
# Configure step
ros2 run micro_ros_setup configure_firmware.sh [APP] [OPTIONS]
```

We will be using the precompiled, ping_pong app. and the possible options we have are given below

The options available for this configuration step are:

--transport or -t: udp, serial or any hardware-specific transport label

--dev or -d: agent string descriptor in a serial-like transport

--ip or -i: agent IP in a network-like transport

--port or -p: agent port in a network-like transport

Run the below command for our setup for the ping_pong app.

```
# Configure step with ping_pong app and serial transport
ros2 run micro_ros_setup configure_firmware.sh ping_pong --transport serial
```

You can check the complete content of the ping_pong app here.

Note: When using udp mode for transport*, the next build step may give you an error and follow this* issue on GitHub to rectify the error. You can use the menuconfig to configure the wifi connectivity for the board

Build firmware :

Run the below command to build the firmware.

```
ros2 run micro_ros_setup build_firmware.sh
```

The build process may take some time to complete and on successful completion ([100%] app build), we can proceed further.

Flash firmware :

Before moving to this step make sure you have connected the ESP32 module to the host machine and proceed to run the below command.

```
ros2 run micro_ros_setup flash_firmware.sh
```

Setting up micro-ROS Agent

Since the ping_pong app is flashed, now we need a micro-ROS agent for it to connect and start talking with the rest of the ROS 2 world. Running the agent helps in making the micro-ROS namespace available to the external ROS 2 system running on the host machine so they can communicate with each other. Follow the below steps to create, build and run the agent.

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

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

# Run a micro-ROS agent
ros2 run micro_ros_agent micro_ros_agent serial --dev [device]
```

Note: In the last step, the [device] is the serial port to which the EPS32 is connected (eg: /dev/ttyUSB0), also this step varies with the type of transport we chose in the firmware configuration step

Testing the micro-ROS app

We may need to press the (EN) button to reset or disconnect and reconnect the ESP32 to establish the connection.

Now, open a new terminal and check the ROS 2 topics and new topics, /microROS/ping and microROS/pong will be available. Subscribe to the topics using the following command

```
source /opt/ros/$ROS_DISTRO/setup.bash

# Subscribe to micro-ROS ping topic
ros2 topic echo /microROS/ping
```

You should see the topic messages published by the Ping Pong node every 5 seconds:

```
user@user:~$ ros2 topic echo /microROS/ping
stamp:
  sec: 20
  nanosec: 867000000
frame_id: '1344887256_1085377743'
---
stamp:
  sec: 25
  nanosec: 942000000
frame_id: '730417256_1085377743'
---
```

Now we know that the pings are getting published, we can check if it can answer someone else's pings (fake pings). For testing that, open a new terminal and subscribe to the /microROS/pong topic.

```
source /opt/ros/$ROS_DISTRO/setup.bash

# Subscribe to micro-ROS pong topic
ros2 topic echo /microROS/pong
```

The pong topic reacts when a ping message is manual send to the ping topic, now we can mimic this by sending a fake ping message using the below command, the changes will be seen within the terminal echoing /microROS/ping and /microROS/pong topics.

```
source /opt/ros/$ROS_DISTRO/setup.bash

# Send a fake ping
ros2 topic pub --once /microROS/ping std_msgs/msg/Header '{frame_id: "fake_ping"}'
```

Now, we should see this fake_ping in the ping subscriber console, along with the board’s pings:

```
user@user:~$ ros2 topic echo /microROS/ping
stamp:
  sec: 0
  nanosec: 0
frame_id: fake_ping
---
stamp:
  sec: 305
  nanosec: 973000000
frame_id: '451230256_1085377743'
---
stamp:
  sec: 310
  nanosec: 957000000
frame_id: '2084670932_1085377743'
---
```

The following change will be seen in the pong subscriber console;

```
user@user:~$ ros2 topic echo /microROS/pong
stamp:
  sec: 0
  nanosec: 0
frame_id: fake_ping
---
```

That brings us to the end.
TLDR; Here we have set up the micro-ROS app, flashed it to an ESP32 board, and tested it using a micro-ROS agent.

Happy learning!