Robotics StackExchange | Archived questions

How to get argument values in ROS2?

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 mypkg mylaunch.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 !

Asked by Yanray47 on 2021-04-07 01:51:05 UTC

Comments

Is there a tutorial of using

launch.substitution.LaunchConfiguration(name) -> string

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

Asked by Yanray47 on 2021-04-07 02:01:13 UTC

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)

Asked by shonigmann on 2021-04-07 18:30:38 UTC

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.

Asked by shonigmann on 2021-04-07 18:52:13 UTC

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?

Asked by Yanray47 on 2021-04-07 21:12:03 UTC

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

Asked by shonigmann on 2021-04-08 11:12:01 UTC

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.

Asked by Yanray47 on 2021-04-09 02:31:43 UTC

How did you manage to solve this?

Asked by ccpjBoss on 2022-03-07 10:11:57 UTC

Answers

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.

Asked by 130s on 2023-06-22 15:09:01 UTC

Comments