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

Set xacro arguments when parsing a file using Python (ROS2)

asked 2021-08-26 17:43:18 -0600

shonigmann gravatar image

Is it possible to set arguments when parsing a file using the python xacro library?

In ROS2, a number of packages parse xacro files directly in the launch file using the python xacro library, with something along the lines of:

import xacro, os

xacro_file = "test.urdf.xacro"
doc = xacro.parse(open(xacro_file))
xacro.process_doc(doc)
robot_description_config = doc.toxml()
robot_description = {'robot_description': robot_description_config}

For some context, say I have a file, test.urdf.xacro that takes an argument named input:

<?xml version="1.0"?>
<robot name="test" xmlns:xacro="http://ros.org/wiki/xacro">
    <xacro:arg name="input" defaut="default"/>
    <link name="$(arg input)"/>
</robot>

Using the python code above, the argument would be the default value and the link would be named default. But how can you pass in a custom value?

Using the command line interface, f I wanted to parse the file with some custom value, I could simply type the following in the terminal:

$ xacro test.urdf.xacro input:="new_input"

and get an output where my link is appropriately named new_input:

<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- |    This document was autogenerated by xacro from test.urdf.xacro                | -->
<!-- |    EDITING THIS FILE BY HAND IS NOT RECOMMENDED                                 | -->
<!-- =================================================================================== -->
<robot name="test">
  <link name="new_input"/>
</robot>

But I'm at a loss when it comes to achieving the same directly using python. Anyone have any ideas?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-07 19:15:47 -0600

shonigmann gravatar image

updated 2021-09-08 11:04:10 -0600

Thanks to JafarAbdi/PickNik's parameter_tools package for the inspiration.

For anyone interested in adding arguments while parsing xacro files using the python library, you can either use his tool or to set the "mappings" argument when calling xacro.process_file(), e.g.:

# load xacro file with `input` set to `new_input`
robot_description_config = xacro.process_file(xacro_file, mappings={"input": "new_input"}).toxml()
robot_description = {'robot_description': robot_description_config}
edit flag offensive delete link more

Comments

Thanks to JafarAbdi's parameter_tools package, there's now a clean way to load xacro files with argument inputs.

tbh, I would depending on a 3rd party package for something which seems like it should be core functionality not "a clean way".

That's not to say it can't be a solution to your problem of course.

gvdhoorn gravatar image gvdhoorn  ( 2021-09-08 02:18:00 -0600 )edit

I've edited the answer to clarify - I more intended to give credit to the tool where I found the answer. That said, while I agree with your sentiment, it does seem like a good workaround to a number of common problems that have been slow to get addressed in the core and so, as things stand, for many this package would represent a "clean" solution. I would love to see equivalent functionality in the core set of launch tools and it does look like many aspects are on the horizon. But some of us are impatient :)

shonigmann gravatar image shonigmann  ( 2021-09-08 11:15:53 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2021-08-26 17:43:18 -0600

Seen: 1,542 times

Last updated: Sep 08 '21