roscpp overview: Initialization and Shutdown | Basics | Advanced: Traits [ROS C Turtle] | Advanced: Custom Allocators [ROS C Turtle] | Advanced: Serialization and Adapting Types [ROS C Turtle] | Publishers and Subscribers | Services | Parameter Server | Timers (Periodic Callbacks) | NodeHandles | Callbacks and Spinning | Logging | Names and Node Information | Time | Exceptions | Compilation Options | Advanced: Internals | tf/Overview | tf/Tutorials | C++ Style Guide

Message Generation

Like all ROS Client Libraries, roscpp takes msg files and generates C++ source code for them. The pattern for this is:

  • package_name/msg/Foo.msgpackage_name::Foo

Similarly, srv files have C++ source code generated. The pattern for this is:

  • package_name/srv/Foo.srvpackage_name::Foo

Note that a limitation of roscpp is that the message and service namespaces overlap.

The source for the generated files are put in the msg_gen/cpp/include/package_name/ and srv_gen/cpp/include/package_name/ directories, for messages and services respectively. The header files are generated with the same name as the filename of the msg/srv.

Thus, including the std_msgs/String message in your code involves:

   1 #include <std_msgs/String.h>

and instantiating the message:

   1 std_msgs::String str;

For details on what C++ code results from different message primitives, please see the msg page.

Note that all fields in a message are currently initialized to a default value of 0 (or empty for strings and variable-length arrays).

Exposing Messages to Other Packages (not necessary in ROS 1.1+)

In ROS 1.1+ this is taken care of automatically, no changes to the manifest are necessary.

In order to expose messages for use by other packages, you must include an export tag in your package's manifest. For example:

    <cpp cflags="-I${prefix}/msg/cpp"/>

If you want to also expose services:

    <cpp cflags="-I${prefix}/msg/cpp -I${prefix}/srv/cpp"/>

Forward-declaring Messages [ROS 1.1+]

There is a macro that lets you easily forward-declare a message and its shared_ptr types:


This will expand to:

   1 template<class ContainerAllocator> struct MyMsg_;
   2 typedef MyMessage_<std::allocator<void> > MyMsg;
   3 typedef boost::shared_ptr<MyMsg> MyMsgPtr;
   4 typedef boost::shared_ptr<MyMsg const> MyMsgConstPtr;

Retrieving Type Information for Fields in Templates

The C++ message generator provides some useful typedefs because C++ does not offer a typeof operator. For every field in a message, it generates a _fieldname_type typedef. This allows template code some useful introspection on messages.

For example, the std_msgs/String message has the following typedef inside it:

   1 typedef std::string _data_type;

which corresponds to the field

   1 std::string data;


An example of where this is useful is for array types. If you want to iterate over an array, you don't need to know the full type of the array:

   1 MyMessage::_my_array_type::iterator it = msg.my_array.begin();
   2 ...

Wiki: roscpp/Overview/Messages (last edited 2011-01-22 04:48:42 by TuckerHermans)