Ask Your Question

ros2 access current LaunchConfiguration

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

bergercookie gravatar image

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

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",
                                    description="Launch RQT?")
run_rviz_arg = DeclareLaunchArgument(name="run_rviz",
                                     description="Launch RVIZ?")

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

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

2 Answers

Sort by ยป oldest newest most voted

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

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(
        description="Launch RQT?")
    run_rviz_arg = DeclareLaunchArgument(
        description="Launch RVIZ?")

    rqt = Node(...,
    rviz = Node(...,

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

See also:

edit flag offensive delete link more


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:

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

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

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:

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



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

Seen: 562 times

Last updated: Aug 10