xml_testdata_loader.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2018 Pilz GmbH & Co. KG
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import xml.etree.ElementTree as ET
17 import re
18 from geometry_msgs.msg import Pose
19 from math import pi
20 
21 DEFAULT_VEL = 0.01
22 DEFAULT_ACC = 0.01
23 DEFAULT_BLEND_RADIUS = 0.01
24 
25 NAME_STR = "name"
26 TYPE_STR = "type"
27 BLEND_RADIUS_STR = "blend_radius"
28 
29 START_POS_STR = "startPos"
30 END_POS_STR = "endPos"
31 PLANNING_GROUP_STR = "planningGroup"
32 LINK_NAME_STR = "targetLink"
33 INTERIM_POS_STR = "interimPos"
34 CENTER_POS_STR = "centerPos"
35 AUXILIARY_POS_STR = "auxiliaryPos"
36 DEFAULT_LINK_NAME = "prbt_tcp"
37 
38 VEL_STR = "vel"
39 ACC_STR = "acc"
40 
41 _PATH_TO_JOINTS = "./poses/pos[@name='{pose_name}']/group[@name='{group_name}']/joints"
42 _PATH_TO_POSE = "./poses/pos[@name='{pose_name}']/group[@name='{group_name}']/xyzQuat"
43 _PATH_TO_PTP = "./ptps/ptp[@name='{name_of_cmd}']"
44 _PATH_TO_LIN = "./lins/lin[@name='{name_of_cmd}']"
45 _PATH_TO_CIRC = "./circs/circ[@name='{name_of_cmd}']"
46 _PATH_TO_SEQUENCE = "./sequences/sequence[@name='{name_of_cmd}']"
47 
48 
50 
51  def __init__(self, path_to_xml_file):
52  self._tree = ET.parse(path_to_xml_file)
53  self._root = self._tree.getroot()
54 
55  # Returns the joint values for the given group and position.
56  def get_joints(self, pose_name, group_name):
57  joint_node = self._root.find(_PATH_TO_JOINTS.format(pose_name=pose_name, group_name=group_name))
58  if joint_node is None:
59  return None
60  return [eval(elem) for elem in re.split(r'[^\S\n\t]+', joint_node.text)]
61 
62  def get_pose(self, pose_name, group_name):
63  node = self._root.find(_PATH_TO_POSE.format(pose_name=pose_name, group_name=group_name))
64  if node is None:
65  return None
66  pose_list = [eval(elem) for elem in re.split(r'[^\S\n\t]+', node.text)]
67  pose = Pose()
68  pose.position.x = pose_list[0]
69  pose.position.y = pose_list[1]
70  pose.position.z = pose_list[2]
71  pose.orientation.x = pose_list[3]
72  pose.orientation.y = pose_list[4]
73  pose.orientation.z = pose_list[5]
74  pose.orientation.w = pose_list[6]
75  return pose
76 
77  # Returns the start- and end-position, as well as
78  # the velocity and acceleration of the ptp command given by its name.
79  # In case of an error 'None' is returned.
80  def get_ptp(self, name_of_cmd):
81  return self._get_cmd(_PATH_TO_PTP, name_of_cmd)
82 
83  # Returns the start- and end-position, as well as
84  # the velocity and acceleration of the lin command given by its name.
85  # In case of an error 'None' is returned.
86  def get_lin(self, name_of_cmd):
87  return self._get_cmd(_PATH_TO_LIN, name_of_cmd)
88 
89  # Returns the start-, end- and auxility-position, as well as
90  # the velocity and acceleration of the circ command given by its name.
91  #
92  # Please note: It is also necessary to state if the auxiliary point
93  # of the circ command is stored as intermediate or center point.
94  def get_circ(self, name_of_cmd, auxiliaray_pos_type=INTERIM_POS_STR):
95  cmdRes = self._get_cmd(_PATH_TO_CIRC, name_of_cmd)
96  if cmdRes is None:
97  return None
98 
99  cmdNode = self._root.find(_PATH_TO_CIRC.format(name_of_cmd=name_of_cmd))
100  if cmdNode is None:
101  return None
102  auxiliaryNode = cmdNode.find("./{}".format(auxiliaray_pos_type))
103  if auxiliaryNode is None:
104  return None
105 
106  return {START_POS_STR: cmdRes[START_POS_STR], auxiliaray_pos_type: auxiliaryNode.text,
107  END_POS_STR: cmdRes[END_POS_STR], VEL_STR: cmdRes[VEL_STR], ACC_STR: cmdRes[ACC_STR],
108  PLANNING_GROUP_STR: cmdRes[PLANNING_GROUP_STR], LINK_NAME_STR: cmdRes[LINK_NAME_STR]}
109 
110  # Returns a list of dictionaries containing the cmds which make-up the
111  # sequence cmd. The cmds in the list are in the order of execution.
112  # In case of an error 'None' is returned.
113  def get_sequence(self, name_of_cmd):
114  # Find the sequence command with the given name
115  sequenceNode = self._root.find(_PATH_TO_SEQUENCE.format(name_of_cmd=name_of_cmd))
116  if sequenceNode is None:
117  return None
118 
119  # Loop over all blend commands
120  sequenceCmds = []
121  for sequenceCmdNode in sequenceNode.getchildren():
122  cmd_name = sequenceCmdNode.get(NAME_STR)
123  if cmd_name is None:
124  return None
125 
126  cmd_type = sequenceCmdNode.get(TYPE_STR)
127  if cmd_type is None:
128  return None
129 
130  blend_radius = sequenceCmdNode.get(BLEND_RADIUS_STR, DEFAULT_BLEND_RADIUS)
131  sequenceCmds.append({NAME_STR: cmd_name, TYPE_STR: cmd_type, BLEND_RADIUS_STR: blend_radius})
132 
133  return sequenceCmds
134 
135  # Returns the start- and end-position, as well as
136  # the velocity and acceleration of the given command type, given by its name.
137  # The values are returned as dictionaries.
138  # In case of an error 'None' is returned.
139  def _get_cmd(self, path_to_cmd_type, name_of_cmd):
140  cmd_node = self._root.find(path_to_cmd_type.format(name_of_cmd=name_of_cmd))
141  if cmd_node is None:
142  return None
143 
144  start_pos_node = cmd_node.find("./{}".format(START_POS_STR))
145  if start_pos_node is None:
146  return None
147 
148  end_pos_node = cmd_node.find("./{}".format(END_POS_STR))
149  if end_pos_node is None:
150  return None
151 
152  planning_group_node = cmd_node.find("./{}".format(PLANNING_GROUP_STR))
153  if planning_group_node is None:
154  return None
155 
156  # Optional parameters
157  vel_node = cmd_node.find("./{}".format(VEL_STR))
158  vel = DEFAULT_VEL if vel_node is None else float(vel_node.text)
159 
160  acc_node = cmd_node.find("./{}".format(ACC_STR))
161  acc = DEFAULT_ACC if acc_node is None else float(acc_node.text)
162 
163  target_link_node = cmd_node.find("./{}".format(LINK_NAME_STR))
164  target_link_name = DEFAULT_LINK_NAME if target_link_node is None else target_link_node.text
165 
166  return {START_POS_STR: start_pos_node.text, END_POS_STR: end_pos_node.text, VEL_STR: vel, ACC_STR: acc,
167  PLANNING_GROUP_STR: planning_group_node.text, LINK_NAME_STR: target_link_name}
def get_circ(self, name_of_cmd, auxiliaray_pos_type=INTERIM_POS_STR)


pilz_industrial_motion_testutils
Author(s):
autogenerated on Mon Apr 6 2020 03:17:28