Nav2 Navigation Stack - humble  humble
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 
38  # Declare the launch arguments
39  declare_namespace_cmd = DeclareLaunchArgument(
40  'namespace',
41  default_value='navigation',
42  description=('Top-level namespace. The value will be used to replace the '
43  '<robot_namespace> keyword on the rviz config file.'))
44 
45  declare_use_namespace_cmd = DeclareLaunchArgument(
46  'use_namespace',
47  default_value='false',
48  description='Whether to apply a namespace to the navigation stack')
49 
50  declare_rviz_config_file_cmd = DeclareLaunchArgument(
51  'rviz_config',
52  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
53  description='Full path to the RVIZ config file to use')
54 
55  # Launch rviz
56  start_rviz_cmd = Node(
57  condition=UnlessCondition(use_namespace),
58  package='rviz2',
59  executable='rviz2',
60  arguments=['-d', rviz_config_file],
61  output='screen')
62 
63  namespaced_rviz_config_file = ReplaceString(
64  source_file=rviz_config_file,
65  replacements={'<robot_namespace>': ('/', namespace)})
66 
67  start_namespaced_rviz_cmd = Node(
68  condition=IfCondition(use_namespace),
69  package='rviz2',
70  executable='rviz2',
71  namespace=namespace,
72  arguments=['-d', namespaced_rviz_config_file],
73  output='screen',
74  remappings=[('/map', 'map'),
75  ('/tf', 'tf'),
76  ('/tf_static', 'tf_static'),
77  ('/goal_pose', 'goal_pose'),
78  ('/clicked_point', 'clicked_point'),
79  ('/initialpose', 'initialpose')])
80 
81  exit_event_handler = RegisterEventHandler(
82  condition=UnlessCondition(use_namespace),
83  event_handler=OnProcessExit(
84  target_action=start_rviz_cmd,
85  on_exit=EmitEvent(event=Shutdown(reason='rviz exited'))))
86 
87  exit_event_handler_namespaced = RegisterEventHandler(
88  condition=IfCondition(use_namespace),
89  event_handler=OnProcessExit(
90  target_action=start_namespaced_rviz_cmd,
91  on_exit=EmitEvent(event=Shutdown(reason='rviz exited'))))
92 
93  # Create the launch description and populate
94  ld = LaunchDescription()
95 
96  # Declare the launch options
97  ld.add_action(declare_namespace_cmd)
98  ld.add_action(declare_use_namespace_cmd)
99  ld.add_action(declare_rviz_config_file_cmd)
100 
101  # Add any conditioned actions
102  ld.add_action(start_rviz_cmd)
103  ld.add_action(start_namespaced_rviz_cmd)
104 
105  # Add other nodes and processes we need
106  ld.add_action(exit_event_handler)
107  ld.add_action(exit_event_handler_namespaced)
108 
109  return ld