launch文件的编写及ROS配置文件的详细介绍

launch文件的编写及ROS配置文件的详细介绍

launch文件的编写及ROS配置文件的详细介绍

1 launch文件介绍及简单应用1.1 launch文件介绍1.2 launch文件简单应用

2 package.xml文件介绍及配置3 CMakeLists.txt文件介绍及配置

1 launch文件介绍及简单应用

1.1 launch文件介绍

根据ROS的架构和通信机制来看,ROS的各个功能的实现离不开节点(node)和话题(topic)、参数(parameter)、服务(service)等构成的网络拓扑(rosgraph),其中每个j节点 都可以完成对应的功能。而一个机器人完整功能的实现,通常需要启动多个节点,如果一个节点一个节点的启动,比较麻烦。官方给出的优化策略是使用 launch 文件,可以一次性启动多个 ROS 节点。通过launch文件以及roslaunch命令可以一次性启动多个节点,并且可以设置丰富的参数。 在官方给出的例程中,发部分功能的实现都是通过launch文件来实现的,我们也应该学会这种优化策略,通过launch文件来启动自己的功能包,高效的实现各种功能。

1.2 launch文件简单应用

1.对于launch文件的存放,我们可以在功能包下新建一个launch文件夹来专门存在launch文件,方便我们进行调用,下面进行实操 VSode在工作空间目录下打开,这里我们打开的是之前演示的VScode_ws工作空间,打开后在功能包下创建launch文件夹即可 2.在launch文件夹下新建一个名为test01的*.launch文件 3.在编写launch文件之前,我们先介绍一下launch文件的格式,launch文件本质是一个xml类型的文件,通过各种标签进行功能的实现,下面列举launch文件的基本格式及各标签的作用 launch文件的基本格式如下:

launch

...

node node标签会指定一个准备运行的ROS节点,node标签是 launch 文件中最重要的标签,因为它实现了launch文件的基本功能,即同时启动多个ROS节点。

pkg:节点所在功能包的名称package_name; type:节点类型是可执行文件(节点)的名称executable_node; name:节点运行时的名称node_name; args:传递命令行设置的参数; respawn:是否自动重启,true表示如果节点未启动或异常关闭,则自动重启;false则表示不自动重启,默认值为false; output:是否将节点信息输出到屏幕,如果不设置该属性,则节点信息会被写入到日志文件,并不会显示到屏幕上。 param 在工程项目开发中,我们常常需要改变程序变量的一些参数,如果在程序中赋值,我们每次修改参数都需要重新编译程序,大大降低了开发效率,而param标签则可以实现传递参数的功能,它可以定义一个将要被设置到参数服务器的参数,它的参数值可以通过文本文件、二进制文件或命令等属性来设置。

name:参数名称param_name type:参数类型double,str,int,bool,yaml value:需要设置的参数值param_value rosparam rosparam标签可以实现节点从参数服务器上加载(load)、导出(dump)和删除(delete)YAML文件

command:功能类型(load、dump、delete) file:参数文件的路径 param:参数名称 include include标签功能和编程语言中的include预处理类似,它可以导入其他launch文件到当前include标签所在的位置,实现launch文件复用。

remap remap标签可以实现节点名称的重映射,每个remap标签包含一个原始名称和一个新名称,在系统运行后原始名称会被替换为新名称。

arg arg标签表示启动参数,该标签允许创建更多可重用和可配置的启动文件,其可以通过命令行、include 标签、定义在高级别的文件这 3 种方式配置值。同时注意:arg标签声明的参数不是全局的,只能在声明的单个启动文件中使用,可以当成函数的局部参数来理解。

注意:arg和param标签的区别:

函数作用arg启动时的参数,只在launch文件中有意义param运行时的参数,参数会存储在参数服务器中

group group标签可以实现将一组配置应用到组内的所有节点,它也具有命名空间ns特点,可以将不同的节点放入不同的 namespace。

...

...

4.下面我们通过几个简单的实例,来演示一下上述函数可以实现的功能

通过launch文件同时启动乌龟GUI节点和键盘控制节点,代码如下

终端下launch文件运行格式如下,第一次运行记得使用指令更新环境变量source ./devel/setup.bash

roslaunch 功能包名 launch文件名字.launch

本教程在VScode终端下的指令为:

source ./devel/setup.bash

roslaunch demo01_helloworld_vs test01.launch

运行结果如下,我们可以看到,键盘可以成功控制乌龟运动。 注意:launch文件启动之前,无需再执行roscore指令启动rosmaster,launch文件可以自启动rosmaster

2 package.xml文件介绍及配置

我们以功能包demo01_helloworld_vs下的package.xml文件为例介绍package.xml文件格式及配置

demo01_helloworld_vs

0.0.0

The demo01_helloworld_vs package

xxx

TODO

catkin

roscpp

rospy

std_msgs

roscpp

rospy

std_msgs

roscpp

rospy

std_msgs

3 CMakeLists.txt文件介绍及配置

我们以功能包demo01_helloworld_vs下的CMakeLists.txt文件为例介绍CMakeLists.txt文件格式及配置

cmake_minimum_required(VERSION 3.0.2) #所需 cmake 版本

project(demo01_helloworld_vs) #功能包名称,会被 ${PROJECT_NAME} 的方式调用

## Compile as C++11, supported in ROS Kinetic and newer

# add_compile_options(-std=c++11)

## 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

roscpp

rospy

std_msgs

)

## System dependencies are found with CMake's conventions

# 默认添加系统依赖

# find_package(Boost REQUIRED COMPONENTS system)

## Uncomment this if the package has a setup.py. This macro ensures

## modules and global scripts declared therein get installed

## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html

# 启动 python 模块支持

# catkin_python_setup()

################################################

## Declare ROS messages, services and actions ##

################################################

## To declare and build messages, services or actions from within this

## package, follow these steps:

## * Let MSG_DEP_SET be the set of packages whose message types you use in

## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).

## * In the file package.xml:

## * add a build_depend tag for "message_generation"

## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET

## * If MSG_DEP_SET isn't empty the following dependency has been pulled in

## but can be declared for certainty nonetheless:

## * add a exec_depend tag for "message_runtime"

## * In this file (CMakeLists.txt):

## * add "message_generation" and every package in MSG_DEP_SET to

## find_package(catkin REQUIRED COMPONENTS ...)

## * add "message_runtime" and every package in MSG_DEP_SET to

## catkin_package(CATKIN_DEPENDS ...)

## * uncomment the add_*_files sections below as needed

## and list every .msg/.srv/.action file to be processed

## * uncomment the generate_messages entry below

## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder

## 配置 msg 源文件

# add_message_files(

# FILES

# Message1.msg

# Message2.msg

# )

## Generate services in the 'srv' folder

## 配置 srv 源文件

# add_service_files(

# FILES

# Service1.srv

# Service2.srv

# )

## Generate actions in the 'action' folder

## 配置 action 源文件

# add_action_files(

# FILES

# Action1.action

# Action2.action

# )

## Generate added messages and services with any dependencies listed here

# 生成消息、服务时的依赖包

# generate_messages(

# DEPENDENCIES

# std_msgs

# )

################################################

## Declare ROS dynamic reconfigure parameters ##

## 声明 ROS 动态参数配置 ##

################################################

## To declare and build dynamic reconfigure parameters within this

## package, follow these steps:

## * In the file package.xml:

## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"

## * In this file (CMakeLists.txt):

## * add "dynamic_reconfigure" to

## find_package(catkin REQUIRED COMPONENTS ...)

## * uncomment the "generate_dynamic_reconfigure_options" section below

## and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder

# generate_dynamic_reconfigure_options(

# cfg/DynReconf1.cfg

# cfg/DynReconf2.cfg

# )

###################################

## catkin specific configuration ##

## catkin 特定配置##

###################################

## 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 projects also need

## CATKIN_DEPENDS: catkin_packages dependent projects also need

## DEPENDS: system dependencies of this project that dependent projects also need

# 运行时依赖

catkin_package(

# INCLUDE_DIRS include

# LIBRARIES demo01_helloworld_vs

# CATKIN_DEPENDS roscpp rospy std_msgs

# DEPENDS system_lib

)

###########

## Build ##

###########

## Specify additional locations of header files

## Your package locations should be listed before other locations

# 添加头文件路径,当前程序包的头文件路径位于其他文件路径之前

include_directories(

# include

${catkin_INCLUDE_DIRS}

)

## Declare a C++ library

# 声明 C++ 库

# add_library(${PROJECT_NAME}

# src/${PROJECT_NAME}/demo01_helloworld_vs.cpp

# )

## Add cmake target dependencies of the library

## as an example, code may need to be generated before libraries

## either from message generation or dynamic reconfigure

# 添加库的 cmake 目标依赖

# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable

## With catkin_make all packages are built within a single CMake context

## The recommended prefix ensures that target names across packages don't collide

# 声明 C++ 可执行文件

# add_executable(${PROJECT_NAME}_node src/demo01_helloworld_vs_node.cpp)

add_executable(helloworld_vs_c src/helloworld_vs_c.cpp)

## Rename C++ executable without prefix

## The above recommended prefix causes long target names, the following renames the

## target back to the shorter version for ease of user use

## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"

# 重命名c++可执行文件

# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable

## same as for the library above

# 添加可执行文件的 cmake 目标依赖

# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

add_dependencies(helloworld_vs_c ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against

# 指定库、可执行文件的链接库

# target_link_libraries(${PROJECT_NAME}_node

# ${catkin_LIBRARIES}

# )

target_link_libraries(helloworld_vs_c

${catkin_LIBRARIES}

)

#############

## Install ##

## 安装 ##

#############

# all install targets should use catkin DESTINATION variables

# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation

## in contrast to setup.py, you can choose the destination

# 设置用于安装的可执行脚本

# catkin_install_python(PROGRAMS

# scripts/my_python_script

# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}

# )

catkin_install_python(PROGRAMS

scripts/helloworld_vs_p.py

DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}

)

## Mark executables for installation

## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html

# install(TARGETS ${PROJECT_NAME}_node

# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}

# )

## Mark libraries for installation

## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html

# install(TARGETS ${PROJECT_NAME}

# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}

# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}

# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}

# )

## Mark cpp header files for installation

# install(DIRECTORY include/${PROJECT_NAME}/

# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}

# FILES_MATCHING PATTERN "*.h"

# PATTERN ".svn" EXCLUDE

# )

## Mark other files for installation (e.g. launch and bag files, etc.)

# install(FILES

# # myfile1

# # myfile2

# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}

# )

#############

## Testing ##

#############

## Add gtest based cpp test target and link libraries

# catkin_add_gtest(${PROJECT_NAME}-test test/test_demo01_helloworld_vs.cpp)

# if(TARGET ${PROJECT_NAME}-test)

# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})

# endif()

## Add folders to be run by python nosetests

# catkin_add_nosetests(test)

launch文件的编写及ROS配置文件介绍到此就更新完毕,创作不易,希望大家多多点赞收藏,感谢大家!!!

参考资料: http://www.autolabor.com.cn/book/ROSTutorials/ http://events.jianshu.io/p/0450e74cbe4a

相关推荐

中国的玻璃发展史
第365用英语怎么说

中国的玻璃发展史

📅 10-09 👁️ 9656
tcl电视保修几年 TCL电视整机保修多久
365bet娱乐登陆

tcl电视保修几年 TCL电视整机保修多久

📅 09-20 👁️ 878
长春电信宽带套餐价格表
第365用英语怎么说

长春电信宽带套餐价格表

📅 08-01 👁️ 4029