ROS Tutorials (3)

創(chuàng)建一個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中經(jīng)常用到的時間戳記timestamp 和協(xié)調(diào)框架信息 coordinate frame information 。

使用msg

1.在一個包中創(chuàng)建一個新的msg,使用如下代碼:

$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg```
這樣msg文件中只有一行內(nèi)容,為 **int64 num**。
2.修改**package.xml**中的內(nèi)容,取消以下兩行代碼的注釋:
```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*是我們所創(chuàng)建的msg的文件名。
4.顯示**msg**

$ rosmsg show beginner_tutorials/Num```
或者是

$ rosmsg show Num```
結(jié)果會顯示出**msg**中的內(nèi)容:**int64 num**。
********
####使用srv
1.創(chuàng)建一個**srv**文件:
我們用**roscp**命令,從一個包中復(fù)制文件到另一個包中:

$ roscp [package_name] [file_to_copy_path] [copy_path]```
2.打開CMakeLists.txt文件,將以下代碼的注釋取消:

add_service_files( 
FILES 
AddTwoInts.srv
)```
上述代碼中的*AddTwoInts.srv*是我們所創(chuàng)建的srv文件名。
3.顯示**srv**

$ rossrv show beginner_tutorials/AddTwoInts```
或者是

$ rossrv show AddTwoInts```
結(jié)果會顯示出**srv**中的內(nèi)容:

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. *

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容