创建一个ROS msg 和 srv
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 the msg directory of a package, and srv files are stored in the *srv directory. *
msg是一个每行是一个域类型和域名的简单文本文件,可用的域类型有:
- int8, int16, int32, int64 (plus uint*)
- loat32, float64
- string
- time, duration
- other msg files
- variable-length array[] and fixed-length array[C]
在ROS中有一个特殊的类型:Header,它包含一个在ROS中经常用到的时间戳记timestamp 和协调框架信息 coordinate frame information 。
使用msg
1.在一个包中创建一个新的msg,使用如下代码:
$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg```
这样msg文件中只有一行内容,为 **int64 num**。
2.修改**package.xml**中的内容,取消以下两行代码的注释:
```xml
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>```
***注:we need "message_generation", while at runtime, we only need "message_runtime". ***
3.打开**CMakeLists.txt**文件,将以下代码的注释取消:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)```
注:Despite its name, message_generation works for both msg and srv.
catkin_package(
...
CATKIN_DEPENDS message_runtime ...
...
)```
add_message_files(
FILES
Num.msg
)```
generate_messages(
DEPENDENCIES
std_msgs
)```
上述代码中的*Num.msg*是我们所创建的msg的文件名。
4.显示**msg**
$ rosmsg show beginner_tutorials/Num```
或者是
$ rosmsg show Num```
结果会显示出**msg**中的内容:**int64 num**。
********
####使用srv
1.创建一个**srv**文件:
我们用**roscp**命令,从一个包中复制文件到另一个包中:
$ roscp [package_name] [file_to_copy_path] [copy_path]```
2.打开CMakeLists.txt文件,将以下代码的注释取消:
add_service_files(
FILES
AddTwoInts.srv
)```
上述代码中的*AddTwoInts.srv*是我们所创建的srv文件名。
3.显示**srv**
$ rossrv show beginner_tutorials/AddTwoInts```
或者是
$ rossrv show AddTwoInts```
结果会显示出**srv**中的内容:
int64 a
int64 b
int64 sum```
注意:
*If you are building C++ nodes which use your new messages, you will also need to declare a dependency between your node and your message, as described in the catkin msg/srv build documentation. *