在自己创建的包中,如beginner_tutorials,新建msg消息目录,新建Num.msg文件:
$ roscd beginner_tutorials
$ mkdir msg
$ cd msg
$ touch Num.msg
$ rosed beginner_tutorials Num.msg
在Num.msg文件中,手工输入代码:
int64 num
打开beginner_tutorials 包下的package.xml,增加依赖:
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
打开 beginner_tutorials 包下的CMakeLists.txt,增加依赖:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
在CMakeLists.txt文件,增加消息文件,取消#,并修改为:
add_message_files(
FILES
Num.msg
)
在CMakeLists.txt文件,设置msg依赖:
generate_messages(
DEPENDENCIES
std_msgs
)
注意:我们在编写msg文件时,不止可以使用原生的int、string等类型,有时候也会用到其他包里预定义好的信息类型,比如geometry_msgs/PoseWithCovariance pose,当我们需要用到其他包里定义好的信息时,我们就要声明对其他包的依赖,比如上面的例子里我们就要声明一个geometry_msgs的依赖。
而在前面的例子中,我们的msg文件中只有这一行:
int64 num
仅仅使用了原生的int类型,所以我们只需要声明std_msgs依赖即可。
在CMakeLists.txt文件,取消CATKIN_DEPENDS的#,并修改为:
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES beginner_tutorials
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
编译代码
$ cd ~/catkin_ws
$ catkin_make
检查消息命令:
$ rosmsg show beginner_tutorials/Num
效果:
int64 num