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

How to get argument from launch-file not used in a Node ?

asked 2023-06-26 06:21:54 -0600

SébastienL gravatar image

Hi,

I read a lot of questions about arguments and launch files and did not find something related to my case (maybe this one https://answers.ros.org/question/3755..., but I can't get the value of the LaunchConfiguration)

So here is my pb, I want an argument be used in the launch file to loop on IncludeLaunchDescription instanciation -> I have a yml file working for 1 drone, and i want to use many drones.

def generate_launch_description():
    # ...
    arg_n_drones = DeclareLaunchArgument(
        "N",
        default_value="1",
        description="number of drones in the simulation."
    )  # this does not seem to be enough to have the argument visible with the --show-args option

    N = 1  # I would like something based on arg_n_drones
    l_include_drone_spawn = []
    for i in range(1,N+1):
        l_include_drone_spawn.append(
            IncludeLaunchDescription(
                "launch/spawn_drone.yml",
            )
        )

    return LaunchDescription([
        # ...
        *l_include_drone_spawn,
    ])

Any help appreciated,

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-06-27 06:44:26 -0600

danzimmerman gravatar image

updated 2023-06-27 07:11:48 -0600

First you need to add your declared argument to the final LaunchDescription that you return:

return LaunchDescription([arg_n_drones, *l_include_drown_spawn])

To use it in your launch file as a normal Python variable, I believe you must use OpaqueFunction:

https://answers.ros.org/question/4158...

https://answers.ros.org/question/4164...

That second answer includes a concrete example of using a declared launch argument to do N things. There are a couple of ways to access its string value. You can use a LaunchConfiguration object inside your OpaqueFunction and use LaunchConfiguration.perform(context):

https://github.com/danzimmerman/dz_la...

or you can access it by name from the context.launch_configurations dictionary:

https://github.com/danzimmerman/dz_la...

(LaunchConfiguration.perform(context) accesses this internally, see here).

edit flag offensive delete link more

Comments

thank you, it was missing indeed. With the correction, the arg is in the output of ros2 launch -s. Any idea on how to get its value ?

context = LaunchContext(argv=sys.argv)
arg_n_drones.visit(context)  # return None
SébastienL gravatar image SébastienL  ( 2023-06-27 07:10:50 -0600 )edit

Updated my answer with info there. I am not sure there's any way besides OpaqueFunction to get a valid context. It's the only way I know.

Creating a new one in generate_launch_description() seems to give an empty dict for context.launch_configurations and a subsequent error:

https://github.com/danzimmerman/dz_la...

danzimmerman gravatar image danzimmerman  ( 2023-06-27 07:33:02 -0600 )edit

Well it seems my problem is solved thank you. I'd already tried the solution with OpaqueFunction but i failed at the time. I suppose that forgetting to put my argument in the LaunchDescription was a prerequisite for testing the other solutions. Your example files are very good, simple and clear !

SébastienL gravatar image SébastienL  ( 2023-06-27 09:09:54 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2023-06-26 06:21:54 -0600

Seen: 516 times

Last updated: Jun 27 '23