May 10, 2024

Tutorial on Establishing an Interface and Communication between Arduino and Robot Operating System (ROS)

In this tutorial, we will learn how to establish a serial communication interface between Arduino and ROS. More precisely, we will learn how to run a ROS node on Arduino that will publish messages on a certain topic. Then, the ROS system running on a Linux machine will read the published messages. The messages will be in the form of a simple string. However, you can easily generalize everything explained in this video tutorial to more general messages, such as data structures containing numbers, positions, velocities, orientations, etc. The YouTube tutorial accompanying this webpage is given below.

But we start with explanations, we need to mention the main motivation for creating this video tutorial. Namely, in practice, Arduino will run a low-level controller that will control servo or stepper motors, and read information from sensors. Then, on top of Arduino, we need to have a higher-level controller, such as a computer running Linus, Raspberry Pi, or NVIDIA Jetson Nano. These higher-level controllers will send control signals to be executed by Arduino and they will receive back information from Arduino. Also, these high-level controllers should run advanced algorithms such as SLAM, Kalman filter, machine learning, MPC, and similar algorithms. We need to integrate this complete communication infrastructure in ROS. Consequently, we need to learn how to communicate with Arduino and how to run a ROS node on Arduino.

STEP 1: Install the necessary libraries

The first step is to install the necessary libraries. Open a terminal and type the following commands:

sudo apt install ros-noetic-rosserial
sudo apt install ros-noetic-rosserial-arduino
sudo apt install ros-noetic-rosserial-python

Next, we need to specify the folder in Arduino for saving sketches and libraries. First, create a folder. In our case, I created a folder in the home folder. Then, we need to tell Arduino where the folder is located. To do that, in Arduino IDE, do the following:

Click on

File→Preferences→
and specify the folder name in the “Sketchbook location” menu

Then, we need to install Arduino-ROS communication library. The name of the library is “Rosserial Arduino Library”. To install this library, do the following

Click on

Sketch→Include Library →Manage Libraries

Then, in the library manager, search for “rosserial”. The library “Rosserial Arduino Library.” will appear. Click on “install” to install the library.

STEP 2: Test Library Installation and Load Hello World Arduino Example – Also Fix cstring error

To test the library installation, we will load a test example provided by the library. In Arduino, click on

File→Examples→Rosserial Arduino Library→HelloWorld

This is the “Hello World” test example. This example will send a string “hello world!” to ROS node. That is, Arduino will run a ROS node, and it will publish a message string “hello world!” to an appropriate topic.

Here is how the HelloWorld example looks like:

/*
 * rosserial Publisher Example
 * Prints "hello world!"
 */

#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle  nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[13] = "hello world!";

void setup()
{
  nh.initNode();
  nh.advertise(chatter);
}

void loop()
{
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}


A detailed explanation of this code is given in the YouTube tutorial. The name of the topic that we will publish to is “chatter”. In the Linux terminal, we will read messages from this topic.

Next, we need to upload this code to Arduino, but before we do that, we need to adjust the board name and the proper port number. After we do that, we need to verify the code. After locally compiling the code (verify code), we get the following error that needs to be resolved:

In file included from /home/aleksandar/ArduinoSketches/libraries/Rosserial_Arduino_Library/src/std_msgs/Time.h:7:0,
                 from /home/aleksandar/ArduinoSketches/libraries/Rosserial_Arduino_Library/src/ros/node_handle.h:40,
                 from /home/aleksandar/ArduinoSketches/libraries/Rosserial_Arduino_Library/src/ros.h:38,
                 from /tmp/.arduinoIDE-unsaved2023724-3418-1u8ostn.6xf4/HelloWorld/HelloWorld.pde:6:
/home/aleksandar/ArduinoSketches/libraries/Rosserial_Arduino_Library/src/ros/msg.h:40:10: fatal error: cstring: No such file or directory
 #include <cstring>
          ^~~~~~~~~
compilation terminated.

exit status 1

Compilation error: exit status 1

Obviously, there is an issue with this library. Let us fix this issue. To fix this issue, we need to do the following. First, we need to find where the library is located. The library should be located in the folder that we specified at the beginning of this tutorial (STEP 1). Then, you need to change several folders until you reach the file “msg.h”. The complete path is

libraries/Rosserial_Arduino_Library/src/ros/msg.h

Then, open this file with your Linux editor, and make the following changes

Line 40: “#include ” change to “#include ”
Line 68: “std::memcpy” change to “memcpy”
Line 182: “std::memcpy” change to “memcpy”

Save these changes. Now, go back to Arduino, and try to verify and upload the code. There will be no errors.

STEP 3: Test the created Arduino ROS Node in ROS and Linux

Open, the first Linux terminal, and run the roscore:

roscore 

Then, open the second terminal and type

sudo chmod 666 /dev/ttyACM0

In this way, we set the proper permissions for our communication port to which Arduino is attached. Here, “dev/ttyACM0” is precisely the port name that is seen from Tools→Port in Arudino. If in your case, the port number is different, you need to change the port number. Next, write in the terminal:

rosrun rosserial_python serial_node.py /dev/ttyACM0

This will run the rosserial Python client. We also specify the name of the port. The file serial_node.py will enable us to communicate over the serial port with Arduino. Finally, open the third terminal, and type:

rostopic list

This will list all the current topics running in our ROS system. We obtain the following result

/chatter

/diagnostics

/rosout

/rosout_agg

The topic “chatter” is the topic that Arduino publishes to. To see the messages published by Arduino, we type in the terminal window

rostopic echo /chatter

As a result, we will obtain the following messages published every 1 second:

data: “hello world!”

data: “hello world!”

data: “hello world!”

data: “hello world!”

data: “hello world!”

data: “hello world!”

data: “hello world!”

data: “hello world!”—