test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 PKG = 'srdfdom'
3 
4 import sys
5 import rospkg
6 import unittest
7 from srdfdom.srdf import SRDF
8 from xml.dom.minidom import parseString
9 import xml.dom
10 
11 try:
12  string_types = (str, unicode)
13 except NameError:
14  string_types = (str)
15 
16 # xml match code from test_xacro.py
17 # by Stuart Glaser and William Woodall
18 
20  c = elt.firstChild
21  while c:
22  if c.nodeType == xml.dom.Node.ELEMENT_NODE:
23  return c
24  c = c.nextSibling
25  return None
26 
28  c = elt.nextSibling
29  while c:
30  if c.nodeType == xml.dom.Node.ELEMENT_NODE:
31  return c
32  c = c.nextSibling
33  return None
34 
36  if len(a.attributes) != len(b.attributes):
37  print("Different number of attributes")
38  return False
39  a_atts = [(a.attributes.item(i).name, a.attributes.item(i).value) for i in range(len(a.attributes))]
40  b_atts = [(b.attributes.item(i).name, b.attributes.item(i).value) for i in range(len(b.attributes))]
41  a_atts.sort()
42  b_atts.sort()
43  for i in range(len(a_atts)):
44  if a_atts[i][0] != b_atts[i][0]:
45  print("Different attribute names: %s and %s" % (a_atts[i][0], b_atts[i][0]))
46  return False
47  try:
48  if abs(float(a_atts[i][1]) - float(b_atts[i][1])) > 1.0e-9:
49  print("Different attribute values: %s and %s" % (a_atts[i][1], b_atts[i][1]))
50  return False
51  except ValueError: # Attribute values aren't numeric
52  if a_atts[i][1] != b_atts[i][1]:
53  print("Different attribute values: %s and %s" % (a_atts[i][1], b_atts[i][1]))
54  return False
55  return True
56 
57 def elements_match(a, b):
58  if not a and not b:
59  return True
60  if not a or not b:
61  return False
62  if a.nodeType != b.nodeType:
63  print("Different node types: %d and %d" % (a.nodeType, b.nodeType))
64  return False
65  if a.nodeName != b.nodeName:
66  print("Different element names: %s and %s" % (a.nodeName, b.nodeName))
67  return False
68  if not all_attributes_match(a, b):
69  return False
71  return False
73  return False
74  return True
75 
76 def xml_matches(a, b):
77  if isinstance(a, string_types):
78  return xml_matches(parseString(a).documentElement, b)
79  if isinstance(b, string_types):
80  return xml_matches(a, parseString(b).documentElement)
81  if a.nodeType == xml.dom.Node.DOCUMENT_NODE:
82  return xml_matches(a.documentElement, b)
83  if b.nodeType == xml.dom.Node.DOCUMENT_NODE:
84  return xml_matches(a, b.documentElement)
85  if not elements_match(a, b):
86  print("Match failed:")
87  a.writexml(sys.stdout)
88  print
89  print('=' * 78)
90  b.writexml(sys.stdout)
91  return False
92  return True
93 
94 
95 class TestSRDFParser(unittest.TestCase):
96 
97 
98  def test_full_srdf(self):
99  srdf_data = '''
100  <robot name="myrobot">
101  <group name="body">
102  <joint name="J1" />
103  <joint name="J2" />
104  <joint name="J3" />
105  <chain base_link="robot_base" tip_link="robot_tip" />
106  <group name="arm" />
107  </group>
108  <group_state name="zero" group="body">
109  <joint name="J1" value="0" />
110  <joint name="J2" value="0" />
111  <joint name="J3" value="0" />
112  </group_state>
113  <end_effector name="tip_ee" parent_link="tip" group="arm" parent_group="body" />
114  <end_effector name="othertip_ee" parent_link="othertip" group="arm" />
115  <virtual_joint name="virtual_joint" type="floating" parent_frame="body_frame" child_link="arm" />
116  <disable_collisions link1="link1" link2="link3" />
117  <disable_collisions reason="Adjacent" link1="link1" link2="link2" />
118  <link_sphere_approximation link="link1" />
119  <link_sphere_approximation link="link2" >
120  <sphere center="1.0 2.0 3.0" radius="1.0" />
121  <sphere center="1.0 2.0 4.0" radius="2.0" />
122  </link_sphere_approximation>
123  </robot>
124  '''
125  expected = '''
126 <robot name="myrobot">
127  <group name="body">
128  <joint name="J1" />
129  <joint name="J2" />
130  <joint name="J3" />
131  <chain base_link="robot_base" tip_link="robot_tip"/>
132  <group name="arm" />
133  </group>
134  <group_state name="zero" group="body">
135  <joint name="J1" value="0" />
136  <joint name="J2" value="0" />
137  <joint name="J3" value="0" />
138  </group_state>
139  <end_effector group="arm" name="tip_ee" parent_group="body" parent_link="tip"/>
140  <end_effector name="othertip_ee" parent_link="othertip" group="arm" />
141  <virtual_joint child_link="arm" name="virtual_joint" parent_frame="body_frame" type="floating" />
142  <disable_collisions link1="link1" link2="link3" />
143  <disable_collisions link1="link1" link2="link2" reason="Adjacent" />
144  <link_sphere_approximation link="link1" />
145  <link_sphere_approximation link="link2" >
146  <sphere center="1.0 2.0 3.0" radius="1.0" />
147  <sphere center="1.0 2.0 4.0" radius="2.0" />
148  </link_sphere_approximation>
149 </robot>
150  '''
151  robot = SRDF.from_xml_string(srdf_data)
152  self.assertTrue(xml_matches(robot.to_xml_string(),expected))
153 
154  def test_simple_srdf(self):
155  datadir=rospkg.RosPack().get_path('srdfdom')+"/test/resources/"
156  stream = open(datadir+'pr2_desc.1.srdf', 'r')
157  robot = SRDF.from_xml_string(stream.read())
158  stream.close()
159  self.assertTrue(len(robot.virtual_joints)==0)
160  self.assertTrue(len(robot.groups)==0)
161  self.assertTrue(len(robot.group_states)==0)
162  self.assertTrue(len(robot.disable_collisionss)==0)
163  self.assertTrue(len(robot.end_effectors)==0)
164 
165  stream = open(datadir+'pr2_desc.2.srdf', 'r')
166  robot = SRDF.from_xml_string(stream.read())
167  stream.close()
168  self.assertTrue(len(robot.virtual_joints)==1)
169  self.assertTrue(len(robot.groups)==1)
170  self.assertTrue(len(robot.group_states)==0)
171  self.assertTrue(len(robot.disable_collisionss)==0)
172  self.assertTrue(len(robot.end_effectors)==0)
173 
174  def test_complex_srdf(self):
175  datadir=rospkg.RosPack().get_path('srdfdom')+"/test/resources/"
176  stream = open(datadir+'pr2_desc.3.srdf', 'r')
177  robot = SRDF.from_xml_string(stream.read())
178  stream.close()
179  self.assertTrue(len(robot.virtual_joints)==1)
180  self.assertTrue(len(robot.groups)==7)
181  self.assertTrue(len(robot.group_states)==2)
182  self.assertTrue(len(robot.disable_collisionss)==2)
183  self.assertTrue(robot.disable_collisionss[0].reason=="adjacent")
184  self.assertTrue(len(robot.end_effectors)==2)
185 
186  self.assertTrue(robot.virtual_joints[0].name=="world_joint")
187  self.assertTrue(robot.virtual_joints[0].type=="planar")
188 
189  for group in robot.groups:
190  if (group.name == "left_arm" or group.name == "right_arm" ):
191  self.assertTrue(len(group.chains)==1)
192  if group.name == "arms":
193  self.assertTrue(len(group.subgroups)==2)
194  if group.name == "base":
195  self.assertTrue(len(group.joints)==1)
196  if (group.name == "l_end_effector" or group.name == "r_end_effector" ):
197  self.assertTrue(len(group.links)==1)
198  self.assertTrue(len(group.joints)==9)
199  if group.name == "whole_body" :
200  self.assertTrue(len(group.joints)==1)
201  self.assertTrue(len(group.subgroups)==2)
202 
203  index=0
204  if robot.group_states[0].group !="arms":
205  index=1
206 
207  self.assertTrue(robot.group_states[index].group =="arms")
208  self.assertTrue(robot.group_states[index].name =="tuck_arms")
209  self.assertTrue(robot.group_states[1-index].group =="base")
210  self.assertTrue(robot.group_states[1-index].name =="home")
211 
212  v=next((joint.value for joint in robot.group_states[index].joints if joint.name=="l_shoulder_pan_joint"),None)
213  self.assertTrue(len(v) == 1)
214  self.assertTrue(v[0] ==0.2)
215 
216  w=next((joint.value for joint in robot.group_states[1-index].joints if joint.name=="world_joint"),None)
217  self.assertTrue(len(w) == 3)
218  self.assertTrue(w[0] ==0.4)
219  self.assertTrue(w[1] ==0)
220  self.assertTrue(w[2] ==-1)
221 
222  index = 0 if (robot.end_effectors[0].name[0] == 'r') else 1
223  self.assertTrue(robot.end_effectors[index].name == 'r_end_effector')
224  self.assertTrue(robot.end_effectors[index].group == 'r_end_effector')
225  self.assertTrue(robot.end_effectors[index].parent_link == 'r_wrist_roll_link')
226 
227 
228 if __name__ == '__main__':
229  import rostest
230  rostest.rosrun(PKG, 'srdf_python_parser_test', TestSRDFParser)
test.TestSRDFParser.test_full_srdf
def test_full_srdf(self)
test valid srdf
Definition: test.py:98
test.TestSRDFParser.test_complex_srdf
def test_complex_srdf(self)
Definition: test.py:174
test.TestSRDFParser.test_simple_srdf
def test_simple_srdf(self)
Definition: test.py:154
test.TestSRDFParser
A python unit test for srdf.
Definition: test.py:95
test.xml_matches
def xml_matches(a, b)
Definition: test.py:76
test.first_child_element
def first_child_element(elt)
Definition: test.py:19
test.next_sibling_element
def next_sibling_element(elt)
Definition: test.py:27
test.all_attributes_match
def all_attributes_match(a, b)
Definition: test.py:35
test.elements_match
def elements_match(a, b)
Definition: test.py:57
srdfdom.srdf
Definition: srdf.py:1


srdfdom
Author(s): Ioan Sucan , Guillaume Walck
autogenerated on Wed Oct 16 2024 02:22:59