1.关于msg和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 themsg 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 fieldtypes you can use are:
int8, int16, int32, int64 (plusuint*)
float32, float64
string
time, duration
other msg files
variable-length array[] and fixed-lengtharray[C]
Thereis also a special type in ROS:Header, the header contains a timestamp and coordinate frame information that are commonly used in ROS. You will frequently see the first line in a msg file have Header header.
Hereis an example of a msg that uses a Header, a string primitive, and two othermsgs :
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: arequest and a response. The two parts are separated by a '---' line. Here is anexample of a srv file:
int64 A
int64 B
---
int64 Sum
In the above example,A and B are the request, and Sum is the response.
2.使用msg
roscd beginner_tutorials
mkdir msg
echo "int64 num" > msg/Num.msg
接下来修改package.xml文件,在相关位置添加如下内容:
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
接下来修改CMakeLists.txt文件,在如下相应位置添加粗体的内容:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES beginner_tutorials
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
CATKIN_DEPENDS message_runtime
)
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
add_message_files(
FILES
Num.msg
)
# generate_messages(
# DEPENDENCIES
# std_msgs
# )
generate_messages(
DEPENDENCIES
std_msgs
)
文件保存后,执行如下命令可以看到相应的信息:
rosmsg show beginner_tutorials/Num
执行输出:
int64 num
而执行rosmsg show Num命令会输出:
[beginner_tutorials/Num]:
int64 num
3.使用srv
roscd beginner_tutorials
mkdir srv
接下来复制一个现成的srv文件:
roscp rospy_tutorials AddTwoInts.srvsrv/AddTwoInts.srv
从rospy_tutorials包里将 AddTwoInts.srv复制为当前目录下的srv/AddTwoInts.srv文件。
接下来package.xml的修改跟msg里的一样,而CMakeLists.txt的修改如下红色标记:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
add_service_files(
FILES
AddTwoInts.srv
)
文件保存后,使用如下命令可查看到刚创建的srv相关信息:
guochongxin@slam:~/ros/catkin_ws/src/beginner_tutorials$rossrv show beginner_tutorials/AddTwoInts
int64 a
int64 b
---
int64 sum
guochongxin@slam:~/ros/catkin_ws/src/beginner_tutorials$ rossrv showAddTwoInts
[beginner_tutorials/AddTwoInts]:
int64 a
int64 b
---
int64 sum
[rospy_tutorials/AddTwoInts]:
int64 a
int64 b
---
int64 sum
4.关于msg和srv相同的操作步骤
在CMakeLists.txt文件中,都需要如下内容:
generate_messages(
DEPENDENCIES
std_msgs
)
确认后,接下来需要执行如下命令重新编译安装以便生成相应的新内容:
roscd beginner_tutorials
cd ../..
catkin_make install
编译无误并安装更新相应的文件。
5.回顾
rospack = ros+pack(age) : provides information related to ROS packages
roscd = ros+cd :changesdirectoryto a ROS package or stack
rosls= ros+ls :listsfilesin a ROS package
roscp = ros+cp :copiesfiles from/to a ROS package
rosmsg = ros+msg : provides informationrelated to ROS message definitions
rossrv = ros+srv : provides informationrelated to ROS service definitions
catkin_make : makes (compiles) a ROSpackage
rosmake =ros+make : makes (compiles) a ROS package (if you're not using a catkinworkspace)
网友评论