YARP
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) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <yarp/os/Bottle.h>
#include <yarp/os/Node.h>
namespace {
YARP_LOG_COMPONENT(CLIENT_V2, "yarp.example.ros.add_int_client_v2")
}
int main(int argc, char* argv[])
{
if (argc != 3) {
yCError(CLIENT_V2, "Call as %s X Y", argv[0]);
return 1;
}
Network yarp;
Node node("/yarp_add_int_client");
RpcClient client;
if (!client.open("add_two_ints")) {
yCError(CLIENT_V2, "Failed to open client");
return 1;
}
Bottle msg;
Bottle reply;
msg.addInt32(atoi(argv[1]));
msg.addInt32(atoi(argv[2]));
if (!client.write(msg, reply)) {
yCError(CLIENT_V2, "Failed to call service");
return 1;
}
yCInfo(CLIENT_V2, "Got %d\n", reply.get(0).asInt32());
return 0;
}

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

cmake_minimum_required(VERSION 3.12)
find_package(YARP COMPONENTS os REQUIRED)
add_executable(add_int_client_v1)
target_sources(add_int_client_v1 PRIVATE add_int_client_v1.cpp)
target_link_libraries(add_int_client_v1 PRIVATE YARP::YARP_os
YARP::YARP_init)

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
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
Network.h
yarp::os::RpcClient
A port that is specialized as an RPC client.
Definition: RpcClient.h:26
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
Node.h
yarp::os::Node
The Node class.
Definition: Node.h:27
LogComponent.h
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
RpcClient.h
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Bottle.h