Ask Your Question
1

Select config file based upon launch argument

asked 2021-11-01 11:58:48 -0500

nealtanner gravatar image

I am trying to select between two configuration files based upon a boolean launch file argument. The example below works, but it seems awfully complicated. Is there a simpler way to do this?

def generate_launch_description():
    red_arg = launch.actions.DeclareLaunchArgument('red', default_value='false')
    launch_description = launch.LaunchDescription([red_arg])
    config_file_path = os.path.join(get_package_share_directory('my_package'), 'config')
    config_file = launch.substitutions.PythonExpression(
        ['"', config_file_path, '" + ("/red.yml" if "',
         launch.substitutions.LaunchConfiguration("color"), '".lower() == "true" else "/blue.yml")'])
    launch_description.add_action(launch.add_action(launch.actions.LogInfo(
        msg=('Config file: ', config_file)

    my_node = Node(package='my_package', executable='my_executable.py', parameters=[config_file])
    launch_description.add_action(my_node)

    return launch_description

For readability, the cryptic PythonExpression evaluates to

"/opt/ros_ws/install/my_package/share/my_package/config" + ("/red.yml" if "<launch arg>".lower() == "true" else "/blue.yml")
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-16 13:09:03 -0500

ChuiV gravatar image

Maybe not the best solution, but might be easier to read. You could use the OpaqueFunction Action to wrap creating your Node. Something like (untested, but the idea will work):

...
from launch.actions import OpaqueFunction

def start_my_executable(context, *args, **kwargs) -> List[LaunchDescriptionEntity]:
    package_path = get_package_share_directory('my_package')
    color_param = context.perform_substitution(LaunchConfiguration('color'))
    config_file_name = 'red.yml' if color_param else 'blue.yml'
    config_file = os.path.join(package_path, 'config', config_file_name)
    return [Node(... , parameters=[config_file])]

def generate_launch_description():
    ...
    launch_description.add_action(OpaqueFunction(function=start_my_executable)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2021-11-01 11:58:48 -0500

Seen: 24 times

Last updated: Nov 16 '21