ROS学习手记 - 7 创建ROS msg & srv


至此,我们初步学习了ROS的基本工具,接下来一步步理解ROS的各个工作部件的创建和工作原理。

本文的详细文档:http://wenku.baidu.com/view/623f41b3376baf1ffd4fad7a

  1. Creating a ROS msg and srv

    This tutorial covers how to create and build msg and srv files as well as therosmsg, rossrv and roscp commandline tools.

1. msg & srv 文件介绍
    msg 文件 就是用来让不同语言能产生message的一个指导性文本文件。
    srv file describes a service. It is composed of two parts: a request and a response.

2. msg & srv 文件存储位置:
    <workspace>/packagename/msg/
    &
    <workspace>/packagename/srv/
  
==== msg file 说明 ====
3. msgfile 的格式和生成
    生成:
         $ cd ~/catkin_ws/src/beginner_tutorials
         $ mkdir msg
         $ echo "int64 num" > msg/Num.msg

    格式:
         string first_name
         string last_name
         uint8 age
         uint32 score

4. msgfile 添加到package.xml的dependency中
      <build_depend>message_generation</build_depend>
      <run_depend>message_runtime</run_depend>

5. 对CMakeLists.txt文件的修改:

  5.1 添加message_generation dependency到CMakeLists.txt中:
      message_generation before the closing parenthesis
        find_package(catkin REQUIRED COMPONENTS
        roscpp
        rospy
        std_msgs
        message_generation  // add ’message_generation’
        )

  5.2 export the message runtime dependency.
      catkin_package(
      ...
      CATKIN_DEPENDS message_runtime ...
      ...)

  5.3 添加msg文件
      add_message_files(
      FILES
      Num.msg
      )
    添加了.msg文件在CMake里,我们保证在你添加其他.msg文件时,CMake知道何时它需要reconfigure重新配置项目。

  5.4 确保generate_messages 函数被调用
      generate_messages(
      DEPENDENCIES
      std_msgs
      )

6. rosmsg 工具的使用
    make sure that ROS can see it using the rosmsg show command.
    Usage:
      $ rosmsg show [message type]
    Example:
      $ rosmsg show beginner_tutorials/Num
      $ rosmsg show Num
===================
      === srv file 说明 ===
 1. srv的路径和生成
      $ roscd beginner_tutorials
      $ mkdir srv
    Instead of creating a new srv definition by hand, we will copy an existing one from another package.
For that, roscp is a useful commandline tool for copying files from one package to another.
    使用roscp工具从一个package拷贝文件到另一个。
    Usage:
      $ roscp [package_name] [file_to_copy_path] [copy_path]
    Now we can copy a service from the rospy_tutorials package:
      $ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

 2. 保证package.xml里有dependency配置(可能独立修改了srv,确保一下)
      <build_depend>message_generation</build_depend>
      <run_depend>message_runtime</run_depend>

 3.1 add the message_generation dependency to generate messages in CMakeLists.txt:
# 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
       )

  3.2 添加 srvice 文件
    add_service_files(
      FILES
      AddTwoInts.srv
    )

  3.3 确保generate_messages 函数被调用
      generate_messages(
      DEPENDENCIES
      std_msgs
      )

4. 使用rossrv工具
    添加以后,确保ROS can see it using the rossrv show command.
    Usage:
    $ rossrv show <service type>
    Example:
    $ rossrv show beginner_tutorials/AddTwoInts
    You will see:
    int64 a
    int64 b
    ---
    int64 sum
    Similar to rosmsg, you can find service files like this without specifying package name:
    $ rossrv show AddTwoInts
    [beginner_tutorials/AddTwoInts]:
    int64 a
    int64 b
    ---
    int64 sum

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

无论是修改了msg还是srv,都需要做的步骤:

  CMakeLists.txt文件的修改
  1.
        find_package(catkin REQUIRED COMPONENTS
        roscpp
        rospy
        std_msgs
        message_generation  // add ’message_generation’
        )

  2. add any packages you depend on which contain .msg files that your messages use (in this case std_msgs), such that it looks like this:
        generate_messages(
          DEPENDENCIES
          std_msgs
        )

终于到了catkin_make出手的时候:

Now that we have made some new messages we need to make our package again:

# In your catkin workspace (by default:~/catkin_ws$)

$ cd ../..
$ catkin_make
$ cd -

至此,我们就编译完成了一套ROS 的 msg & srv
Any .msg file in the msg directory will generate code for use in all supportedlanguages.
The C++ message header file will be generated in ~/catkin_ws/devel/include/beginner_tutorials/.
The Python script will be created in ~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg.
The lisp file appears in ~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/.

The full specification for the message format is available at theMessage Description Language page.

Getting Help

We've seen quite a few ROS tools already. It can be difficult to keeptrack of what arguments each command requires. Luckily, most ROS tools providetheir own help.

Try:

$ rosmsg -h

You should see a list of different rosmsg subcommands.

Commands:

  •   rosmsgshow Show message description
  •   rosmsgusers  Find files that use message
  •   rosmsgmd5  Display message md5sum
  •   rosmsgpackage  List messages in a package
  •   rosmsgpackages List packages that contain messages

You can also get help for subcommands

$ rosmsg show -h

  • This shows the arguments that are needed for rosmsg show:

·       Usage: rosmsg show[options] <message type>

·        

·       Options:

·         -h, --help show this help message and exit
  -r, --raw   show raw message text, including comments

Review

Lets just list some of the commands we've used so far:

  • 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)

Next Tutorial

Now that you've made a new ROS msg and srv, let's look at writing a simplepublisher and subscriber(python)(c++).




Published At
comments powered by Disqus