Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
rviz_launch.py
1 # Copyright (c) 2018 Intel Corporation
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 
17 from ament_index_python.packages import get_package_share_directory
18 
19 from launch import LaunchDescription
20 from launch.actions import DeclareLaunchArgument, EmitEvent, RegisterEventHandler
21 from launch.conditions import IfCondition, UnlessCondition
22 from launch.event_handlers import OnProcessExit
23 from launch.events import Shutdown
24 from launch.substitutions import LaunchConfiguration
25 from launch_ros.actions import Node
26 from nav2_common.launch import ReplaceString
27 
28 
29 def generate_launch_description():
30  # Get the launch directory
31  bringup_dir = get_package_share_directory('nav2_bringup')
32 
33  # Create the launch configuration variables
34  namespace = LaunchConfiguration('namespace')
35  use_namespace = LaunchConfiguration('use_namespace')
36  rviz_config_file = LaunchConfiguration('rviz_config')
37  use_sim_time = LaunchConfiguration('use_sim_time')
38 
39  # Declare the launch arguments
40  declare_namespace_cmd = DeclareLaunchArgument(
41  'namespace',
42  default_value='navigation',
43  description=(
44  'Top-level namespace. The value will be used to replace the '
45  '<robot_namespace> keyword on the rviz config file.'
46  ),
47  )
48 
49  declare_use_namespace_cmd = DeclareLaunchArgument(
50  'use_namespace',
51  default_value='false',
52  description='Whether to apply a namespace to the navigation stack',
53  )
54 
55  declare_rviz_config_file_cmd = DeclareLaunchArgument(
56  'rviz_config',
57  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
58  description='Full path to the RVIZ config file to use',
59  )
60 
61  declare_use_sim_time_cmd = DeclareLaunchArgument(
62  'use_sim_time',
63  default_value='false',
64  description='Use simulation (Gazebo) clock if true')
65 
66  # Launch rviz
67  start_rviz_cmd = Node(
68  condition=UnlessCondition(use_namespace),
69  package='rviz2',
70  executable='rviz2',
71  arguments=['-d', rviz_config_file],
72  output='screen',
73  parameters=[{'use_sim_time': use_sim_time}],
74  )
75 
76  namespaced_rviz_config_file = ReplaceString(
77  source_file=rviz_config_file,
78  replacements={'<robot_namespace>': ('/', namespace)},
79  )
80 
81  start_namespaced_rviz_cmd = Node(
82  condition=IfCondition(use_namespace),
83  package='rviz2',
84  executable='rviz2',
85  namespace=namespace,
86  arguments=['-d', namespaced_rviz_config_file],
87  parameters=[{'use_sim_time': use_sim_time}],
88  output='screen',
89  remappings=[
90  ('/map', 'map'),
91  ('/tf', 'tf'),
92  ('/tf_static', 'tf_static'),
93  ('/goal_pose', 'goal_pose'),
94  ('/clicked_point', 'clicked_point'),
95  ('/initialpose', 'initialpose'),
96  ],
97  )
98 
99  exit_event_handler = RegisterEventHandler(
100  condition=UnlessCondition(use_namespace),
101  event_handler=OnProcessExit(
102  target_action=start_rviz_cmd,
103  on_exit=EmitEvent(event=Shutdown(reason='rviz exited')),
104  ),
105  )
106 
107  exit_event_handler_namespaced = RegisterEventHandler(
108  condition=IfCondition(use_namespace),
109  event_handler=OnProcessExit(
110  target_action=start_namespaced_rviz_cmd,
111  on_exit=EmitEvent(event=Shutdown(reason='rviz exited')),
112  ),
113  )
114 
115  # Create the launch description and populate
116  ld = LaunchDescription()
117 
118  # Declare the launch options
119  ld.add_action(declare_namespace_cmd)
120  ld.add_action(declare_use_namespace_cmd)
121  ld.add_action(declare_rviz_config_file_cmd)
122  ld.add_action(declare_use_sim_time_cmd)
123 
124  # Add any conditioned actions
125  ld.add_action(start_rviz_cmd)
126  ld.add_action(start_namespaced_rviz_cmd)
127 
128  # Add other nodes and processes we need
129  ld.add_action(exit_event_handler)
130  ld.add_action(exit_event_handler_namespaced)
131 
132  return ld