ROS使用——建工程

 

//创建工作空间
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文件
 
 
learning_cplus 
0.0.0 
这是学习ROS C++编程的功能包 
Leslie___Cheung_csdn 
TODO 
Leslie___Cheung_csdn 
catkin 
roscpp 
rospy 
std_msgs 
message_generation 
roscpp 
rospy 
std_msgs 
roscpp
rospy
std_msgs
message_runtime 
 
 


//编译并执行
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

本文只是为自己做个记录,全程参考:

ROS教程(三):创建程序包及节点(图文)_创建ros节点程序_Leslie___Cheung的博客-CSDN博客

你可能感兴趣的:(机器人,linux,c++)