launch_utils.py
Go to the documentation of this file.
1 # Copyright 2023 Ekumen, Inc.
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 from typing import List
16 
17 from launch import LaunchDescription
18 from launch import LaunchDescriptionSource
19 from launch.actions import DeclareLaunchArgument
20 from launch.actions import IncludeLaunchDescription
21 from launch.actions import OpaqueFunction
22 import launch.logging
23 from launch.utilities import perform_substitutions
24 from launch.substitutions import LaunchConfiguration
25 
26 from launch_ros.actions import Node
27 from launch_ros.descriptions import Parameter
28 
29 import yaml
30 
31 
32 def with_launch_arguments(arguments_to_declare: List[DeclareLaunchArgument]):
33  """Decorate generate_launch_description function to resolve launch arguments early."""
34 
35  def decorator(get_launch_description_f):
36  def wrapper():
37  launch_configurations_for_arguments = [
38  LaunchConfiguration(arg.name) for arg in arguments_to_declare
39  ]
40 
41  def action(context):
42  resolved_args = {
43  perform_substitutions(
44  context, config.variable_name
45  ): config.perform(context)
46  for config in launch_configurations_for_arguments
47  }
48  return [
49  IncludeLaunchDescription(
50  LaunchDescriptionSource(
51  get_launch_description_f(**resolved_args)
52  )
53  )
54  ]
55 
56  return LaunchDescription(
57  [
58  *arguments_to_declare,
59  OpaqueFunction(function=action),
60  ]
61  )
62 
63  return wrapper
64 
65  return decorator
66 
67 
69  *, params_file, name, namespace='/', extra_params={}, **kwargs
70 ):
71  """
72  Declare arguments from param file.
73 
74  Currently ROS 2 argument handling is a bit broken ...
75  If you specify a parameter file, there's no way to override the parameter value, except with
76  other parameter file.
77 
78  Some magic to be able to easily override parameter values specified in a file from launch ...
79  """
80  full_name = f'{namespace}/{name}'
81  full_name_without_starting_slash = full_name.lstrip('/')
82  full_name_with_slash = f'/{full_name_without_starting_slash}'
83  with open(params_file) as params_file:
84  params_file_dict = yaml.safe_load(params_file)
85  node_name_key = None
86  if full_name_with_slash in params_file_dict:
87  node_name_key = full_name_with_slash
88  if full_name_without_starting_slash in params_file_dict:
89  node_name_key = full_name_without_starting_slash
90  if node_name_key is None:
91  launch.logging.get_logger().warning(
92  f"parameters file has no parameters for node '{full_name_without_starting_slash}'"
93  )
94  return [Node(name=name, namespace=namespace, **kwargs)]
95  entries_for_node = params_file_dict[node_name_key]
96  if 'ros__parameters' not in entries_for_node:
97  launch.logging.get_logger().warning(
98  f"parameters file does not have a 'ros__parameters' entry for node "
99  f"'{full_name_without_starting_slash}'"
100  )
101  return [Node(name=name, namespace=namespace, **kwargs)]
102  params_dict = entries_for_node['ros__parameters']
103  params_dict.update(extra_params)
104  actions = []
105  parameters = []
106  for param_name, param_value in params_dict.items():
107  actions.append(
108  DeclareLaunchArgument(
109  name=f'{full_name_without_starting_slash}.{param_name}',
110  default_value=str(param_value),
111  )
112  )
113  parameters.append(
114  Parameter(
115  name=param_name,
116  value=LaunchConfiguration(
117  f'{full_name_without_starting_slash}.{param_name}'
118  ),
119  )
120  )
121  actions.append(
122  Node(name=name, namespace=namespace, parameters=parameters, **kwargs)
123  )
124  return actions
beluga_example.launch_utils.get_node_with_arguments_declared_from_params_file
def get_node_with_arguments_declared_from_params_file(*params_file, name, namespace='/', extra_params={}, **kwargs)
Definition: launch_utils.py:68
beluga_example.launch_utils.with_launch_arguments
def with_launch_arguments(List[DeclareLaunchArgument] arguments_to_declare)
Definition: launch_utils.py:32


beluga_example
Author(s):
autogenerated on Tue Jul 16 2024 03:00:09