Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
nav_to_pose_example_launch.py
1 # Copyright (c) 2021 Samsung Research America
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 import os
16 from pathlib import Path
17 import tempfile
18 
19 from ament_index_python.packages import get_package_share_directory
20 
21 from launch import LaunchDescription
22 from launch.actions import (
23  AppendEnvironmentVariable,
24  DeclareLaunchArgument,
25  ExecuteProcess,
26  IncludeLaunchDescription,
27  OpaqueFunction,
28  RegisterEventHandler,
29 )
30 from launch.conditions import IfCondition
31 from launch.event_handlers import OnShutdown
32 from launch.launch_description_sources import PythonLaunchDescriptionSource
33 from launch.substitutions import Command, LaunchConfiguration, PythonExpression
34 from launch_ros.actions import Node
35 
36 
37 def generate_launch_description():
38  nav2_bringup_dir = get_package_share_directory('nav2_bringup')
39  sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
40  desc_dir = get_package_share_directory('nav2_minimal_tb4_description')
41 
42  robot_sdf = os.path.join(desc_dir, 'urdf', 'standard', 'turtlebot4.urdf.xacro')
43  world = os.path.join(sim_dir, 'worlds', 'depot.sdf')
44  map_yaml_file = os.path.join(nav2_bringup_dir, 'maps', 'depot.yaml')
45 
46  # Launch configuration variables
47  use_rviz = LaunchConfiguration('use_rviz')
48  headless = LaunchConfiguration('headless')
49 
50  # Declare the launch arguments
51  declare_use_rviz_cmd = DeclareLaunchArgument(
52  'use_rviz', default_value='True', description='Whether to start RVIZ'
53  )
54 
55  declare_simulator_cmd = DeclareLaunchArgument(
56  'headless', default_value='False', description='Whether to execute gzclient)'
57  )
58 
59  # start the simulation
60  world_sdf = tempfile.mktemp(prefix='nav2_', suffix='.sdf')
61  world_sdf_xacro = ExecuteProcess(
62  cmd=['xacro', '-o', world_sdf, ['headless:=', headless], world])
63  start_gazebo_server_cmd = IncludeLaunchDescription(
64  PythonLaunchDescriptionSource(
65  os.path.join(get_package_share_directory('ros_gz_sim'), 'launch',
66  'gz_sim.launch.py')),
67  launch_arguments={'gz_args': ['-r -s ', world_sdf]}.items())
68 
69  remove_temp_sdf_file = RegisterEventHandler(event_handler=OnShutdown(
70  on_shutdown=[
71  OpaqueFunction(function=lambda _: os.remove(world_sdf))
72  ]))
73 
74  set_env_vars_resources = AppendEnvironmentVariable(
75  'GZ_SIM_RESOURCE_PATH',
76  os.path.join(sim_dir, 'worlds'))
77  start_gazebo_client_cmd = IncludeLaunchDescription(
78  PythonLaunchDescriptionSource(
79  os.path.join(get_package_share_directory('ros_gz_sim'),
80  'launch',
81  'gz_sim.launch.py')
82  ),
83  condition=IfCondition(PythonExpression(
84  ['not ', headless])),
85  launch_arguments={'gz_args': ['-v4 -g ']}.items(),
86  )
87 
88  spawn_robot_cmd = IncludeLaunchDescription(
89  PythonLaunchDescriptionSource(
90  os.path.join(sim_dir, 'launch', 'spawn_tb4.launch.py')),
91  launch_arguments={'use_sim_time': 'True',
92  'robot_sdf': robot_sdf,
93  'x_pose': '-8.0',
94  'y_pose': '0.0',
95  'z_pose': '0.0',
96  'roll': '0.0',
97  'pitch': '0.0',
98  'yaw': '0.0'}.items())
99 
100  start_robot_state_publisher_cmd = Node(
101  package='robot_state_publisher',
102  executable='robot_state_publisher',
103  name='robot_state_publisher',
104  output='screen',
105  parameters=[
106  {'use_sim_time': True, 'robot_description': Command(['xacro', ' ', robot_sdf])}
107  ]
108  )
109 
110  # start the visualization
111  rviz_cmd = IncludeLaunchDescription(
112  PythonLaunchDescriptionSource(
113  os.path.join(nav2_bringup_dir, 'launch', 'rviz_launch.py')
114  ),
115  condition=IfCondition(use_rviz),
116  launch_arguments={'namespace': '', 'use_namespace': 'False'}.items(),
117  )
118 
119  # start navigation
120  bringup_cmd = IncludeLaunchDescription(
121  PythonLaunchDescriptionSource(
122  os.path.join(nav2_bringup_dir, 'launch', 'bringup_launch.py')
123  ),
124  launch_arguments={'map': map_yaml_file}.items(),
125  )
126 
127  # start the demo autonomy task
128  demo_cmd = Node(
129  package='nav2_simple_commander',
130  executable='example_nav_to_pose',
131  emulate_tty=True,
132  output='screen',
133  )
134 
135  set_env_vars_resources2 = AppendEnvironmentVariable(
136  'GZ_SIM_RESOURCE_PATH',
137  str(Path(os.path.join(desc_dir)).parent.resolve()))
138 
139  ld = LaunchDescription()
140  ld.add_action(declare_use_rviz_cmd)
141  ld.add_action(declare_simulator_cmd)
142  ld.add_action(world_sdf_xacro)
143  ld.add_action(remove_temp_sdf_file)
144  ld.add_action(set_env_vars_resources)
145  ld.add_action(set_env_vars_resources2)
146  ld.add_action(start_gazebo_server_cmd)
147  ld.add_action(start_gazebo_client_cmd)
148  ld.add_action(spawn_robot_cmd)
149  ld.add_action(start_robot_state_publisher_cmd)
150  ld.add_action(rviz_cmd)
151  ld.add_action(bringup_cmd)
152  ld.add_action(demo_cmd)
153  return ld