Ask Your Question
0

ros2 argument in lauch.py

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 launch.py. How to write it with ros2 ?

<launch>
    <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">
    </include>
</launch>

I want to run the camer launch file with argument , not use the string(d435_launch.py). 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,
                            IncludeLaunchDescription)
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(
        'camera_type_tel', 
        default_value='d435',
        choices=['d435', 'astra_pro'],
        description='camera type')        
    camera_type_launch = camera_type_tel+'_launch.py'
    # Specify the actions
    camera_group = GroupAction([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(os.path.join(camera_driver_transfer_dir, 'launch',  camera_type_launch))) #this is error                
            #PythonLaunchDescriptionSource(os.path.join(camera_driver_transfer_dir, 'launch',  'd435_launch.py'))) #this is ok
    ])

    # Create the launch description and populate
    ld = LaunchDescription()
    ld.add_action(declare_camera_type_tel)
    ld.add_action(camera_group)

    return ld
edit retag flag offensive close merge delete

Comments

What type of error are you receiving? Please also add the information from the terminal. This line: camera_type_launch = camera_type_tel+'_launch.py' 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?

<launch>
    <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">
    </include>
</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: https://answers.ros.org/question/3963...

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

3 Answers

Sort by » oldest newest most voted
2

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: https://answers.ros.org/question/3963...)

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, '_launch.py')
camera_group = GroupAction([
    IncludeLaunchDescription(
        PythonLaunchDescriptionSource(camera_type_launch))
])

should work just fine.

edit flag offensive delete link more

Comments

you are right. that is what I want.

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

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: https://design.ros2.org/articles/roslaunch_xml.html, https://docs.ros.org/en/foxy/How-To-G...

So something like this should work:

<launch>   
     <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"/>    
</launch>
edit flag offensive delete link more
0

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 setup.py/CMakeLists.txt like there:

edit flag offensive delete link more

Comments

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

Stats

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

Seen: 61 times

Last updated: Jul 06