Establish a Ros msg and SRV

Source: Internet
Author: User

MSG is a simple text file that describes the ROS message fields, which are often used to generate source code for messages in different languages.

An SRV file describes a service that consists of two parts, request and response.

The msg file is stored in a packet's MSG directory, and the SRV file is stored in the SRV directory. MSG is a simple text file in which each line consists of the type of a field and the name of the field .

The types of fields you can use are:

UINT*) float32, float64stringtime, durationother msg filesvariablefixed -length Array[c]

There is also a special type headerin Ros that includes a timestamp and a coordinate framework information that is often used in Ros. You will often see the first line of code in the msg file is:

Header Header

Here is an example of a msg using the header:

  Header Header  string  child_frame_id  geometry_msgs/posewithcovariance Pose  geometry_msgs/twistwithcovariance Twist

The SRV file and the msg file are the same except that they consist of two parts: request and response, which are separated by '---'. The following is an example of an SRV file:

Int64 Aint64 B---int64 Sum

In the example above, A and B are the requests, and sum is the response.

Next we define a new message in the package defined earlier

$ cd ~/catkin_ws/src/"Int64 num" > Msg/num.msg

Of course we can add more information to this message file:

string first_name string last_nameuint8 Ageuint32 Score

Then make sure that our message files are converted to C + + source code. What needs to be done is:

(1) Open the package.xml and make sure that the two lines of the facet are in it:

<build_depend>message_generation</build_depend> <run_depend>message_runtime</run_depend >

At the time of construction, we need "message_generation" at runtime we Need "Message_runtime".

(2) Open CMakeLists.txt, you can use the rosed command we learned earlier:

rosed beginner_tutorials CMakeLists.txt

Adding message_generation is dependent on the find_package so that the message can be generated.

 This to your CMakeLists.txt, modify the existing text to add message_generation before the closing parenthesisfind_package (Catkin REQUIRED Components   roscpp   rospy   std_msgs   message_generation)

Add the Catkin_depends message_runtime as follows:

Catkin_package (...  Catkin_depends message_runtime  ... ...)
Locate the following code slice:
# add_message_files (#   files#   message1.msg#   message2.msg#)

Change it to:

Add_message_files (  files  num.msg)

By adding this. msg file manually, we can let cmake know when we need to reconfigure our project.

Next we have to make sure that the generate_messages () function is called and we want to open the following code:

Generate_messages (  DEPENDENCIES  std_msgs)
We will then be able to generate the source file from our message definition.

Do not worry about the production of source files, first learn some other commands, we use the command line tool rosmsg show to view the details of the message:

$ rosmsg Show [message type]

As an example:

$ rosmsg Show Beginner_tutorials/num

return Result:

Int64 num

Here we see that the type of a message consists of two parts:

Beginner_tutorials--The name of the package whose message is defined

Num--The name of the message num

Of course, if you forget the message in which package, you can also use the name of the package:

$ rosmsg Show Num

It will show you the type and details of your package.

[beginner_tutorials/Num]:int64 Num

After reading the news, let's look at the relevant content of the service:

To create an SRV directory:

$ roscd beginner_tutorials$ mkdir SRV
We copy an SRV definition from other projects here. ROSCP This command-line tool can help us copy files from one project to another.
$ ROSCP [package_name] [File_to_copy_path] [Copy_path]

Next we copy a service from the Rospy_tutorials package.

$ ROSCP rospy_tutorials addtwoints.srv srv/addtwoints.srv

ROSCP will search for addtwoints.srv files in this package rospy_tutorials. Then copy to the specified path.

Like the previous message, the service file also needs to ensure that it can be converted to C + + or other language source code.

(1) Open the package.xml and make sure that the two lines of the facet are in it:

<build_depend>message_generation</build_depend>  <run_depend>message_runtime</run_ Depend>

(2) Open CMakeLists.txt file:

Add message_generation dependency to find_package, in fact, we have already configured before, regardless of message_generation the name of this thing, it is to MSG and SRV play a role.

Add_service_files () modified to:

Add_service_files (  files  addtwoints.srv)

This allows us to generate source files from the services we define.

Let's take a look at some of the service's command-line tools, such as the Rossrv Show command line:

$ rossrv Show <service type>

For example:

$ rossrv Show Beginner_tutorials/addtwoints

Get results:

Int64 Aint64 b---int64 sum

As with Rosmsg show, if you forget the name of the package, you can show the name of the service directly:

$ rossrv Show addtwoints[beginner_tutorials/addtwoints]:int64 aint64 b---int64 sum[rospy_ Tutorials/addtwoints]:int64 aint64 b---int64 sum

The type of SRV and detailed information are given automatically.

The configuration of all the above files is set up, so let's look at how to make the above file into a Ros-supported language code and open CMakeLists.txt:

First, find the following code in the CMakeLists.txt file:

# generate_messages (#   dependencies# #  std_msgs  # Or Other packages containing msgs#)

Uncomment this code, and if you use some. msg files, add the packets that contain the files as dependencies.

Generate_messages (  DEPENDENCIES  std_msgs)

Now that we have added some new information, we need to recompile these packages:

# in your Catkin workspace$ CD . /.. $ catkin_make$ cd–        // back to the previous directory

All. msg files in the MSG directory will produce source files for the languages supported by Ros.

The header file for C + + information is generated in:


The python script is generated in:


Lisp files are generated in:


So far, we have done some of the tasks of the message and the service, and finally said something about the commands, which are helpful hints.

For example:

$ rosmsg-h


Rosmsg show-h

Establish a Ros msg and SRV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.