YARP  2.3.70.1
Yet Another Robot Platform
Some examples

There are many code examples available in the "example/" subdirectory of YARP.

If you're reading this online, click on the examples link to see many of them.

Here we work through an example of using YARP communication between programs. We will have two programs, a sender and receiver, simple_sender.cpp and simple_receiver.cpp

Here's simple_sender.cpp:

/*
* Copyright: (C) 2010 RobotCub Consortium
* Author: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <yarp/os/Port.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/Time.h>
#include <stdio.h>
using namespace yarp::os;
int main() {
Network yarp;
Port output;
output.open("/sender");
int top = 100;
for (int i=1; i<=top; i++) {
// prepare a message
Bottle bot;
bot.addString("testing");
bot.addInt(i);
bot.addString("of");
bot.addInt(top);
// send the message
output.write(bot);
printf("Sent message: %s\n", bot.toString().c_str());
// wait a while
}
output.close();
return 0;
}

Here's simple_receiver.cpp:

/*
* Copyright: (C) 2010 RobotCub Consortium
* Author: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <yarp/os/Port.h>
#include <yarp/os/Bottle.h>
#include <stdio.h>
using namespace yarp::os;
int main() {
Network yarp;
Bottle bot;
Port input;
input.open("/receiver");
// usually, we create connections externally, but just for this example...
Network::connect("/sender","/receiver");
input.read(bot);
printf("Got message: %s\n", bot.toString().c_str());
input.close();
return 0;
}

Here's an example CMakeLists.txt file to help with compiling if you use CMake (of course you can compile any way you like, you don't have to use CMake). Make sure you set the YARP_DIR environment variable to point to where you compiled YARP (or give the path through the CMake GUI).

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

After compiling, on three separate consoles, do:

 yarp server               # this starts the YARP name server
 ./simple_sender           # this runs the sender
 ./simple_receiver         # this runs the receiver

Or in windows that would be:

 yarp.exe server           # this starts the YARP name server
 simple_sender.exe         # this runs the sender
 simple_receiver.exe       # this runs the receiver

You'll need to be in the right directories to run the executables, or have them in your path.

Here's what you should see on the terminal for the sender:

Sent message: testing 1 of 100
Sent message: testing 2 of 100
Sent message: testing 3 of 100
Sent message: testing 4 of 100
...

Here's what you should see on the terminal for the receiver:

yarp: Receiving input from /sender to /receiver using tcp
Got message: testing 7 of 100
yarp: Removing input from /sender to /receiver

You can run the receiver many times to connect and reconnect.

Buffered port example

/*
* Copyright: (C) 2010 RobotCub Consortium
* Author: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#include <yarp/os/Bottle.h>
#include <yarp/os/Time.h>
#include <stdio.h>
using namespace yarp::os;
int main() {
// Initialize YARP - some OSes need network and time service initialization
Network yarp;
// Work locally - don't rely on name server (just for this example).
// If you have a YARP name server running, you can remove this line.
// Create two ports that we'll be using to transmit "Bottle" objects.
// The ports are buffered, so that sending and receiving can happen
// in the background.
// we will want to read every message, with no skipping of "old" messages
// when new ones come in
in.setStrict();
// Name the ports
in.open("/in");
out.open("/out");
// Connect the ports so that anything written from /out arrives to /in
Network::connect("/out","/in");
// Send one "Bottle" object. The port is responsible for creating
// and reusing/destroying that object, since it needs to be sure
// it exists until communication to all recipients (just one in
// this case) is complete.
Bottle& outBot1 = out.prepare(); // Get the object
outBot1.fromString("hello world"); // Set it up the way we want
printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
out.write(); // Now send it on its way
// Send another "Bottle" object
Bottle& outBot2 = out.prepare();
outBot2.fromString("2 3 5 7 11");
printf("Writing bottle 2 (%s)\n", outBot2.toString().c_str());
out.writeStrict(); // writeStrict() will wait for any
// previous communication to finish;
// write() would skip sending if
// there was something being sent
// Read the first object
Bottle *inBot1 = in.read();
printf("Bottle 1 is: %s\n", inBot1->toString().c_str());
// Read the second object
Bottle *inBot2 = in.read();
printf("Bottle 2 is: %s\n", inBot2->toString().c_str());
return 0;
}