Introduction to msg and srv
msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.
srv: an srv file describes a service. It is composed of two parts: a request and a response.
msg files are stored in the msg directory of a package, and srv files are stored in the srv directory.
msgs are just simple text files with a field type and field name per line. The field types you can use are:
There is also a special type in ROS:Header, the header contains a timestamp and coordinate frame information that are commonly used in ROS. You will commonly see the first line in a msg file haveHeader header.
Here is an example of a msg that uses a Header, a string primitive, and two other msgs :
Header header
string child_frame_id
geometry_msgs/PoseWithCovariance pose
geometry_msgs/TwistWithCovariance twist
srv files are just like msg files, except they contain two parts: a request and a response. The two parts are separated by a '---' line. Here is an example of a srv file:
int64 A
int64 B
---
int64 Sum
In the above example, A andB are the request, andSum is the response.
Let's define a new msg in the package that was created in the previous tutorial.
$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg
There's one more step, though. We need to make sure that the msg files are turned into source code for C++, Python, and other languages:
Open CMakeLists.txt in your favorite text editor. Remove# to uncomment the following line:
#rosbuild_genmsg()
That's all you need to do to create a msg. Let's make sure that ROS can see it using therosmsg show command.
Usage:
$ rosmsg show [message type]
Example:
$ rosmsg show beginner_tutorials/Num
You will see:
int64 num
In the previous example, the message type consists of two parts:
beginner_tutorials -- the package where the message is defined
Num -- The name of the msgNum.
If you can't remember which Package a msg is in, you can leave out the package name. Try:
$ rosmsg show Num
You will see:
[beginner_tutorials/Num]:
int64 num
Let's use the package we just created to create a srv:
$ roscd beginner_tutorials
$ mkdir srv
Instead of creating a new srv definition by hand, we will copy an existing one from another package.
roscp is a useful commandline tool for copying files from one package to another.
Usage:
$ roscp [package_name] [file_to_copy_path] [copy_path]
Now we can copy a service from the rospy_tutorials package:
$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
There's one more step, though. We need to make sure that the srv files are turned into source code for C++, Python, and other languages:
Open CMakeLists.txt in your favorite text editor. Remove# to uncomment the following line:
#rosbuild_gensrv()
That's all you need to do to create a srv. Let's make sure that ROS can see it using therossrv show command.
Usage:
$ rossrv show <service type>
Example:
$ rossrv show beginner_tutorials/AddTwoInts
You will see:
int64 a
int64 b
---
int64 sum
Now that we have made some new messages we need to make our package again
$ rosmake beginner_tutorials
We've seen quite a few ROS tools already. It can be difficult to keep track of what arguments each command requires. Luckily, most ROS tools provide their own help.
Try:
$ rosmsg -h
You should see a list of differentrosmsg subcommands.
Commands:
rosmsg show Show message description
rosmsg users Find files that use message
rosmsg md5 Display message md5sum
rosmsg package List messages in a package
rosmsg packages List packages that contain messages
You can also get help for subcommands
$ rosmsg show -h
This shows the arguments that are needed for rosmsg show:
Usage: rosmsg show [options] <message type>
Options:
-h, --help show this help message and exit
-r, --raw show raw message text, including comments