test_urdf.py
Go to the documentation of this file.
00001 from __future__ import print_function
00002 
00003 import unittest
00004 import mock
00005 import os
00006 import sys
00007 
00008 # Add path to import xml_matching
00009 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '.')))
00010 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
00011                                              '../src')))
00012 
00013 from xml.dom import minidom  # noqa
00014 from xml_matching import xml_matches  # noqa
00015 from urdf_parser_py import urdf  # noqa
00016 
00017 
00018 class ParseException(Exception):
00019     pass
00020 
00021 
00022 class TestURDFParser(unittest.TestCase):
00023     @mock.patch('urdf_parser_py.xml_reflection.on_error',
00024                 mock.Mock(side_effect=ParseException))
00025     def parse(self, xml):
00026         return urdf.Robot.from_xml_string(xml)
00027 
00028     def parse_and_compare(self, orig):
00029         xml = minidom.parseString(orig)
00030         robot = urdf.Robot.from_xml_string(orig)
00031         rewritten = minidom.parseString(robot.to_xml_string())
00032         self.assertTrue(xml_matches(xml, rewritten))
00033 
00034     def test_new_transmission(self):
00035         xml = '''<?xml version="1.0"?>
00036 <robot name="test">
00037   <transmission name="simple_trans">
00038     <type>transmission_interface/SimpleTransmission</type>
00039     <joint name="foo_joint">
00040       <hardwareInterface>EffortJointInterface</hardwareInterface>
00041     </joint>
00042     <actuator name="foo_motor">
00043       <mechanicalReduction>50.0</mechanicalReduction>
00044     </actuator>
00045   </transmission>
00046 </robot>'''
00047         self.parse_and_compare(xml)
00048 
00049     def test_new_transmission_multiple_joints(self):
00050         xml = '''<?xml version="1.0"?>
00051 <robot name="test">
00052   <transmission name="simple_trans">
00053     <type>transmission_interface/SimpleTransmission</type>
00054     <joint name="foo_joint">
00055       <hardwareInterface>EffortJointInterface</hardwareInterface>
00056     </joint>
00057     <joint name="bar_joint">
00058       <hardwareInterface>EffortJointInterface</hardwareInterface>
00059       <hardwareInterface>EffortJointInterface</hardwareInterface>
00060     </joint>
00061     <actuator name="foo_motor">
00062       <mechanicalReduction>50.0</mechanicalReduction>
00063     </actuator>
00064   </transmission>
00065 </robot>'''
00066         self.parse_and_compare(xml)
00067 
00068     def test_new_transmission_multiple_actuators(self):
00069         xml = '''<?xml version="1.0"?>
00070 <robot name="test">
00071   <transmission name="simple_trans">
00072     <type>transmission_interface/SimpleTransmission</type>
00073     <joint name="foo_joint">
00074       <hardwareInterface>EffortJointInterface</hardwareInterface>
00075     </joint>
00076     <actuator name="foo_motor">
00077       <mechanicalReduction>50.0</mechanicalReduction>
00078     </actuator>
00079     <actuator name="bar_motor"/>
00080   </transmission>
00081 </robot>'''
00082         self.parse_and_compare(xml)
00083 
00084     def test_new_transmission_missing_joint(self):
00085         xml = '''<?xml version="1.0"?>
00086 <robot name="test">
00087   <transmission name="simple_trans">
00088     <type>transmission_interface/SimpleTransmission</type>
00089   </transmission>
00090 </robot>'''
00091         self.assertRaises(Exception, self.parse, xml)
00092 
00093     def test_new_transmission_missing_actuator(self):
00094         xml = '''<?xml version="1.0"?>
00095 <robot name="test">
00096   <transmission name="simple_trans">
00097     <type>transmission_interface/SimpleTransmission</type>
00098     <joint name="foo_joint">
00099       <hardwareInterface>EffortJointInterface</hardwareInterface>
00100     </joint>
00101   </transmission>
00102 </robot>'''
00103         self.assertRaises(Exception, self.parse, xml)
00104 
00105     def test_old_transmission(self):
00106         xml = '''<?xml version="1.0"?>
00107 <robot name="test">
00108   <transmission name="PR2_trans" type="SimpleTransmission">
00109     <joint name="foo_joint"/>
00110     <actuator name="foo_motor"/>
00111     <mechanicalReduction>1.0</mechanicalReduction>
00112   </transmission>
00113 </robot>'''
00114         self.parse_and_compare(xml)
00115 
00116     def test_link_material_missing_color_and_texture(self):
00117         xml = '''<?xml version="1.0"?>
00118 <robot name="test">
00119   <link name="link">
00120     <visual>
00121       <geometry>
00122         <cylinder length="1" radius="1"/>
00123       </geometry>
00124       <material name="mat"/>
00125     </visual>
00126   </link>
00127 </robot>'''
00128         self.parse_and_compare(xml)
00129 
00130     def test_robot_material(self):
00131         xml = '''<?xml version="1.0"?>
00132 <robot name="test">
00133   <material name="mat">
00134     <color rgba="0.0 0.0 0.0 1.0"/>
00135   </material>
00136 </robot>'''
00137         self.parse_and_compare(xml)
00138 
00139     def test_robot_material_missing_color_and_texture(self):
00140         xml = '''<?xml version="1.0"?>
00141 <robot name="test">
00142   <material name="mat"/>
00143 </robot>'''
00144         self.assertRaises(ParseException, self.parse, xml)
00145 
00146 
00147 class LinkOriginTestCase(unittest.TestCase):
00148     @mock.patch('urdf_parser_py.xml_reflection.on_error',
00149                 mock.Mock(side_effect=ParseException))
00150     def parse(self, xml):
00151         return urdf.Robot.from_xml_string(xml)
00152 
00153     def test_robot_link_defaults(self):
00154         xml = '''<?xml version="1.0"?>
00155 <robot name="test">
00156   <link name="test_link">
00157     <inertial>
00158       <mass value="10.0"/>
00159       <origin/>
00160     </inertial>
00161   </link>
00162 </robot>'''
00163         robot = self.parse(xml)
00164         origin = robot.links[0].inertial.origin
00165         self.assertEquals(origin.xyz, [0, 0, 0])
00166         self.assertEquals(origin.rpy, [0, 0, 0])
00167 
00168     def test_robot_link_defaults_xyz_set(self):
00169         xml = '''<?xml version="1.0"?>
00170 <robot name="test">
00171   <link name="test_link">
00172     <inertial>
00173       <mass value="10.0"/>
00174       <origin xyz="1 2 3"/>
00175     </inertial>
00176   </link>
00177 </robot>'''
00178         robot = self.parse(xml)
00179         origin = robot.links[0].inertial.origin
00180         self.assertEquals(origin.xyz, [1, 2, 3])
00181         self.assertEquals(origin.rpy, [0, 0, 0])
00182 
00183 
00184 if __name__ == '__main__':
00185     unittest.main()


urdfdom_py
Author(s): Thomas Moulard, David Lu, Kelsey Hawkins, Antonio El Khoury, Eric Cousineau, Ioan Sucan , Jackie Kay
autogenerated on Thu Jun 6 2019 20:07:42