程序员

【ROS学习-9】创建ROS msg和srv

2018-04-06  本文已影响24人  网路元素

1.关于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.

   msgs are just simple text files with a field type and field name per line. The field types you can use are:

 int8, int16, int32, int64 (plus uint*)

 float32, float64

 string

 time, duration

 other msg files

 variable-length array[] and fixed-length array[C]

   There is also a special type in ROS: Header, the header contains a timestamp and coordinate frame information that are commonly used in ROS. You will frequently see the first line in a msg file have Header header.

   Here is an example of a msg that uses a Header, a string primitive, and two other msgs :

   srv files are just like msg files, except they contain two parts: a request and a response. The two parts are separated by a '---' line. Here is an example of a srv file:

   In the above example, A and B are the request, and Sum is the response.

2.使用msg

  roscd beginner_tutorials

  mkdir msg

  echo "int64 num" > msg/Num.msg

接下来修改package.xml文件,在相关位置添加如下内容:

  message_generation

  message_runtime

接下来修改CMakeLists.txt文件,在如下相应位置添加标红的内容:

  find_package(catkin REQUIRED COMPONENTS

  roscpp

  rospy

  std_msgs

  message_generation

  )

  catkin_package(

  # INCLUDE_DIRS include

  # LIBRARIES beginner_tutorials

  # CATKIN_DEPENDS roscpp rospy std_msgs

  # DEPENDS system_lib

  CATKIN_DEPENDS message_runtime

  )

  # add_message_files(

  # FILES

  # Message1.msg

  # Message2.msg

  # )

  add_message_files(

  FILES

  Num.msg

  )

  # generate_messages(

  # DEPENDENCIES

  # std_msgs

  # )

  generate_messages(

  DEPENDENCIES

  std_msgs

  )

   文件保存后,执行如下命令可以看到相应的信息:

rosmsg show beginner_tutorials/Num 

   执行输出:

  int64 num

而执行rosmsg show Num命令会输出:

  [beginner_tutorials/Num]:

  int64 num

3.使用srv

  roscd beginner_tutorials

  mkdir srv

接下来复制一个现成的srv文件:

  roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

从 rospy_tutorials包里将 AddTwoInts.srv复制为当前目录下的srv/AddTwoInts.srv文件。

接下来package.xml的修改跟msg里的一样,而CMakeLists.txt的修改如下红色标记:

  find_package(catkin REQUIRED COMPONENTS

  roscpp

  rospy

  std_msgs

  message_generation

  )

  # add_service_files(

  # FILES

  # Service1.srv

  # Service2.srv

  # )

  add_service_files(

  FILES

  AddTwoInts.srv

  )

文件保存后,使用如下命令可查看到刚创建的srv相关信息:

guochongxin@slam:~/ros/catkin_ws/src/beginner_tutorials$rossrv show beginner_tutorials/AddTwoInts 

  int64 a

  int64 b

  ---

  int64 sum

  guochongxin@slam:~/ros/catkin_ws/src/beginner_tutorials$ rossrv show AddTwoInts

  [beginner_tutorials/AddTwoInts]:

  int64 a

  int64 b

  ---

  int64 sum

  [rospy_tutorials/AddTwoInts]:

  int64 a

  int64 b

  ---

  int64 sum

4.关于msg和srv相同的操作步骤

在CMakeLists.txt文件中,都需要如下内容:

  generate_messages(

  DEPENDENCIES

  std_msgs

  )

   确认后,接下来需要执行如下命令重新编译安装以便生成相应的新内容:

  roscd beginner_tutorials

  cd ../..

  catkin_make install

   编译无误并安装更新相应的文件。

5.回顾

   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)

6.参考网址

http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv

上一篇下一篇

猜你喜欢

热点阅读