Ask Your Question

ros2 argument in

asked 2022-07-01 04:37:21 -0500

drink gravatar image

updated 2022-07-06 21:06:00 -0500

I have a launch file with ROS1 as below. I want to wirte it with ros2 How to write it with ros2 ?

    <arg name="camera_type_tel" default="astrapro" doc="camera type [astrapro, astra,d435]"/>
    <include file="$(find camera_driver_transfer)/launch/$(arg camera_type_tel).launch">

I want to run the camer launch file with argument , not use the string( I wirte it as below: but when I run it ,it is error! how can I do ?

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import (DeclareLaunchArgument, GroupAction,
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    # Get the launch directory
    camera_driver_transfer_dir = get_package_share_directory('camera_driver_transfer')
    camera_type_tel = LaunchConfiguration('camera_type_tel')

    declare_camera_type_tel = DeclareLaunchArgument(
        choices=['d435', 'astra_pro'],
        description='camera type')        
    camera_type_launch = camera_type_tel+''
    # Specify the actions
    camera_group = GroupAction([
            PythonLaunchDescriptionSource(os.path.join(camera_driver_transfer_dir, 'launch',  camera_type_launch))) #this is error                
            #PythonLaunchDescriptionSource(os.path.join(camera_driver_transfer_dir, 'launch',  ''))) #this is ok

    # Create the launch description and populate
    ld = LaunchDescription()

    return ld
edit retag flag offensive close merge delete


What type of error are you receiving? Please also add the information from the terminal. This line: camera_type_launch = camera_type_tel+'' looks incorrect to me, where did you find such use of DeclareLaunchArgument as a string to concatenate? Are you sure it is a string? The later error may come from this.

ljaniec gravatar image ljaniec  ( 2022-07-03 17:09:41 -0500 )edit

yes, you are right. I want to get the result of DeclareLaunchArgument as sting type.But it seens it has not. I don't know how to do it. Have you a good way to implement this feature on ROS2?

    <arg name="camera_type_tel" default="astrapro" doc="camera type [astrapro, astra,d435]"/>
    <include file="$(find camera_driver_transfer)/launch/$(arg camera_type_tel).launch">
drink gravatar image drink  ( 2022-07-04 02:37:20 -0500 )edit

I think this question answers how you can get it as a string:

ljaniec gravatar image ljaniec  ( 2022-07-04 04:39:36 -0500 )edit

3 Answers

Sort by » oldest newest most voted

answered 2022-07-06 10:18:07 -0500

ChuiV gravatar image

We need to always remember that launch works in 2 sort of steps: Describe and Execution. (See my response here:

But since you plan to use the substitution object as an argument to something that accepts substitutions, you can combine the strings and substitution as a tuple (Though you can't do things like os.path.join with substitutions). So changing to use

camera_type_launch = (camera_driver_transfer_dir, '/launch/', camera_type_tel, '')
camera_group = GroupAction([

should work just fine.

edit flag offensive delete link more


you are right. that is what I want.

drink gravatar image drink  ( 2022-07-06 21:04:41 -0500 )edit

answered 2022-07-04 05:07:02 -0500

GeorgNo gravatar image

updated 2022-07-04 05:13:57 -0500

ljaniec gravatar image

You can still write launch files using XML syntax:,

So something like this should work:

     <arg name="camera_type_tel" default="astrapro" doc="camera type [astrapro, astra,d435]"/>     
    <include file="$(find-pkg-share camera_driver_transfer)/launch/$(arg camera_type_tel).launch"/>    
edit flag offensive delete link more

answered 2022-07-01 07:11:09 -0500

ljaniec gravatar image

The easiest route is to use an example launch files from the ROS2 tutorials:

If you have your own package, you can base your dependencies in like there:

edit flag offensive delete link more


update the question

drink gravatar image drink  ( 2022-07-01 22:04:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-07-01 04:37:21 -0500

Seen: 61 times

Last updated: Jul 06