YARP  2.3.70.1
Yet Another Robot Platform
Writing code to talk to ROS topics

While you can configure conventional YARP ports to talk to ROS topics (see Use ROS with existing YARP programs), YARP provides specialized classes that simplify this task:

An important thing to keep in mind is that YARP needs to be aware of ROS types. If you are writing new code it is easy to generate ROS compatible YARP data types starting.

For example the following code shows how to write a program that listen to the /chatter topic. In this case we use the ROS type std_msgs/String.

The first thing to do is to generate a YARP compatible type for std_msgs/String:

$ yarpidl_rosmsg String

This will produce the file String.h which can be used directly in our YARP program.

Type the following code in a file, e.g. listener.cpp:

/*
* Copyright (C) 2016 iCub Facility
* Authors: Lorenzo Natale
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <iostream>
#include <yarp/os/all.h>
using namespace yarp::os;
using namespace std;
/* Make sure you run yarpidl_rosmsg std_msg/String */
/* to generate String.h */
#include "String.h"
int main(int argc, char *argv[]) {
Network yarp;
/* creates a node called /yarp/listener */
Node node("/yarp/listener");
/* subscribe to topic chatter */
if (!subscriber.topic("/chatter")) {
cerr<< "Failed to subscriber to /chatter\n";
return -1;
}
/* read data from the topic */
while (true) {
String data;
subscriber.read(data);
cout << "Received:" << data.data << " " << endl;
}
return 0;
}

An example CMakeLists.txt file to compile this code would be:

cmake_minimum_required(VERSION 3.0)
find_package(YARP REQUIRED)
add_executable(listener listener.cpp)
target_link_libraries(listener ${YARP_LIBRARIES})

You can now publish the string "Hello World" to the topic /chatter using ROS:

$ rostopic pub /chatter std_msgs/String "hello yarp" --r 10

and listen to it using the YARP program above:

$ ./listener
yarp: Port /yarp/listener active at tcp://192.168.152.130:10002
yarp: Port /chatter-@/yarp/listener active at tcp://192.168.152.130:10003
yarp: Receiving input from /rostopic_36275_1464212662655 to /chatter-@/yarp/listener using tcpros+role.pub+topic./chatter
Received:hello yarp
Received:hello yarp
Received:hello yarp
...

You can verify the existance of a node called /yarp/listener by running:

$ rosnode list
/rosout
/yarp/listener

We can now write our own publisher using YARP. The code is quite simple, type it in a file, e.g. talker.cpp:

/*
* Copyright (C) 2016 iCub Facility
* Authors: Lorenzo Natale
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <iostream>
#include <yarp/os/all.h>
using namespace yarp::os;
using namespace std;
/* Make sure you run yarpidl_rosmsg std_msg/String */
/* to generate String.h */
#include "String.h"
int main(int argc, char *argv[]) {
Network yarp;
/* creates a node called /yarp/talker */
Node node("/yarp/talker");
/* subscribe to topic chatter */
if (!publisher.topic("/chatter")) {
cerr<< "Failed to create publisher to /chatter\n";
return -1;
}
while (true) {
/* prepare some data */
String data;
data.data="Hello from YARP";
/* publish it to the topic */
publisher.write(data);
/* wait some time to avoid flooding with messages */
}
return 0;
}

an example CMakeLists.txt file to compile this code would be:

cmake_minimum_required(VERSION 3.0)
find_package(YARP REQUIRED)
add_executable(talker talker.cpp)
target_link_libraries(talker ${YARP_LIBRARIES})

We can run this code on a terminal:

$ ./talker
yarp: Port /yarp/talker active at tcp://192.168.152.130:10004
yarp: Port /chatter+@/yarp/talker active at tcp://192.168.152.130:10006
yarp: Sending output from /chatter+@/yarp/talker to /listener using tcpros
yarp: Removing output from /chatter+@/yarp/talker to /listener

You can verify the existance of a node called /yarp/talker and topic /chatter by running:

$ rosnode list
/rosout
/yarp/talker

$ rostopic list
/chatter
/rosout
/rosout_agg

And see the output using the listener from ros tutorials:

$ rosrun roscpp_tutorials listener
[ INFO] [1464213813.965287858]: I heard: [Hello from YARP]
[ INFO] [1464213814.066799996]: I heard: [Hello from YARP]
[ INFO] [1464213814.169264100]: I heard: [Hello from YARP]
[ INFO] [1464213814.271588965]: I heard: [Hello from YARP]
[ INFO] [1464213814.373758392]: I heard: [Hello from YARP]
...

The following tutorials show how to write your own data type using ROS syntax: