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 .../>
<param .../>
<rosparam .../>
<include .../>
<remap .../>
<arg .../>
<group> </group>
</launch>
launch
<!-- launch 标签是 launch 文件的根节点,它是其他子标签的容器,没有其他特殊功能。 -->
<launch>
...
</launch>
nodenode
标签会指定一个准备运行的ROS节点,node标签是 launch 文件中最重要的标签,因为它实现了launch文件的基本功能,即同时启动多个ROS节点。
<node pkg="package_name" type="executable_node" name="node_name" args="$()" respawn="true" output="sceen">
pkg
:节点所在功能包的名称package_name
;type
:节点类型是可执行文件(节点)的名称executable_node
;name
:节点运行时的名称node_name
;args
:传递命令行设置的参数;respawn
:是否自动重启,true
表示如果节点未启动或异常关闭,则自动重启;false
则表示不自动重启,默认值为false
;output
:是否将节点信息输出到屏幕,如果不设置该属性,则节点信息会被写入到日志文件,并不会显示到屏幕上。
param
在工程项目开发中,我们常常需要改变程序变量的一些参数,如果在程序中赋值,我们每次修改参数都需要重新编译程序,大大降低了开发效率,而param
标签则可以实现传递参数的功能,它可以定义一个将要被设置到参数服务器的参数,它的参数值可以通过文本文件、二进制文件或命令等属性来设置。
<param name="param_name" type="param_type" value="param_value" />
<!-- param 标签可以嵌入到 node 标签中,以此来作为该 node 的私有参数 -->
<node>
<param name="param_name" type="param_type" value="param_value" />
</node>
name
:参数名称param_name
type
:参数类型double,str,int,bool,yaml
value
:需要设置的参数值param_value
rosparamrosparam
标签可以实现节点从参数服务器上加载(load)、导出(dump)和删除(delete)YAML
文件
<!-- 加载package_name功能包下的example.yaml文件 -->
<rosparam command="load" file="$(find package_name)/example.yaml">
<!-- 导出example_out.yaml文件到package_name功能包下 -->
<rosparam command="dump" file="$(find package_name)/example_out.yaml" />
<!-- 删除参数 -->
<rosparam command="delete" param="xxx/param">
command
:功能类型(load、dump、delete)file
:参数文件的路径param
:参数名称
includeinclude
标签功能和编程语言中的include预处理类似,它可以导入其他launch文件到当前include
标签所在的位置,实现launch文件复用。
<include file="$(find package_name)/launch_file_name">
remapremap
标签可以实现节点名称的重映射,每个remap
标签包含一个原始名称和一个新名称,在系统运行后原始名称会被替换为新名称。
<remap from="turtle1/cmd_vel" to="/cmd_vel" />
<!-- remap 标签同样可以嵌入到 node 标签中,以此来作为该 node 的私有重映射 -->
<node>
<remap from="turtle1/cmd_vel" to="/cmd_vel" />
</node>
argarg
标签表示启动参数,该标签允许创建更多可重用和可配置的启动文件,其可以通过命令行、include 标签、定义在高级别的文件这 3 种方式配置值。同时注意:arg
标签声明的参数不是全局的,只能在声明的单个启动文件中使用,可以当成函数的局部参数来理解。
<arg name="arg_name" default="arg_default" />
<arg name="arg_name" value="arg_value" />
<!-- 命令行传递的 arg 参数可以覆盖 default,但不能覆盖 value。 -->
注意:arg
和param
标签的区别:
函数 | 作用 |
---|---|
arg | 启动时的参数,只在launch文件中有意义 |
param | 运行时的参数,参数会存储在参数服务器中 |
groupgroup
标签可以实现将一组配置应用到组内的所有节点,它也具有命名空间ns
特点,可以将不同的节点放入不同的 namespace
。
<!-- 用法1 -->
<group ns="namespace_1">
<node pkg="pkg_name1" .../>
<node pkg="pkg_name2" .../>
...
</group>
<group ns="namespace_2">
<node pkg="pkg_name3" .../>
<node pkg="pkg_name4" .../>
...
</group>
<!-- 用法2 -->
<!-- if = value:value 为 true 则包含内部信息 -->
<group if="$(arg foo1)">
<node pkg="pkg_name1" .../>
</group>
<!-- unless = value:value 为 false 则包含内部信息 -->
<group unless="$(arg foo2)">
<node pkg="pkg_name2" .../>
</group>
<!--
当 foo1 == true 时包含其标签内部
当 foo2 == false 时包含其标签内部
-->
4.下面我们通过几个简单的实例,来演示一下上述函数可以实现的功能
- 通过launch文件同时启动乌龟GUI节点和键盘控制节点,代码如下
<launch>
<!-- 启动乌龟GUI节点 -->
<node pkg="turtlesim" type="turtlesim_node" name="Turtle_Gui" output="screen" />
<!-- 启动键盘控制节点 -->
<node pkg="turtlesim" type="turtle_teleop_key" name="Key_Control" output="screen" />
</launch>
终端下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文件格式及配置
<?xml version="1.0"?>
<!-- 格式: 以前是 1,推荐使用格式 2 -->
<package format="2">
<!-- 功能包名 -->
<name>demo01_helloworld_vs</name>
<!-- 版本 -->
<version>0.0.0</version>
<!-- 功能包描述信息 -->
<description>The demo01_helloworld_vs package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<!-- 开发维护人员信息 -->
<maintainer email="xxx@todo.todo">xxx</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<!-- 许可证信息,ROS核心组件默认 BSD -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/demo01_helloworld_vs</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<!-- 依赖的构建工具,这是必须的 -->
<buildtool_depend>catkin</buildtool_depend>
<!-- 指定构建此软件包所需的软件包 -->
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</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>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
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配置文件介绍到此就更新完毕,创作不易,希望大家多多点赞收藏,感谢大家!!!文章来源:https://www.toymoban.com/news/detail-437617.html
参考资料:
http://www.autolabor.com.cn/book/ROSTutorials/
http://events.jianshu.io/p/0450e74cbe4a文章来源地址https://www.toymoban.com/news/detail-437617.html
到了这里,关于launch文件的编写及ROS配置文件的详细介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!