Integrating ESP32 with ROS over WiFi

In this blog post, I will guide you on how to connect an ESP32 board to the Robot Operating System (ROS) using WiFi. This is a beginner-friendly guide that will take you through the process of integrating ESP32 with ROS using the rosserial_arduino library, which provides a ROS communication protocol for Arduino boards.

see my github.

Copy
https://github.com/Xiangyu-Fu/ESP32_ROS_wifi

Prerequisites

Before we begin, ensure you have the following development environment:

Ubuntu 20.04

ROS Noetic

PlatformIO espressif32

ESP32 Arduino Framework

frankjoshua/Rosserial Arduino Library@^0.9.1

You can also use a Raspberry Pi instead of a PC.

Environment Setup

On your own PC or Raspberry Pi

You'll need to install the required ROS packages. Use the following commands in your terminal:

Copy
$ sudo apt-get install ros-${ROS_DISTRO}-rosserial-arduino
$ sudo apt-get install ros-${ROS_DISTRO}-rosserial

After installing the necessary packages, launch the ROS master node:

Copy
$ roscore

Then, in a new terminal window, run the rosserial node:

Copy
$ rosrun rosserial_python serial_node.py tcp

This starts the ROS node that will connect with our ESP32 over a TCP connection.

On your embedded device (Here, ESP32)

Ensure you have flashed the appropriate example code onto the board. You can use the PlatformIO environment or Arduino IDE for this.

Running the Example

Dependencies

The first step is to include the necessary libraries. These are the Arduino core library, the WiFi library for connecting to the network, and the ROS library for interacting with the ROS system.

Copy
#include <arduino.h>
#include "WiFi.h"
#include <ros.h>
#include <std_msgs/String.h>

We define several global variables:

IP addresses for the Arduino board and the ROS server.

The server port for the ROS server.

The SSID and password for the WiFi network.

The message we're going to send to the ROS server ("hello world!") and how frequently we're going to send it.

A handle for the ROS node and a publisher for sending messages to the ROS system.

Connecting to WiFi

Our setupWiFi() function connects the Arduino board to the WiFi network. It repeatedly checks the status of the connection and prints out a period for each attempt until the connection is established. Once connected, it prints out the SSID and local IP address of the WiFi connection.

Copy
void setupWiFi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500);Serial.print("."); }
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}

Main Setup

In the setup() function, we initialize the Serial communication for debugging, set up the WiFi connection, establish the connection to the ROS server, initialize the ROS node, and advertise the publisher.

Copy
void setup(){
Serial.begin(115200);
setupWiFi();

nh.getHardware()->setConnection(server, serverPort);
nh.initNode();

Serial.print("ROS IP = ");
Serial.println(nh.getHardware()->getLocalIP());

nh.advertise(chatter);
}

Main Loop

In the main loop(), we check the time and, if enough time has passed since the last message, we send a new message. We also check the connection status and print a message to the Serial monitor accordingly. After these checks, we call nh.spinOnce() to handle any incoming messages, and then delay for a millisecond.

Copy
void loop(){
if(millis() - last_time >= period)
{
last_time = millis();
if (nh.connected())
{
Serial.println("Connected");
str_msg.data = hello;
chatter.publish( &str_msg );
} else {
Serial.println("Not Connected");
}
}
nh.spinOnce();
delay(1);
}

That's it! Now you're ready to start using Arduino with ROS over WiFi. Happy coding!

Once you've set up everything and the ESP32 is connected to the same network as your PC or Raspberry Pi, the ESP32 should start sending "hello world!" messages to the ROS system. You can visualize this output using a tool like rqt_console or simply monitor the output in your terminal.

If everything is set up correctly, you should see a screen similar to the following:

And that's it! Your ESP32 is now connected to ROS over WiFi. This basic framework can be the foundation for more complex projects where the ESP32 interacts with ROS to control robots or other devices.

Final Thoughts

We've just walked through the process of connecting an ESP32 to ROS over WiFi. While this example was relatively simple, it provides a powerful foundation for more complex projects. ROS is a robust system for controlling and managing robots, and integrating it with the flexible and powerful ESP32 opens up a wide range of possibilities. Whether you're building a drone, a mobile robot, or any other device that needs to interface with ROS, this guide provides the basics you need to get started.

So now it's your turn. Happy coding!