0% found this document useful (0 votes)
4 views

lecture4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lecture4

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

Programming for Robotics

Introduction to ROS
Course 4

Péter Fankhauser, Dominic Jud, Martin Wermelinger


Prof. Dr. Marco Hutter

Péter Fankhauser | 27.02.2017 | 1


Course Structure
Course 1 Course 2 Course 3 Course 4 Course 5
Deadline for Ex. 1. Deadline for Ex. 2. Deadline for Ex. 3. Deadline for Ex. 4.
Lecture 1
Lecture 2 Lecture 3 Lecture 4 Case Study
Exercise 1 Intro.
Exercise 2 Intro. Exercise 3 Intro. Exercise 4 Intro. Exercise 5 Intro.

Exercise 1 Exercise 5
Exercise 2 Exercise 3 Exercise 4

Deadline for Ex. 5.

Péter Fankhauser | 27.02.2017 | 2


Overview Course 4

§ ROS services
§ ROS actions (actionlib)
§ ROS time
§ ROS bags

Péter Fankhauser | 27.02.2017 | 3


ROS Services

§ Request/response communication between


nodes is realized with services
Request
§ The service server advertises the service Node 1 Node 2
Service Client Response Service Server
§ The service client accesses this service
§ Similar in structure to messages, services Request Request
service
are defined in *.srv files Response name Response
List available services with
*.srv Service definition
> rosservice list Request
---
Show the type of a service Response
> rosservice type /service_name
Call a service with the request contents More info
> rosservice call /service_name args http://wiki.ros.org/Services

Péter Fankhauser | 27.02.2017 | 4


ROS Services
Examples

nav_msgs/GetPlan.srv
std_srvs/Trigger.srv
geometry_msgs/PoseStamped start
--- Request geometry_msgs/PoseStamped goal
bool success float32 tolerance
string message Response ---
nav_msgs/Path plan

Péter Fankhauser | 27.02.2017 | 5


ROS Service Example
Starting a roscore and a add_two_ints_server node

In console nr. 1:
Start a roscore with
> roscore

In console nr. 2:
Run a service demo node with
> rosrun roscpp_tutorials add_two_ints_server

Péter Fankhauser | 27.02.2017 | 6


ROS Service Example
Console Nr. 3 – Analyze and call service

See the available services with


> rosservice list

See the type of the service with


> rosservice type /add_two_ints

Show the service definition with


> rossrv show roscpp_tutorials/TwoInts

Call the service (use Tab for auto-complete)


> rosservice call /add_two_ints "a: 10
b: 5"

Péter Fankhauser | 27.02.2017 | 7


ROS C++ Client Library (roscpp)
Service Server add_two_ints_server.cpp
#include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
§ Create a service server with
bool add(roscpp_tutorials::TwoInts::Request &request,
ros::ServiceServer service = roscpp_tutorials::TwoInts::Response &response)
nodeHandle.advertiseService(service_name, {
callback_function); response.sum = request.a + request.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)request.a,
(long int)request.b);
§ When a service request is received, ROS_INFO(" sending back response: [%ld]",
callback function is called with the request (long int)response.sum);
return true;
as argument }

§ Fill in the response to the response int main(int argc, char **argv)
argument {
ros::init(argc, argv, "add_two_ints_server");
§ Return to function with true to indicate that it ros::NodeHandle nh;
ros::ServiceServer service =
has been executed properly nh.advertiseService("add_two_ints", add);
ros::spin();
More info return 0;
http://wiki.ros.org/roscpp/Overview/Services }

Péter Fankhauser | 27.02.2017 | 8


ROS C++ Client Library (roscpp)
add_two_ints_client.cpp
Service Client #include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
#include <cstdlib>
§ Create a service client with
int main(int argc, char **argv) {
ros::ServiceClient client = ros::init(argc, argv, "add_two_ints_client");
nodeHandle.serviceClient<service_type> if (argc != 3) {
ROS_INFO("usage: add_two_ints_client X Y");
(service_name); return 1;
}
§ Create service request contents
service.request ros::NodeHandle nh;
ros::ServiceClient client =
§ Call service with nh.serviceClient<roscpp_tutorials::TwoInts>("add_two_ints");
roscpp_tutorials::TwoInts service;
client.call(service); service.request.a = atoi(argv[1]);
service.request.b = atoi(argv[2]);
§ Response is stored in service.response if (client.call(service)) {
ROS_INFO("Sum: %ld", (long int)service.response.sum);
} else {
ROS_ERROR("Failed to call service add_two_ints");
return 1;
More info }
http://wiki.ros.org/roscpp/Overview/Services return 0;
}
Péter Fankhauser | 27.02.2017 | 9
ROS Actions (actionlib)

Action
§ Similar to service calls, but provide possibility to Goal
§ Cancel the task (preempt) Cancel
§ Receive feedback on the progress Node 1 Status Node 2
Action Client Result Action Server
§ Best way to implement interfaces to time-
Feedback
extended, goal-oriented behaviors
§ Similar in structure to services, action are
*.action Action definition
defined in *.action files
Goal
§ Internally, actions are implemented with a set of ---
Result
topics ---
Feedback

More info
http://wiki.ros.org/actionlib
http://wiki.ros.org/actionlib/DetailedDescription

Péter Fankhauser | 27.02.2017 | 10


ROS Actions (actionlib)

Averaging.action
int32 samples FollowPath.action
--- Goal navigation_msgs/Path path
float32 mean
---
float32 std_dev Result bool success
---
int32 sample Feedback ---
float32 data float32 remaining_distance
float32 mean float32 initial_distance
float32 std_dev

Péter Fankhauser | 27.02.2017 | 11


ROS Parameters, Dynamic Reconfigure, Topics, Services, and
Actions Comparison

Parameters Dynamic Topics Services Actions


Reconfigure

Description Global constant Local, changeable Continuous data Blocking call for Non-blocking,
parameters parameters streams processing a request preemptable goal
oriented tasks

Application Constant settings Tuning parameters One-way continuous Short triggers or Task executions and
data flow calculations robot actions

Examples Topic names, camera Controller parameters Sensor data, robot Trigger change, Navigation, grasping,
settings, calibration state request state, motion execution
data, robot setup compute quantity

Péter Fankhauser | 27.02.2017 | 12


ROS Time

§ Normally, ROS uses the PC’s system clock § To take advantage of the simulated time, you
as time source (wall time) should always use the ROS Time APIs:
§ For simulations or playback of logged data, it § ros::Time
is convenient to work with a simulated time ros::Time begin = ros::Time::now();
(pause, slow-down etc.) double secs = begin.toSec();

§ To work with a simulated clock: § ros::Duration


§ Set the /use_sim_time parameter ros::Duration duration(0.5); // 0.5s
> rosparam set use_sim_time true § ros::Rate
§ Publish the time on the topic /clock from ros::Rate rate(10); // 10Hz
§ Gazebo (enabled by default) § If wall time is required, use
§ ROS bag (use option --clock) ros::WallTime, ros::WallDuration,
and ros::WallRate More info
http://wiki.ros.org/Clock
http://wiki.ros.org/roscpp/Overview/Time
Péter Fankhauser | 27.02.2017 | 13
ROS Bags

§ A bag is a format for storing message data Show information about a bag
§ Binary format with file extension *.bag > rosbag info bag_name.bag

§ Suited for logging and recording datasets for Read a bag and publish its contents
later visualization and analysis > rosbag play bag_name.bag
Record all topics in a bag
Playback options can be defined e.g.
> rosbag record --all
> rosbag play --rate=0.5 bag_name.bag
Record given topics
--rate=factor Publish rate factor
> rosbag record topic_1 topic_2 topic_3 --clock Publish the clock time (set
param use_sim_time to true)
Stop recording with Ctrl + C --loop Loop playback
Bags are saved with start date and time as file etc.
name in the current folder (e.g. 2017-02-07- More info
01-27-13.bag) http://wiki.ros.org/rosbag/Commandline

Péter Fankhauser | 27.02.2017 | 14


Further References

§ ROS Wiki § ROS Cheat Sheet


§ http://wiki.ros.org/ § https://github.com/ros/cheatsheet/releases/dow
§ Installation nload/0.0.1/ROScheatsheet_catkin.pdf
§ http://wiki.ros.org/ROS/Installation § ROS Best Practices
§ Tutorials § https://github.com/ethz-
asl/ros_best_practices/wiki
§ http://wiki.ros.org/ROS/Tutorials
§ ROS Package Template
§ Available packages
§ https://github.com/ethz-
§ http://www.ros.org/browse/
asl/ros_best_practices/tree/master/ros_packag
e_template

Péter Fankhauser | 27.02.2017 | 15


Contact Information

ETH Zurich Lecturers


Robotic Systems Lab Péter Fankhauser ([email protected])
Prof. Dr. Marco Hutter Dominic Jud
LEE J 225 Martin Wermelinger
Leonhardstrasse 21
8092 Zurich Course website:
Switzerland http://www.rsl.ethz.ch/education-
students/lectures/ros.html
http://www.rsl.ethz.ch

Péter Fankhauser | 27.02.2017 | 16

You might also like