YARP
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.

Simple port example

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

simple_sender

Here's simple_sender/simple_sender.cpp:

11 #include <yarp/os/Bottle.h>
12 #include <yarp/os/Network.h>
13 #include <yarp/os/Port.h>
14 #include <yarp/os/Time.h>
15 
16 #include <cstdio>
17 
18 using yarp::os::Bottle;
19 using yarp::os::Network;
20 using yarp::os::Port;
21 
22 constexpr double loop_delay = 1.0;
23 constexpr size_t top = 100;
24 
25 int main(int argc, char* argv[])
26 {
27  YARP_UNUSED(argc);
28  YARP_UNUSED(argv);
29 
30  Network yarp;
31  Port output;
32  output.open("/sender");
33  for (size_t i = 1; i <= top; i++) {
34  // prepare a message
35  Bottle bot;
36  bot.addString("testing");
37  bot.addInt32(i);
38  bot.addString("of");
39  bot.addInt32(top);
40  // send the message
41  output.write(bot);
42  printf("Sent message: %s\n", bot.toString().c_str());
43  // wait a while
44  yarp::os::Time::delay(loop_delay);
45  }
46  output.close();
47  return 0;
48 }

simple_sender/CMakeLists.txt

8 if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
9  cmake_minimum_required(VERSION 3.12)
10  project(simple_sender)
11  find_package(YARP REQUIRED COMPONENTS os)
12 endif()
13 
14 add_executable(simple_sender)
15 target_sources(simple_sender PRIVATE simple_sender.cpp)
16 target_link_libraries(simple_sender PRIVATE YARP::YARP_os
17  YARP::YARP_init)

simple_receiver

Here's simple_receiver/simple_receiver.cpp:

11 #include <yarp/os/Bottle.h>
12 #include <yarp/os/Network.h>
13 #include <yarp/os/Port.h>
14 
15 #include <cstdio>
16 
17 using yarp::os::Bottle;
18 using yarp::os::Network;
19 using yarp::os::Port;
20 
21 int main(int argc, char* argv[])
22 {
23  YARP_UNUSED(argc);
24  YARP_UNUSED(argv);
25 
26  Network yarp;
27  Bottle bot;
28  Port input;
29  input.open("/receiver");
30  // usually, we create connections externally, but just for this example...
31  Network::connect("/sender", "/receiver");
32  input.read(bot);
33  printf("Got message: %s\n", bot.toString().c_str());
34  input.close();
35  return 0;
36 }

simple_receiver/CMakeLists.txt

8 if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
9  cmake_minimum_required(VERSION 3.12)
10  project(simple_receiver)
11  find_package(YARP REQUIRED COMPONENTS os)
12 endif()
13 
14 add_executable(simple_receiver)
15 target_sources(simple_receiver PRIVATE simple_receiver.cpp)
16 target_link_libraries(simple_receiver PRIVATE YARP::YARP_os
17  YARP::YARP_init)

Running the example

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

11 #include <yarp/os/Bottle.h>
12 #include <yarp/os/BufferedPort.h>
13 #include <yarp/os/Network.h>
14 
15 #include <cstdio>
16 
17 using yarp::os::Bottle;
19 using yarp::os::Network;
20 
21 int main(int argc, char* argv[])
22 {
23  YARP_UNUSED(argc);
24  YARP_UNUSED(argv);
25 
26  // Initialize YARP - some OSes need network and time service initialization
27  Network yarp;
28 
29  // Work locally - don't rely on name server (just for this example).
30  // If you have a YARP name server running, you can remove this line.
31  Network::setLocalMode(true);
32 
33  // Create two ports that we'll be using to transmit "Bottle" objects.
34  // The ports are buffered, so that sending and receiving can happen
35  // in the background.
36  BufferedPort<Bottle> in;
37  BufferedPort<Bottle> out;
38 
39  // we will want to read every message, with no skipping of "old" messages
40  // when new ones come in
41  in.setStrict();
42 
43  // Name the ports
44  in.open("/in");
45  out.open("/out");
46 
47  // Connect the ports so that anything written from /out arrives to /in
48  Network::connect("/out", "/in");
49 
50  // Send one "Bottle" object. The port is responsible for creating
51  // and reusing/destroying that object, since it needs to be sure
52  // it exists until communication to all recipients (just one in
53  // this case) is complete.
54  Bottle& outBot1 = out.prepare(); // Get the object
55  outBot1.fromString("hello world"); // Set it up the way we want
56  printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
57  out.write(); // Now send it on its way
58 
59  // Send another "Bottle" object
60  Bottle& outBot2 = out.prepare();
61  outBot2.fromString("2 3 5 7 11");
62  printf("Writing bottle 2 (%s)\n", outBot2.toString().c_str());
63  out.writeStrict(); // writeStrict() will wait for any
64  // previous communication to finish;
65  // write() would skip sending if
66  // there was something being sent
67 
68  // Read the first object
69  Bottle* inBot1 = in.read();
70  printf("Bottle 1 is: %s\n", inBot1->toString().c_str());
71 
72  // Read the second object
73  Bottle* inBot2 = in.read();
74  printf("Bottle 2 is: %s\n", inBot2->toString().c_str());
75 
76  return 0;
77 }
8 if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
9  cmake_minimum_required(VERSION 3.12)
10  project(buffered_port)
11  find_package(YARP REQUIRED COMPONENTS os)
12 endif()
13 
14 add_executable(buffered_port)
15 target_sources(buffered_port PRIVATE buffered_port.cpp)
16 target_link_libraries(buffered_port PRIVATE YARP::YARP_os
17  YARP::YARP_init)
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
Network.h
Port.h
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
BufferedPort.h
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
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
Time.h
yarp::os::Time::delay
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114
Bottle.h