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
29 def generate_launch_description():
31 Bring up the multi-robots with given launch arguments.
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}"
40 bringup_dir = get_package_share_directory(
'nav2_bringup')
41 launch_dir = os.path.join(bringup_dir,
'launch')
44 world = LaunchConfiguration(
'world')
45 simulator = LaunchConfiguration(
'simulator')
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')
57 declare_world_cmd = DeclareLaunchArgument(
59 default_value=os.path.join(bringup_dir,
'worlds',
'world_only.model'),
60 description=
'Full path to world file to load')
62 declare_simulator_cmd = DeclareLaunchArgument(
64 default_value=
'gazebo',
65 description=
'The simulator to use (gazebo or gzserver)')
67 declare_map_yaml_cmd = DeclareLaunchArgument(
69 default_value=os.path.join(bringup_dir,
'maps',
'turtlebot3_world.yaml'),
70 description=
'Full path to map file to load')
72 declare_params_file_cmd = DeclareLaunchArgument(
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')
77 declare_autostart_cmd = DeclareLaunchArgument(
78 'autostart', default_value=
'false',
79 description=
'Automatically startup the stacks')
81 declare_rviz_config_file_cmd = DeclareLaunchArgument(
83 default_value=os.path.join(bringup_dir,
'rviz',
'nav2_namespaced_view.rviz'),
84 description=
'Full path to the RVIZ config file to use.')
86 declare_use_robot_state_pub_cmd = DeclareLaunchArgument(
87 'use_robot_state_pub',
89 description=
'Whether to start the robot state publisher')
91 declare_use_rviz_cmd = DeclareLaunchArgument(
94 description=
'Whether to start RVIZ')
97 start_gazebo_cmd = ExecuteProcess(
98 cmd=[simulator,
'--verbose',
'-s',
'libgazebo_ros_init.so',
99 '-s',
'libgazebo_ros_factory.so', world],
102 robots_list = ParseMultiRobotPose(
'robots').value()
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)]),
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()),
119 IncludeLaunchDescription(
120 PythonLaunchDescriptionSource(os.path.join(bringup_dir,
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,
130 'use_simulator':
'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())
142 bringup_cmd_group.append(group)
145 ld = LaunchDescription()
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)
158 ld.add_action(start_gazebo_cmd)
160 ld.add_action(LogInfo(msg=[
'number_of_robots=', str(len(robots_list))]))
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]))
173 for cmd
in bringup_cmd_group: