Skip to content

Commit

Permalink
Make sure to add the version when creating a new URDF. (#62) (#66)
Browse files Browse the repository at this point in the history
It turns out that when I added the version stuff, I completely
forgot about creating a new URDF.  This PR fixes it so that
we can successfully round-trip through the URDF parser, and
adds tests to ensure the same.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette authored Dec 4, 2020
1 parent 52376a1 commit d6fd7ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/urdf_parser_py/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,16 @@ def check_valid(self):


class Robot(xmlr.Object):
def __init__(self, name=None, version=None):
SUPPORTED_VERSIONS = ["1.0"]

def __init__(self, name=None, version="1.0"):
self.aggregate_init()

self.name = name
if version is not None:
self.version = version
if version not in self.SUPPORTED_VERSIONS:
raise ValueError("Invalid version; only %s is supported" % (','.join(self.SUPPORTED_VERSIONS)))

self.version = version
self.joints = []
self.links = []
self.materials = []
Expand Down Expand Up @@ -559,8 +563,8 @@ def post_read_xml(self):
if int(split[0]) < 0 or int(split[1]) < 0:
raise ValueError("Version number must be positive")

if self.version != "1.0":
raise ValueError("Invalid version; only 1.0 is supported")
if self.version not in self.SUPPORTED_VERSIONS:
raise ValueError("Invalid version; only %s is supported" % (','.join(self.SUPPORTED_VERSIONS)))

@classmethod
def from_parameter_server(cls, key='robot_description'):
Expand Down
16 changes: 16 additions & 0 deletions test/test_urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,5 +483,21 @@ def test_xml_and_urdfdom_robot_only_supported_since_melodic(self):
orig_xml = minidom.parseString(self.xml)
self.assertTrue(xml_matches(robot_xml, orig_xml))

class TestCreateNew(unittest.TestCase):
def test_new_urdf(self):
testcase = urdf.URDF('robot_name').to_xml()
self.assertTrue('name' in testcase.keys())
self.assertTrue('version' in testcase.keys())
self.assertEqual(testcase.get('name'), 'robot_name')
self.assertEqual(testcase.get('version'), '1.0')

def test_new_urdf_with_version(self):
testcase = urdf.URDF('robot_name', '1.0').to_xml()
self.assertTrue('name' in testcase.keys())
self.assertTrue('version' in testcase.keys())
self.assertEqual(testcase.get('name'), 'robot_name')
self.assertEqual(testcase.get('version'), '1.0')


if __name__ == '__main__':
unittest.main()

0 comments on commit d6fd7ec

Please # to comment.