[ROS-Beginner]10.创建一个ROS msg和srv

1. msg和srv的介绍

  (1)msg: msg文件是简单的文本文件,用来描述ROS message的领域,它用来生成针对不同语言的source代码。存储在package中的msg目录中。每行都是field type 及 field name。

      可以使用的field type有:

  • int8, int16, int32, int64 (plus uint*)
  • float32, float64
  • string
  • time, duration
  • other msg files
  • variable-length array[] and fixed-length array[C]

  另外还有一种奇特的类型:Header,它包含了时间戳timestamp和ROS中常用的coordinate frame。

  msg文件例子:

  Header header
  string child_frame_id
  geometry_msgs/PoseWithCovariance pose
  geometry_msgs/TwistWithCovariance twist

 

  (2)srv: srv文件描述一个service,由两部分组成:request和response。存储在srv目录中。与msg文件不同的是,它包含了request和response两部分,两部分被---划分开。

   srv文件例子:

int64 A
int64 B
---
int64 Sum

 

2. 使用msg

  (1)在我们之前创建的package中定义一个新的msg:

$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg

    例子里的.msg文件只有一行,可以增加元素,一行一个,如:

string first_name
string last_name
uint8 age
uint32 score

  (2)确保.msg被转成了给c++,python或者其他语言的source code:打开package.xml文件,确保有以下两行,且没有被标注掉:

  <build_depend>message_generation</build_depend>
  <exec_depend>message_runtime</exec_depend>

   (3) 打开CMakeLists.txt,增加message_generation dependency,注意不要直接粘贴下面的代码,对照进行修改。

find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation
)

   (4)注意:有时不用find_package关联dependencies也可以build成功,这是因为catkin会把所有的projects结合到一起。但是不要忘记去call find_package,不然独立使用容易出错。

   (5)此外,确保输出 message runtime dependency

catkin_package(
  ...
  CATKIN_DEPENDS message_runtime ...
  ...)

   (6)找到以下一块代码,取消标注。

# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )

      (7)用自己的.msg取代Message*.msg里的stand,如同:

add_message_files(
  FILES
  Num.msg
)

      (8)通过手动添加.msg文件,确保CMake知道,当它需要重新配置这个项目,在你添加了其他的.msg文件之后。

   (9)现在我们确认generate_messages()函数被呼叫,取消以下标注。

# generate_messages(
#   DEPENDENCIES
#   std_msgs
# )

3. 使用rosmsg

用法:

$ rosmsg show [message type]

举例:

$ rosmsg show beginner_tutorials/Num

可以得到:

int64 num

在之前的例子里,message类型有两部分组成:

  • beginner_tutorials -- the package where the message is defined

  • Num -- The name of the msg Num.

如果忘记了msg在哪个package里,可以输入:

$ rosmsg show Num

将会得到:

[beginner_tutorials/Num]:
int64 num

4. 使用srv

4.1 创建srv

使用之前创建的package来创建一个srv:

$ roscd beginner_tutorials
$ mkdir srv

我们用roscp来从别的package里复制一个已有的srv,而不是创建一个新的srv,用法:

$ roscp [package_name] [file_to_copy_path] [copy_path]

比如,从rospy_tutorials package复制一个service

$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

我们还有一步,就是确保srv被转成了C++,Python或者其他语言的source code。

除非之前做过,打开package.xml,确保有以下两行:

  <build_depend>message_generation</build_depend>
  <exec_depend>message_runtime</exec_depend>

和之前一样,在build time, 我们需要message_generation, 但在 runtime, 我们只需要message_runtime。

除非之前做过了,我们要增加message_generation dependency到CMakeLists.txt中生成message

# Do not just add this line to your CMakeLists.txt, modify the existing line
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)

取消下列标注:

# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

为了service文件,替换placeholder Service*.srv

add_service_files(
  FILES
  AddTwoInts.srv
)

 

4.2使用rossrv

用法:

$ rossrv show <service type>

比如:

$ rossrv show beginner_tutorials/AddTwoInts

将看到:

int64 a
int64 b
---
int64 sum

和rosmsg类似,可以发现service文件,而不用确定package名字:

$ rossrv show AddTwoInts
[beginner_tutorials/AddTwoInts]:
int64 a
int64 b
---
int64 sum

[rospy_tutorials/AddTwoInts]:
int64 a
int64 b
---
int64 sum

这里有两个services,第一个是之前beginner_tutorials package,第二个是预先就有的rospy_tutorials package。

5. msg和srv的一般步骤

除非之前做了,改CMakeLists.txt:

# generate_messages(
#   DEPENDENCIES
# #  std_msgs  # Or other packages containing msgs
# )

取消标注,增加包含需要用的.msg的package。

generate_messages(
  DEPENDENCIES
  std_msgs
)

现在我们已经make了一些message,这些是我们需要的来再一次make我们的package:

# In your catkin workspace
$ roscd beginner_tutorials
$ cd ../..
$ catkin_make install
$ cd -

任何在msg中的.msg文件将生成代码来给所有支持的语言使用。C++ 的message头文件生成在~/catkin_ws/devel/include/beginner_tutorials/,lisp文件出现在~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/里。

相似地,任何srv文件夹中的.srv将生成支持语言的代码。对C++,会生成message头文件在同样的文件夹中。

如果在build 使用新message的 C++ Node,需要声明node和message之间的dependency。

6. 获取帮助

我们已经学了很多ROS的tools,很难记住每个命令需要什么语句,但是大部分ROS tools提供了帮助。

比如:

$ rosmsg -h

可以得到:

Commands:
  rosmsg show     Show message description
  rosmsg list     List all messages
  rosmsg md5      Display message md5sum
  rosmsg package  List messages in a package
  rosmsg packages List packages that contain messages

也可以寻求子命令的帮助:

$ 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

7. 复习

  • rospack = ros+pack(age) : provides information related to ROS packages
  • roscd = ros+cd : changes directory to a ROS package or stack

  • rosls = ros+ls : lists files in a ROS package

  • roscp = ros+cp : copies files from/to a ROS package

  • rosmsg = ros+msg : provides information related to ROS message definitions
  • rossrv = ros+srv : provides information related to ROS service definitions
  • catkin_make : makes (compiles) a ROS package
    • rosmake = ros+make : makes (compiles) a ROS package (if you're not using a catkin workspace)

原文链接: https://www.cnblogs.com/jjxforever/p/8016113.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    [ROS-Beginner]10.创建一个ROS msg和srv

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/265122

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午5:00
下一篇 2023年2月14日 下午5:00

相关推荐