Skip to content

Commit a693a9b

Browse files
faggAlexanderFabisch
authored andcommitted
Strip comments from URDF XML before parsing
1 parent 0685227 commit a693a9b

2 files changed

Lines changed: 304 additions & 2 deletions

File tree

pytransform3d/test/test_urdf.py

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,3 +1276,306 @@ def test_rotation_matrix_conversion():
12761276
]
12771277
),
12781278
)
1279+
1280+
1281+
def test_xml_comment():
1282+
urdf = """
1283+
<?xml version="1.0" ?>
1284+
<!-- =================================================================================== -->
1285+
<!-- | This document was autogenerated by xacro from ur10_robot.urdf.xacro | -->
1286+
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
1287+
<!-- =================================================================================== -->
1288+
<robot name="ur10" xmlns:xacro="http://www.ros.org/wiki/xacro">
1289+
<!-- common stuff -->
1290+
<gazebo>
1291+
<plugin filename="libgazebo_ros_control.so" name="ros_control">
1292+
<!--robotNamespace>/</robotNamespace-->
1293+
<!--robotSimType>gazebo_ros_control/DefaultRobotHWSim</robotSimType-->
1294+
</plugin>
1295+
<!--
1296+
<plugin name="gazebo_ros_power_monitor_controller" filename="libgazebo_ros_power_monitor.so">
1297+
<alwaysOn>true</alwaysOn>
1298+
<updateRate>1.0</updateRate>
1299+
<timeout>5</timeout>
1300+
<powerStateTopic>power_state</powerStateTopic>
1301+
<powerStateRate>10.0</powerStateRate>
1302+
<fullChargeCapacity>87.78</fullChargeCapacity>
1303+
<dischargeRate>-474</dischargeRate>
1304+
<chargeRate>525</chargeRate>
1305+
<dischargeVoltage>15.52</dischargeVoltage>
1306+
<chargeVoltage>16.41</chargeVoltage>
1307+
</plugin>
1308+
-->
1309+
</gazebo>
1310+
<!-- ur10 -->
1311+
<!-- arm -->
1312+
<link name="base_link">
1313+
<visual>
1314+
<geometry>
1315+
<mesh filename="package://ur_description/meshes/ur10/visual/Base.dae"/>
1316+
</geometry>
1317+
<material name="LightGrey">
1318+
<color rgba="0.7 0.7 0.7 1.0"/>
1319+
</material>
1320+
</visual>
1321+
<collision>
1322+
<geometry>
1323+
<mesh filename="package://ur_description/meshes/ur10/collision/base.stl"/>
1324+
</geometry>
1325+
</collision>
1326+
<inertial>
1327+
<mass value="4.0"/>
1328+
<origin rpy="0 0 0" xyz="0.0 0.0 0.0"/>
1329+
<inertia ixx="0.0061063308908" ixy="0.0" ixz="0.0" iyy="0.0061063308908" iyz="0.0" izz="0.01125"/>
1330+
</inertial>
1331+
</link>
1332+
<joint name="shoulder_pan_joint" type="revolute">
1333+
<parent link="base_link"/>
1334+
<child link="shoulder_link"/>
1335+
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.1273"/>
1336+
<axis xyz="0 0 1"/>
1337+
<limit effort="330.0" lower="-6.2831853" upper="6.2831853" velocity="2.16"/>
1338+
<dynamics damping="0.0" friction="0.0"/>
1339+
</joint>
1340+
<link name="shoulder_link">
1341+
<visual>
1342+
<geometry>
1343+
<mesh filename="package://ur_description/meshes/ur10/visual/Shoulder.dae"/>
1344+
</geometry>
1345+
<material name="LightGrey">
1346+
<color rgba="0.7 0.7 0.7 1.0"/>
1347+
</material>
1348+
</visual>
1349+
<collision>
1350+
<geometry>
1351+
<mesh filename="package://ur_description/meshes/ur10/collision/shoulder.stl"/>
1352+
</geometry>
1353+
</collision>
1354+
<inertial>
1355+
<mass value="7.778"/>
1356+
<origin rpy="0 0 0" xyz="0.0 0.0 0.0"/>
1357+
<inertia ixx="0.0314743125769" ixy="0.0" ixz="0.0" iyy="0.0314743125769" iyz="0.0" izz="0.021875625"/>
1358+
</inertial>
1359+
</link>
1360+
<joint name="shoulder_lift_joint" type="revolute">
1361+
<parent link="shoulder_link"/>
1362+
<child link="upper_arm_link"/>
1363+
<origin rpy="0.0 1.570796325 0.0" xyz="0.0 0.220941 0.0"/>
1364+
<axis xyz="0 1 0"/>
1365+
<limit effort="330.0" lower="-6.2831853" upper="6.2831853" velocity="2.16"/>
1366+
<dynamics damping="0.0" friction="0.0"/>
1367+
</joint>
1368+
<link name="upper_arm_link">
1369+
<visual>
1370+
<geometry>
1371+
<mesh filename="package://ur_description/meshes/ur10/visual/UpperArm.dae"/>
1372+
</geometry>
1373+
<material name="LightGrey">
1374+
<color rgba="0.7 0.7 0.7 1.0"/>
1375+
</material>
1376+
</visual>
1377+
<collision>
1378+
<geometry>
1379+
<mesh filename="package://ur_description/meshes/ur10/collision/upper_arm.stl"/>
1380+
</geometry>
1381+
</collision>
1382+
<inertial>
1383+
<mass value="12.93"/>
1384+
<origin rpy="0 0 0" xyz="0.0 0.0 0.306"/>
1385+
<inertia ixx="0.421753803798" ixy="0.0" ixz="0.0" iyy="0.421753803798" iyz="0.0" izz="0.036365625"/>
1386+
</inertial>
1387+
</link>
1388+
<joint name="elbow_joint" type="revolute">
1389+
<parent link="upper_arm_link"/>
1390+
<child link="forearm_link"/>
1391+
<origin rpy="0.0 0.0 0.0" xyz="0.0 -0.1719 0.612"/>
1392+
<axis xyz="0 1 0"/>
1393+
<limit effort="150.0" lower="-6.2831853" upper="6.2831853" velocity="3.15"/>
1394+
<dynamics damping="0.0" friction="0.0"/>
1395+
</joint>
1396+
<link name="forearm_link">
1397+
<visual>
1398+
<geometry>
1399+
<mesh filename="package://ur_description/meshes/ur10/visual/Forearm.dae"/>
1400+
</geometry>
1401+
<material name="LightGrey">
1402+
<color rgba="0.7 0.7 0.7 1.0"/>
1403+
</material>
1404+
</visual>
1405+
<collision>
1406+
<geometry>
1407+
<mesh filename="package://ur_description/meshes/ur10/collision/forearm.stl"/>
1408+
</geometry>
1409+
</collision>
1410+
<inertial>
1411+
<mass value="3.87"/>
1412+
<origin rpy="0 0 0" xyz="0.0 0.0 0.28615"/>
1413+
<inertia ixx="0.111069694097" ixy="0.0" ixz="0.0" iyy="0.111069694097" iyz="0.0" izz="0.010884375"/>
1414+
</inertial>
1415+
</link>
1416+
<joint name="wrist_1_joint" type="revolute">
1417+
<parent link="forearm_link"/>
1418+
<child link="wrist_1_link"/>
1419+
<origin rpy="0.0 1.570796325 0.0" xyz="0.0 0.0 0.5723"/>
1420+
<axis xyz="0 1 0"/>
1421+
<limit effort="54.0" lower="-6.2831853" upper="6.2831853" velocity="3.2"/>
1422+
<dynamics damping="0.0" friction="0.0"/>
1423+
</joint>
1424+
<link name="wrist_1_link">
1425+
<visual>
1426+
<geometry>
1427+
<mesh filename="package://ur_description/meshes/ur10/visual/Wrist1.dae"/>
1428+
</geometry>
1429+
<material name="LightGrey">
1430+
<color rgba="0.7 0.7 0.7 1.0"/>
1431+
</material>
1432+
</visual>
1433+
<collision>
1434+
<geometry>
1435+
<mesh filename="package://ur_description/meshes/ur10/collision/wrist_1.stl"/>
1436+
</geometry>
1437+
</collision>
1438+
<inertial>
1439+
<mass value="1.96"/>
1440+
<origin rpy="0 0 0" xyz="0.0 0.0 0.0"/>
1441+
<inertia ixx="0.0051082479567" ixy="0.0" ixz="0.0" iyy="0.0051082479567" iyz="0.0" izz="0.0055125"/>
1442+
</inertial>
1443+
</link>
1444+
<joint name="wrist_2_joint" type="revolute">
1445+
<parent link="wrist_1_link"/>
1446+
<child link="wrist_2_link"/>
1447+
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.1149 0.0"/>
1448+
<axis xyz="0 0 1"/>
1449+
<limit effort="54.0" lower="-6.2831853" upper="6.2831853" velocity="3.2"/>
1450+
<dynamics damping="0.0" friction="0.0"/>
1451+
</joint>
1452+
<link name="wrist_2_link">
1453+
<visual>
1454+
<geometry>
1455+
<mesh filename="package://ur_description/meshes/ur10/visual/Wrist2.dae"/>
1456+
</geometry>
1457+
<material name="LightGrey">
1458+
<color rgba="0.7 0.7 0.7 1.0"/>
1459+
</material>
1460+
</visual>
1461+
<collision>
1462+
<geometry>
1463+
<mesh filename="package://ur_description/meshes/ur10/collision/wrist_2.stl"/>
1464+
</geometry>
1465+
</collision>
1466+
<inertial>
1467+
<mass value="1.96"/>
1468+
<origin rpy="0 0 0" xyz="0.0 0.0 0.0"/>
1469+
<inertia ixx="0.0051082479567" ixy="0.0" ixz="0.0" iyy="0.0051082479567" iyz="0.0" izz="0.0055125"/>
1470+
</inertial>
1471+
</link>
1472+
<joint name="wrist_3_joint" type="revolute">
1473+
<parent link="wrist_2_link"/>
1474+
<child link="wrist_3_link"/>
1475+
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.1157"/>
1476+
<axis xyz="0 1 0"/>
1477+
<limit effort="54.0" lower="-6.2831853" upper="6.2831853" velocity="3.2"/>
1478+
<dynamics damping="0.0" friction="0.0"/>
1479+
</joint>
1480+
<link name="wrist_3_link">
1481+
<visual>
1482+
<geometry>
1483+
<mesh filename="package://ur_description/meshes/ur10/visual/Wrist3.dae"/>
1484+
</geometry>
1485+
<material name="LightGrey">
1486+
<color rgba="0.7 0.7 0.7 1.0"/>
1487+
</material>
1488+
</visual>
1489+
<collision>
1490+
<geometry>
1491+
<mesh filename="package://ur_description/meshes/ur10/collision/wrist_3.stl"/>
1492+
</geometry>
1493+
</collision>
1494+
<inertial>
1495+
<mass value="0.202"/>
1496+
<origin rpy="0 0 0" xyz="0.0 0.0 0.0"/>
1497+
<inertia ixx="0.000526462289415" ixy="0.0" ixz="0.0" iyy="0.000526462289415" iyz="0.0" izz="0.000568125"/>
1498+
</inertial>
1499+
</link>
1500+
<joint name="ee_fixed_joint" type="fixed">
1501+
<parent link="wrist_3_link"/>
1502+
<child link="ee_link"/>
1503+
<origin rpy="0.0 0.0 1.570796325" xyz="0.0 0.0922 0.0"/>
1504+
</joint>
1505+
<link name="ee_link">
1506+
<collision>
1507+
<geometry>
1508+
<box size="0.01 0.01 0.01"/>
1509+
</geometry>
1510+
<origin rpy="0 0 0" xyz="-0.01 0 0"/>
1511+
</collision>
1512+
</link>
1513+
<transmission name="shoulder_pan_trans">
1514+
<type>transmission_interface/SimpleTransmission</type>
1515+
<joint name="shoulder_pan_joint">
1516+
<hardwareInterface>PositionJointInterface</hardwareInterface>
1517+
</joint>
1518+
<actuator name="shoulder_pan_motor">
1519+
<mechanicalReduction>1</mechanicalReduction>
1520+
</actuator>
1521+
</transmission>
1522+
<transmission name="shoulder_lift_trans">
1523+
<type>transmission_interface/SimpleTransmission</type>
1524+
<joint name="shoulder_lift_joint">
1525+
<hardwareInterface>PositionJointInterface</hardwareInterface>
1526+
</joint>
1527+
<actuator name="shoulder_lift_motor">
1528+
<mechanicalReduction>1</mechanicalReduction>
1529+
</actuator>
1530+
</transmission>
1531+
<transmission name="elbow_trans">
1532+
<type>transmission_interface/SimpleTransmission</type>
1533+
<joint name="elbow_joint">
1534+
<hardwareInterface>PositionJointInterface</hardwareInterface>
1535+
</joint>
1536+
<actuator name="elbow_motor">
1537+
<mechanicalReduction>1</mechanicalReduction>
1538+
</actuator>
1539+
</transmission>
1540+
<transmission name="wrist_1_trans">
1541+
<type>transmission_interface/SimpleTransmission</type>
1542+
<joint name="wrist_1_joint">
1543+
<hardwareInterface>PositionJointInterface</hardwareInterface>
1544+
</joint>
1545+
<actuator name="wrist_1_motor">
1546+
<mechanicalReduction>1</mechanicalReduction>
1547+
</actuator>
1548+
</transmission>
1549+
<transmission name="wrist_2_trans">
1550+
<type>transmission_interface/SimpleTransmission</type>
1551+
<joint name="wrist_2_joint">
1552+
<hardwareInterface>PositionJointInterface</hardwareInterface>
1553+
</joint>
1554+
<actuator name="wrist_2_motor">
1555+
<mechanicalReduction>1</mechanicalReduction>
1556+
</actuator>
1557+
</transmission>
1558+
<transmission name="wrist_3_trans">
1559+
<type>transmission_interface/SimpleTransmission</type>
1560+
<joint name="wrist_3_joint">
1561+
<hardwareInterface>PositionJointInterface</hardwareInterface>
1562+
</joint>
1563+
<actuator name="wrist_3_motor">
1564+
<mechanicalReduction>1</mechanicalReduction>
1565+
</actuator>
1566+
</transmission>
1567+
<link name="world"/>
1568+
<joint name="world_joint" type="fixed">
1569+
<parent link="world"/>
1570+
<child link="base_link"/>
1571+
<origin rpy="0.0 0.0 0.0" xyz="0.0 0.0 0.0"/>
1572+
</joint>
1573+
</robot>
1574+
"""
1575+
(robot_name, links, _) = parse_urdf(urdf)
1576+
1577+
assert robot_name == 'ur10'
1578+
assert len(links) == 9
1579+
assert links[0].name == 'base_link'
1580+
1581+

pytransform3d/urdf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def parse_urdf(urdf_xml, mesh_path=None, package_dir=None, strict_check=True):
406406
# declaration. Hence, we have to convert them to bytes first.
407407
urdf_xml = bytes(urdf_xml.encode("utf-8"))
408408
try:
409-
root = etree.XML(urdf_xml, parser=etree.XMLParser(recover=True))
409+
root = etree.XML(urdf_xml, parser=etree.XMLParser(recover=True, remove_comments=True))
410410
except etree.XMLSyntaxError as e:
411411
raise UrdfException("Invalid XML.") from e
412412

@@ -476,7 +476,6 @@ def _normalize_namespace(root):
476476
for element in root.iter():
477477
element.tag = re.sub(r"\{.*?\}", "", element.tag)
478478

479-
480479
def _parse_material(material):
481480
"""Parse material."""
482481
if "name" not in material.attrib:

0 commit comments

Comments
 (0)