6.创建一个话题以及自建一个msg

                                                                      6.创建一个话题以及自建一个msg

ROS消息通信中使用的发布者(Publisher)和订阅者(Subscriber)。在ROS中,发送端称为发布者,接收端称为订阅者。两者采用的是松耦合机制,可以随时建立和断开联系,可以一对多同时进行通信;本节旨在创建一个简单的msg文件,并创建和运行发布者和订阅者节点。

1.创建包(submap_switch)

cd ~/catkin_ws/src
catkin_create_pkg submap_switch std_msgs rospy roscpp
  • 依赖包文件 
    std_msgs 
    rospy 
    roscpp

自动生成以下Cmake文件,可以根据实际需求在这里创建包时添加,也可后续在Cmake文件中添加!

## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) 
## is used, also find other catkin packages 
find_package(catkin REQUIRED COMPONENTS 
  nav_msgs
  roscpp 
)
 95 ###################################
 96 ## catkin specific configuration ##
 97 ###################################
 98 ## The catkin_package macro generates cmake config files for your package
 99 ## Declare things to be passed to dependent projects
100 ## INCLUDE_DIRS: uncomment this if your package contains header files
101 ## LIBRARIES: libraries you create in this project that dependent projects also need
102 ## CATKIN_DEPENDS: catkin_packages dependent projects also need
103 ## DEPENDS: system dependencies of this project that dependent projects also need
104 catkin_package(
105 #  INCLUDE_DIRS include
106   LIBRARIES submap_switch
107   CATKIN_DEPENDS nav_msgs roscpp
108 #  DEPENDS system_lib
109 )

2.添加源代码(C++/Python)

cd ~/catkin_ws/src/submap_switch
vim submap_switch.cpp

3.编译源代码

a.修改CMakeList.txt文件,主要修改一下五项,将其开启(自动生成的CmakeList.txt未自动开启)!

cd ~/catkin_ws/src/beginner_tutorials/CMakeLists.txt

第一项:添加编译设置中编译依赖包

 95 ###################################
 96 ## catkin specific configuration ##
 97 ###################################
 98 ## The catkin_package macro generates cmake config files for your package
 99 ## Declare things to be passed to dependent projects
100 ## INCLUDE_DIRS: uncomment this if your package contains header files
101 ## LIBRARIES: libraries you create in this project that dependent projects also need
102 ## CATKIN_DEPENDS: catkin_packages dependent projects also need
103 ## DEPENDS: system dependencies of this project that dependent projects also need
104 catkin_package(
105 #  INCLUDE_DIRS include
106   LIBRARIES submap_switch
107   CATKIN_DEPENDS nav_msgs roscpp
108 #  DEPENDS system_lib
109 )

第二~五项:

111 ###########
112 ## Build ##
113 ###########
#第二项:添加编译的头文件路径
115 ## Specify additional locations of header files
116 ## Your package locations should be listed before other locations
117 include_directories(
118 # include
119   ${catkin_INCLUDE_DIRS}
120 )

#第三项:添加需要编译的源码
132 ## Declare a C++ executable
133 ## With catkin_make all packages are built within a single CMake context
134 ## The recommended prefix ensures that target names across packages don't collide
135 # add_executable(${PROJECT_NAME}_node src/submap_switch_node.cpp)
136   add_executable(${PROJECT_NAME}_node src/submap_switch.cpp)

#第四项:添加需要编译依赖可执行文件
144 ## Add cmake target dependencies of the executable
145 ## same as for the library above
146  add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

#第五项:添加链接的指定库
148 ## Specify libraries to link a library or executable target against
149  target_link_libraries(${PROJECT_NAME}_node
150    ${catkin_LIBRARIES}
151 )

b.编译包

cmake_minimum_required(VERSION 2.8.3) 
project(submap_switch)

包名即是CMakeList.txt中的project名!

catkin_make -DCATKIN_WHITELIST_PACKAGES="submap_switch"

6.创建一个话题以及自建一个msg

这样一个功能包创建完成!

 

c.如何验证是否编译成功?

启动ros,执行节点submap_switch即可!(巧用teb自动补齐键,也可以做判断是否编译成功)

roscore
rosrun submap_switch submap_switch

如果提示非节点等提示,请设置好rosg工作环境:

临时设置ROS工作环境

source /home/dongdong/catkin_ws/devel/setup.bash

如果永久性配置

echo "source /home/dongdong/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

查看是否配置成功

echo $ROS_PACKAGE_PATH