xapparser.py
Go to the documentation of this file.
1 # # -*- coding: utf-8 -*-
2 
3 # Copyright 2013 Séverin Lemaignan
4 # http://www.ros.org/wiki/nao
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # # Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # # Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # # Neither the name of the University of Freiburg nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 #
30 
31 import os
32 import math
33 import xml.etree.ElementTree as ET
34 
35 NS="{http://www.aldebaran-robotics.com/schema/choregraphe/position.xsd}"
36 
37 def _makeJointDict(motors, use_radians = True):
38  pose = {}
39  for p in motors.findall(NS + "Motor"):
40  name = p.find(NS + 'name').text
41  value = float(p.find(NS + 'value').text)
42  if not use_radians:
43  value = math.radians(value)
44 
45  pose[name] = value
46 
47  return pose
48 
49 def getpostures(xap_file):
50  """ Parses a Aldebaran Choregraphe posture library (.xap files)
51  into a Python dictionary of postures.
52  """
53 
54  if not os.path.exists(xap_file):
55  raise RuntimeError("The XAP file %s does not exist." % xap_file)
56 
57  try:
58  tree = ET.parse(xap_file)
59  except ET.ParseError:
60  raise RuntimeError("The XAP file %s is not a valid XML file." % xap_file)
61 
62 
63  root=tree.getroot()
64 
65  postures = {}
66 
67 
68  positions = [p for p in root.iter(NS + 'position')]
69 
70  if not positions:
71  raise RuntimeError("The XAP file %s does not contain any pose." % xap_file)
72 
73  for p in positions:
74  name = p.find(NS + 'name').text
75  version = p.find(NS + 'version') # it *seems* that the 'version' 2 indicates joints stored in radians
76  pose = _makeJointDict(p.find(NS + "Motors"), version is not None and version.text=='2')
77 
78  postures[name] = pose
79 
80  return postures
81 
82 if __name__ == "__main__":
83 
84  import sys
85  if len(sys.argv) != 2:
86  print("Usage: python xapparser.py <file.xap>")
87  sys.exit(1)
88 
89  print(getpostures(sys.argv[1]))
90 
def _makeJointDict(motors, use_radians=True)
Definition: xapparser.py:37
def getpostures(xap_file)
Definition: xapparser.py:49


naoqi_pose
Author(s): Armin Hornung, Séverin Lemaignan
autogenerated on Thu Jul 16 2020 03:18:32