Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for quaternion in URDF #84

Draft
wants to merge 2 commits into
base: ros2
Choose a base branch
from
Draft
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
11 changes: 8 additions & 3 deletions src/urdf_parser_py/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@

xmlr.add_type('element_link', xmlr.SimpleElementType('link', str))
xmlr.add_type('element_xyz', xmlr.SimpleElementType('xyz', 'vector3'))
xmlr.add_type('element_quat_xyzw', xmlr.SimpleElementType('quat_xyzw', 'vector4'))

verbose = True


class Pose(xmlr.Object):
def __init__(self, xyz=None, rpy=None):
def __init__(self, xyz=None, rpy=None, quat_xyzw=None):
self.xyz = xyz
self.rpy = rpy
self.quat_xyzw = quat_xyzw

def check_valid(self):
assert (self.xyz is None or len(self.xyz) == 3) and \
(self.rpy is None or len(self.rpy) == 3)
(self.rpy is None or len(self.rpy) == 3) and \
(self.quat_xyzw is None or len(self.quat_xyzw) == 4) and \
(self.rpy is None or self.quat_xyzw is None)

# Aliases for backwards compatibility
@property
Expand All @@ -38,7 +42,8 @@ def position(self, value): self.xyz = value

xmlr.reflect(Pose, tag='origin', params=[
xmlr.Attribute('xyz', 'vector3', False, default=[0, 0, 0]),
xmlr.Attribute('rpy', 'vector3', False, default=[0, 0, 0])
xmlr.Attribute('rpy', 'vector3', False, default=[0, 0, 0]),
xmlr.Attribute('quat_xyzw', 'vector4', False)
])


Expand Down
7 changes: 6 additions & 1 deletion src/urdf_parser_py/xml_reflection/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,12 @@ def get_element_path(element):
# and find where the problematic parent element.
for attribute in map(self.attribute_map.get, unset_attributes):
try:
attribute.set_default(obj)
if attribute.xml_var == 'rpy':
if 'quat_xyzw' in unset_attributes:
attribute.set_default(obj)
else:
attribute.set_default(obj)

except ParseError:
raise
except Exception as e:
Expand Down
34 changes: 34 additions & 0 deletions test/test_urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def test_robot_link_defaults(self):
origin = robot.links[0].inertial.origin
self.assertEqual(origin.xyz, [0, 0, 0])
self.assertEqual(origin.rpy, [0, 0, 0])
self.assertEqual(origin.quat_xyzw, None)

def test_robot_link_defaults_xyz_set(self):
xml = '''<?xml version="1.0"?>
Expand All @@ -288,7 +289,40 @@ def test_robot_link_defaults_xyz_set(self):
origin = robot.links[0].inertial.origin
self.assertEqual(origin.xyz, [1, 2, 3])
self.assertEqual(origin.rpy, [0, 0, 0])
self.assertEqual(origin.quat_xyzw, None)

def test_robot_link_defaults_xyz_rpy_set(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<link name="test_link">
<inertial>
<mass value="10.0"/>
<origin xyz="1 2 3" rpy="1 2 3"/>
</inertial>
</link>
</robot>'''
robot = self.parse(xml)
origin = robot.links[0].inertial.origin
self.assertEqual(origin.xyz, [1, 2, 3])
self.assertEqual(origin.rpy, [1, 2, 3])
self.assertEqual(origin.quat_xyzw, None)

def test_robot_link_defaults_xyz_quat_xyzw_set(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<link name="test_link">
<inertial>
<mass value="10.0"/>
<origin xyz="1 2 3" quat_xyzw="0.1 0.2 0.3 0.4"/>
</inertial>
</link>
</robot>'''
robot = self.parse(xml)
origin = robot.links[0].inertial.origin
self.assertEqual(origin.xyz, [1, 2, 3])
self.assertEqual(origin.rpy, None)
self.assertEqual(origin.quat_xyzw, [0.1, 0.2, 0.3, 0.4])

def test_xml_with_UTF8_encoding(self):
xml = b'''<?xml version="1.0" encoding="UTF-8"?>
<robot name="test">
Expand Down