ArticlesRobot

Introduction to rosserial_arduino

Connecting Arduino to ROS

This article was translated from Arabic->English by: Nour Taweel

 

Because of its easy use and low cost, Arduino boards succeed to spread to a variety of applications, especially in robotics and electronic devices. But can this board alone be used to construct a robot that is able to navigate autonomously and plot a 2D map? The answer simply is NO!

The most recognized Arduino boards like UNO or MEGA have AVR internal Micro-controllers with processing potentials that aren’t suitable for such complicated applications or image processing.

So, can Arduino be used in high-level applications? Yes, it can be used in some aspects of these applications, for example, connecting robot’s sensors and motors to a more advanced computer – PC or Raspberry SBC – that will run the actual processing.

Since ROS is used with complex robot applications, we can dedicate it to the image processing task and use Arduino to get sensors’ readings. Now the question is how to communicate these sampled reading to our ROS?

 

What is rosserial?

 

This information is communicated with the help of the rosserial package. rosserial is a protocol to send data through a serial interface. In a client-server rosserial implementation, a rosserial-server is a computer running ROS and a rosserial-client is the microprocessor that receives sensors’ data and transports it to the server in the form of ROS messages. rosserial-server in this implementation is a publishing node while rosserial-client is a subscriber node, although this can sometimes be the other way round.

Rosserial-client package is available for several microprocessor types including Arduino, STM32, embeddedlinux and others. While rosserial-server package is available in Python or C++ versions.

 

Image courtesy  ROS Integration for Miniature Mobile Robots

 

This article will go through the setup of Arduino and rosserial.

 

Installing rosserial package on Ubuntu

There are two methods to install any package on Ubuntu:

  • Using apt-get

To install rosserial on ROS kinetic, type

  • Source code clone

First, create a new folder in your ROS workspace and name it rosseiral_ws for example. Create another folder inside named src

Move to your src folder

Clone the source code in this directory

When the clone is complete, we need to build the package in the workspace. Go back to the main folder

Then type this command. It will build the package creating build and devel folders

To introduce the installed package to ROS, use the following

 

Setup ros_lib into Arduino IDE

 

For Arduino to be able to communicate with ROS, ros_lib must be installed. The next steps will guide you through this.

In your Arduino environment go to file-> preferences. For sketchbook location, look for libraries folder -create it yourself in case you can’t find it –

  • Now, in the command window, type
  • In a new window, use rosrun command to generate ros_lib
You can see ros_lib included in your custom libraries list

 

Example 1: Blinking LED

 

This is the most basic example application. The following code sets up a node that subscribes to a topic that changes the state of a LED connected to pin 13. This code can be found in ros_lib -> Blink

Let’s explain the code:

 

includes ROS library in the program

includes the messaging library that manages communication between publishers and subscribers. Since this application doesn’t need to exchange real data, an empty message is used and the code only creates a subscriber and no publisher gets created.

The operation is illustrated as

 

makes this program as a node in ROS . It’s a node object.

Define the method that will be called when the topic message is received. When this method is called, the value on pin 13 is toggled.

Creates a subscriber to the “toggle_led” topic with the callback function “messageCb” to be called on receiving a message.

defines pin 13 as output

init the node

checks if there are any callbacks/services to control the update rate of ROS.

subscribe/start listening

 

To run the code, open cmd window and type

To run rosserial client that passes the messages from Arduino to ROS

 

Before uploading your code, check which port your Arduino is attached to by going to tools->port, or by using dmesg command

To turn on the LED

Now you can see the LED blinking

ROS blinking LED

 

The following code is used as a template to write your custom publisher

In general, you need to do these steps:

  • Include ROS lib
  • Create a node to communicate with ROS
  • Create a publishing node: the first parameter is the topic’s name and the second is the callback function.
  • Start this publisher

 

Example 2: Motor Controller

 

This application is a little more complicated. Let’s assume a two-wheeled robot needs to be controlled using ROS. In terms of hardware, we need:

  • Arduino board
  • Motor driver
  • DC motors

L298 is used for driving the motors. It contains two H-bridge circuits, which has four input pins to enable the transistors and the motor is connected between the two output pins out1 and out2. This figure shows the internal structure of L298

 

 

H-bridge controls the voltage polarity. Therefore, if the goal is to control the rotation direction all you need to do is control which transistor to enable. For example, enable transistors 1&4 to rotate clockwise. While enabling 2&3 will produce anti-clockwise rotation.

 

 

We will connect 1 and 2 to Arduino pins and Out1 to one motor. Also, 3 and 4  to Arduino pins and Out2 to the other motor. The goal here is to control the speed of the motors, thus, the input voltage needs to be controlled using a Pulse Width Modulation (PWM). PWM values are between 0 and 225 while motors duty values are between 0% and 100%. As an example, to rotate in full duty, PWM=255. And to stop rotating PWM=0

In ROS there is a twist message in geometry_msgs package called cmd_vel which we can make use of to send speed values to the motors.

The next block diagram illustrates the idea

 

 

Once the code is uploaded to Arduino, the robot can be controlled using the ROS via teleop_twist_keyboard package that enables the ROS to control the linear velocity (forward and backwards) in addition to angular velocity on the z-axis. The Twist messages via cmd_vel topic are used.

Type

This output is produced

 

 

u: for left circle

i: forward

o: right circle

j: turn anti-clockwise

K: stop all motors

l: turn clockwise

m: left circle backwards

,: straight backwards

.: right circle backwards

 

This article presented why it is important to connect Arduino with a ROS system. We demonstrated how to setup Arduino and Ubuntu for this connection using the rosserial protocol and ros_lib library. We then showed a simple blinking LED example and a slightly advanced one of a moving two-wheeled robot. You can check also our other tutorial about IMU sensor connected with Arduino and ROS.

 

Maisa jazba

A Mechatronic engineer from University of Aleppo, Syria. She works as a lecturer at the same university. Misa believes that people should be given the correct scientific information and informed in practical terms by touching the equipment and interacting with it and moving away from the idea of using simulation all the time.

3 Comments

  1. In the whole Universe there isn’t such a thing like “Arduino chips”; those things don’t exist. Somebody took a nice microcontroller from Atmel and then created the Arduino platform. And as a matter of fact, the “Arduino language” doesn’t exist either. The arduinos are programmed in C++.

    Please don’t invent things that mislead the people that is new in this topic.

    1. Hi Xavier

      This is Yahya from Atadiat authors team. Yes you’re totally correct. Using the word “Arduino chips” is totally incorrect and was wrongly translated from the original text. About the “Arduino language”, you’re also right but it’s very common to say Arduino-C (AKA language) while I personally avoid using this misleading term.

      Please check our article “A Tour Inside Arduino Core” … to know that we don’t deal with Arduino as dummies.

      https://atadiat.com/en/e-arduino-core-source-files-make-new-core-building-steps/

      Thanks for your notes again.

  2. I have managed to get it working. i forgot to use this command “rosrun rosserial_python serial_node.py /dev/ttyACM0”
    Thank you very much for sharing the tutorial.
    Kind regards

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.