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

Edit environmental variable with ROS2 python launch file

asked 2021-12-01 04:14:31 -0500

fnax8902 gravatar image

Hello, I want to edit a environmental variable using my ros2 python launch file. I launch mutiple nodes so setting it once is not an option, as the variable will have to change for each node. Is it possible to set in in the Node description?

test = Node(
        package="test",
        executable="test",
        namespace=namespace,
        name="test_node",
        output="screen",
        emulate_tty=True,
        parameters=[params],
    )

return LaunchDescription([
    test
])
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-01-09 03:35:33 -0500

updated 2023-01-16 04:56:01 -0500

it can be done by using additional_envex. from github https://github.com/ros2/launch/blob/r...

    .. doctest::
        >>> ld = LaunchDescription([
        ...     ExecuteProcess(
        ...         cmd=['my_cmd'],
        ...         additional_env={'env_variable': 'env_var_value'},
        ...     ),
        ... ])
    .. code-block:: xml
        <launch>
            <executable cmd="my_cmd">
                <env name="env_variable" value="env_var_value"/>
            </executable>
        </launch>
"""

is look to be posibale in xml luanchfiles cant fine a way in python

https://design.ros2.org/articles/rosl...

edit flag offensive delete link more
0

answered 2021-12-01 04:27:50 -0500

fnax8902 gravatar image

Okay, I found the solution:

from launch.actions import SetEnvironmentVariable

return LaunchDescription([
    SetEnvironmentVariable(name='TEST', value='whatever'),
    Node(
        package="test",
        executable="test",
        namespace=namespace,
        name="test_node",
        output="screen",
        emulate_tty=True,
    )
])
edit flag offensive delete link more

Comments

Nevermind, It doesn't set the environmental variable for the specific node. All nodes will have the value of the last launch description called

fnax8902 gravatar image fnax8902  ( 2021-12-01 05:08:43 -0500 )edit
1

Seems like a correct solution to me, I tried it out. Run print(os.environ.get("TEST")) in your test_node, it prints 'whatever', remove the SetEnvironmentVariable, run again and it should print None. What's your issue?

tnajjar gravatar image tnajjar  ( 2022-01-10 16:27:10 -0500 )edit

it might work with nested LaunchDestriptions

lucasrj4 gravatar image lucasrj4  ( 2023-01-09 05:44:58 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-12-01 04:14:31 -0500

Seen: 1,863 times

Last updated: Jan 16 '23