Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
launch_config_as_bool.py
1 # Copyright (c) 2025 Nishalan Govender
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 launch
16 from launch import LaunchContext
17 from launch.substitutions import LaunchConfiguration
18 from launch.utilities import perform_substitutions
19 
20 
21 class LaunchConfigAsBool(launch.Substitution):
22  """
23  Converts a LaunchConfiguration value into a normalized boolean string: 'true' or 'false'.
24 
25  Allows CLI arguments like 'True', 'true', '1', 'yes' and 'False', 'false', '0', 'no'.
26  Returns a string 'true' or 'false' for use in PythonExpression and IfCondition contexts.
27  """
28 
29  def __init__(self, name: str) -> None:
30  super().__init__()
31  self._config_config = LaunchConfiguration(name)
32 
33  def perform(self, context: LaunchContext) -> str:
34  value = perform_substitutions(context, [self._config_config])
35  if value.strip().lower() in ['true', '1', 'yes', 'on']:
36  return 'True'
37  return 'False'
38 
39  def describe(self) -> str:
40  return f'LaunchConfigAsBool({self._config.describe()})'