6 Using ROS with the Raspberry - PolytechAngersMecatroniqueClub/Tutorials GitHub Wiki

Using ROS with the Raspberry

Installing ROS

Before installing ROS, it is needed to explicit to apt-get where it can download the ROS programs. To do that, create and edit a new file named “ros-latest.list” in the “/etc/apt/sources.list.d/” directory (root privileges are needed):

$ sudo nano /etc/apt/sources.list.d/ros-latest.list

In this file, just write the following line:

deb http://packages.ros.org/ros/ubuntu xenial main

And close while saving with Ctrl+X - Y - Enter.

Note that while writing this tutorial the current version of Lubuntu is “xenial”. If you have an other version replace “xenial” by your name version. To identify your Ubuntu version:

$ lsb_release -sc

Note that all the previous steps can be done with only one command:

$ sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

This is not finished yet, to be able to download from this new address you need to be authenticated. To set up your keys, use the following command:

$ sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

The key should be imported correctly.

The you can update apt-get with this new source:

$ sudo apt-get update

Finally, you can download and install ROS. Here we install the desktop kinetic ROS version:

$ sudo apt-get install ros-kinetic-desktop

This can take a while, go grab a coffee… Note: do not choose the ros-kinetic-desktop-full version for the raspberry! We do not need it and it takes more time to install!

Initialize rosdep

The first time you will run ROS (and only once), you should initialize the dependencies first. To do that, you can run the following commands:

$ sudo rosdep init
$ rosdep update

Now the dependencies are initialized, you can continue to the next step.

Setting up the catkin workspace

Usually the ROS projects are in a catkin_ws folder (ws stands for workspace). To create it, do

$ mkdir ~/catkin_ws

in this “catkin_ws” directory you should have a “src” folder that will contain all your source code. Thus do

$ cd ~/catkin_ws
$ mkdir src

Now everything is set, you can use the catkin_make command to initialize your workspace. To use the catkin_make command, you have to specify where is this command. To do so, use the following command

$ source /opt/ros/kinetic/setup.bash

This should do nothing (on the terminal I mean), but now you can do the following

$ catkin_make -j2

This command will create files and folders. Now you should have a “build” and a “devel” folders next to your “src”. Note that the source command should be done every time you open a new terminal. Note that to use the programs of your catkin workspace you should source your workspace and not the general ROS setup, by doing

$ cd ~/catkin_ws
$ source devel/setup.bash

Avoid the source command each time

The source command to add setup.bash needs to be done each time you open a new terminal... In order to automate this source command, you can edit the .bashrc file:

$ nano ~/.bashrc

by adding at the end the folowing lines:

# source for ROS
source /opt/ros/kinetic/setup.bash
source ~/catkin_ws/devel/setup.bash

Once this is done, you will not have to do the source command ever again.

RPLiDAR in ROS

Allowing ROS to access the LiDAR

Before installing or using the RPLiDAR ROS node, you should allow it to access the sensor when plugged into the raspberry. Most of the time the LiDAR is mapped to /dev/ttyUSB0. The idea it to allow ROS to access the /dev/ttyUSB0. To do that you can do

$ sudo chmod 666 /dev/ttyUSB0

But this solution is not really nice, since you will have to do it each time you will reboot the raspberry or replug the LiDAR. To make it more permanently, you can add a dev rule (device rule). To do so, create and edit a new file, named “49-rplidar.rules” in the folder “/etc/udev/rules.d/”

$ sudo nano /etc/udev/rules.d/49-rplidar.rules

In this file write the line

KERNEL=="ttyUSB*", MODE=="0666"

And save and close the file with Ctrl+X - Y - Enter. Then reboot the raspberry

$ sudo reboot

Installing and using the RPLiDAR node

To use the RPLiDAR ROS node you first need to download the source from github (https://github.com/robopeak/rplidar_ros). Extract the downloaded “rplidar_ros-master.zip” archive and put the extracted folder into your “catkin_ws/src” folder. Rename the “rplidar_ros-master” folder by removing the “-master” that is not useful. You should now have a “rplidar_ros” folder into the “~/catkin_ws/src” folder.

You can then make the project by doing

$ catkin_make -j2

in the “catkin_ws” directory. The -j2 option is to avoid that the raspberry freeze because it uses all the four cores at their maximum.

Note that if you are using a new terminal, you have to do

$ source devel/setup.bash

first! Otherwise the catkin_make command will not be recognize.

Once the compilation is done, you can install the rplidar node with

$ catkin_make install

Congratulation, the node is installed, you can start using it. To do so, open a new terminal, and lets named it terminal 1 (1$), and an other terminal (2$). On the first terminal, we will start the roscore node:

1$ cd catkin_ws
1$ source devel/setup.bash
1$ roscore

If everything went well you sould have a message like

started code service [/rosout]

Now on the second terminal, start the rplidar_ros node (without closing the terminal 1)

2$ cd catkin_ws
2$ source devel/setup.bash
2$ roslaunch rplidar_ros view_rplidar.launch

If rviz opens but the LiDAR is not displayed, make sure that the LiDAR is plugged correctly to the raspberry and that you gave the rights to ROS to access the ttyUSB0 device.

Using Rviz in a remote computer

It can be useful to run rviz in a remote computer in order to have a visual representation of the robot’s LiDAR measurements, map…

To do so, you first need to have ROS installed on both systems. Lets name them IBOT for the robot and UI for the remote computer that will only use rviz.

Assuming that IBOT has 192.168.49.1 as its IP address, and UI has 192.168.0.2.

The idea is to run roscore only on IBOT and specify to UI the IP of the running roscore.

To do that, on IBOT use the commands

IBOT$ export ROS_MASTER_URI=http://192.168.49.1:11311
IBOT$ export ROS_IP=192.168.49.1

then run roscode

IBOT$ roscore

On UI, use the commands

UI$ export ROS_MASTER_URI=http://192.168.49.1:11311
UI$ export ROS_IP=192.168.0.2

and run rviz

UI$ rosrun rviz rviz

Note that each time you will open a new terminal you will have to do the export command! To check the state of the environment variables you can do

$ printenv

or

$ printenv | grep ROS

if you only want the ROS variables.

If you want the export to be permanent, you can edit the bashrc file that initialize the terminals.

$ sudo gedit ~/.bashrc

by adding the two exports at the end. For the IBOT robot for instance:

export ROS_MASTER_URI=http://192.168.49.1:11311
export ROS_IP=192.168.49.1

Kinect1, Raspberry Pi and ROS

install freenect

In order to install the kinect1 drivers, use the commands

$ sudo apt-get update
$ sudo apt-get install freenect

Then you can plug the kinect to the RPI, and test it with the command

$ freenect-glview

This should display the color and depth images on the screen.

install ros-freenect

To interface the kinect with ROS, you can install a freenect node by doing

$ sudo apt-get install ros-kinetic-freenect-stack

To launch the ROS node

$ roslaunch freenect_launch freenect.launch