Sie sind auf Seite 1von 53

ROS TUTORIAL

Yen Chao
2015/08/06

INTRODUCTION
What is ROS?
General Concepts

WHAT IS ROS?
Robot Operating System
It provides the services you would
expect from an operating system:

hardware abstraction
low-level device control
message-passing between processes
package management

It is a collection of tools, libraries, and


conventions that helps you to write
robot software.

WHY WE NEED ROS?


Creating truly robust, generalpurpose robot software is hard.
Computer Vision
Artificial Intelligence
Natural Language Processetc.

The goal of ROS


Support codereuse
Encourage collaboration

WHAT IS ROS?
ROS provides a distributed framework
of processes that enable these
processes
to be designed individually,
to be loosely coupled at runtime,
to be grouped together and be shared to
others.

GENERAL CONCEPTS: NODE


Nodes:
Nodes are processes that perform
computation.
E.g. Camera node, Keyboard control node.

Nodes communicate with each other


by two different ways.
Message: Publish/Subscribe on Topics
Service: Request/Response to other Nodes
Message

GENERAL CONCEPTS: MASTER


Nodes communicate with each other
with the help of ROS Master.
ROS Master:
The ROS Master helps nodes to find and
send messages to each other.

ROS
Master

Register name,
message and
service
Look up

Nod
e

GENERAL CONCEPTS: MESSAGE


Message:
Nodes publish/subscribe on Topics.

Topic:
A name used to identify the content of the
message.
E.g. odometry

Message Type (msg):


Describe the fields of a ROS message.
E.g. Pose
float32 x, float32 y, float32 theta

http://robohub.org/ros-101-intro-to-the-robot-operating-

GENERAL CONCEPTS: MESSAGE

GENERAL CONCEPTS: SERVICE


Service:
Nodes request/response to other nodes.

Service Type (srv)


Describe the fields of ROS service request
and respond.
E.g. move_to
Pose goal
--Pose reached

http://robohub.org/ros-101-intro-to-the-robot-operating-

GENERAL CONCEPTS: SERVICE

SUMMARY
ROS is a flexible framework for
writing robot software with useful
tools, librariesetc.
Processes are called nodes in ROS.
Nodes communicate with each other
by
Message
Service

ROS Master helps nodes find and


send messages to each other.

GETTING STARTED
Installation
Understanding Node
Understanding Message
Understanding Service

UNDERSTANDING NODE: BASIC


roscore
roscore = master + rosout + parameter
server
The very first thing you should run when
using ROS.

rosnode
rosnode displays information about the
ROS nodes that are currently running.

rosrun
rosrun allows you to run a node by its
name and package.

UNDERSTANDING NODE: TURLESIM


Install the tutorial by
sudo apt-get install ros-indigo-ros-tutorials

Run the turtlesim_node by


rosrun turtlesim turtlesim_node

Show all the nodes that are running


by
rosnode list

Reassign Names from the commandline


rosrun turtlesim turtlesim_node
__name:=my_turtle

UNDERSTANDING MESSAGE :
TURLESIM
Turtle keyboard teleoperation process
rosrun turtlesim turtle_teleop_key

Now you can use keyboard to control


your turtle.

UNDERSTANDING MESSAGE :
RQT_GRAPH
ROS provides rqt_graph to help us
visualize whats going on between
processes.
rosrun rqt_graph rqt_graph

Teleop_turtle and turtlesim are

UNDERSTANDING MESSAGE:
ROSTOPIC
rostopic is a tool that help us to
interact with the topics.
rostopicecho
rostopic list
rostopic type [topic]
rosmsg show [msg]
rostopic pub [topic] [msg_type] [args]

UNDERSTANDING SERVICE:
ROSSERVICE
Service:
Request/Response

rosservice is a tool that help us to


interact with services.
rosservice list
rosservice type [service]
rossrv show [service]
rosservice call [service] [args]

SUMMARY
roscore = master + rosout +
parameter server
rosnode : ROS tool to get information
about a node.
rosrun: Run a node from a given
package.
rqt_graph : Show the relationship of
nodes.
rostopic : ROS tool to interact with
topics.

WRITING A PROGRAM
Creating a ROS Workspace
Creating a ROS Package
Creating a ROS msg
Writing a Simple Publisher and Subscriber (C++)
Creating a ROS srv
Writing a Simple Server and Client (C++)

CREATING A ROS WORKSPACE


(CATKIN)
To create a catkin workspace:
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace

We can use catkin_make command to


build the workspace
cd ~/catkin_ws/
catkin_make

Source the setup file


source devel/setup.bash

CREATING A ROS PACKAGE(CATKIN)


Packages:
The main unit for organizing software in ROS.
E.g. Face Recognition package, turtlesim
package.

Create a catkin package


catkin_create_pkg <package_name>
[depend1] [depend2] [depend3]
catkin_create_pkg beginner_tutorials std_msgs
rospy roscpp
catkin_make
source ~/catkin_ws/devel/setup.bash

CREATING A ROS MSG


msg:
msg files are simple text files that
describe the fields of a ROS message.

Create a msg file


cd ~/catkin_ws/src/beginner_tutorials
mkdir msg
echo "int64 num" > msg/Num.msg

Prepare to generate C++ source code


In package.xml, uncomment
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>

CREATING A ROS MSG


In CMakeLists.txt
Add message_generation to find package
function.
Add CATKIN_DEPENDS message_runtime to
catkin_package function.
Add FILES Num.msg to add_message_files
function.
generate_messages( DEPENDENCIES std_msgs )
Uncomment following:

Generate C++ source code


catkin_make
The C++ message header file will be
generated in
~/catkin_ws/devel/include/beginner_tutorials/

WRITING A SIMPLE PUBLISHER(C++)


Create a src directory
mkdir -p ~/catkin_ws/src/beginner_tutorials/src

Create the src/talker.cpp file


https://
raw.githubusercontent.com/ros/ros_tutorials
/indigo-devel/roscpp_tutorials/talker/talke
r.cpp "ros/ros.h"
#include

Include ROS library.


#include "std_msgs/String.h"

Include string message type.

WRITING A SIMPLE PUBLISHER (C++)


Initialize ROS.
ros::init(argc, argv, "talker");

Create a node handle.


ros::NodeHandle n;

Register with the master.

ros::Publisher chatter_pub = n.advertise<std_msgs::String>


("chatter", 1000);

Set loop rate (Hz)


ros::Rate loop_rate(10);

WRITING A SIMPLE PUBLISHER (C++)


While ROS is running.
while (ros::ok())

Set the message.

std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();

Publish the message.


chatter_pub.publish(msg);
loop_rate.sleep();
Sleep for a while (10Hz -> 0.1 sec)

WRITING A SIMPLE SUBSCRIBER(C+


+)
Create the src/listener.cpp file
https://raw.github.com/ros/ros_tutorials/ind
igo-devel/roscpp_tutorials/listener/listener
.cpp

Initialize argv,
ROS.
ros::init(argc,
listener");
ros::NodeHandle
Create a node
n; handle.

Registersubwith
the master.
ros::Subscriber
= n.subscribe("chatter",
1000, chatterCallback);

WRITING A SIMPLE SUBSCRIBER (C+


+)
ros::spin()
ros::spin();

ros::spin() enters a loop, calling message


callbacks as fast as possible.

Callback function

void chatterCallback(const std_msgs::String::ConstPtr& msg)


{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}

ROS_INFO acts like print in python or


cout with endl in c++.

BUILDING CODES
In CMakeLists.txt, add:
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp

Build
cd ~/catkin_ws
catkin_make

EXAMINE YOUR PROGRAMS


Source setup file after calling
catkin_make
source ./devel/setup.bash

Use rosrun to run both processes


rosrun beginner_tutorials talker
rosrun beginner_tutorials listener

SUMMARY
Create a workspace
catkin_init_workspace

Create a package
catkin_create_pkg

Create a ROS msg


Create *.msg file
Add message_generation and
message_runtime dependencies in
CMakeLists.txt
catkin_make

SUMMARY
Write a program
Initialize ROS.
ros::init(argc, argv, my_node");

Create a node handle.

ros::NodeHandle n;

Build the program


CMakeLists.txt

add_executable(my_node src/my_node.cpp)
target_link_libraries(my_node ${catkin_LIBRARIES})
add_dependencies(my_node my_package_generate_messages_cpp

APPENDIX

MESSAGING PATTERN

Publish/Subscribe
http://zguide.zeromq.org/page:all

Request/Response

GENERAL CONCEPTS
ROS has three levels of concepts:
The File system level
The Computation Graph level
The Community level.

FILE SYSTEM
ROS resource management
Package
The main unit for organizing software in ROS (e.g. odometer).

Package manifest
Repository (rosbuild stack)
A collection of packages can be released together (e.g.
navigation).

Message type
Service type

COMPUTATION GRAPH
The peer-to-peer network of ROS processes
that are processing data together
Node
ROS Master
Parameter Server
Message
Topic
Service
Bag

UNDERSTANDING MESSAGE: TOPIC

/turtle1/cmd_vel is called a topic.


Topic:
The topic is a name used to identify the content of the
message.
A node publishes a message to a giventopic.
Another node subscribes the topic to get the message.

INSTALLATION
You can find installation guide on
http://wiki.ros.org/ROS/Installation
The current LTS release of ROS is ROS
Indigo.
ROS IndigoONLYsupports Saucy
(13.10) and Trusty (14.04) for debian
packages.

INSTALL ROS INDIGO ON UBUNTU


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

Set up your keys


sudo apt-key adv --keyserver hkp://pool.skskeyservers.net --recv-key 0xB01FA116

Installation
sudo apt-get update
sudo apt-get install ros-indigo-desktop-full

INITIALIZATION & SETUP


Initialize rosdep
sudo rosdep init
rosdep update

Environment setup
echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc
source ~/.bashrc

Getting rosinstall
sudo apt-get install python-rosinstall

CREATING A ROS SRV


srv:
An srv file describes a service.
It is composed of two parts: a request and a
response, separated by ---.

Create a srv file


cd ~/catkin_ws/src/beginner_tutorials
mkdir srv
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

CREATING A ROS SRV


Prepare to generate C++ source code
In package.xml, uncomment
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>

In CMakeLists.txt
Add message_generation to find package function.
Add CATKIN_DEPENDS message_runtime to catkin_package
function.

Add FILES AddTwoInts.srv to add_service_files


function.

Generate C++ source code


catkin_make
The C++ message header file will be generated
in ~/catkin_ws/devel/include/beginner_tutorials/

WRITING A SIMPLE SERVER(C++)


Create the
src/add_two_ints_server.cpp file.
#include
"beginner_tutorials/AddTwoInts.h"
Include
AddTwoInt service type we
bool created.
add(beginner_tutorials::AddTwoInts::Request &req,
beginner_tutorials::AddTwoInts::Response &res)
{ Callback function
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a,
(long int)req.b);
ROS_INFO("sending back response: [%ld]", (long
int)res.sum);
return true;
}

WRITING A SIMPLE SERVER (C++)


Initialize ROS.
ros::init(argc, argv, "add_two_ints_server");

Create a node handle.


ros::NodeHandle n;

Register service name and callback


ros::ServiceServer service = n.advertiseService("add_two_ints", add)
function.
ros::spin();

Wait for service being called.

WRITING A SIMPLE CLIENT(C++)


Create the src/add_two_ints_client.cpp
file
Include
AddTwoInt service type we
#include
"beginner_tutorials/AddTwoInts.h"
created.
ros::init(argc, argv, add_two_ints_client");

Initialize ROS.
ros::NodeHandle n;

Create a node handle.

ros::ServiceClient client = n.serviceClient


<beginner_tutorials::AddTwoInts>("add_two_ints");

Register with the master.

WRITING A SIMPLE CLIENT (C++)


Set a service with inputs.
beginner_tutorials::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);

the service and wait for reply.


if Call
(client.call(srv))
client.call(srv) returns true if the call is
success, else it returns false.

BUILDING CODES
In CMakeLists.txt, add:
add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
add_dependencies(add_two_ints_server
beginner_tutorials_generate_messages_cpp)
add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})
add_dependencies(add_two_ints_client
beginner_tutorials_generate_messages_cpp)

Build
cd ~/catkin_ws
catkin_make

EXAMINE YOUR PROGRAMS


Source the setup file after calling
catkin_make.
source ./devel/setup.bash

Use rosrun to run both processes.


rosrun beginner_tutorials add_two_ints_server
rosrun beginner_tutorials add_two_ints_client X Y

ADD_TWO_INTS_SERVER.CPP
#include "ros/ros.h
#include "beginner_tutorials/AddTwoInts.h
bool add(beginner_tutorials::AddTwoInts::Request &req,
beginner_tutorials::AddTwoInts::Response &res) {
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a,
(long int)req.b);
ROS_INFO("sending back response: [%ld]", (long
int)res.sum);
return true;
}
int main(int argc, char **argv) {
ros::init(argc, argv, "add_two_ints_server");
ros::NodeHandle n;
ros::ServiceServer service =
n.advertiseService("add_two_ints", add);
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;

ADD_TWO_INTS_CLIENT.CPP
#include "ros/ros.h
#include "beginner_tutorials/AddTwoInts.h
#include <cstdlib>
int main(int argc, char **argv) {
ros::init(argc, argv, "add_two_ints_client");
if (argc != 3) {
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
beginner_tutorials::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
if (client.call(srv)) {
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
} else {
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}

Das könnte Ihnen auch gefallen