PROGRAMMING FOR ROBOTICS
ROS
Contents
• ROS services
• ROS action
• ROS time
• ROS bags
Robot Operating System
ROS Services
• Request/response communication between nodes is
realized with services
• The service server advertises the service
• The service client accesses this service
• Similar in structure to messages, services are defined
in *.srv files
• List available services with:
> rosservice list
• Show type of services
> rosservice type /service_name
• Call service with the request contents
> rosservice call /service_name args
http://wiki.ros.org/Services
Robot Operating System
ROS Services (Example)
Robot Operating System
ROS Services (Example)
Start a roscore with
> roscore
Run a service demo node with
> rosrun roscpp_tutorials add_two_ints_server
Robot Operating System
ROS Services (Example)
See a availale services with
> rosservice list
See the type of services with
> rosservice type /add_two_ints
Show the services definition with
> Rossrv show roscpp_tutorials/TwoInts
Call the services
> rosservice call /add_two_ints “a:10 b:5”
Robot Operating System
ROS Services (Example)
Create a service server with #include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
bool add(roscpp_tutorials::TwoInts::Request
> ros::ServiceServer service = &request,
nh.advertiseService(service_name, callback_function) roscpp_tutorials::TwoInts::Response &response)
{
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, callback ROS_INFO(" sending back response: [%ld]",
function is called with the request as argument (long int)response.sum);
• Fill in the response to the response argument return true;
}
• Return to function with true to indicate that it
int main(int argc, char **argv)
has been executed properly {
ros::init(argc, argv, "add_two_ints_server");
ros::NodeHandle nh;
ros::ServiceServer service =
nh.advertiseService("add_two_ints", add);
ros::spin();
return 0;
}
Robot Operating System
ROS Services (Example)
Create a service client with #include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
> ros::ServiceServer client = nh. #include <cstdlib>
serviceClient<service_type>(service_name) 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;}
• Create service request contents ros::NodeHandle nh;
service.request ros::ServiceClient client =
nh.serviceClient<roscpp_tutorials::TwoInts>("add_two_ints");
roscpp_tutorials::TwoInts service;
• Call service with service.request.a = atoi(argv[1]);
service.request.b = atoi(argv[2]);
client.call(service) if (client.call(service)) {
ROS_INFO("Sum: %ld", (long int)service.response.sum);
} else {
• Response is stored in service.response ROS_ERROR("Failed to call service add_two_ints");
return 1;}
return 0;}
Robot Operating System
ROS Actions
• Similar to service calls, but provide possibility to
• Cancel the task (preempt)
• Receive feedback on the progress
• Best way to implement interfaces to time
extended, goal-oriented behaviors
• Similar in structure to services, action are
defined in *.action files
• Internally, actions are implemented with a set of
topics
Robot Operating System
ROS Actions
Robot Operating System
ROS Parameters, Dynamic Reconfigure, Topics, Services, and
Actions Comparison
Robot Operating System
ROS Time
• Normally, ROS uses the PC’s system clock • To take advantage of the simulated time, you should
as time source (wall time) always use the ROS Time APIs:
• For simulations or playback of logged data, it is ros::Time
convenient to work with a simulated time (pause, slow-
ros::Time begin = ros::Time::now();
down etc.)
double secs = begin.toSec();
• To work with a simulated clock:
• Set the /use_sim_time parameter
ros::Duration
> rosparam use /use_sim_time
ros::Duration duration(0.5);
• Publish the time on the topic /clock from
• Gazebo (enabled by default) ros::Rate
• ROS bag (use option --clock)
ros::Rate rate(10);
Robot Operating System
ROS Bag
• A bag is a format for storing message data Show information about a bag
• Binary format with file extension *.bag
• Suited for logging and recording datasets for later > rosbag info name_bag.bag
visualization and analysis
Read a bag and publish its content
Record all topics in a bag
> rosbag record –all
> Rosbag play name_bag.bag
Record given topics
> rosbag record topic_1 topic_2 topic_3
Stop recording with Ctrl + C
Bags are saved with start date and time as file name in the
current folder (e.g. 2017-02-07-01-27-13.bag)
Robot Operating System
Further References
• Ros Wiki • ROS Cheat Sheet
• http://wiki.ros.org/ • https://github.com/ros/cheatsheet/r
• Installation eleases/dow
• http://wiki.ros.org/ROS/Installa nload/0.0.1/ROScheatsheet_catkin.
tion pdf
• Tutorials • ROS Best Practices
• http://wiki.ros.org/ROS/Tutoria • https://github.com/ethzasl/ros_best
ls _practices/wiki
• Available packages • ROS Package Template
• http://www.ros.org/browse/ • https://github.com/ethzasl/ros_best
_practices/tree/master/ros_packag
e_template
Robot Operating System