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

How to pass array from .launch file to .xacro file?

asked 2021-09-01 11:11:28 -0500

rbtcs1101 gravatar image

Hi all. I want to set the origin of the joint between my base_link frame and world frame using parameters or arguments like:

<origin xyz="$(arg pose_trans)" rpy="$(arg pose_rot)" />

and the arguments pose_trans and pose_rot are defined in my launch file. But when passing the arguments from .launch file to .xacro file like:

<param name="robot_description" command="$(find xacro)/xacro --inorder
                   '$(find <pkg>)/urdf/<file>.urdf.xacro'
                   pose_trans:=$(arg pose_trans)
                   pose_rot:=$(arg pose_rot)" />

Then I got the error:

xacro: error: expected exactly one input file as argument

Is there a way to pass arrays from the .launch file to .xacro file? Or how to set a parameter in the .launch file to change origin in .xacro file? Because I don't want to hardcode origin in the .xacro file.

Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-08-05 09:12:32 -0500

Mike Scheutzow gravatar image

The likely problem is that your arg values contain space characters (and note: this mechanism passes strings, not arrays):

command="$(find xacro)/xacro --inorder
                   '$(find <pkg>)/urdf/<file>.urdf.xacro'
                   pose_trans:=$(arg pose_trans)
                   pose_rot:=$(arg pose_rot)"

Your syntax expands to a command string like this:

/some/path/xacro --inorder /another/path/urdf/myfile.urdf.xacro pose_trans:=0 1 2 pose_rot:=3 4 5

Those digits with a space in front are the cause of the error message. One way to fix this in the launch file is to add single quotes around the entire arg expression, like this:

command="$(find xacro)/xacro --inorder
                   '$(find <pkg>)/urdf/<file>.urdf.xacro'
                   'pose_trans:=$(arg pose_trans)'
                   'pose_rot:=$(arg pose_rot)' "

The single-quotes will be stripped off before xacro sees them, so you shouldn't need to change anything in your xacro file.

edit flag offensive delete link more
0

answered 2023-08-04 13:11:09 -0500

swan gravatar image

I have discovered a working solution. It's a little bit of a hack.

In your launch file do:

<origin xyz="$(arg pose_trans)" rpy="'$(arg pose_rot)'" />

In your .xacro do:

<xacro:arg name="pose_rot" default=""/>
<xacro:property name="pose_rot" value="${$(arg pose_rot)}"/>

Why does this work?

xacro: error: expected exactly one input file as argument

This error is caused by the args being passed to the xacro command in plain text without quotes. By adding an apostrophe style quote '' in my version, you can pass your array string as usual. This is because roslaunch is providing your string arg to the command without quotes by default (e.g. xacro xyz.urdf.xacro pose_rot:=[1, 2, 3] instead of `xacro xyz.urdf.xacro pose_rot:='[1, 2, 3]'). An alternate solution here is to just leave out the spaces in your array that you are passing.

The second piece we make use of in the xacro is the yaml parser contained in the xacro documentation. Specifically, when we use ${} around a string that is formatted as an array, the xacro parses it like it does a yaml array.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-09-01 11:11:28 -0500

Seen: 162 times

Last updated: Aug 05 '23