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_parent_link = "";
00066     for f in doc.getElementsByTagName('library_kinematics_models')[0].getElementsByTagName("technique_common")[0].getElementsByTagName("link"):
00067         if f.getAttribute("name") == parent_link:
00068             tmp_parent_link = f.getAttribute("sid");
00069     kmodel_id = doc.getElementsByTagName('library_kinematics_models')[0].getElementsByTagName("kinematics_model")[0].getAttribute("id")
00070     tmp.setAttribute('link', '%s/%s'%(kmodel_id,tmp_parent_link))
00071     if translate == None:
00072         tl = doc.createElement('translate')
00073         if name == "lhsensor":
00074             tl.appendChild(doc.createTextNode('0 0.12 0'))
00075         elif name == "rhsensor":
00076             tl.appendChild(doc.createTextNode('0 -0.12 0'))
00077         else:
00078             tl.appendChild(doc.createTextNode('0 0 0'))
00079     else:
00080         tl = doc.createElement('translate')
00081         tl.appendChild(doc.createTextNode(translate))
00082 
00083     if rotate == None:
00084         ro = doc.createElement('rotate')
00085         #ro.appendChild(doc.createTextNode('1 0 0 0'))
00086         if name == "lhsensor" or name == "rhsensor":
00087             ro.appendChild(doc.createTextNode('0 0 1 180'))
00088         else:
00089             ro.appendChild(doc.createTextNode('1 0 0 0'))
00090     else:
00091         ro = doc.createElement('rotate')
00092         ro.appendChild(doc.createTextNode(rotate))
00093 
00094     tmp.appendChild(tl)
00095     tmp.appendChild(ro)
00096 
00097     tec.appendChild(tmp)
00098     ex.appendChild(tec)
00099     target_articulated_system.appendChild(ex)
00100 
00101     ### add to library_sensors
00102     plst = library_sensors_node.getElementsByTagName('technique')
00103     targetNode = None
00104     for p in plst:
00105         if p.hasAttribute('profile') and p.getAttribute('profile') == 'OpenRAVE':
00106             targetNode = p
00107 
00108     if targetNode != None:
00109         sen = doc.createElement('sensor')
00110         sen.setAttribute('id', 'sensor%d' % (sensor_num - 1))
00111 
00112         if sensor_type == 'force':
00113             sen.setAttribute('type', 'base_force6d')
00114             sen.setAttribute('sid', '%d'%force_sensor_num)
00115             force_sensor_num = force_sensor_num + 1
00116             lf = doc.createElement('load_range_force')
00117             lf.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00118             lt = doc.createElement('load_range_torque')
00119             lt.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00120             sen.appendChild(lf)
00121             sen.appendChild(lt)
00122         elif sensor_type == 'acceleration':
00123             sen.setAttribute('type', 'base_imu')
00124             sen.setAttribute('sid', '%d'%acc_sensor_num)
00125             acc_sensor_num = acc_sensor_num + 1
00126             max_acc = doc.createElement('max_acceleration')
00127             max_acc.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00128             sen.appendChild(max_acc)
00129         elif sensor_type == 'gyro':
00130             sen.setAttribute('type', 'base_imu')
00131             sen.setAttribute('sid', '%d'%gyro_sensor_num)
00132             gyro_sensor_num = gyro_sensor_num + 1
00133             max_ang = doc.createElement('max_angular_velocity')
00134             max_ang.appendChild(doc.createTextNode('-1.0 -1.0 -1.0'))
00135             sen.appendChild(max_ang)
00136 
00137         targetNode.appendChild(sen)
00138 
00139 def initialize_for_add_sensor ():
00140     global sensor_num, library_sensors_node, target_articulated_system, doc
00141     global force_sensor_num, gyro_sensor_num, acc_sensor_num
00142     argvs = sys.argv
00143     argc = len(argvs)
00144     if argc > 1:
00145         fname = argvs[1]
00146     else:
00147         print 'usage: %s intput_filename > outputfile'
00148         exit(0)
00149 
00150     sensor_num = 1
00151     force_sensor_num = 0
00152     gyro_sensor_num = 0
00153     acc_sensor_num = 0
00154 
00155     doc = xml.dom.minidom.parse(fname)
00156 
00157     plst = doc.getElementsByTagName('extra')
00158     library_sensors_node = None
00159     for p in plst:
00160         if p.hasAttribute('id') and p.getAttribute('id') == 'sensors' and p.hasAttribute('type') and p.getAttribute('type') == 'library_sensors':
00161             library_sensors_node = p
00162 
00163     plst = doc.getElementsByTagName('articulated_system')
00164     target_articulated_system = None
00165     for p in plst:
00166         if p.hasAttribute('id') and p.getAttribute('id').find('_motion') != -1:
00167             target_articulated_system = p
00168     return library_sensors_node != None and target_articulated_system != None
00169 
00170 if __name__ == '__main__':
00171     if initialize_for_add_sensor():
00172         ## now added sensors are hard coded....
00173         add_sensor(doc, 'lhsensor', 'l_hand', 'force')
00174         add_sensor(doc, 'rhsensor', 'r_hand', 'force')
00175         add_sensor(doc, 'lfsensor', 'l_foot', 'force')
00176         add_sensor(doc, 'rfsensor', 'r_foot', 'force')
00177         add_sensor(doc, 'gsensor', 'imu_link', 'acceleration')
00178         add_sensor(doc, 'gyrometer', 'imu_link', 'gyro')
00179         #add_sensor(doc, 'lgsensor', 'l_hand', 'acceleration')
00180         #add_sensor(doc, 'lgyrometer', 'l_hand', 'gyro')
00181         #add_sensor(doc, 'rgsensor', 'r_hand', 'acceleration')
00182         #add_sensor(doc, 'rgyrometer', 'r_hand', 'gyro')
00183         #add_sensor(doc, 'hgsensor', 'head_imu_link', 'acceleration')
00184         #add_sensor(doc, 'hgyrometer', 'head_imu_link', 'gyro')
00185         sys.stdout.write(doc.toprettyxml(indent = '\t'))


hrpsys_gazebo_atlas
Author(s): Yohei Kakiuchi
autogenerated on Wed Sep 16 2015 10:53:59