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

[ros2] Launch file arguments, subsititutions, xacro and node parameters

asked 2021-03-11 10:58:35 -0500

updated 2021-03-12 06:10:59 -0500

Hi all,

I am having a "fight" with launch-file argument in ROS2 for a few days already. The scenario is the following:

  1. Set parameter when calling launch file.
  2. Use it as argument for xacro file holding robot description.
  3. Use the output of processed file (URDF) as parameters for a node.

I managed to find a solution for the first two steps, but now I have an issue with the third one. The example launch file is:

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import Command, LaunchConfiguration, PathJoinSubstitution

from launch_ros.actions import Node

def generate_launch_description():
    declared_arguments = []
    declared_arguments.append(DeclareLaunchArgument(
        'robot_param',
        description='Some xacro robot param.'))

    robot_param = LaunchConfiguration('robot_param')

    robot_description_content = Command([
        PathJoinSubstitution([get_package_prefix('xacro'), 'bin', 'xacro']),
        ' ',
        PathJoinSubstitution([get_package_share_directory('ur_description'), 'urdf', 'ur.xacro']),
        ' ',
        'robot_param:=', robot_param,
        ])

    robot_description = {'robot_description': robot_description_content}

    robot_state_pub_node = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        output='screen',
        parameters=[robot_description]
    )

The Issue here is that "robot_description_content" is a string, and it cannot be used directly by the node. Only file paths are allowed. On the other hand, I cannot wrap this into a parameter map. Then I get a mapping issue.

What is the proper way to use this? Am I missing something with substitutions?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2021-03-11 11:55:21 -0500

vinny gravatar image

updated 2021-03-12 09:42:46 -0500

edit 2

This is the full example launch file I was able to use to pass arguments to XACRO

import launch
from launch.substitutions import Command, LaunchConfiguration
import launch_ros
import os

def generate_launch_description():
pkg_share = launch_ros.substitutions.FindPackageShare(package='test_robot').find('test_robot')
default_model_path = os.path.join(pkg_share, 'urdf/test_robot.xacro')
default_with_gripper = 'False'

robot_state_publisher_node = launch_ros.actions.Node(
    package='robot_state_publisher',
    executable='robot_state_publisher',
    parameters=[{'robot_description': Command(['xacro ', LaunchConfiguration('model'), ' with_gripper:=', LaunchConfiguration('spawn_with_gripper')])}]
)
return launch.LaunchDescription([
    launch.actions.DeclareLaunchArgument(name='model', default_value=default_model_path,
                                        description='Absolute path to robot urdf file'),
    launch.actions.DeclareLaunchArgument(name='spawn_with_gripper', default_value=default_with_gripper,
                                description='Spawn xacro with gripper?'),
    robot_state_publisher_node,
])

edit: I did not understand the full question, the below is not a satisfactory answer

I think the robot_param:= is where you are having the issue.

You should be able to do the following directly:

    robot_state_publisher_node = launch_ros.actions.Node(
    package='robot_state_publisher',
    executable='robot_state_publisher',
    parameters=[{'robot_description': Command(['xacro ', <path_to_your_xacro_here>])}]
)

With the following imports

import launch
from launch.substitutions import Command, LaunchConfiguration
import launch_ros
import os
edit flag offensive delete link more

Comments

Hi vinny, thanks for your answer. I extended my question with the imports. Although this was not an issue.

I don't understand your answer: "I think the robot_param:= is where you are having the issue.". It there another way to set an argument in the xacro file? Did you test your approach? For me either way is not working....

destogl gravatar image destogl  ( 2021-03-12 06:21:54 -0500 )edit

Oh I was not implying your imports were the issue , but regardless I originally misunderstood your question.

Can you please share how you are adding to the LaunchDescription?

I was able to pass arguments to an xacro using launch in the following file, but I am not sure what the full differences against your file are since I don't have the full launch file.

vinny gravatar image vinny  ( 2021-03-12 09:36:23 -0500 )edit

I have edited the above answer for an example launch file I was able to get working. I was having trouble getting it to fit in the comments

vinny gravatar image vinny  ( 2021-03-12 09:43:23 -0500 )edit

You answer is valid thanks! But actuall mistake was that I had comments in following format in my xacro file: "SOMETHING: xyz" and this was recognized as some mapping.

destogl gravatar image destogl  ( 2021-03-25 08:52:17 -0500 )edit
0

answered 2021-03-12 09:47:07 -0500

avzmpy gravatar image

This might be what you're looking for:

import os
import xacro

def generate_launch_description():
  ...
  robot_urdf = os.path.join(get_package_share_directory(
        'awesome_description_pkg'), 'urdf', 'robot.xacro')
  robot_urdf = xacro.process_file(robot_urdf)
  robot_description = {'robot_description': robot_urdf.toxml()}
  robot_state_pub_node = Node(
      package='robot_state_publisher',
      executable='robot_state_publisher',
      output='screen',
      parameters=[robot_description]
  )

If not, robot state publisher can take urdf file as an argument, so you could process the xacro file with xacro python module, save it and pass that file name as argument. Something along the lines:

robot_state_pub_node = Node(
    package='robot_state_publisher',
    executable='robot_state_publisher',
    output='screen',
    arguments=[robot_urdf]
)

Where robot_urdf is preprocessed xacro file i.e. urdf description of the robot.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-03-11 10:58:35 -0500

Seen: 5,362 times

Last updated: Mar 12 '21