xapparser.py
Go to the documentation of this file.
00001 # # -*- coding: utf-8 -*-
00002 
00003 # Copyright 2013 Séverin Lemaignan
00004 # http://www.ros.org/wiki/nao
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #
00009 #    # Redistributions of source code must retain the above copyright
00010 #       notice, this list of conditions and the following disclaimer.
00011 #    # Redistributions in binary form must reproduce the above copyright
00012 #       notice, this list of conditions and the following disclaimer in the
00013 #       documentation and/or other materials provided with the distribution.
00014 #    # Neither the name of the University of Freiburg nor the names of its
00015 #       contributors may be used to endorse or promote products derived from
00016 #       this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 # POSSIBILITY OF SUCH DAMAGE.
00029 #
00030 
00031 import os
00032 import math
00033 import xml.etree.ElementTree as ET
00034 
00035 NS="{http://www.aldebaran-robotics.com/schema/choregraphe/position.xsd}"
00036 
00037 def _makeJointDict(motors, use_radians = True):
00038     pose = {}
00039     for p in motors.findall(NS + "Motor"):
00040         name = p.find(NS + 'name').text
00041         value = float(p.find(NS + 'value').text)
00042         if not use_radians:
00043             value = math.radians(value)
00044 
00045         pose[name] = value
00046 
00047     return pose
00048 
00049 def getpostures(xap_file):
00050     """ Parses a Aldebaran Choregraphe posture library (.xap files)
00051     into a Python dictionary of postures.
00052     """
00053 
00054     if not os.path.exists(xap_file):
00055         raise RuntimeError("The XAP file %s does not exist." % xap_file)
00056 
00057     try:
00058         tree = ET.parse(xap_file)
00059     except ET.ParseError:
00060         raise RuntimeError("The XAP file %s is not a valid XML file." % xap_file)
00061 
00062 
00063     root=tree.getroot()
00064 
00065     postures = {}
00066 
00067 
00068     positions = [p for p in root.iter(NS + 'position')]
00069 
00070     if not positions:
00071         raise RuntimeError("The XAP file %s does not contain any pose." % xap_file)
00072 
00073     for p in positions:
00074         name = p.find(NS + 'name').text
00075         version = p.find(NS + 'version') # it *seems* that the 'version' 2 indicates joints stored in radians
00076         pose = _makeJointDict(p.find(NS + "Motors"), version is not None and version.text=='2')
00077 
00078         postures[name] = pose
00079 
00080     return postures
00081 
00082 if __name__ == "__main__":
00083 
00084     import sys
00085     if len(sys.argv) != 2:
00086         print("Usage: python xapparser.py <file.xap>")
00087         sys.exit(1)
00088 
00089     print(getpostures(sys.argv[1]))
00090 


nao_pose
Author(s): Armin Hornung, Séverin Lemaignan
autogenerated on Sat Jun 27 2015 11:22:34