Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/roboticstoolbox/tools/urdf/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,13 +1855,6 @@ def __init__(
if materials is None:
materials = []

# TODO, what does this next line do?
# why arent the other things validated
try:
self._validate_transmissions()
except Exception:
pass

self.name = name
self.other_xml = other_xml

Expand Down Expand Up @@ -1891,6 +1884,13 @@ def __init__(
stage="urdf",
)

self._link_map = {link.name: link for link in self._links}
self._joint_map = {joint.name: joint for joint in self._joints}
self._transmission_map = {
transmission.name: transmission for transmission in self._transmissions
}
self._validate_transmissions()

@property
def name(self) -> Any:
"""str : The name of the URDF."""
Expand All @@ -1916,8 +1916,7 @@ def link_map(self) -> Any: # pragma nocover
directly. If you want to add or remove links, use
the appropriate functions.
"""
# `_link_map` is never set: AttributeError, see findings
return copy.copy(self._link_map) # type: ignore[attr-defined]
return copy.copy(self._link_map)

@property
def joints(self) -> Any:
Expand All @@ -1935,7 +1934,7 @@ def joint_map(self) -> Any: # pragma nocover
directly. If you want to add or remove joints, use
the appropriate functions.
"""
return copy.copy(self._joint_map) # type: ignore[attr-defined]
return copy.copy(self._joint_map)

@property
def transmissions(self) -> Any:
Expand All @@ -1953,7 +1952,7 @@ def transmission_map(self) -> Any: # pragma nocover
directly. If you want to add or remove transmissions, use
the appropriate functions.
"""
return copy.copy(self._transmission_map) # type: ignore[attr-defined]
return copy.copy(self._transmission_map)

@property
def other_xml(self) -> Any: # pragma nocover
Expand Down Expand Up @@ -2058,7 +2057,7 @@ def _validate_transmissions(self) -> None:
"""
for t in self.transmissions: # pragma nocover
for joint in t.joints:
if joint.name not in self._joint_map: # type: ignore[attr-defined]
if joint.name not in self._joint_map:
raise ValueError(
f"Transmission {t.name} has invalid joint name {joint.name}"
)
Expand Down
55 changes: 55 additions & 0 deletions tests/test_urdf_name_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Parsed URDF names must be available to callers and transmission validation."""

import numpy.testing as nt
import pytest

from roboticstoolbox.tools.urdf import URDF, URDFError

ROBOT = """<robot name="name_maps">
<link name="base"/>
<link name="arm"/>
<joint name="hinge" type="continuous">
<parent link="base"/><child link="arm"/>
</joint>
<transmission name="drive">
<type>transmission_interface/SimpleTransmission</type>
<joint name="{joint}">
<hardwareInterface>PositionJointInterface</hardwareInterface>
</joint>
<actuator name="motor">
<hardwareInterface>PositionJointInterface</hardwareInterface>
<mechanicalReduction>2</mechanicalReduction>
</actuator>
</transmission>
</robot>"""


def test_name_maps_preserve_parsed_object_identity_and_return_copies():
robot = URDF.loadstr(ROBOT.format(joint="hinge"), None)

for mapping_name, sequence_name in (
("link_map", "links"),
("joint_map", "joints"),
("transmission_map", "transmissions"),
):
mapping = getattr(robot, mapping_name)
objects = getattr(robot, sequence_name)
nt.assert_equal(set(mapping) == {item.name for item in objects}, True)
for item in objects:
nt.assert_equal(mapping[item.name] is item, True)
mapping.clear()
nt.assert_equal(len(getattr(robot, mapping_name)), len(objects))


def test_no_joints_or_transmissions_have_empty_name_maps():
robot = URDF.loadstr('<robot name="fixed"><link name="base"/></robot>', None)
nt.assert_equal(robot.link_map["base"] is robot.links[0], True)
nt.assert_equal(robot.joint_map, {})
nt.assert_equal(robot.transmission_map, {})


def test_transmission_must_reference_an_existing_joint():
with pytest.raises(
URDFError, match="Transmission drive has invalid joint name absent"
):
URDF.loadstr(ROBOT.format(joint="absent"), None)
Loading