0
$\begingroup$

Rosanswers logo

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?


Originally posted by destogl on ROS Answers with karma: 877 on 2021-03-11

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

Originally posted by vinny with karma: 291 on 2021-03-11

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by destogl on 2021-03-12:
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....

Comment by vinny on 2021-03-12:
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.

Comment by vinny on 2021-03-12:
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

Comment by destogl on 2021-03-25:
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.

$\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.