8 from xml.dom.minidom
import parseString
12 string_types = (str, unicode)
22 if c.nodeType == xml.dom.Node.ELEMENT_NODE:
30 if c.nodeType == xml.dom.Node.ELEMENT_NODE:
36 if len(a.attributes) != len(b.attributes):
37 print(
"Different number of attributes")
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))]
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]))
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]))
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]))
62 if a.nodeType != b.nodeType:
63 print(
"Different node types: %d and %d" % (a.nodeType, b.nodeType))
65 if a.nodeName != b.nodeName:
66 print(
"Different element names: %s and %s" % (a.nodeName, b.nodeName))
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:
83 if b.nodeType == xml.dom.Node.DOCUMENT_NODE:
86 print(
"Match failed:")
87 a.writexml(sys.stdout)
90 b.writexml(sys.stdout)
100 <robot name="myrobot">
105 <chain base_link="robot_base" tip_link="robot_tip" />
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" />
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>
126 <robot name="myrobot">
131 <chain base_link="robot_base" tip_link="robot_tip"/>
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" />
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>
151 robot = SRDF.from_xml_string(srdf_data)
152 self.assertTrue(
xml_matches(robot.to_xml_string(),expected))
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())
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)
165 stream = open(datadir+
'pr2_desc.2.srdf',
'r')
166 robot = SRDF.from_xml_string(stream.read())
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)
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())
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)
186 self.assertTrue(robot.virtual_joints[0].name==
"world_joint")
187 self.assertTrue(robot.virtual_joints[0].type==
"planar")
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)
204 if robot.group_states[0].group !=
"arms":
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")
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)
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)
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')
228 if __name__ ==
'__main__':
230 rostest.rosrun(PKG,
'srdf_python_parser_test', TestSRDFParser)