How to get argument values in ROS2? [closed]

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

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 reopen merge delete

Closed for the following reason duplicate question by Yanray47
close date 2021-04-09 02:32:17.423728

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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )edit