ROS2教程(8) - 在ROS中使用launch、launch替换文件 - Linux
创建launch文件
编写launch文件
mkdir launch
- launch/turtlesim_mimic_launch.xml
<launch>
<node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim1"/>
<node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim2"/>
<node pkg="turtlesim" exec="mimic" name="mimic">
<remap from="/input/pose" to="/turtlesim1/turtle1/pose"/>
<remap from="/output/cmd_vel" to="/turtlesim2/turtle1/cmd_vel"/>
</node>
</launch>
// 启动两个乌龟窗口,让一只乌龟模仿另一只乌龟的动作
// 在不同的工作空间启动两个turtlesim节点
// 最后一个节点也来自turtlesim包,但是是不同的执行文件:mimic。此节点进行了重映射,这意味着mimic将订阅/turtlesim1/sim的位姿信息,并将其重新发布给/turtlesim2/sim。换句话说,turtlesim2将模仿turtlesim1的动作。
run
# 终端1
cd launch && ros2 launch turtlesim_mimic_launch.xml
# 终端2
ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"
# 终端3
rqt_graph
# 除了直接运行启动文件之外,还可以运行由包提供的文件
ros2 launch <package_name> <launch_file_name>
# 在package.xml添加依赖项
<exec_depend>ros2launch</exec_depend>
将launch文件集成到ROS中
crate pkg
mkdir -p launch_ws/src
cd launch_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 cpp_launch_example
CMakeLists.txt
# Install launch files.
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}/
)
launch
- launch/my_script_launch.xml
<launch>
<node pkg="demo_nodes_cpp" exec="talker" name="talker"/>
</launch>
run
# build
colcon build
# run
ros2 launch cpp_launch_example my_script_launch.xml
替换
crate pkg
ros2 pkg create --build-type ament_cmake --license Apache-2.0 launch_tutorial
mkdir launch_tutorial/launch
CMakeLists.txt
# add
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}/
)
父launch文件
- launch/example_main.launch.yaml
launch:
- let:
name: 'background_r'
value: '200'
- include:
file: '$(find-pkg-share launch_tutorial)/launch/example_substitutions.launch.yaml'
arg:
- name: 'turtlesim_ns'
value: 'turtlesim2'
- name: 'use_provided_red'
value: 'True'
- name: 'new_background_r'
value: '$(var background_r)'
替换launch文件
- launch/example_substitutions.launch.yaml
launch:
- arg:
name: 'turtlesim_ns'
default: 'turtlesim1'
- arg:
name: 'use_provided_red'
default: 'False'
- arg:
name: 'new_background_r'
default: '200'
- node:
pkg: 'turtlesim'
namespace: '$(var turtlesim_ns)'
exec: 'turtlesim_node'
name: 'sim'
- executable:
cmd: 'ros2 service call $(var turtlesim_ns)/spawn turtlesim/srv/Spawn "{x: 5, y: 2, theta: 0.2}"'
- executable:
cmd: 'ros2 param set $(var turtlesim_ns)/sim background_r 120'
- timer:
period: 2.0
children:
- executable:
cmd: 'ros2 param set $(var turtlesim_ns)/sim background_r $(var new_background_r)'
if: '$(eval "$(var new_background_r) == 200 and $(var use_provided_red)")'
run
colcon build
# 启动父launch文件
ros2 launch launch_tutorial example_main.launch.yaml
# 查看替换launch文件参数
ros2 launch launch_tutorial example_substitutions.launch.yaml --show-args

# 启动替换launch文件
ros2 launch launch_tutorial example_substitutions.launch.yaml turtlesim_ns:='turtlesim3' use_provided_red:='True' new_background_r:=200
原文地址:https://blog.csdn.net/2301_79695159/article/details/140589196
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!