0
$\begingroup$

I have a boolean launch argument as follows

    use_custom_controller   =   LaunchConfiguration("use_custom_controller")
    launch_description.add_action(
        DeclareLaunchArgument(
            "use_custom_controller", 
            default_value   =   "false", 
            description     =   "If True loads custom controller else diff_drive_controller"
        ), 
    )

I would like to use this argument as a conditional expression to change the value of a string variable called default_controller.

For example,

if use_custom_controller: 
    default_controller = "custom_controller"
else: 
    default_controller = "diff_drive_controller"

How can I achieve this in a launch script of ros2 ?

$\endgroup$

1 Answer 1

0
$\begingroup$

The simpler approach is to have two spawners with different arguments and your boolean used in the launch.conditions.IfCondition.

Otherwise you have to use opaque functions

from launch.actions import OpaqueFunction
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration

def evaluate_launchconfig(context, *args, **kwargs):
    use_custom_controller   =   LaunchConfiguration("use_custom_controller")
    if use_custom_controller.evaluate(context): 
        default_controller = "custom_controller"
    else: 
        default_controller = "diff_drive_controller"    
    
    return Node(
        package="controller_manager",
        executable="spawner",
        arguments=[default_controller, "-c", "/controller_manager"],
    )

def generate_launch_description():
        OpaqueFunction(function=evaluate_launchconfig),
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.