I. MSG and SRV INTRODUCTION
The Msg:msg file declares the individual fields of a ROS message using a simple text format. Just create an msg file that you can use to generate the message definition code for different languages.
The Srv:srv file declares a service, including request and response (response).
msg files and SRV files are stored in the packet's MSG directory and the SRV directory, respectively.
1. msg File
Msgs is a simple TXT file, each line defines an information field, with the format of each line:
信息域的名称 信息域的类型
There are several types of information fields:
int8int16int32int64uintfloat32float64string
Here is an example, this example uses a header type, an original string type, and two other predefined other types:
Headerheaderstring child_frame_idgeometry_msgs/PoseWithCovariance posegeometry_msgs/TwistWithCovariance twist
2. srv file
An SRV file is similar to an MSG file, except that it contains two parts--request and response. Two parts ---
separated by:
int64 Aint64 B---int64 Sum
In the above example, A and B are request,sum are response.
Second, use MSG1, create a msg
Let's use the msg file in the previously created package to beginner_tutorials
define a msg, and if this package has been removed, please refer to the first article in this series to recreate the package.
First enter the package directory:
roscd beginner_tutorials
Create a msg directory:
mkdir msg
Then create the file using the following method Num.msg
(you can also create it yourself and save it with the editor):
echo"int64 num" > msg/Num.msg
The file created above contains only one row, and of course you can add more information fields to each row, for example:
string first_namestring last_nameuint8 ageuint32 score
Once created, we need to make sure that the msg file can be converted to code in C++/python or other languages.
Open the package.xml
file and make sure there are two lines in it and remove their comments:
<build_depend>message_generation</build_depend><run_depend>message_runtime</run_depend>
It needs to be pointed out that we need it during the build phase "message_generation"
, and we need it at runtime "message_runtime"
.
Then open the file under the package directory CMakeLists.txt
and find_message
add dependencies in the call message_generation
so that you can generate ROS information.
As shown below, add an entry in parentheses message_generation
:
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
You may notice that sometimes when you build a project, even if you do not CMakeLists.txt
call find_package
it in, the build is still normal. This is because catkin
all of your projects are integrated, and as long as your previous project is called find_package
, the current project will be configured for the same call. But this is a hidden problem, and when you build a project on your own, the lack of a find_package
call can cause the build to fail.
Then similar to the above, make sure you join the message_runtime
dependency:
Then find the following code snippet:
Remove the comment and replace it with:
add_message_files( FILES Num.msg)
Then, make sure that the generate_messages()
function is called:
Remove the comment and modify it to:
generate_messages( DEPENDENCIES std_msgs)
Now that all the files are ready, sum up, there are probably a few steps:
- Use the MSG file syntax to define a MSG in the MSG directory
- Modify
CmakeLists.txt
, in the find_package
call, add the message_generation
dependent
- Modify
CmakeLists.txt
, catkin_message
add dependencies under message_runtime
- Modify
CmakeLists.txt
, remove add_message_files
comments, add our own definition of msg file
- Modify
CmakeLists.txt
, remove generate_messages()
a comment
Third, the use of rosmsg
We can use the rosmsg
command to view the details of our defined MSG to ensure that it can be correctly identified by Ros:
show beginner_tutorials/Num
Will get:
int64 num
If you forget which packet the MSG is in, you can also omit the package name:
show Num
Get:
[beginner_tutorials/Num]:int64 num
Iv. using SRV1, creating an SRV
Also define the SRV file first:
roscd beginner_tutorialsmkdir srv
To avoid the hassle of manual creation, we can use roscp
commands to copy a file from another package srv
, using:
roscp[package_name][file_to_copy_path][copy_path]
Here, we rospy_tutorials
copy a srv
file from:
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
Similar to the above, to ensure that the SRV file can be converted to code, we need to package.xml
add content to:
<build_depend>message_generation</build_depend> <run_depend>message_runtime</run_depend>
The previous step in creating the MSG has already been done, so we can omit this step here.
The same, adding message_generation
dependencies, we've done too:
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
The next step is to add our SRV file to the project:
Find this section:
# add_service_files(# FILES# Service1.srv# Service2.srv# )
Remove the comment and modify it to:
add_service_files( FILES AddTwoInts.srv)
It can be found that the process of creating SRV is basically consistent with MSG.
2. Using Rossrv
We can use the rossrv
command to view information about the services we create to ensure that it is correctly identified by Ros:
show beginner_tutorials/AddTwoInts
Get:
int64 aint64 b---int64 sum
Similarly rosmsg
, you can omit the package name:
$ rossrv show AddTwoInts[beginner_tutorials/AddTwoInts]:int64 aint64 b---int64 sum[rospy_tutorials/AddTwoInts]:int64 aint64 b---int64 sum
V. Basic process of using MSG and SRV 1, msg dependency
We have done this in the previous step to modify CMakeLists.txt
:
# generate_messages(# DEPENDENCIES# # std_msgs # Or other packages containing msgs# )
Remove the comments and add any packages that we depend on:
generate_messages( DEPENDENCIES std_msgs)
Here to explain the dependencies, as described earlier, when we write msg files, we can not only use the original int, string and other types, and sometimes use other packages of predefined information types, such as in the previous example geometry_msgs/PoseWithCovariance pose
, when we need to use the other package to define the information , we have to declare our dependence on other packages, such as in the example above, to declare a geometry_msgs
dependency.
In the previous example, only this line is in our msg file:
int64 num
Only the native int type is used, so we just need to declare the std_msgs
dependency.
2. Start building
All MSG and SRV files have been defined, and the CmakeLists.txt file has been modified so that we can build our package now beginner_tutorial
.
Now that we are in the beginner_tutorial
package directory, execute the following command into the top-level directory of the workspace:
cd ../..
Perform:
install
Now, all the. msg files in the MSG directory will generate the source code for the Ros-supported language, the C + + header file is in ~/catkin_ws/devel/include/beginner_tutorials/
, Python is basically in ~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg
, and the Lisp file is in ~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/
.
Similar to the location of MSG, the SRV header file for C + + is located in the same directory as the MSG header file, while Python and Lisp exist outside the MSG directory where an SRV directory holds the code generated by the SRV file.
Vi. Getting Help
We've learned a lot of ROS commands, and it's hard to remember the parameters and usage of each command, and we can use help to see the specifics.
For example:
-h
Or:
show -h
Seven, review
7. Creating Ros MSG and SRV