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

How to get argument values in ROS2?

asked 2021-04-07 01:51:05 -0600

Yanray47 gravatar image

Currently I'd like to pass a value from command line to a ROS2 argument like this.

msg_param = DeclareLaunchArgument('msg', default_value='True')

I wish to get the value directly in launch file, just like this:

ros2 launch my_pkg my_launch.launch.py msg:='False', then I could use the value of msg directly. That's False.

I have tried several methods like LaunchConfiguration, LaunchConfiguration.perform(), either there is an error or I couldn't get the actual value of 'msg'. It's always some formats like launch.substitutions.launch_configuration.LaunchConfiguration object at 0x7f5e7c302100

Thank you for sharing !

edit retag flag offensive close merge delete

Comments

1

Is there a tutorial of using

launch.substitution.LaunchConfiguration(name) -> string

I haven't figured out how to get the string value.

Yanray47 gravatar image Yanray47  ( 2021-04-07 02:01:13 -0600 )edit

I admit launch substitutions are still a bit fuzzy for me and I'd like to see a better tutorial, so hopefully someone else will chime in to confirm.

But while we wait for that, here's what I do for reference:

# create an empty launch description:
ld = LaunchDescription()

# define the launch argument and launch configuration for the variable in question:
my_param = LaunchConfiguration('my_param')
my_param_arg = DeclareLaunchArgument('my_param', default_value='val')

# then when you want to access the value at runtime, you can use the launch configuration (`my_param`); e.g.:
info_cmd = LogInfo(msg=my_param)
node_cmd = Node(package='my_package', executable='my_executable', output='screen', 'arguments=['--my-param', my_param])

# and of course, add everything to the launch description:
ld.add_action(my_param)
ld.add_action(my_param_arg)
ld.add_action(info_cmd)
ld.add_action(node_cmd)
shonigmann gravatar image shonigmann  ( 2021-04-07 18:30:38 -0600 )edit

I don't think there's any way of directly pulling the value out of a launch configuration, without evaluating it in another Launch object, which provides it with the launch context.

shonigmann gravatar image shonigmann  ( 2021-04-07 18:52:13 -0600 )edit

Thank you for your reply. I have tried to use LogInfo and I could see the value from terminal. However, how could I use that value in my launch file?

I also used LaunchConfiguration('my_param').perform(LaunchContext()) However, it says the value 'my_param' doesn't exist. Probably before it returns LaunchDescription, I'm not able to use it?

Yanray47 gravatar image Yanray47  ( 2021-04-07 21:12:03 -0600 )edit

For reference, can you give more information on what you're trying to do with the value?

shonigmann gravatar image shonigmann  ( 2021-04-08 11:12:01 -0600 )edit

I want to get a True or False value passed from command line. Initially, it's a LaunchConfiguration object. I thought I could not be used since I'd like to get the True or False directly not an object. After test, the value would be passed to my program instead of an object.

The problem is solved. I'll close this session.

Yanray47 gravatar image Yanray47  ( 2021-04-09 02:31:43 -0600 )edit

How did you manage to solve this?

ccpjBoss gravatar image ccpjBoss  ( 2022-03-07 09:11:57 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-22 15:09:01 -0600

130s gravatar image

I managed to realize taking an arg from a downstream launch, and set the value passed by the arg. Hint was in MetroRobots/rosetta_launch (that was announced in discourse.ros.org#29648). Haven't tried from CLI that the OP is asking about, but in theory there's no difference IINM.

Downstream launch (here in xml. Shouldn't matter as that's not the point IMO):

<launch>
    <include file="$(find raphael)/config/upstream.launch.py">
        <arg name="pizza_type" value="olives" />
    </include>
</launch>

raphael/config/upstream.launch.py

import launch
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
import launch_ros.actions

def generate_launch_description():
    return launch.LaunchDescription([
        DeclareLaunchArgument('pizza_type', default_value='mushrooms'),
        launch_ros.actions.Node(name='does_machines', package='donatello', executable='donatello_node',
                                parameters=[{'pizza': LaunchConfiguration('pizza_type')}]),
    ])

Note: DeclareLaunchArgument may not be required in this context. At least I haven't noticed any problem without it.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-04-07 01:51:05 -0600

Seen: 2,728 times

Last updated: Jun 22 '23