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文件
<?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">

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值