Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
collision_monitor_node.launch.py
1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2022 Samsung R&D Institute Russia
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import os
18 
19 from ament_index_python.packages import get_package_share_directory
20 
21 from launch import LaunchDescription
22 from launch.actions import DeclareLaunchArgument
23 from launch.substitutions import LaunchConfiguration
24 from launch_ros.actions import Node
25 from launch_ros.descriptions import ParameterFile
26 from nav2_common.launch import RewrittenYaml
27 
28 
29 def generate_launch_description():
30  # Environment
31  package_dir = get_package_share_directory('nav2_collision_monitor')
32 
33  # Constant parameters
34  lifecycle_nodes = ['collision_monitor']
35  autostart = True
36 
37  # Launch arguments
38  # 1. Create the launch configuration variables
39  namespace = LaunchConfiguration('namespace')
40  use_sim_time = LaunchConfiguration('use_sim_time')
41  params_file = LaunchConfiguration('params_file')
42 
43  # 2. Declare the launch arguments
44  declare_namespace_cmd = DeclareLaunchArgument(
45  'namespace',
46  default_value='',
47  description='Top-level namespace')
48 
49  declare_use_sim_time_cmd = DeclareLaunchArgument(
50  'use_sim_time',
51  default_value='True',
52  description='Use simulation (Gazebo) clock if true')
53 
54  declare_params_file_cmd = DeclareLaunchArgument(
55  'params_file',
56  default_value=os.path.join(package_dir, 'params', 'collision_monitor_params.yaml'),
57  description='Full path to the ROS2 parameters file to use for all launched nodes')
58 
59  # Create our own temporary YAML files that include substitutions
60  param_substitutions = {
61  'use_sim_time': use_sim_time}
62 
63  configured_params = ParameterFile(
64  RewrittenYaml(
65  source_file=params_file,
66  root_key=namespace,
67  param_rewrites=param_substitutions,
68  convert_types=True),
69  allow_substs=True)
70 
71  # Nodes launching commands
72  start_lifecycle_manager_cmd = Node(
73  package='nav2_lifecycle_manager',
74  executable='lifecycle_manager',
75  name='lifecycle_manager',
76  output='screen',
77  emulate_tty=True, # https://github.com/ros2/launch/issues/188
78  parameters=[{'use_sim_time': use_sim_time},
79  {'autostart': autostart},
80  {'node_names': lifecycle_nodes}])
81 
82  start_collision_monitor_cmd = Node(
83  package='nav2_collision_monitor',
84  executable='collision_monitor',
85  output='screen',
86  emulate_tty=True, # https://github.com/ros2/launch/issues/188
87  parameters=[configured_params])
88 
89  ld = LaunchDescription()
90 
91  # Launch arguments
92  ld.add_action(declare_namespace_cmd)
93  ld.add_action(declare_use_sim_time_cmd)
94  ld.add_action(declare_params_file_cmd)
95 
96  # Node launching commands
97  ld.add_action(start_lifecycle_manager_cmd)
98  ld.add_action(start_collision_monitor_cmd)
99 
100  return ld