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

ROS2 python launch, how to forward launch arguments to xacro models

asked 2022-07-22 06:49:13 -0500

Markus Bader gravatar image

Hi

I like to use LaunchArgument to process my xacro file, which contains my robot model for gazebo and the robot_description for my robot_state_publisher.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-22 06:51:28 -0500

Markus Bader gravatar image

I found a solution using the OpaqueFunction.

Arguments are now evaluated and set in the create_robot_description which returns a LaunchConfiguration which than can be used as argument in the LaunchDescription.

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration, TextSubstitution
from launch_ros.actions import Node
from launch.actions import OpaqueFunction
from launch.actions import SetLaunchConfiguration

import xacro

def generate_launch_description():

    use_sim_time     = LaunchConfiguration('use_sim_time', default='true')
    namespace_arg    = DeclareLaunchArgument('namespace', default_value=TextSubstitution(text='r0'))
    robot_arg        = DeclareLaunchArgument('robot',     default_value=TextSubstitution(text='pioneer3dx'))
    X_launch_arg     = DeclareLaunchArgument('X',         default_value=TextSubstitution(text='0.0'))
    Y_launch_arg     = DeclareLaunchArgument('Y',         default_value=TextSubstitution(text='0.0'))
    Theta_launch_arg = DeclareLaunchArgument('Theta',     default_value=TextSubstitution(text='0.0'))

    models_dir = get_package_share_directory('tuw_gazebo_models') + '/models'



    def create_robot_description(context): 
        xacro_file = os.path.join(get_package_share_directory('tuw_gazebo_models'), 'models', context.launch_configurations['robot'], 'main.xacro')    
        assert os.path.exists(xacro_file), "The main.xacro doesnt exist in "+str(xacro_file)
        robot_description_config = xacro.process_file(xacro_file, 
            mappings={  "namespace": context.launch_configurations['namespace'], 
                        "models_dir": models_dir})
        robot_desc = robot_description_config.toxml()
        return [SetLaunchConfiguration('robot_desc', robot_desc)]

    create_robot_description_arg = OpaqueFunction(function=create_robot_description)

    return LaunchDescription([
        namespace_arg,
        robot_arg,
        X_launch_arg,
        Y_launch_arg,
        Theta_launch_arg,
        create_robot_description_arg,
        DeclareLaunchArgument(
            'use_sim_time',
            default_value='false',
            description='Use simulation (Gazebo) clock if true'),

        Node(package='tuw_gazebo_models', 
            name="publisher_robot", 
            executable='spawn_robot.py', 
            arguments=[LaunchConfiguration('robot_desc')],
            parameters=[{
                "X": LaunchConfiguration('X'),
                "Y": LaunchConfiguration('Y'),
                "Theta": LaunchConfiguration('Theta'),
                "namespace": LaunchConfiguration('namespace')}],
            output='screen'),
        Node(
            package="robot_state_publisher",
            executable="robot_state_publisher",
            name="robot_state_publisher",
            namespace=[LaunchConfiguration('namespace')],
            parameters=[{
                "robot_description": LaunchConfiguration('robot_desc')}],
            output="screen"),
    ])
edit flag offensive delete link more

Comments

I had done it calling xacro and storing the urdf file temporarily, this solution is neater!

martimorta gravatar image martimorta  ( 2022-07-22 07:49:06 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-07-22 06:49:13 -0500

Seen: 835 times

Last updated: Jul 22 '22