How to use rosserial with two Arduinos and Raspberry Pi

Arduino is a great development board for reading data from various sensors and controlling the robot’s DC motors. Raspberry Pi is good at running ROS on Linux. So, we can benefit from both systems to build smart robots. The easiest way is to connect these boards via USB cable and make them communicate through ROS nodes.

In this tutorial, I’ll show you how to use two Arduino boards as an extension of the Raspberry Pi computer. Before starting writing the ROS nodes, I have to set the Pi to identify each Arduino. This identification becomes necessary when the robot’s architecture is complex.

Only one ROS node can run on an Arduino board. And because I have two Arduino, I will use one to generate a random number and another to control the LED connected to pin 13.

I will run one ROS nodes that will send and receive data according to this flowchart

The description of the flowchart

  • The user will start all ROS nodes by running a .launch file.
  • The first Arduino board will run a random number script and send data to Raspberry Pi.
  • A ROS node will receive a random number from the first Arduino board. The node will run on Raspberry Pi and will command the LED on the second Arduino board.
  • The second Arduino board will turn ON and OFF the LED (pin 13) depending on the commands received from the ROS node running on the Pi board.

The ROS node to generate a random number

Testing the node
Step 1: Open a Linux Terminal and type the command:

roscore

Step 2: Open a second Linux Terminal and type the following command:

rosrun rosserial_python serial_node.py /dev/ttyACM*

Step 3: To see the random numbers generated by the Arduino node, open a third Terminal and type the following command:

rostopic echo /random_number

The ROS node that displays and calculates the LED’s stage

This ROS node in Python will run on Raspberry Pi. Before you start writing the Python code, you must create the workspace and the package that contains the node. All you need to do is in this article.

The ROS node that controls the LED

Write the launch file

<launch>
<node pkg=”rosserial_python” type=”serial_node.py” name=”twoArduino_LED” output=”screen”>
<param name=”port” value=”/dev/ttyACM0″/>
<param name=”baud” value=”57600″/>
</node>
<node pkg=”rosserial_python” type=”serial_node.py” name=”twoArduinos_RandNo” output=”screen”>
<param name=”port” value=”/dev/ttyACM1″/>
<param name=”baud” value=”57600″/>
</node>
<node name=”random_number” pkg=”pi_and_arduino” type=”twoArduinos_Pi.py” output=”screen” />
</launch>

References:

Leave a Comment

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

Don't Miss Out!

Get the latest news, tutorials, reviews and more direct to your inbox when you subscribe!