0% found this document useful (0 votes)
18 views25 pages

ROS Tutorial

This document provides a tutorial on installing and using ROS (Robot Operating System). It discusses installing ROS on Ubuntu 16.04, setting up a ROS workspace, basic ROS concepts like nodes and topics, and ROS tools for querying the system. The tutorial also covers simulating robots with Gazebo, creating ROS packages, building robot models, and includes exercises for practicing package development. The author is Antonio Marin-Hernandez and it was published on April 18, 2020.

Uploaded by

Andreiv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views25 pages

ROS Tutorial

This document provides a tutorial on installing and using ROS (Robot Operating System). It discusses installing ROS on Ubuntu 16.04, setting up a ROS workspace, basic ROS concepts like nodes and topics, and ROS tools for querying the system. The tutorial also covers simulating robots with Gazebo, creating ROS packages, building robot models, and includes exercises for practicing package development. The author is Antonio Marin-Hernandez and it was published on April 18, 2020.

Uploaded by

Andreiv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Universidad Veracruzana

Research Center on Artificial Intelligence

LAAS-CNRS
Robotics, Action and Perception (RAP) group

ROS Tutorial:
Robotics Operation System

Author:
Antonio Marin-Hernandez

April 18, 2020


ii
Contents

1 ROS Installation 1
1.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Environment Setup . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Creating a Workspace . . . . . . . . . . . . . . . . . . 2

2 Beginning with ROS 5


2.1 Basic Concepts in ROS . . . . . . . . . . . . . . . . . . . . . 5
2.2 ROS Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 rosversion . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.2 roscd . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.3 rospack . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.4 rosmsg . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.5 rostopic . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3 Simulation with Gazebo 9


3.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Initializing Gazebo Simulation . . . . . . . . . . . . . . . . . 9

4 Creating Packages in ROS with catkin 13


4.1 Creating a package . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Compiling your Package . . . . . . . . . . . . . . . . . . . . . 14
4.3 Customizing your Package . . . . . . . . . . . . . . . . . . . . 16
4.4 Reading and Writing Topics in your code . . . . . . . . . . . 17
4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.5.1 Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.5.2 Square . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.5.3 Spiral . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5 Creating your Own Robot Model 19


5.1 URDF models . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.2 Robot State Publisher . . . . . . . . . . . . . . . . . . . . . . 19
5.3 Joint States . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

iii
iv CONTENTS

6 References 21
Chapter 1

ROS Installation

ROS is developed by Willow Garage Inc.TM , with contributions from all


around the world. There are more than 2,000 packages, going from diverse
robotic platforms to many computer algorithms, and passing from many
hardware drivers (sensors and actuators).

1.1 Requirements
Depending on your OS, the installation of ROS requires different packages
and dependencies, as example:

• for Linux you will need:


GCC version 4.4 or higher, CMake, Python version 2.7.

1.2 Installation
Along this tutorial ROS Kinetic and Ubuntu 16.04 (Precise) are preferred,
so here are instructions to install on this particular distribution.
To begin with the installation, first of all, setup your computer to accept
software from the site: packages.ros.org. ROS Kinetic ONLY supports
Wily and Xenial distributions. However, you can install another ROS ver-
sion following instructions on the web, Melodic is then prefered.
We begin adding the ROS repository by the following instruction:
$ sudo sh -c ’echo "deb http://packages.ros.org/ros/ubuntu $(
lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.
list’

next, we have to set up the keys by:


$ sudo apt-key adv --keyserver ’hkp://keyserver.ubuntu.com:80’ --
recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
\end{lstlisting

1
2 CHAPTER 1. ROS INSTALLATION

%$ wget http://packages.ros.org/ros.key -O - | sudo apt-key add -

And now, make sure your package index is up-to-date by:

\begin{lstlisting}
$ sudo apt update

Now, lets proceed to install ROS, the Desktop-Full Install is recom-


mended, it includes: ROS, rqt, rviz, robot-generic libraries, 2D/3D simula-
tors, navigation and 2D/3D perception.
$ sudo apt install ros-kinetic-desktop-full

To install this tool and other dependencies for building ROS packages,
run:
sudo apt install python-rosdep python-rosinstall python-rosinstall
-generator python-wstool build-essential

Before you can use ROS, you will need to initialize rosdep.
rosdep enables you to easily install system dependencies for source you
want to compile and is required to run some core components in ROS.
sudo apt install python-rosdep

Finally you need to initialize rosdep. Write following instructions:


$ sudo rosdep init
$ rosdep update

1.3 Environment Setup


1.3.1 Creating a Workspace
First, you need to choose a workspace directory, preferably an empty direc-
tory, or better to create a new one, for example can be called : ros-workspace,
which can be createed at your home’s path.
$ mkdir -p ~/my_workspace/src

from now, the directory my_workspace (without src directory) will be your
workspace and be referred as </path/to/your/workspace>.

Go to the path, that you just have created.


$ cd <path/to/your/workspace>

This path will be the root from where ROS will search to find your
projects. In order to access by ROS commands, it should be initiated. To
do it, in your workspace execute following lines in your terminal:
1.3. ENVIRONMENT SETUP 3

source <path/to/ros/install>/setup.bash

Now, let’s create a workspace with catkin:


cd <path/to/your/workspace>/src
catkin_init_workspace

Even though the workspace is empty (there are no packages in the ’src’
folder, just a single CMakeLists.txt link) you can still "build" the workspace:
cd <path/to/your/workspace>
catkin_make

What makes directories devel and build in your </path/to/your/-


workspace>
In order to access easily to your workspace put following lines into your
.bashrc or .login file, depending on your system:
source <path/to/ros/install>/setup.bash
source <path/to/your/workspace>/devel/setup.bash

To test configuration go to your home directory


cd

and type
roscd

now, you should be at


pwd
<path/to/your/workspace>/devel
4 CHAPTER 1. ROS INSTALLATION
Chapter 2

Beginning with ROS

In this section, we will discuss about initial concepts and tools to query
system and/or communicate between modules (nodes) in ROS.

2.1 Basic Concepts in ROS


ROS is aka operating system for robots, which could be considered as a
client/server system. ROS system is composed from different packages that
include nodes, topics, services and parameters. Nodes, called also rosnodes,
are the executables programs.
Topics and services are ways of communication between nodes. They
depend from each node (see node definitions). Services rely on a query
made by a given node or from terminal, getting a response from the node
offering the service. In a different way, topics require a subscription to a
node that will be broadcasting some particular info.

Figure 2.1: ROS concepts.

5
6 CHAPTER 2. BEGINNING WITH ROS

2.2 ROS Tools


ROS provides some useful tools to get information about the topics and
services in run.

2.2.1 rosversion
The rosversion command tool, is used to recover current version in your
system, by running the following command, you should get:
$ rosversion -d
kinetic

unless you are using another ROS distribution (i.e. groovy or fuerte)

2.2.2 roscd
roscd will locate you at your ROS workspace:
$ roscd
$ pwd
<path/to/your/workspace>/devel

2.2.3 rospack
You can find the path where a package is installed by:
$ rospack find <package>
<the/path/to/package>

2.2.4 rosmsg
Depending on your installed packages different types of messages can be
provided. It’s possible to list all types of messages installed by running the
following command:
$ rosmsg list
geometry_msgs/Point
geometry_msgs/Point32
geometry_msgs/PointStamped
geometry_msgs/Polygon
...

Following commands and tools require communication with master so,


lets run the following command to enable master’s communication:
$ roscore

Command roscore enables master and basic requirements and services


for ROS package communication.
2.2. ROS TOOLS 7

2.2.5 rostopic
In another terminal write the following command:
$ rostopic list
/rosout
/rosout_agg

by the moment we left this command and we will return to it later.


8 CHAPTER 2. BEGINNING WITH ROS
Chapter 3

Simulation with Gazebo

ROS includes a physics based simulation engine called Gazebo, initially de-
veloped in conjunction with the Player/Stage project.
Gazebo in ROS is considered as a node, offering different topics and
services.

3.1 Requirements
To successful do this section it is required the following ROS packages:
gazebo_ros
turtlebot_bringup
turtlebot_description
turtlebot_gazebo

All these packages should be available with apt-get command or bias


Ubuntu "Software Center".

3.2 Initializing Gazebo Simulation


To launch Gazebo with an empty world write the following command from
a terminal,
$ roslaunch gazebo_worlds empty_world.launch

this will display a window with a plane.


In order to known all services offered by gazebo node, you can type in
another terminal window:
$ rosservice list gazebo
from we get the following message
/gazebo/apply_body_wrench
/gazebo/apply_joint_effort
/gazebo/clear_body_wrenches
/gazebo/clear_joint_forces

9
10 CHAPTER 3. SIMULATION WITH GAZEBO

/gazebo/delete_model
/gazebo/get_joint_properties
/gazebo/get_link_properties
/gazebo/get_link_state
/gazebo/get_loggers
/gazebo/get_model_properties
/gazebo/get_model_state
/gazebo/get_physics_properties
/gazebo/get_world_properties
/gazebo/pause_physics
/gazebo/reset_simulation
/gazebo/reset_world
/gazebo/set_joint_properties
/gazebo/set_link_properties
/gazebo/set_link_state
/gazebo/set_logger_level
/gazebo/set_model_configuration
/gazebo/set_model_state
/gazebo/set_parameters
/gazebo/set_physics_properties
/gazebo/spawn_gazebo_model
/gazebo/spawn_urdf_model
/gazebo/unpause_physics

To put objects in the world there are many ways. All objects in the
world are considered robots, while some of them are not autonomous.
Change to gazebo_worlds path by:
$ roscd gazebo_worlds/

Now, list contents in directory


$ ls

you should get something similar to:


CMakeLists.txt
launch
objects
test
Makefile
added_threading_stuff_test.patch
manifest.xml
scripts
worlds
Media
bin
meshes
src

To include an object into the scene run the following command:


$ rosrun gazebo spawn_model -file objects/desk1.model -gazebo -
model desk1 -x 0
3.2. INITIALIZING GAZEBO SIMULATION 11

you should see green desk in the gazebo window.


spawn_model is a service offered by node gazebo, there are many ways to
access services, in this case is by a rosrun a gazebo node with spawn_model
service. spawn_model has different parameters, to known it type:
$ rosrun gazebo spawn_model

you should get something like:


Commands:
-[urdf|gazebo|trimesh] - specify incoming xml is urdf or
gazebo format
-[file|param] [<file_name>|<param_name>] - source of the model
xml or the trimesh file
-model <model_name> - name of the model to be spawned.
-reference_frame <entity_name> - optinal: name of the model/
body where initial pose is defined.
If left empty or specified as
"world", gazebo world
frame is used.
-namespace <ros_namespace> - optional: all subsequent ROS
interface plugins will be inside of this namespace.
-unpause - optional: !!!Experimental!!! unpause physics after
spawning model
-wait - optional: !!!Experimental!!! wait for model to exist
-trimesh_mass <mass in kg> - required if -trimesh is used:
linear mass
-trimesh_ixx <moment of inertia in kg*m^2> - required if -
trimesh is used: moment of inertia about x-axis
-trimesh_iyy <moment of inertia in kg*m^2> - required if -
trimesh is used: moment of inertia about y-axis
-trimesh_izz <moment of inertia in kg*m^2> - required if -
trimesh is used: moment of inertia about z-axis
-trimesh_gravity <bool> - required if -trimesh is used:
gravity turned on for this trimesh model
-trimesh_material <material name as a string> - required if -
trimesh is used: E.g. Gazebo/Blue
-trimesh_name <link name as a string> - required if -trimesh
is used: name of the link containing the trimesh
-x <x in meters> - optional: initial pose, use 0 if left out
-y <y in meters> - optional: initial pose, use 0 if left out
-z <z in meters> - optional: initial pose, use 0 if left out
-R <roll in radians> - optional: initial pose, use 0 if left
out
-P <pitch in radians> - optional: initial pose, use 0 if left
out
-Y <yaw in radians> - optional: initial pose, use 0 if left
out
-J <joint_name joint_position> - optional: initialize the
specified joint at the specified value

lets put another object in the world by typing :


$ rosrun gazebo spawn_model -file objects/000.580.67.model -gazebo
-model cup -z 2
12 CHAPTER 3. SIMULATION WITH GAZEBO
Chapter 4

Creating Packages in ROS


with catkin

A package to be considered as a catkin package, it must meet a few require-


ments:
The package must contain a catkin compliant package.xml file. This
file provides meta information about the package. The package must also
contain a CMakeLists.txt file, which is used by catkin.
There can be no more than one package in each folder. This means
no nested packages nor multiple packages sharing the same directory. The
simplest possible package might look like this:
my_package/
CMakeLists.txt
package.xml

4.1 Creating a package


To create a package you need to be at <path/to/your/workspace>/src
directory. If it’s your first package, your src directory should contain only a
CMakeLists.txt file. If is not your first package it should contain a direc-
tory for each package previously created. Finally if it is empty you should
initialize your workspace as has been described in chapter 2.

Lets create our first package by writing the following command:


$ catkin_create_pkg my_first_rospkg std_msgs rospy roscpp
Created file my_first_rospkg/package.xml
Created file my_first_rospkg/CMakeLists.txt
Created folder my_first_rospkg/include/my_first_rospkg
Created folder my_first_rospkg/src
Successfully created files in <path/to/your/workspace>/src/
my_first_rospkg. Please adjust the values in package.xml.

13
14 CHAPTER 4. CREATING PACKAGES IN ROS WITH CATKIN

We have created the new package called my_first_rospkg which de-


pends on: std_msgs rospy & roscpp.
Now, you have inside your src directory a new one, called my_first_rospkg;
and inside latter, you should have some files and directories just created.
The catkin_create_pkg instruction has the following format:
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

Using command rospack we can recover dependencies of a given pack-


age, for example:
$ rospack depends1 my_first_rospkg
roscpp
rospy
std_msgs

The previous list returned by rospack command, are the primarily de-
pendencies. However as each dependencies have also their dependencies, our
package has indirect dependencies. They can be recovered by:
$ rospack depends my_first_rospkg
catkin
console_bridge
cpp_common
rostime
roscpp_traits
roscpp_serialization
genmsg
genpy
message_runtime
gencpp
genlisp
message_generation
rosbuild
rosconsole
std_msgs
rosgraph_msgs
xmlrpcpp
roscpp
rosgraph
rospack
roslib
rospy

We can individually recover the indirect dependencies by using same


command on each of the primarily dependencies.

4.2 Compiling your Package


At this stage you can compile your new package and running it. However it
doesn’t contains nothing in their code, so it should do anything. To compile
4.2. COMPILING YOUR PACKAGE 15

your package you need to be at your </path/to/your/workspace> and


not in the src directory. Then you should run the command:

$ catkin_make
Base path: </path/to/your/workspace>
Source space: </path/to/your/workspace>/src
Build space: </path/to/your/workspace>/build
Devel space: </path/to/your/workspace>/devel
Install space: </path/to/your/workspace>/install
####
#### Running command: "cmake </path/to/your/workspace>/src -
DCATKIN_DEVEL_PREFIX=</path/to/your/workspace>/devel -
DCMAKE_INSTALL_PREFIX=</path/to/your/workspace>/install" in
"</path/to/your/workspace>/build"
####
-- Using CATKIN_DEVEL_PREFIX: </path/to/your/workspace>/devel
-- Using CMAKE_PREFIX_PATH: </path/to/your/workspace>/devel;/opt/
ros/hydro
-- This workspace overlays: </path/to/your/workspace>/devel;/opt/
ros/hydro
-- Using PYTHON_EXECUTABLE: /usr/bin/X11/python
-- Using Debian Python package layout
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: </path/to/your/workspace>/build/
test_results
-- Found gtest sources under ’/usr/src/gtest’: gtests will be
built
-- catkin 0.5.81
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - my_first_rospkg
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: ’my_first_rospkg’
-- ==> add_subdirectory(my_first_rospkg)
-- Configuring done
-- Generating done
-- Build files have been written to: </path/to/your/workspace>/
build
####
#### Running command: "make -j4 -l4" in "</path/to/your/workspace
>/build"
####

In opposite way as cmake command works, catkin compiles everything


inside the workspace a not only one package. NOTE: You should always
run catkin_make command from your root workspace, i.e. <path/to/y-
our/workspace>.
16 CHAPTER 4. CREATING PACKAGES IN ROS WITH CATKIN

4.3 Customizing your Package


Lets begin gazebo simulator with the turtlebot robot by tapping on your
teminal:
$ roslaunch turtlebot_gazebo turtlebot_empty_world.launch

Turtlebot uses the following topic /mobile_base/commands/velocity


to get speed commands. This topic is of the type geometry_msgs/Twist,
as can be see using the rostopic info command. In order to communicate
with our package with the robot, it’s necessary to create a code able to write
on this topic.
Lets add this code to our package. Open your favorite code editor, e.g.
emacs or gedit, and create a file called my_first_code.cpp in the src
directory of my_first_rospkg and then copy following lines inside it:
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

int main(int argc, char **argv)


{
ros::init(argc, argv, "my_first_node");
ros::NodeHandle n;
ros::Publisher speed_pub = n.advertise<geometry_msgs::Twist>("/
mobile_base/commands/velocity", 1000);
ros::Rate loop_rate(10);

int count = 0;

while (ros::ok()) {
geometry_msgs::Twist speedMsg;
speedMsg.linear.x=0.0;
speedMsg.linear.y=0.0;
speedMsg.linear.z=0.0;
speedMsg.angular.x=0.0;
speedMsg.angular.y=0.0;
speedMsg.angular.z=0.5;

speed_pub.publish(speedMsg);

ros::spinOnce();
loop_rate.sleep();
++count;
}

return 0;
}

Now, lets modify CMakeLists.txt file on the home of your package, by


uncomment and modifying or writing the following lines:
## Declare a cpp executable
add_executable(my_first_rospkg_node src/my_first_code.cpp)
4.4. READING AND WRITING TOPICS IN YOUR CODE 17

and
## Specify libraries to link a library or executable target
against
target_link_libraries(my_first_rospkg_node
${catkin_LIBRARIES}
)

Now lets compile the node by running catkin_make command on the top
of your workspace. At this stage you are able to make move your simulated
robot on gazebo by running the command:
$ rosrun my_first_package my_first_rospkg_node

4.4 Reading and Writing Topics in your code


#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include "nav_msgs/Odometry.h"

ros::Publisher speed_pub;
geometry_msgs::Twist speedMsg;

void poseCallback(const nav_msgs::Odometry::ConstPtr& odomMsg)


{

ROS_INFO("Turtlebot -> Reading Odometry Message %f,%f\n",


odomMsg->pose.pose.position.x,
odomMsg->pose.pose.position.y);

speedMsg.linear.x=0.5;
speedMsg.linear.y=0.0;
speedMsg.linear.z=0.0;
speedMsg.angular.x=0.0;
speedMsg.angular.y=0.0;
speedMsg.angular.z=0.0;

if ((fabs(odomMsg->pose.pose.position.x)>2.00) ||
(fabs(odomMsg->pose.pose.position.y)>2.00)){
speedMsg.linear.x=-0.5;
}
speed_pub.publish(speedMsg);
}

int main(int argc, char **argv)


{
ros::init(argc, argv, "publisher");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/odom", 1000,poseCallback);
18 CHAPTER 4. CREATING PACKAGES IN ROS WITH CATKIN

speed_pub = n.advertise<geometry_msgs::Twist>("/mobile_base/
commands/velocity", 1000);

printf("P3DX Reader initialized\n");


ros::spin();
return 0;
}

4.5 Exercises
4.5.1 Circles
Make your simulated robot to move describing a circle of radius r = 1.0m

4.5.2 Square
Following a square of 1m by side

4.5.3 Spiral
Chapter 5

Creating your Own Robot


Model

5.1 URDF models

5.2 Robot State Publisher

5.3 Joint States

19
20 CHAPTER 5. CREATING YOUR OWN ROBOT MODEL
Chapter 6

References

21

You might also like