Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
cloned_multi_tb3_simulation_launch.py
1 # Copyright (c) 2023 LG Electronics.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 
16 import os
17 
18 from ament_index_python.packages import get_package_share_directory
19 from launch import LaunchDescription
20 from launch.actions import (DeclareLaunchArgument, ExecuteProcess, GroupAction,
21  IncludeLaunchDescription, LogInfo)
22 from launch.conditions import IfCondition
23 from launch.launch_description_sources import PythonLaunchDescriptionSource
24 from launch.substitutions import LaunchConfiguration, TextSubstitution
25 
26 from nav2_common.launch import ParseMultiRobotPose
27 
28 
29 def generate_launch_description():
30  """
31  Bring up the multi-robots with given launch arguments.
32 
33  Launch arguments consist of robot name(which is namespace) and pose for initialization.
34  Keep general yaml format for pose information.
35  ex) robots:="robot1={x: 1.0, y: 1.0, yaw: 1.5707}; robot2={x: 1.0, y: 1.0, yaw: 1.5707}"
36  ex) robots:="robot3={x: 1.0, y: 1.0, z: 1.0, roll: 0.0, pitch: 1.5707, yaw: 1.5707};
37  robot4={x: 1.0, y: 1.0, z: 1.0, roll: 0.0, pitch: 1.5707, yaw: 1.5707}"
38  """
39  # Get the launch directory
40  bringup_dir = get_package_share_directory('nav2_bringup')
41  launch_dir = os.path.join(bringup_dir, 'launch')
42 
43  # Simulation settings
44  world = LaunchConfiguration('world')
45  simulator = LaunchConfiguration('simulator')
46 
47  # On this example all robots are launched with the same settings
48  map_yaml_file = LaunchConfiguration('map')
49  params_file = LaunchConfiguration('params_file')
50  autostart = LaunchConfiguration('autostart')
51  rviz_config_file = LaunchConfiguration('rviz_config')
52  use_robot_state_pub = LaunchConfiguration('use_robot_state_pub')
53  use_rviz = LaunchConfiguration('use_rviz')
54  log_settings = LaunchConfiguration('log_settings', default='true')
55 
56  # Declare the launch arguments
57  declare_world_cmd = DeclareLaunchArgument(
58  'world',
59  default_value=os.path.join(bringup_dir, 'worlds', 'world_only.model'),
60  description='Full path to world file to load')
61 
62  declare_simulator_cmd = DeclareLaunchArgument(
63  'simulator',
64  default_value='gazebo',
65  description='The simulator to use (gazebo or gzserver)')
66 
67  declare_map_yaml_cmd = DeclareLaunchArgument(
68  'map',
69  default_value=os.path.join(bringup_dir, 'maps', 'turtlebot3_world.yaml'),
70  description='Full path to map file to load')
71 
72  declare_params_file_cmd = DeclareLaunchArgument(
73  'params_file',
74  default_value=os.path.join(bringup_dir, 'params', 'nav2_multirobot_params_all.yaml'),
75  description='Full path to the ROS2 parameters file to use for all launched nodes')
76 
77  declare_autostart_cmd = DeclareLaunchArgument(
78  'autostart', default_value='false',
79  description='Automatically startup the stacks')
80 
81  declare_rviz_config_file_cmd = DeclareLaunchArgument(
82  'rviz_config',
83  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_namespaced_view.rviz'),
84  description='Full path to the RVIZ config file to use.')
85 
86  declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
87  'use_robot_state_pub',
88  default_value='True',
89  description='Whether to start the robot state publisher')
90 
91  declare_use_rviz_cmd = DeclareLaunchArgument(
92  'use_rviz',
93  default_value='True',
94  description='Whether to start RVIZ')
95 
96  # Start Gazebo with plugin providing the robot spawning service
97  start_gazebo_cmd = ExecuteProcess(
98  cmd=[simulator, '--verbose', '-s', 'libgazebo_ros_init.so',
99  '-s', 'libgazebo_ros_factory.so', world],
100  output='screen')
101 
102  robots_list = ParseMultiRobotPose('robots').value()
103 
104  # Define commands for launching the navigation instances
105  bringup_cmd_group = []
106  for robot_name in robots_list:
107  init_pose = robots_list[robot_name]
108  group = GroupAction([
109  LogInfo(msg=['Launching namespace=', robot_name, ' init_pose=', str(init_pose)]),
110 
111  IncludeLaunchDescription(
112  PythonLaunchDescriptionSource(
113  os.path.join(launch_dir, 'rviz_launch.py')),
114  condition=IfCondition(use_rviz),
115  launch_arguments={'namespace': TextSubstitution(text=robot_name),
116  'use_namespace': 'True',
117  'rviz_config': rviz_config_file}.items()),
118 
119  IncludeLaunchDescription(
120  PythonLaunchDescriptionSource(os.path.join(bringup_dir,
121  'launch',
122  'tb3_simulation_launch.py')),
123  launch_arguments={'namespace': robot_name,
124  'use_namespace': 'True',
125  'map': map_yaml_file,
126  'use_sim_time': 'True',
127  'params_file': params_file,
128  'autostart': autostart,
129  'use_rviz': 'False',
130  'use_simulator': 'False',
131  'headless': 'False',
132  'use_robot_state_pub': use_robot_state_pub,
133  'x_pose': TextSubstitution(text=str(init_pose['x'])),
134  'y_pose': TextSubstitution(text=str(init_pose['y'])),
135  'z_pose': TextSubstitution(text=str(init_pose['z'])),
136  'roll': TextSubstitution(text=str(init_pose['roll'])),
137  'pitch': TextSubstitution(text=str(init_pose['pitch'])),
138  'yaw': TextSubstitution(text=str(init_pose['yaw'])),
139  'robot_name':TextSubstitution(text=robot_name), }.items())
140  ])
141 
142  bringup_cmd_group.append(group)
143 
144  # Create the launch description and populate
145  ld = LaunchDescription()
146 
147  # Declare the launch options
148  ld.add_action(declare_simulator_cmd)
149  ld.add_action(declare_world_cmd)
150  ld.add_action(declare_map_yaml_cmd)
151  ld.add_action(declare_params_file_cmd)
152  ld.add_action(declare_use_rviz_cmd)
153  ld.add_action(declare_autostart_cmd)
154  ld.add_action(declare_rviz_config_file_cmd)
155  ld.add_action(declare_use_robot_state_pub_cmd)
156 
157  # Add the actions to start gazebo, robots and simulations
158  ld.add_action(start_gazebo_cmd)
159 
160  ld.add_action(LogInfo(msg=['number_of_robots=', str(len(robots_list))]))
161 
162  ld.add_action(LogInfo(condition=IfCondition(log_settings),
163  msg=['map yaml: ', map_yaml_file]))
164  ld.add_action(LogInfo(condition=IfCondition(log_settings),
165  msg=['params yaml: ', params_file]))
166  ld.add_action(LogInfo(condition=IfCondition(log_settings),
167  msg=['rviz config file: ', rviz_config_file]))
168  ld.add_action(LogInfo(condition=IfCondition(log_settings),
169  msg=['using robot state pub: ', use_robot_state_pub]))
170  ld.add_action(LogInfo(condition=IfCondition(log_settings),
171  msg=['autostart: ', autostart]))
172 
173  for cmd in bringup_cmd_group:
174  ld.add_action(cmd)
175 
176  return ld