track_utils.py
Go to the documentation of this file.
00001 import sys
00002 from articulation_msgs.msg import *
00003 from articulation_msgs.srv import *
00004 from geometry_msgs.msg import Pose, Point, Quaternion
00005 from sensor_msgs.msg import ChannelFloat32
00006 import logging
00007 import fileinput
00008 import numpy
00009 import tf
00010 from tf import transformations
00011 import glob
00012 import rospy
00013 import copy
00014 
00015 def readtrack(filename):
00016   rospy.loginfo("Opening logfile '%s'", filename)
00017   
00018   msg = TrackMsg()
00019   channel_w = None
00020   channel_h = None
00021   for line in fileinput.input(filename):
00022     line = line.strip()
00023 #    rospy.loginfo("Reading line: '%s'",line)
00024     data = line.strip().split()
00025     data_float = [float(column) for column in data]
00026     if len(data) < 3:
00027        continue
00028 
00029     msg.header.stamp = rospy.get_rostime()
00030     msg.header.frame_id = "/"
00031     msg.id = 0
00032     pose = Pose(Point(0, 0, 0), Quaternion(0, 0, 0, 1))
00033     
00034     if len(data) >= 3:
00035       pose.position.x = data_float[0]
00036       pose.position.y = data_float[1]
00037       pose.position.z = data_float[2]
00038       
00039     if len(data) >= 7:
00040       pose.orientation.x = data_float[3]
00041       pose.orientation.y = data_float[4]
00042       pose.orientation.z = data_float[5]
00043       pose.orientation.w = data_float[6]
00044       
00045     if len(data) >= 9:
00046       if channel_w is None or channel_h is None:
00047         channel_w = ChannelFloat32('width', [])
00048         channel_h = ChannelFloat32('height', [])
00049         msg.channels.append(channel_w)
00050         msg.channels.append(channel_h)
00051       channel_w.values.append(data_float[7])
00052       channel_h.values.append(data_float[8])
00053       
00054     msg.pose.append(pose)
00055   return msg
00056 
00057 def readtrack2(filename):
00058   rospy.loginfo("Opening logfile '%s'", filename)
00059   
00060   msg = TrackMsg()
00061   
00062   for line_number,line in enumerate( fileinput.input(filename) ):
00063     line = line.strip()
00064     data = line.strip().split()
00065     if data[0]=="#":
00066       # treat the first comment as a field list (when it starts with "x y z .."
00067       if len(msg.channels) == 0:
00068         for a,b in zip(data[1:8],["x","y","z","qx","qy","qz","qw"]):
00069           if a!=b:
00070             continue
00071         for field in data[2:]:
00072           ch = ChannelFloat32(field, [])
00073           msg.channels.append(ch)
00074       continue
00075       
00076     data_float = [float(column) for column in data]
00077     if len(data) < 3:
00078        continue
00079 
00080     if len(data) != len(msg.channels):
00081       raise  BaseException("invalid number of fields!! line %d has only %d fields, should have %d! line: \n'%s'" % (
00082         line_number,len(data),len(msg.channels),line))
00083 
00084     msg.header.stamp = rospy.get_rostime()
00085     msg.header.frame_id = "/"
00086     msg.id = 0
00087     pose = Pose(Point(0, 0, 0), Quaternion(0, 0, 0, 1))
00088     
00089     if len(data) >= 3:
00090       pose.position.x = data_float[0]
00091       pose.position.y = data_float[1]
00092       pose.position.z = data_float[2]
00093       
00094     if len(data) >= 7:
00095       pose.orientation.x = data_float[3]
00096       pose.orientation.y = data_float[4]
00097       pose.orientation.z = data_float[5]
00098       pose.orientation.w = data_float[6]
00099       
00100     msg.pose.append(pose)
00101     
00102     for i,ch in enumerate(msg.channels):
00103       ch.values.append(data_float[i])
00104       
00105   return msg
00106 
00107 def downsample(track,interval):
00108   track2 = TrackMsg()
00109   track2.header = track.header
00110   track2.id = track.id
00111   track2.pose = [p for i,p in enumerate(track.pose) if i%interval==0] 
00112   for channel in track.channels:
00113     ch = ChannelFloat32()
00114     ch.name = channel.name
00115     ch.values = [p for i,p in enumerate(channel.values) if i%interval==0]
00116     track2.channels.append(ch)
00117   return track2
00118 
00119 
00120 def sub_track(track, begin, end):
00121   track2 = TrackMsg()
00122   track2.header = track.header
00123   track2.id = track.id
00124   track2.pose = track.pose[begin:end]
00125   for channel in track.channels:
00126     ch = ChannelFloat32()
00127     ch.name = channel.name
00128     ch.values = channel.values[begin:end]
00129     track2.channels.append(ch)
00130   return track2
00131 
00132 def xyz_to_mat44(pos):
00133     return transformations.translation_matrix((pos.x, pos.y, pos.z))
00134 
00135 def xyzw_to_mat44(ori):
00136     return transformations.quaternion_matrix((ori.x, ori.y, ori.z, ori.w))
00137 
00138 def pose_to_numpy( p ):
00139   return numpy.dot( xyz_to_mat44( p.position ), xyzw_to_mat44( p.orientation ) );  
00140   
00141 def numpy_to_pose( P ):
00142   xyz = tuple(transformations.translation_from_matrix(P))[:3]
00143   quat = tuple(transformations.quaternion_from_matrix(P))
00144   return geometry_msgs.msg.Pose(geometry_msgs.msg.Point(*xyz), geometry_msgs.msg.Quaternion(*quat));
00145   
00146 def zero_start(poses):
00147   if len(poses)==0:
00148     return poses;
00149   ref = pose_to_numpy( poses[0] );
00150   
00151   zero_poses = [ numpy_to_pose( numpy.dot( transformations.inverse_matrix(ref), pose_to_numpy(p) ) ) for p in poses]
00152   return zero_poses
00153 
00154 def set_param(model,paramname,value,type):
00155   for p in model.params:
00156     if(p.name==paramname):
00157       p.value = value
00158       if(p.type != type):
00159         raise NameError('parameter %s exists but has different type'%paramname)
00160       return
00161   model.params.append(ParamMsg(paramname,value,type));
00162   
00163 def get_param(model, paramname):
00164   list = [p.value for p in model.params if p.name == paramname]
00165   
00166   if len(list)==0:
00167     raise NameError('parameter %s not found in list'%paramname)
00168   if len(list)>1:
00169     raise NameError('parameter %s not unique!'%paramname)
00170 
00171   return list[0]
00172 
00173 def get_channel(model, channelname):
00174   l = [p for p in model.track.channels if p.name == channelname]
00175   
00176   if len(l)==0:
00177     raise NameError('channel %s not found; channels=[%s]'%(channelname," ".join([ch.name for ch in model.track.channels])))
00178   if len(l)>1:
00179     raise NameError('channel %s not unique!'%channelname)
00180 
00181   l[0].values = list(l[0].values)
00182   return l[0].values
00183 
00184 def get_channel_track(track, channelname):
00185   l = [p for p in track.channels if p.name == channelname]
00186   
00187   if len(l)==0:
00188     raise NameError('channel %s not found in list'%channelname)
00189   if len(l)>1:
00190     raise NameError('channel %s not unique!'%channelname)
00191 
00192   l[0].values = list(l[0].values)
00193   return l[0].values
00194   
00195 def sort_with(l,k):
00196   lk = zip(k,l)
00197   lk.sort()
00198   l = [a[1] for a in lk]
00199   return l 
00200 
00201 def sort_by_configuration(model):
00202   if get_param(model,"dofs")==0:
00203     return model
00204   q =  copy.deepcopy( get_channel(model,'q0') )
00205   
00206   model.track.pose = sort_with(model.track.pose,q)
00207   model.track.pose_projected = sort_with(model.track.pose_projected,q);
00208   for ch in model.track.channels:
00209     ch.values = sort_with(ch.values,q)
00210 
00211   return model
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


articulation_models
Author(s): Juergen Sturm
autogenerated on Wed Dec 26 2012 15:35:18