Quick Code v0.2 — ROS and Arduino

Sammy Hasan
2 min readApr 17, 2018

--

In this tutorial we will take a look at ROSserial , esp. ROSserial_arduino. This allows the arduino to run a ROS node, and talk the main ROS Master which is running on a linux machine.

If you haven’t guessed it!, the communication is done over serial, and we’ll be conecting the arduino to the linux machine via the accompanying USB cable that probably came with your arduino.

For this we would require:

  1. A linux machine with ROS installed, I have Ubuntu 16.04 with ROS Kinetic
  2. An arduino, the official ROSserial tutorial says that ROSserial is mainly for Arduino Uno and Leonardo. I’ve tested this with an Arduino Uno and all works well.
  3. The Arduino IDE.

So, how does this work?

There is a library that allows the arduino to talk over serial to the ROS Master, we will install this library and use #include <> in our code for the necessary libs.

Furthermore, there is a ROS node that we will need to run on the linux machine that picks up these messages from the serial port and passes them over to ROS Master.

Let’s Do it!

  1. Installing the ROS package that passes the serial messages on to ROS Master.

Simply run:

sudo apt-get install ros-kinetic-rosserial-arduino
sudo apt-get install ros-kinetic-rosserial

2. Installing the ROSlib for the arduino:

For this, navigate in ‘Terminal’ to the libraries folder of the Arduino IDE, this is usually located at:

/home/Arduino/libraries 

and then run,

rosrun rosserial_arduino make_libraries.py .

That dot at the end is important, it specifies the installation path. Which is the current path in the terminal.

A new folder will appear, ‘ros_lib’ which contains all that is needed for the Arduino part.

Test Run !

We can easily test run this by connecting the Arduino via usb and noting the port on which it is connected. Mine connects on ‘/dev/ttyACM0’ , this will be needed later when we launch the ROS node that passes on the serial data.

Anyways, after connecting the Arduino, we will launch one of the preinstalled examples, the ‘Hello World!’ of ROSserial, literally!

This allows the arduino to publish a string message ‘hello world!’ on the ‘/chatter’ topic.

The steps arre:

  1. In the Arduino IDE go to: File/Examples/ros_lib/HelloWorld.
  2. upload this Sketch to the Arduino, if you get an avr_dude() error, use this link. :)
  3. In ‘Terminal’, launch ROS Master with,
roscore

4. in a new ‘Terminal’ window, run the ROS node that passes on the serial data, type and run

rosrun rosserial_python serial_node.py /dev/ttyACM0

you should change ‘/dev/ttyACM0’ to whatever port your Arduino is connected on.

5. listening in on the topics. We can list the topics in yet another ‘Terminal’ window using

rostopic list

You should see the ‘/chatter’ topic in the list. we can see the messages published on this topic using,

rostopic echo /chatter

Done!

You should see the message ‘hello world!’ being printed out again and again.

ROSserial has much more functionalities, which can be found here!

--

--