SMACC2
rviz_launch.py
Go to the documentation of this file.
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
15import os
16
17from ament_index_python.packages import get_package_share_directory
18
19from launch import LaunchDescription
20from launch.actions import DeclareLaunchArgument, EmitEvent, RegisterEventHandler
21from launch.conditions import IfCondition, UnlessCondition
22from launch.event_handlers import OnProcessExit
23from launch.events import Shutdown
24from launch.substitutions import LaunchConfiguration
25from launch_ros.actions import Node
26from nav2_common.launch import ReplaceString
27
28
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=(
43 "Top-level namespace. The value will be used to replace the "
44 "<robot_namespace> keyword on the rviz config file."
45 ),
46 )
47
48 declare_use_namespace_cmd = DeclareLaunchArgument(
49 "use_namespace",
50 default_value="false",
51 description="Whether to apply a namespace to the navigation stack",
52 )
53
54 declare_rviz_config_file_cmd = DeclareLaunchArgument(
55 "rviz_config",
56 default_value=os.path.join(bringup_dir, "rviz", "nav2_default_view.rviz"),
57 description="Full path to the RVIZ config file to use",
58 )
59
60 # Launch rviz
61 start_rviz_cmd = Node(
62 condition=UnlessCondition(use_namespace),
63 package="rviz2",
64 executable="rviz2",
65 name="rviz2",
66 arguments=["-d", rviz_config_file],
67 output="screen",
68 )
69
70 namespaced_rviz_config_file = ReplaceString(
71 source_file=rviz_config_file, replacements={"<robot_namespace>": ("/", namespace)}
72 )
73
74 start_namespaced_rviz_cmd = Node(
75 condition=IfCondition(use_namespace),
76 package="rviz2",
77 executable="rviz2",
78 name="rviz2",
79 namespace=namespace,
80 arguments=["-d", namespaced_rviz_config_file],
81 output="screen",
82 remappings=[
83 ("/tf", "tf"),
84 ("/tf_static", "tf_static"),
85 ("/goal_pose", "goal_pose"),
86 ("/clicked_point", "clicked_point"),
87 ("/initialpose", "initialpose"),
88 ],
89 )
90
91 exit_event_handler = RegisterEventHandler(
92 condition=UnlessCondition(use_namespace),
93 event_handler=OnProcessExit(
94 target_action=start_rviz_cmd, on_exit=EmitEvent(event=Shutdown(reason="rviz exited"))
95 ),
96 )
97
98 exit_event_handler_namespaced = RegisterEventHandler(
99 condition=IfCondition(use_namespace),
100 event_handler=OnProcessExit(
101 target_action=start_namespaced_rviz_cmd,
102 on_exit=EmitEvent(event=Shutdown(reason="rviz exited")),
103 ),
104 )
105
106 # Create the launch description and populate
107 ld = LaunchDescription()
108
109 # Declare the launch options
110 ld.add_action(declare_namespace_cmd)
111 ld.add_action(declare_use_namespace_cmd)
112 ld.add_action(declare_rviz_config_file_cmd)
113
114 # Add any conditioned actions
115 ld.add_action(start_rviz_cmd)
116 ld.add_action(start_namespaced_rviz_cmd)
117
118 # Add other nodes and processes we need
119 ld.add_action(exit_event_handler)
120 ld.add_action(exit_event_handler_namespaced)
121
122 return ld
def generate_launch_description()
Definition: rviz_launch.py:29