00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import roslib
00036 roslib.load_manifest('pioneer_arm_description')
00037 import rospy
00038
00039 import sys
00040 import argparse
00041 import subprocess
00042 import tempfile
00043 from xml.dom.minidom import parse, parseString
00044
00045
00046 def xacro_function_evaluator(file, xacro=None, robot=None, pairs=None):
00047
00048
00049 xacro_xml = '<?xml version="1.0" ?>\n'
00050 xacro_xml += '<robot '
00051
00052 if robot:
00053 xacro_xml += 'name="%s" ' % robot
00054 xacro_xml += 'xmlns:xacro="http://ros.org/wiki/xacro">\n'
00055 xacro_xml += '\t<include filename="%s" />\n' % file
00056
00057
00058 if xacro:
00059 xacro_xml += '\t<xacro:%s ' % xacro
00060
00061 for pair in pairs:
00062 xacro_xml += '%s="%s" ' % (pair[0], pair[1])
00063 xacro_xml += '/>\n'
00064
00065
00066 xacro_xml += '</robot>\n'
00067
00068
00069 temp = tempfile.NamedTemporaryFile(mode='w+', suffix='.xml')
00070 temp.write(xacro_xml)
00071 temp.seek(0)
00072
00073
00074 p = subprocess.Popen(["rosrun", "xacro", "xacro.py", temp.name], stdout=subprocess.PIPE)
00075 output = p.communicate()[0]
00076
00077
00078 temp.close()
00079
00080
00081 return output
00082
00083
00084 def model_inserter(xml1, xml2):
00085
00086
00087 try:
00088 model1_dom = parseString(xml1)
00089 except:
00090 raise Exception("Unable to parse model1")
00091
00092
00093 try:
00094 model2_dom = parseString(xml2)
00095 except:
00096 raise Exception("Unable to parse model2")
00097
00098
00099 node = model2_dom.documentElement.firstChild
00100 while node:
00101 if node.nodeType == node.ELEMENT_NODE:
00102 model1_dom.documentElement.appendChild(node.cloneNode(deep=True))
00103 node = node.nextSibling
00104
00105
00106 return model1_dom.toprettyxml(indent=" ")
00107
00108
00109 if __name__ == "__main__":
00110
00111
00112 parser = argparse.ArgumentParser(description='Insert Pioneer Arm model into an existing URDF/robot_description. The output will be streamed to stdout.')
00113 parser.add_argument('-f', '--file', required=True, help='xml/xacro file descriping the parent model')
00114 parser.add_argument('-l', '--link', required=True, help='link name in the parent model to attach the Pioneer Arm')
00115 parser.add_argument('-x', '--x', type=float, default=0.0, help='X-offset from link origin')
00116 parser.add_argument('-y', '--y', type=float, default=0.0, help='Y-offset from link origin')
00117 parser.add_argument('-z', '--z', type=float, default=0.0, help='Z-offset from link origin')
00118 parser.add_argument('-R', '--roll', type=float, default=0.0, help='Roll-offset from link origin')
00119 parser.add_argument('-P', '--pitch', type=float, default=0.0, help='Pitch-offset from link origin')
00120 parser.add_argument('-Y', '--yaw', type=float, default=0.0, help='Yaw-offset from link origin')
00121
00122 args, unknown_args = parser.parse_known_args()
00123
00124
00125 parent_xml = xacro_function_evaluator(file=args.file, robot='robot')
00126
00127
00128 arm_xml = xacro_function_evaluator(file='$(find pioneer_arm_description)/urdf/pioneer_arm.urdf.xacro', xacro='pioneer_arm_urdf', pairs=[['parent', args.link], ['robot_description', 'robot_description'], ['x', args.x], ['y', args.y], ['z', args.z], ['roll', args.roll], ['pitch', args.pitch], ['yaw', args.yaw]])
00129
00130
00131 robot_xml = model_inserter(parent_xml, arm_xml)
00132
00133
00134 sys.stdout.write(robot_xml)
00135