转自:http://blog.sina.com.cn/s/blog_661159d50100od32.html
参考网址:http://www.ros.org/wiki/ROS/Tutorials/CreatingMsgAndSrv
1,msg和srv介绍:
msg:msg文件是描述ROS 消息的描述文件,是用来为消息生成不同语言下源代码。msg文件存储在该package路径下的msg文件路径。
msg文件包括以上几种类型:
- int8, int16, int32, int64 (plus uint*)
- float32, float64
- string
- time, duration
- other msg files
- variable-length array[] and fixed-length array[C]
srv:srv文件是用来描述服务的,由请求和响反应两部分组成。srv文件存储在srv文件路径。
srv文件就像msg文件,另外还包括请求和响应两部分,由“----”分开,例如:
int 64 A
int 64 B
---
int64 Sum
其中,A,B是请求者,Sum是响应者。
2,创建一个msg
例子:
(1) 我们在已经创建好的beginner_tutorials package上创建msg
$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg
然后,
$ gedit CMakeLists.txt --打开该package的CMakeList.txt,取消“# rosbuild_genmsg()”前的“#”号。
(2) 查看msg文件
用法:
$ rosmsg show [message type]
例子:
$ rosmsg show beginner_tutorials/Num
得到结果:
int64 num
另外,也可以直接:
$ rosmsg show Num
得到结果:
[beginner_tutorials/Num]:
int64 num
注:在这个例子,消息类型包括:
-
beginner_tutorials -- the package where the message is defined
-
Num -- The name of the msg Num.
(3) rosmsg帮助
用法:
$ rosmsg -h
得到结果:
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
Type rosmsg <command> -h for more detailed usage
或者:
$ rosmsg show -h
得到结果:
Usage: rosmsg show [options] <message type>
Options:
-h, --help show this help message and exit
-r, --raw show raw message text, including comments
-b BAGFILE, --bag=BAGFILE
show message from .bag file
3,创建一个srv
(1) 在我们已经创建好的package上创建srv文件
$ roscd beginner_tutorials
$ mkdir srv
$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv --将AddTwoInts.srv复制到srv/AddTwoInts.srv
$ gedit CMakeLists.txt --打开该package下的CMakeLists.txt,将“# rosbuild_gensrv()”前的“#”去掉。
(2) 查看srv文件
rossrv命令
用法:
$ rossrv show <servicetype>
例子:
$ rossrv show beginner_tutorials/AddTwoInts
得到结果:int64 a
int64 b
---
int64 sum
$ rosmake beginner_tutorials --编译更新过的beginner_tutorials package
得到结果:
[ rosmake ] Packages requested are: ['beginner_tutorials']
[ rosmake ] Logging to directory/home/jason/.ros/rosmake/rosmake_output-20110110-102351
[ rosmake ] Expanded args ['beginner_tutorials'] to:
['beginner_tutorials']
[ rosmake ] Checking rosdeps compliance for packages beginner_tutorials. This may take a few seconds.
[ rosmake ] rosdep check passed all system dependencies in packages
[rosmake-0] Starting >>> roslib [ make ]
[rosmake-0] Finished <<< roslib ROS_NOBUILD in package roslib
[rosmake-1] Starting >>> roslang [ make ]
[rosmake-1] Finished <<< roslang ROS_NOBUILD in package roslang
No Makefile in package roslang
[rosmake-1] Starting >>> xmlrpcpp [ make ]
[rosmake-1] Finished <<< xmlrpcpp ROS_NOBUILD in package xmlrpcpp
[rosmake-0] Starting >>> rosconsole [ make ]
[rosmake-0] Finished <<< rosconsole ROS_NOBUILD in package rosconsole
[rosmake-0] Starting >>> std_msgs [ make ]
[rosmake-0] Finished <<< std_msgs ROS_NOBUILD in package std_msgs
[rosmake-0] Starting >>> rospy [ make ]
[rosmake-0] Finished <<< rospy ROS_NOBUILD in package rospy
[rosmake-0] Starting >>> roscpp [ make ]
[rosmake-0] Finished <<< roscpp ROS_NOBUILD in package roscpp
[rosmake-0] Starting >>> beginner_tutorials [ make ]
[rosmake-1] Starting >>> rosout [ make ]
[rosmake-1] Finished <<< rosout ROS_NOBUILD in package rosout
[rosmake-0] Finished <<< beginner_tutorials [PASS] [ 7.58 seconds ]
[ rosmake ] Results:
[ rosmake ] Built 11 packages with 0 failures.
[ rosmake ] Summary output to directory
[ rosmake ] /home/jason/.ros/rosmake/rosmake_output-20110110-102351
(3) rossrv帮助
用法:
$ rossrv -h
得到结果:
Commands:
rossrv show Show message description
rossrv users Find files that use message
rossrv md5 Display message md5sum
rossrv package List messages in a package
rossrv packages List packages that contain messages
Type rossrv <command> -h for more detailed usage
或者:
$ rossrv show -h
得到结果:
Usage: rossrv show [options] <service type>
Options:
-h, --help show this help message and exit
-r, --raw show raw message text, including comments
-b BAGFILE, --bag=BAGFILE
show message from .bag file