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:
yarp::os::Node
: a class to create a ROS node. yarp::os::Subscriber
: a class to subscribe to a topic. yarp::os::Publisher
: a class to publish to a topic.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:
This will produce the file yarp/rosmsg/String.h
which can be used directly in our YARP program.
Type the following code in a file, e.g. listener.cpp:
An example CMakeLists.txt file to compile this code would be:
You can now publish the string "Hello World" to the topic /chatter using ROS:
and listen to it using the YARP program above:
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 existence of a node called /yarp/listener by running:
/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:
an example CMakeLists.txt
file to compile this code would be:
We can run this code on a terminal:
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 existence of a node called /yarp/talker
and topic /chatter
by running:
/rosout /yarp/talker
/chatter /rosout /rosout_agg
And see the output using the listener from ros tutorials:
[ 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: