創建一個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. *