//创建工作空间
mkdir ‐p ~/catkin_ws/src
cd catkin_ws
catkin_make
gedit ~/.bashrc //添加source /home/username/catkin_ws/devel/setup.bash
//创建程序包及节点
//创建功能包
cd catkin_ws/src
catkin_create_pkg learning_cplus roscpp rospy std_msgs //使用catkin_create_pkg指令 来创建一个功能包。格式如下:catkin_create_pkg 功能包名称 依赖项1 依赖项2 ......
cd ..
catkin_make
cd
source ~/.bashrc
rosd learning_cplus//进入指定功能包learning_cplus
//创建节点
roscd learning_cplus
cd src
touch publisher.cpp
在 publisher.cpp写入内容,listener同publisher创建一样
//修改Cmakelists.txt
// 首先是find_package依赖项,修改如下
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
//调用生成的消息修改如下
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
//个人编译输出配置修改如下
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent project s also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects a lso need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES learning_cplus
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
//在Build位置添加
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${catkin_LIBRARIES})
add_dependencies(publisher learning_cplus_generate_messages_cpp)
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener learning_cplus_generate_messages_cpp)
//配置package.xml文件
<?xml version="1.0"?>
<package format="2">
<name>learning_cplus</name>
<version>0.0.0</version>
<description>这是学习ROS C++编程的功能包</description>
<maintainer email="xxxxx@xx.com">Leslie___Cheung_csdn</maintainer>
<license>TODO</license>
<author email="xxxxx@xx.com">Leslie___Cheung_csdn</author>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
<export>
</export>
</package>
//编译并执行
cd catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash
//另启3个终端
//启动ROS Master节点管理器
roscore
//启动listener订阅节点。
rosrun learning_cplus listener
//启动publisher发布节点。
rosrun learning_cplus publisher
至此完成了,以后可参考以上建工程。
1、catkin_make 错误
error: ‘data’ was not declared in this scope
ROS_INFO("I heard: [%s]", msg‐>data.c_str());
解决:代码里有中文
2、CMakelists.txt里没有添加下面这一句
add_dependencies(publisher learning_cplus_generate_messages_cpp)
fatal error: visualization_msgs/Markers": No such file or directory
#include <visualization_msgs/Markers">
本文只是为自己做个记录,全程参考: