Skip to content

Commit

Permalink
urdf, sdf: Use sealed XML reflection bits
Browse files Browse the repository at this point in the history
Clean up tests.
  • Loading branch information
EricCousineau-TRI committed Nov 20, 2018
1 parent ccc3098 commit 97bc2f3
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 221 deletions.
82 changes: 42 additions & 40 deletions src/urdf_parser_py/sdf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from urdf_parser_py.xml_reflection.basics import *
import urdf_parser_py.xml_reflection as xmlr
from urdf_parser_py import _now_private_property
import urdf_parser_py._xml_reflection as _xmlr

# What is the scope of plugins? Model, World, Sensor?
_xmlr.start_namespace('sdf')

xmlr.start_namespace('sdf')


class Pose(xmlr.Object):
class Pose(_xmlr.Object):
def __init__(self, vec=None, extra=None):
self.xyz = None
self.rpy = None
Expand All @@ -32,37 +30,37 @@ def as_vec(self):
rpy = self.rpy if self.rpy else [0, 0, 0]
return xyz + rpy

def read_xml(self, node):
def _read_xml(self, node):
# Better way to do this? Define type?
vec = get_type('vector6').read_xml(node)
self.load_vec(vec)
vec = _xmlr.get_type('vector6').read_xml_value(node)
self.from_vec(vec)

def write_xml(self, node):
def _write_xml(self, node):
vec = self.as_vec()
get_type('vector6').write_xml(node, vec)
_xmlr.get_type('vector6').write_xml_value(node, vec)

def check_valid(self):
def _check_valid(self):
assert self.xyz is not None or self.rpy is not None


name_attribute = xmlr.Attribute('name', str)
pose_element = xmlr.Element('pose', Pose, False)
_name_attribute = _xmlr.Attribute('name', str)
_pose_element = _xmlr.Element('pose', Pose, required=False)


class Entity(xmlr.Object):
class Entity(_xmlr.Object):
def __init__(self, name=None, pose=None):
self.name = name
self.pose = pose


xmlr.reflect(Entity, params=[
name_attribute,
pose_element
_xmlr.reflect(Entity, params=[
_name_attribute,
_pose_element
])


class Inertia(xmlr.Object):
KEYS = ['ixx', 'ixy', 'ixz', 'iyy', 'iyz', 'izz']
class Inertia(_xmlr.Object):
_KEYS = ['ixx', 'ixy', 'ixz', 'iyy', 'iyz', 'izz']

def __init__(self, ixx=0.0, ixy=0.0, ixz=0.0, iyy=0.0, iyz=0.0, izz=0.0):
self.ixx = ixx
Expand All @@ -79,24 +77,21 @@ def to_matrix(self):
[self.ixz, self.iyz, self.izz]]


xmlr.reflect(Inertia,
params=[xmlr.Element(key, float) for key in Inertia.KEYS])

# Pretty much copy-paste... Better method?
# Use multiple inheritance to separate the objects out so they are unique?
_xmlr.reflect(Inertia, tag='inertia',
params=[_xmlr.Element(key, float) for key in Inertia._KEYS])


class Inertial(xmlr.Object):
class Inertial(_xmlr.Object):
def __init__(self, mass=0.0, inertia=None, pose=None):
self.mass = mass
self.inertia = inertia
self.pose = pose


xmlr.reflect(Inertial, params=[
xmlr.Element('mass', float),
xmlr.Element('inertia', Inertia),
pose_element
_xmlr.reflect(Inertial, tag='inertial', params=[
_xmlr.Element('mass', float),
_xmlr.Element('inertia', Inertia),
_pose_element
])


Expand All @@ -107,14 +102,21 @@ def __init__(self, name=None, pose=None, inertial=None, kinematic=False):
self.kinematic = kinematic


xmlr.reflect(Link, parent_cls=Entity, params=[
xmlr.Element('inertial', Inertial),
xmlr.Attribute('kinematic', bool, False),
xmlr.AggregateElement('visual', Visual, var='visuals'),
xmlr.AggregateElement('collision', Collision, var='collisions')
_xmlr.reflect(Link, tag='link', parent_cls=Entity, params=[
_xmlr.Element('inertial', Inertial),
_xmlr.Attribute('kinematic', bool, False),
_xmlr.AggregateElement('visual', Visual, var='visuals'),
_xmlr.AggregateElement('collision', Collision, var='collisions')
])


class Joint(Entity):
pass


_xmlr.reflect(Joint, tag='joint', parent_cls=Entity, params=[])


class Model(Entity):
def __init__(self, name=None, pose=None):
Entity.__init__(self, name, pose)
Expand All @@ -123,10 +125,10 @@ def __init__(self, name=None, pose=None):
self.plugins = []


xmlr.reflect(Model, parent_cls=Entity, params=[
xmlr.AggregateElement('link', Link, var='links'),
xmlr.AggregateElement('joint', Joint, var='joints'),
xmlr.AggregateElement('plugin', Plugin, var='plugins')
_xmlr.reflect(Model, parent_cls=Entity, params=[
_xmlr.AggregateElement('link', Link, var='links'),
_xmlr.AggregateElement('joint', Joint, var='joints'),
_xmlr.AggregateElement('plugin', Plugin, var='plugins')
])

xmlr.end_namespace('sdf')
_xmlr.end_namespace('sdf')
Loading

0 comments on commit 97bc2f3

Please sign in to comment.