$search
00001 #!/usr/bin/env python 00002 ################################################################# 00003 ##\file 00004 # 00005 # \note 00006 # Copyright (c) 2011-2012 \n 00007 # Fraunhofer Institute for Manufacturing Engineering 00008 # and Automation (IPA) \n\n 00009 # 00010 ################################################################# 00011 # 00012 # \note 00013 # Project name: care-o-bot 00014 # \note 00015 # ROS stack name: cob_calibration 00016 # \note 00017 # ROS package name: cob_robot_calibration 00018 # 00019 # \author 00020 # Author: Sebastian Haug, email:sebhaug@gmail.com 00021 # \author 00022 # Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de 00023 # 00024 # \date Date of creation: January 2012 00025 # 00026 ################################################################# 00027 # 00028 # Redistribution and use in source and binary forms, with or without 00029 # modification, are permitted provided that the following conditions are met: 00030 # 00031 # - Redistributions of source code must retain the above copyright 00032 # notice, this list of conditions and the following disclaimer. \n 00033 # - Redistributions in binary form must reproduce the above copyright 00034 # notice, this list of conditions and the following disclaimer in the 00035 # documentation and/or other materials provided with the distribution. \n 00036 # - Neither the name of the Fraunhofer Institute for Manufacturing 00037 # Engineering and Automation (IPA) nor the names of its 00038 # contributors may be used to endorse or promote products derived from 00039 # this software without specific prior written permission. \n 00040 # 00041 # This program is free software: you can redistribute it and/or modify 00042 # it under the terms of the GNU Lesser General Public License LGPL as 00043 # published by the Free Software Foundation, either version 3 of the 00044 # License, or (at your option) any later version. 00045 # 00046 # This program is distributed in the hope that it will be useful, 00047 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00048 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00049 # GNU Lesser General Public License LGPL for more details. 00050 # 00051 # You should have received a copy of the GNU Lesser General Public 00052 # License LGPL along with this program. 00053 # If not, see <http://www.gnu.org/licenses/>. 00054 # 00055 ################################################################# 00056 00057 from xml.dom import minidom 00058 00059 class CalibrationUrdfUpdater(): 00060 ''' 00061 Parses calibration.urdf.xarco and provides method to update it 00062 ''' 00063 00064 def __init__(self, urdf_in, urdf_out, debug=False): 00065 ''' 00066 Init object with paths to input and output xml 00067 ''' 00068 self.file_urdf_in = urdf_in 00069 self.file_urdf_out = urdf_out 00070 self.debug = debug 00071 00072 def update(self, attributes2update): 00073 ''' 00074 Read urdf xml file (self.file_urdf_in) and replace values according to attributes2update dictionary. 00075 Save results to urdf xml file (self.file_urdf_out) 00076 00077 @param attributes2update: names of arguments (of property tags) in urdf xml file which need to be updated -> new values 00078 @type attributes2update: dictionary 00079 ''' 00080 # parse xml file to dom 00081 print "--> loading calibration xml file from '%s'" % self.file_urdf_in 00082 xml_dom = minidom.parse(file(self.file_urdf_in)) 00083 00084 # get all property elements 00085 for node in xml_dom.getElementsByTagName("property"): 00086 # sanity check, all property elements need name and value attributes 00087 assert node.hasAttribute("name") and node.hasAttribute("value") 00088 attr_name = node.getAttribute("name") 00089 attr_value = node.getAttribute("value") 00090 00091 # if attributes name is in attributes2update dict, update attributes value to new value (attributes2update[name]) 00092 if attr_name in attributes2update: 00093 attr_value_new = attributes2update[attr_name] 00094 node.setAttribute("value", str(attr_value_new)) 00095 if self.debug: 00096 print "Updating '%s' from '%s' to '%s'" % (attr_name, attr_value, attr_value_new) 00097 00098 # convert changed dom to pretty xml string 00099 xml_string = xml_dom.toprettyxml(indent="", newl="") 00100 00101 # save string to xml_out 00102 print "--> saving results to calibration xml file '%s'" % self.file_urdf_out 00103 f = open(self.file_urdf_out, "w") 00104 f.writelines(xml_string) 00105 f.close()