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

ros2 access current LaunchConfiguration

asked 2019-05-07 06:36:29 -0600

bergercookie gravatar image

updated 2019-05-08 16:42:32 -0600

William gravatar image

I'm in the middle of migrating a ROS1 app of mine to ROS2 and I'm currently trying to convert a .launch file to a python script.

I want to be able to pass a command line argument to the script and then get its value in the generate_launch_description call.

This is how I currently pass extra arguments to the LaunchDescription:

run_rqt_arg = DeclareLaunchArgument(name="run_rqt",
                                    default_value="True",
                                    description="Launch RQT?")
run_rviz_arg = DeclareLaunchArgument(name="run_rviz",
                                     default_value="True",
                                     description="Launch RVIZ?")

if run_rqt:
    rqt = Node(...)
    l.append(rqt)
if run_rviz:
    rviz = Node(...)
    l.append(rviz)

return LaunchDescription(l, run_rqt, run_rviz)

I'm running the launch script via ros2 launch ...

Is the passing of extra arguments handled correctly? How do I fetch the values of these cli args at runtime?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2019-05-08 16:53:57 -0600

William gravatar image

The launch argument's value is stored in the "Launch Configurations", and those can be accessed using a substitution called launch.substitution.LaunchConfiguration(name) -> string.

If you want to do something conditionally based on this, I'd recommend using a launch.Condition which may be passed to any launch.Action, e.g.:

def generate_launch_description():
    run_rqt_arg = DeclareLaunchArgument(
        name="run_rqt",
        default_value="True",
        description="Launch RQT?")
    run_rviz_arg = DeclareLaunchArgument(
        name="run_rviz",
        default_value="True",
        description="Launch RVIZ?")

    rqt = Node(...,
        condition=launch.conditions.IfCondition(launch.LaunchConfiguration("run_rqt")))
    rviz = Node(...,
        condition=launch.conditions.IfCondition(launch.LaunchConfiguration("run_rviz")))

    return launch.LaunchDescription([run_rqt_arg, run_rviz_arg, run_rqt, run_rviz])

See also:

edit flag offensive delete link more

Comments

Hi William, is this feature (launch.substitution.LaunchConfiguration(name) -> string) still supported in the current dashing version? I have been trying to use launch.substitutions.LaunchConfiguration to access my arguments but it does not return a string. Any ideas on that?

I have also opened a question for this: https://answers.ros.org/question/3407...

relffok gravatar image relffok  ( 2019-12-26 13:16:28 -0600 )edit

This is not supported anymore indeed.

achille gravatar image achille  ( 2021-03-23 18:32:28 -0600 )edit

So this answer is now out-of-date?

M@t gravatar image M@t  ( 2021-07-05 18:52:27 -0600 )edit
9

answered 2020-08-10 19:04:50 -0600

I was able to evaluate launch arguments in a custom function outside of the LaunchDescription by using the OpaqueFunction launch action feature, see this link:

https://github.com/jrgnicho/collabora...

edit flag offensive delete link more

Comments

1

I had the same issue of evaluating launch arguments. After a bit of struggle, the OpaqueFunction worked better than expected. My code is so much cleaner. Thank you for this!

ROS2 launch package definitely needs some documentation. It is a complete mystery right now. If anyone has questions on how I managed to use OpaqueFunction, I can help you out.

spoluri gravatar image spoluri  ( 2021-08-26 15:01:45 -0600 )edit

Give @jrgnicho an award

ccpjBoss gravatar image ccpjBoss  ( 2022-03-10 05:20:01 -0600 )edit

Thanks for the tip @jrgnicho, I much prefer your workaround.

chutsu gravatar image chutsu  ( 2023-04-16 10:21:59 -0600 )edit
2

answered 2021-07-10 08:56:49 -0600

RobotDreams gravatar image

This is an old question that is still being asked due to lack of examples in the ROS2 tutorial. The best answer I have seen yet is by @djchopp

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

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2019-05-07 06:36:29 -0600

Seen: 8,217 times

Last updated: Jul 10 '21