ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Use value of LaunchConfiguration in ROS2 launch file like a variable

asked 2022-08-30 09:20:38 -0500

Zeckurbo gravatar image

updated 2022-08-31 09:42:48 -0500

Hello,

I want to add two strings together, passed as LaunchConfiguration in my launch file. I have found many answers on that regarding a boolean and to use conditions instead, but is there something simillar to work with the value of the launch description directly in the code? (e.g. use the path to read a file )

EDIT: I found IfCondition(PythonExpression(["'", LaunchConfiguration('test'), "' == 'true']))._predicate_func(LaunchContext)).perform(LaunchContext) Maybe something simmilar can be used to get the real value.

launch_ros.parameter_descriptions.ParameterValue(value, value_type=str) could also be a possible option

Thanks

edit retag flag offensive close merge delete

Comments

You need to check opaquefunction for retrieving the value as string and manipulating according to your needs. Seems to be multiple stages in launch. You can read more about it here #q396345

pmuthu2s gravatar image pmuthu2s  ( 2022-08-31 03:25:00 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-08-31 09:36:33 -0500

Zeckurbo gravatar image

updated 2023-05-12 05:54:21 -0500

130s gravatar image

I found a way to do it :D Here is an example:

from launch import LaunchDescription, LaunchContext
from launch.actions import OpaqueFunction
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

def example_func(context: LaunchContext, arg1: LaunchConfiguration):
    value = context.perform_substitution(namespace)
    value +="/miracle/" # you can use it as a python variable now :D
    return Node(name=value)

def generate_launch_description():
    name = LaunchConfiguration('name')
    return LaunchDescription([
        OpaqueFunction(function=example_func, args=[namespace])
    ])

Normally, ROS2 will first build the context and afterwards execute everything, so it is not possible to get any value. With the OpaqueFunction() one can delay the execution of code, so that the context exists and the value can be retrieved. (See documentation)

I hope this example is understandable and can help someone.

edit flag offensive delete link more

Comments

Should

name = LaunchConfiguration('name')

be like this?

namespace = LaunchConfiguration('name')
130s gravatar image 130s  ( 2023-05-12 05:54:50 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-08-30 09:20:38 -0500

Seen: 3,187 times

Last updated: May 12 '23