add_sensor_to_collada.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys, os
00004 from xml.dom.minidom import parse, parseString
00005 import xml.dom
00006 
00007 reload(sys)
00008 sys.setdefaultencoding('utf-8')
00009 
00010 #### >>> copied from xacro/src/xacro.py
00011 # Better pretty printing of xml
00012 # Taken from http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/
00013 def fixed_writexml(self, writer, indent="", addindent="", newl=""):
00014     # indent = current indentation
00015     # addindent = indentation to add to higher levels
00016     # newl = newline string
00017     writer.write(indent+"<" + self.tagName)
00018 
00019     attrs = self._get_attributes()
00020     a_names = attrs.keys()
00021     a_names.sort()
00022 
00023     for a_name in a_names:
00024         writer.write(" %s=\"" % a_name)
00025         xml.dom.minidom._write_data(writer, attrs[a_name].value)
00026         writer.write("\"")
00027     if self.childNodes:
00028         if len(self.childNodes) == 1 \
00029           and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
00030             writer.write(">")
00031             self.childNodes[0].writexml(writer, "", "", "")
00032             writer.write("</%s>%s" % (self.tagName, newl))
00033             return
00034         writer.write(">%s"%(newl))
00035         for node in self.childNodes:
00036             if node.nodeType is not xml.dom.minidom.Node.TEXT_NODE: # 3:
00037                 node.writexml(writer,indent+addindent,addindent,newl) 
00038                 #node.writexml(writer,indent+addindent,addindent,newl)
00039         writer.write("%s</%s>%s" % (indent,self.tagName,newl))
00040     else:
00041         writer.write("/>%s"%(newl))
00042 # replace minidom's function with ours
00043 xml.dom.minidom.Element.writexml = fixed_writexml
00044 #### <<< copied from xacro/src/xacro.py
00045 
00046 ## kinematics_model -> id
00047 def add_sensor (doc, name, parent_link, sensor_type, translate = None, rotate = None):
00048     global sensor_num, library_sensors_node, target_articulated_system
00049     global force_sensor_num, gyro_sensor_num, acc_sensor_num
00050 
00051     ### add sensor to articulated system
00052     ex = doc.createElement('extra')
00053     ex.setAttribute('name', name)
00054     ex.setAttribute('type', 'attach_sensor')
00055 
00056     tec = doc.createElement('technique')
00057     tec.setAttribute('profile', 'OpenRAVE')
00058 
00059     tmp = doc.createElement('instance_sensor')
00060     tmp.setAttribute('url', '#sensor%d' % sensor_num)
00061     sensor_num = sensor_num + 1
00062     tec.appendChild(tmp)
00063 
00064     tmp = doc.createElement('frame_origin')
00065     tmp.setAttribute('link', 'kmodel0/%s'%parent_link)
00066     if translate == None:
00067         tl = doc.createElement('translate')
00068         tl.appendChild(doc.createTextNode('0 0 0'))
00069     else:
00070         tl = doc.createElement('translate')
00071         tl.appendChild(doc.createTextNode(translate))
00072 
00073     if rotate == None:
00074         ro = doc.createElement('rotate')
00075         ro.appendChild(doc.createTextNode('1 0 0 0'))
00076     else:
00077         ro = doc.createElement('rotate')
00078         ro.appendChild(doc.createTextNode(rotate))
00079 
00080     tmp.appendChild(tl)
00081     tmp.appendChild(ro)
00082 
00083     tec.appendChild(tmp)
00084     ex.appendChild(tec)
00085     target_articulated_system.appendChild(ex)
00086 
00087     ### add to library_sensors
00088     plst = library_sensors_node.getElementsByTagName('technique')
00089     targetNode = None
00090     for p in plst:
00091         if p.hasAttribute('profile') and p.getAttribute('profile') == 'OpenRAVE':
00092             targetNode = p
00093 
00094     if targetNode != None:
00095         sen = doc.createElement('sensor')
00096         sen.setAttribute('id', 'sensor%d' % (sensor_num - 1))
00097 
00098         if sensor_type == 'force':
00099             sen.setAttribute('type', 'base_force6d')
00100             sen.setAttribute('sid', '%d'%force_sensor_num)
00101             force_sensor_num = force_sensor_num + 1
00102             lf = doc.createElement('load_range_force')
00103             lf.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00104             lt = doc.createElement('load_range_torque')
00105             lt.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00106             sen.appendChild(lf)
00107             sen.appendChild(lt)
00108         elif sensor_type == 'acceleration':
00109             sen.setAttribute('type', 'base_imu')
00110             sen.setAttribute('sid', '%d'%acc_sensor_num)
00111             acc_sensor_num = acc_sensor_num + 1
00112             max_acc = doc.createElement('max_acceleration')
00113             max_acc.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00114             sen.appendChild(max_acc)
00115         elif sensor_type == 'gyro':
00116             sen.setAttribute('type', 'base_imu')
00117             sen.setAttribute('sid', '%d'%gyro_sensor_num)
00118             gyro_sensor_num = gyro_sensor_num + 1
00119             max_ang = doc.createElement('max_angular_velocity')
00120             max_ang.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00121             sen.appendChild(max_ang)
00122 
00123         targetNode.appendChild(sen)
00124 
00125 if __name__ == '__main__':
00126     global sensor_num, library_sensors_node, target_articulated_system
00127     global force_sensor_num, gyro_sensor_num, acc_sensor_num
00128     argvs = sys.argv
00129     argc = len(argvs)
00130     if argc > 1:
00131         fname = argvs[1]
00132     else:
00133         print 'usage: %s intput_filename > outputfile'
00134         exit(0)
00135 
00136     sensor_num = 1
00137     force_sensor_num = 0
00138     gyro_sensor_num = 0
00139     acc_sensor_num = 0
00140 
00141     doc = xml.dom.minidom.parse(fname)
00142 
00143     plst = doc.getElementsByTagName('extra')
00144     library_sensors_node = None
00145     for p in plst:
00146         if p.hasAttribute('id') and p.getAttribute('id') == 'sensors' and p.hasAttribute('type') and p.getAttribute('type') == 'library_sensors':
00147             library_sensors_node = p
00148 
00149     plst = doc.getElementsByTagName('articulated_system')
00150     target_articulated_system = None
00151     for p in plst:
00152         if p.hasAttribute('id') and p.getAttribute('id') == 'robot0_motion':
00153             target_articulated_system = p
00154 
00155     if library_sensors_node != None and target_articulated_system != None:
00156         ## now added sensors are hard coded....
00157         add_sensor(doc, 'lhsensor', 'l_hand', 'force')
00158         add_sensor(doc, 'rhsensor', 'r_hand', 'force')
00159         add_sensor(doc, 'lfsensor', 'l_foot', 'force')
00160         add_sensor(doc, 'rfsensor', 'r_foot', 'force')
00161         add_sensor(doc, 'gsensor', 'imu_link', 'acceleration')
00162         add_sensor(doc, 'gyrometer', 'imu_link', 'gyro')
00163         #add_sensor(doc, 'lgsensor', 'l_hand', 'acceleration')
00164         #add_sensor(doc, 'lgyrometer', 'l_hand', 'gyro')
00165         #add_sensor(doc, 'rgsensor', 'r_hand', 'acceleration')
00166         #add_sensor(doc, 'rgyrometer', 'r_hand', 'gyro')
00167         #add_sensor(doc, 'hgsensor', 'head_imu_link', 'acceleration')
00168         #add_sensor(doc, 'hgyrometer', 'head_imu_link', 'gyro')
00169         sys.stdout.write(doc.toprettyxml(indent = '\t'))
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


hrpsys_gazebo
Author(s): Yohei Kakiuchi
autogenerated on Thu Jun 27 2013 14:58:14