当前位置:网站首页>How is the launch of ros2 different?
How is the launch of ros2 different?
2022-07-26 04:15:00 【Mr anhydrous】
One 、 summary
stay launch Multi node execution ,ROS2 And ROS1 There's a big difference . First ,ros2 And ros1 The difference , lie in ROS2 No more *.launch Script files , In its place 3 Three formats are implemented :
- python Script
- xml file
- yaml file
Two 、 start-up launch file
2.1 launch Use of
adopt launch file ,ROS2 Many nodes can be started at the same time , This simplifies using the command line to start different programs multiple times Node.
2.2 launch Start command
1)xml file yaml file
ros2 launch <package_name> <launch_file_name>2) about py file , It can also be started directly launch file , like this :
ros2 launch turtlesim_mimic_launch.py
3、 ... and 、launch The content of the document
3.1 launch File content description
because launch file After all, files are multi node execution scripts , therefore , And “ ros2 run ” The contents of the instructions are roughly the same , Such as package name 、 The node name . Of course , To enrich some . Such as :
<node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim2">
<param name="background_r" value="$(var background_r)"/>
<param name="background_g" value="$(var background_g)"/>
<param name="background_b" value="$(var background_b)"/>
</node>3.2 About include
include Can be in a launch file Contains additional launch file
3.3 About group
group You can put more than one node Put together
Four 、 Practice writing a launch file
4.1 Necessary routine
Whether it's python、xml still yaml, To write launch file The steps are almost the same .
- Set the default value of command line parameters ,
- Set up launch file The inclusive relationship of , adopt label
- Set up Node Information , Include name、namespace、parameter
- If you need to set remmaping Is set remapping Relationship
4.2 for instance
Here's the official document An example , It's for turtles turtlesim Example :
from launch import LaunchDescriptionfrom launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='turtlesim',
namespace='turtlesim1',
executable='turtlesim_node',
name='sim'
),
Node(
package='turtlesim',
namespace='turtlesim2',
executable='turtlesim_node',
name='sim'
),
Node(
package='turtlesim',
executable='mimic',
name='mimic',
remappings=[
('/input/pose', '/turtlesim1/turtle1/pose'),
('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
])
])The above code is very clear , Is to start two turtle nodes , One mimic node ; Some of the above concepts that need to be explained are : What is the function of namespaces ? What's the point of remapping ?( After execution , Analyze with command line )
4.3 remap What's the effect
remap Is a very useful technique , It's usually used to make Topic Mapping . A node doesn't know this Topic, But by renaming , Or pseudonym , Or re disguise ( Be careful , Subscribers here only accept certain names Topic), Make some receive this Topic.
<remap from="/different_topic" to="/needed_topic"/>hold from Medium topic, convert to to designated topic
Yes 2 Uses :
- Put one node Originally released topic, Map to another name
- Put the others node Published original topic, Map to the desired topic
4.4 Execute with parameters launch
We noticed the above launch file There is args, such as background_r, When the command line is started, the data can be transmitted through , adopt key:=value form . Such as :
ros2 launch <package_name> <launch_file_name> background_r:=2555、 ... and 、 Reference resources : Three ways of writing
The following code is for reference , Are three file implementation methods of the same content , Because it's longer , I hope you can read it slowly .
5.1 be based on python The case of
# example.launch.py
import os
from ament_index_python import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import IncludeLaunchDescription
from launch.actions import GroupAction
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
from launch.substitutions import TextSubstitution
from launch_ros.actions import Node
from launch_ros.actions import PushRosNamespace
def generate_launch_description():
# args that can be set from the command line or a default will be used
background_r_launch_arg = DeclareLaunchArgument(
"background_r", default_value=TextSubstitution(text="0")
)
background_g_launch_arg = DeclareLaunchArgument(
"background_g", default_value=TextSubstitution(text="255")
)
background_b_launch_arg = DeclareLaunchArgument(
"background_b", default_value=TextSubstitution(text="0")
)
chatter_ns_launch_arg = DeclareLaunchArgument(
"chatter_ns", default_value=TextSubstitution(text="my/chatter/ns")
)
# include another launch file
launch_include = IncludeLaunchDescription(
PythonLaunchDescriptionSource(
os.path.join(
get_package_share_directory('demo_nodes_cpp'),
'launch/topics/talker_listener.launch.py'))
)
# include another launch file in the chatter_ns namespace
launch_include_with_namespace = GroupAction(
actions=[
# push-ros-namespace to set namespace of included nodes
PushRosNamespace(LaunchConfiguration('chatter_ns')),
IncludeLaunchDescription(
PythonLaunchDescriptionSource(
os.path.join(
get_package_share_directory('demo_nodes_cpp'),
'launch/topics/talker_listener.launch.py'))
),
]
)
# start a turtlesim_node in the turtlesim1 namespace
turtlesim_node = Node(
package='turtlesim',
namespace='turtlesim1',
executable='turtlesim_node',
name='sim'
)
# start another turtlesim_node in the turtlesim2 namespace
# and use args to set parameters
turtlesim_node_with_parameters = Node(
package='turtlesim',
namespace='turtlesim2',
executable='turtlesim_node',
name='sim',
parameters=[{
"background_r": LaunchConfiguration('background_r'),
"background_g": LaunchConfiguration('background_g'),
"background_b": LaunchConfiguration('background_b'),
}]
)
# perform remap so both turtles listen to the same command topic
forward_turtlesim_commands_to_second_turtlesim_node = Node(
package='turtlesim',
executable='mimic',
name='mimic',
remappings=[
('/input/pose', '/turtlesim1/turtle1/pose'),
('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
]
)
return LaunchDescription([
background_r_launch_arg,
background_g_launch_arg,
background_b_launch_arg,
chatter_ns_launch_arg,
launch_include,
launch_include_with_namespace,
turtlesim_node,
turtlesim_node_with_parameters,
forward_turtlesim_commands_to_second_turtlesim_node,
])This is the use of python Written , The last thing to come back is LaunchDescription Information , This is equivalent to xml in Content in Tags
5.2 be based on xml Writing
<!-- example.launch.xml -->
<launch>
<!-- args that can be set from the command line or a default will be used -->
<arg name="background_r" default="0"/>
<arg name="background_g" default="255"/>
<arg name="background_b" default="0"/>
<arg name="chatter_ns" default="my/chatter/ns"/>
<!-- include another launch file -->
<include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"/>
<!-- include another launch file in the chatter_ns namespace-->
<group>
<!-- push-ros-namespace to set namespace of included nodes -->
<push-ros-namespace namespace="$(var chatter_ns)"/>
<include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"/>
</group>
<!-- start a turtlesim_node in the turtlesim1 namespace -->
<node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim1"/>
<!-- start another turtlesim_node in the turtlesim2 namespace
and use args to set parameters -->
<node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim2">
<param name="background_r" value="$(var background_r)"/>
<param name="background_g" value="$(var background_g)"/>
<param name="background_b" value="$(var background_b)"/>
</node>
<!-- perform remap so both turtles listen to the same command topic -->
<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>5.3 be based on yaml The file of
# example.launch.yaml
launch:
# args that can be set from the command line or a default will be used
- arg:
name: "background_r"
default: "0"
- arg:
name: "background_g"
default: "255"
- arg:
name: "background_b"
default: "0"
- arg:
name: "chatter_ns"
default: "my/chatter/ns"
# include another launch file
- include:
file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"
# include another launch file in the chatter_ns namespace
- group:
- push-ros-namespace:
namespace: "$(var chatter_ns)"
- include:
file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"
# start a turtlesim_node in the turtlesim1 namespace
- node:
pkg: "turtlesim"
exec: "turtlesim_node"
name: "sim"
namespace: "turtlesim1"
# start another turtlesim_node in the turtlesim2 namespace and use args to set parameters
- node:
pkg: "turtlesim"
exec: "turtlesim_node"
name: "sim"
namespace: "turtlesim2"
param:
-
name: "background_r"
value: "$(var background_r)"
-
name: "background_g"
value: "$(var background_g)"
-
name: "background_b"
value: "$(var background_b)"
# perform remap so both turtles listen to the same command topic
- node:
pkg: "turtlesim"
exec: "mimic"
name: "mimic"
remap:
-
from: "/input/pose"
to: "/turtlesim1/turtle1/pose"
-
from: "/output/cmd_vel"
to: "/turtlesim2/turtle1/cmd_vel"Reference article :
ROS2 Medium launch File entry 6 A question - You know (zhihu.com)
边栏推荐
- 2.9.4 Boolean object type processing and convenient methods of ext JS
- PathMatchingResourcePatternResolver解析配置文件 资源文件
- Use of rule engine drools
- Sorting and searching
- 吴恩达机器学习课后习题——逻辑回归
- Apisex's exploration in the field of API and microservices
- 【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
- How to write abstract in English thesis?
- Recommendation | DBT skills training manual: baby, you are the reason why you live
- 动态规划 爬楼梯
猜你喜欢

Leetcode:1184. Distance between bus stops -- simple

Luoda development - audio stream processing - AAC / loopbacktest as an example

PHP method to find the location of session storage file

Advanced content of MySQL -- three MySQL logs that must be understood binlog, redo log and undo log

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

构建关系抽取的动词源

firewall 命令简单操作

2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation

What are the duplicate check rules for English papers?

dijango学习
随机推荐
Use of rule engine drools
How to make your language academic when writing a thesis? Just remember four sentences!
5 years, 1.4W times, NFT og's road to immortality Web3 column
PathMatchingResourcePatternResolver解析配置文件 资源文件
[digital ic/fpga] Hot unique code detection
香甜的黄油
【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
Write a paper for help, how to write the discussion part?
MATLAB绘图
When you try to delete all bad code in the program | daily anecdotes
生活相关——减少期待,更快乐
PHP save array to var file_ export、serialize
匿名函数的作用
动态规划 爬楼梯
The era of smart clothes has come. Zhinar invites you to discuss swords in Yangcheng. Guangya Exhibition is waiting for you on August 4
Mantium 如何在 Amazon SageMaker 上使用 DeepSpeed 实现低延迟 GPT-J 推理
Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
dijango学习
Web测试方法大全
In PHP, you can use the abs() function to turn negative numbers into positive numbers