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

ROS2 string repr of parameter within launch file

asked 2021-07-08 06:48:03 -0500

definitive gravatar image

Is it possible to get string representation of parameter? LaunchConfiguration('arg_name') returns LaunchConfiguration object, while I need string to perform several actions.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-07-08 07:32:53 -0500

Kenji Miyake gravatar image

It's automatically evaluated in the launch phase, so generally, you don't have to care about it.

If you'd like to print the value for debugging, you can use OpaqueFunction.
Related: https://answers.ros.org/question/3226...

I put a minimal example here.

# false
$ ros2 launch sample.launch.py use_sim_time:=false
use_sim_time: false

$ ros2 param get /publisher use_sim_time 
Boolean value is: False

# true
$ ros2 launch sample.launch.py use_sim_time:=true
use_sim_time: true

$ ros2 param get /publisher use_sim_time 
Boolean value is: True

The launch file is:

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, OpaqueFunction
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node, SetParameter


def launch_setup(context, *args, **kwargs):
    use_sim_time = LaunchConfiguration("use_sim_time")
    print(f"use_sim_time: {use_sim_time.perform(context)}")
    set_use_sim_time = SetParameter(name="use_sim_time", value=use_sim_time)

    node = Node(
        package="examples_rclcpp_minimal_publisher",
        executable="publisher_lambda",
        name="publisher",
    )

    return [
        set_use_sim_time,
        node,
    ]


def generate_launch_description():
    return LaunchDescription(
        [
            DeclareLaunchArgument("use_sim_time", default_value="false"),
            OpaqueFunction(function=launch_setup),
        ]
    )
edit flag offensive delete link more

Comments

Thanks for providing the example @Kenji! It works perfectly.

M@t gravatar image M@t  ( 2021-08-23 21:17:11 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-07-08 06:48:03 -0500

Seen: 991 times

Last updated: Jul 08 '21