Mastering ROS for Robotics Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

ROS messages

The ROS nodes can write or read data that has a different type. The types of data are described using a simplified message description language, also called ROS messages. These datatype descriptions can be used to generate source code for the appropriate message type in different target languages.

The data type description of ROS messages is stored in .msg files in the msg subdirectory of a ROS package. Even though the ROS framework provides a large set of robotic-specific messages already implemented, developers can define their own message type inside their nodes.

The message definition can consist of two types: fields and constants. The field is split into field types and field names. The field type is the data type of the transmitting message and field name is the name of it. The constants define a constant value in the message file.

Here is an example of message definitions:

int32 number 
string name 
float32 speed 

Here, the first part is the field type and the second is the field name. The field type is the data type and the field name can be used to access the value from the message. For example, we can use msg.number for accessing the value of the number from the message.

Here is a table showing some of the built-in field types that we can use in our message:

Other kinds of messages are designed to cover a specific application necessity, such as exchanging common geometrical (geometry_msgs) or sensor (sensor_msgs) information. A special type of ROS message is called a message header. Headers can carry information, such as time, frame of reference or frame_id, and sequence number. Using headers, we will get numbered messages and more clarity in who is sending the current message. The header information is mainly used to send data such as robot joint transforms (TF). Here is an example of the message header:

uint32 seq 
time stamp 
string frame_id 

The rosmsg command tool can be used to inspect the message header and the field types. The following command helps to view the message header of a particular message:

$ rosmsg show std_msgs/Header  

This will give you an output like the preceding example message header. We will look at the rosmsg command and how to work with custom message definitions further in the upcoming sections.