YARP  2.3.68+283-20170530.4+git10f9853
Yet Another Robot Platform
Use YARP to talk to ROS services

Please see "example/ros" in the YARP source code for full examples.

For simple cases, we can just use YARP Bottles whose content matches ROS types structurally. For example, to call a ROS service that adds two integers, we could do this:

/*
* Copyright (C) 2013 iCub Facility
* Authors: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <stdio.h>
#include <stdlib.h>
#include <yarp/os/all.h>
using namespace yarp::os;
int main(int argc, char *argv[]) {
if (argc!=3) {
fprintf(stderr,"Call as %s X Y\n", argv[0]);
return 1;
}
Network yarp;
Node node("/yarp_add_int_client");
RpcClient client;
if (!client.open("add_two_ints")) {
fprintf(stderr,"Failed to open client\n");
return 1;
}
Bottle msg, reply;
msg.addInt(atoi(argv[1]));
msg.addInt(atoi(argv[2]));
if (!client.write(msg,reply)) {
fprintf(stderr,"Failed to call service\n");
return 1;
}
printf("Got %d\n", reply.get(0).asInt());
return 0;
}

An example CMakeLists.txt file to compile this and link with YARP would be:

cmake_minimum_required(VERSION 2.8.9)
find_package(YARP REQUIRED)
include_directories(${YARP_INCLUDE_DIRS})
add_executable(add_int_client_v1 add_int_client_v1.cpp)
target_link_libraries(add_int_client_v1 ${YARP_LIBRARIES})

On the ROS side we'd do:

rosrun rospy_tutorials add_two_ints_server

Then on the YARP side we can try it out (assume the above program is compiled as 'add_int_client_v1'):

$ ./add_int_client_v1 4 6
yarp: Port /yarp_add_int_client active at tcp://192.168.1.2:35731
yarp: Port /add_two_ints+1@/yarp_add_int_client active at tcp://192.168.1.2:35004
Got 10