Ask Your Question
0

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",
                                    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

1 Answer

Sort by ยป oldest newest most voted
1

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(
        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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 105 times

Last updated: May 08