|
1 |
| -import os |
2 |
| - |
3 |
| -from ament_index_python import get_package_share_directory |
4 |
| - |
5 | 1 | from launch import LaunchDescription
|
6 |
| -from launch.actions import DeclareLaunchArgument |
7 |
| -from launch.actions import GroupAction |
8 |
| -from launch.actions import IncludeLaunchDescription |
9 |
| -from launch.launch_description_sources import PythonLaunchDescriptionSource |
10 |
| -from launch.substitutions import LaunchConfiguration |
11 |
| -from launch.substitutions import TextSubstitution |
12 |
| -from launch_ros.actions import Node |
13 |
| -from launch_ros.actions import PushRosNamespace |
| 2 | +from launch.actions import DeclareLaunchArgument, GroupAction, IncludeLaunchDescription |
| 3 | +from launch.substitutions import LaunchConfiguration, PathJoinSubstitution |
| 4 | +from launch_ros.actions import Node, PushROSNamespace |
| 5 | +from launch_ros.substitutions import FindPackageShare |
14 | 6 |
|
15 | 7 |
|
16 | 8 | def generate_launch_description():
|
| 9 | + launch_dir = PathJoinSubstitution([FindPackageShare('demo_nodes_cpp'), 'launch', 'topics']) |
| 10 | + return LaunchDescription([ |
| 11 | + # args that can be set from the command line or a default will be used |
| 12 | + DeclareLaunchArgument('background_r', default_value='0'), |
| 13 | + DeclareLaunchArgument('background_g', default_value='255'), |
| 14 | + DeclareLaunchArgument('background_b', default_value='0'), |
| 15 | + DeclareLaunchArgument('chatter_ns', default_value='my/chatter/ns'), |
17 | 16 |
|
18 |
| - # args that can be set from the command line or a default will be used |
19 |
| - background_r_launch_arg = DeclareLaunchArgument( |
20 |
| - 'background_r', default_value=TextSubstitution(text='0') |
21 |
| - ) |
22 |
| - background_g_launch_arg = DeclareLaunchArgument( |
23 |
| - 'background_g', default_value=TextSubstitution(text='255') |
24 |
| - ) |
25 |
| - background_b_launch_arg = DeclareLaunchArgument( |
26 |
| - 'background_b', default_value=TextSubstitution(text='0') |
27 |
| - ) |
28 |
| - chatter_ns_launch_arg = DeclareLaunchArgument( |
29 |
| - 'chatter_ns', default_value=TextSubstitution(text='my/chatter/ns') |
30 |
| - ) |
| 17 | + # include another launch file |
| 18 | + IncludeLaunchDescription( |
| 19 | + PathJoinSubstitution([launch_dir, 'talker_listener.launch.py']) |
| 20 | + ), |
31 | 21 |
|
32 |
| - # include another launch file |
33 |
| - launch_include = IncludeLaunchDescription( |
34 |
| - PythonLaunchDescriptionSource( |
35 |
| - os.path.join( |
36 |
| - get_package_share_directory('demo_nodes_cpp'), |
37 |
| - 'launch/topics/talker_listener.launch.py')) |
38 |
| - ) |
39 |
| - # include another launch file in the chatter_ns namespace |
40 |
| - launch_include_with_namespace = GroupAction( |
41 |
| - actions=[ |
42 |
| - # push_ros_namespace to set namespace of included nodes |
43 |
| - PushRosNamespace('chatter_ns'), |
44 |
| - IncludeLaunchDescription( |
45 |
| - PythonLaunchDescriptionSource( |
46 |
| - os.path.join( |
47 |
| - get_package_share_directory('demo_nodes_cpp'), |
48 |
| - 'launch/topics/talker_listener.launch.py')) |
49 |
| - ), |
50 |
| - ] |
51 |
| - ) |
| 22 | + # include a Python launch file in the chatter_py_ns namespace |
| 23 | + GroupAction( |
| 24 | + actions=[ |
| 25 | + # push_ros_namespace first to set namespace of included nodes for following actions |
| 26 | + PushROSNamespace(LaunchConfiguration('chatter_ns')), |
| 27 | + IncludeLaunchDescription( |
| 28 | + PathJoinSubstitution([launch_dir, 'talker_listener_launch.py'])), |
| 29 | + ] |
| 30 | + ), |
52 | 31 |
|
53 |
| - # start a turtlesim_node in the turtlesim1 namespace |
54 |
| - turtlesim_node = Node( |
55 |
| - package='turtlesim', |
56 |
| - namespace='turtlesim1', |
57 |
| - executable='turtlesim_node', |
58 |
| - name='sim' |
59 |
| - ) |
| 32 | + # include a xml launch file in the chatter_xml_ns namespace |
| 33 | + GroupAction( |
| 34 | + actions=[ |
| 35 | + # push_ros_namespace first to set namespace of included nodes for following actions |
| 36 | + PushROSNamespace('chatter_xml_ns'), |
| 37 | + IncludeLaunchDescription( |
| 38 | + PathJoinSubstitution([launch_dir, 'talker_listener_launch.xml'])), |
| 39 | + ] |
| 40 | + ), |
60 | 41 |
|
61 |
| - # start another turtlesim_node in the turtlesim2 namespace |
62 |
| - # and use args to set parameters |
63 |
| - turtlesim_node_with_parameters = Node( |
64 |
| - package='turtlesim', |
65 |
| - namespace='turtlesim2', |
66 |
| - executable='turtlesim_node', |
67 |
| - name='sim', |
68 |
| - parameters=[{ |
69 |
| - 'background_r': LaunchConfiguration('background_r'), |
70 |
| - 'background_g': LaunchConfiguration('background_g'), |
71 |
| - 'background_b': LaunchConfiguration('background_b'), |
72 |
| - }] |
73 |
| - ) |
| 42 | + # include a yaml launch file in the chatter_yaml_ns namespace |
| 43 | + GroupAction( |
| 44 | + actions=[ |
| 45 | + # push_ros_namespace first to set namespace of included nodes for following actions |
| 46 | + PushROSNamespace('chatter_yaml_ns'), |
| 47 | + IncludeLaunchDescription( |
| 48 | + PathJoinSubstitution([launch_dir, 'talker_listener_launch.yaml'])), |
| 49 | + ] |
| 50 | + ), |
74 | 51 |
|
75 |
| - # perform remap so both turtles listen to the same command topic |
76 |
| - forward_turtlesim_commands_to_second_turtlesim_node = Node( |
77 |
| - package='turtlesim', |
78 |
| - executable='mimic', |
79 |
| - name='mimic', |
80 |
| - remappings=[ |
81 |
| - ('/input/pose', '/turtlesim1/turtle1/pose'), |
82 |
| - ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'), |
83 |
| - ] |
84 |
| - ) |
| 52 | + # start a turtlesim_node in the turtlesim1 namespace |
| 53 | + Node( |
| 54 | + package='turtlesim', |
| 55 | + namespace='turtlesim1', |
| 56 | + executable='turtlesim_node', |
| 57 | + name='sim' |
| 58 | + ), |
85 | 59 |
|
86 |
| - return LaunchDescription([ |
87 |
| - background_r_launch_arg, |
88 |
| - background_g_launch_arg, |
89 |
| - background_b_launch_arg, |
90 |
| - chatter_ns_launch_arg, |
91 |
| - launch_include, |
92 |
| - launch_include_with_namespace, |
93 |
| - turtlesim_node, |
94 |
| - turtlesim_node_with_parameters, |
95 |
| - forward_turtlesim_commands_to_second_turtlesim_node, |
| 60 | + # start another turtlesim_node in the turtlesim2 namespace |
| 61 | + # and use args to set parameters |
| 62 | + Node( |
| 63 | + package='turtlesim', |
| 64 | + namespace='turtlesim2', |
| 65 | + executable='turtlesim_node', |
| 66 | + name='sim', |
| 67 | + parameters=[{ |
| 68 | + 'background_r': LaunchConfiguration('background_r'), |
| 69 | + 'background_g': LaunchConfiguration('background_g'), |
| 70 | + 'background_b': LaunchConfiguration('background_b'), |
| 71 | + }] |
| 72 | + ), |
| 73 | + |
| 74 | + # perform remap so both turtles listen to the same command topic |
| 75 | + Node( |
| 76 | + package='turtlesim', |
| 77 | + executable='mimic', |
| 78 | + name='mimic', |
| 79 | + remappings=[ |
| 80 | + ('/input/pose', '/turtlesim1/turtle1/pose'), |
| 81 | + ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'), |
| 82 | + ] |
| 83 | + ), |
96 | 84 | ])
|
0 commit comments