YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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:

8#include <yarp/os/Bottle.h>
9#include <yarp/os/Network.h>
10#include <yarp/os/Port.h>
11#include <yarp/os/Time.h>
12
13#include <cstdio>
14
17using yarp::os::Port;
18
19constexpr double loop_delay = 1.0;
20constexpr size_t top = 100;
21
22int main(int argc, char* argv[])
23{
24 YARP_UNUSED(argc);
25 YARP_UNUSED(argv);
26
27 Network yarp;
28 Port output;
29 output.open("/sender");
30 for (size_t i = 1; i <= top; i++) {
31 // prepare a message
32 Bottle bot;
33 bot.addString("testing");
34 bot.addInt32(i);
35 bot.addString("of");
36 bot.addInt32(top);
37 // send the message
38 output.write(bot);
39 printf("Sent message: %s\n", bot.toString().c_str());
40 // wait a while
41 yarp::os::Time::delay(loop_delay);
42 }
43 output.close();
44 return 0;
45}
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
A mini-server for network communication.
Definition Port.h:46
int main(int argc, char *argv[])
Definition main.cpp:121
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
The main, catch-all namespace for YARP.
Definition dirs.h:16
#define YARP_UNUSED(var)
Definition api.h:162

simple_sender/CMakeLists.txt

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

simple_receiver

Here's simple_receiver/simple_receiver.cpp:

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

simple_receiver/CMakeLists.txt

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

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

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