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 sys
00036
00037 import roslib
00038 roslib.load_manifest('starmac_vicon_testing')
00039 import numpy as np
00040 import matplotlib.pyplot as plt
00041 from matplotlib.patches import Ellipse
00042
00043 from starmac_tools.load_bag import BagLoader
00044
00045 def add_circle(x, y, radius, **kwargs):
00046 e = Ellipse((x,y), radius*2., radius*2., fill=False, **kwargs)
00047 plt.gca().add_artist(e)
00048 return e
00049
00050 def add_title_and_labels(title=None,xlabel=None,ylabel=None):
00051 if title is not None:
00052 plt.title(title)
00053 if xlabel is not None:
00054 plt.xlabel(xlabel)
00055 if ylabel is not None:
00056 plt.ylabel(ylabel)
00057
00058 def newfig(subplot=111,title=None,xlabel=None,ylabel=None):
00059 plt.figure()
00060 plt.grid(True)
00061 plt.subplot(subplot)
00062 add_title_and_labels(title, xlabel, ylabel)
00063
00064 def subplot_vs_time(s,title=None,ylabel=None):
00065 subplot(s,title=title,xlabel='t [s]',ylabel=ylabel)
00066
00067 def subplot(s,title=None,xlabel=None,ylabel=None):
00068 plt.subplot(s)
00069 plt.grid(True)
00070 add_title_and_labels(title, xlabel, ylabel)
00071
00072 def fig_xyz():
00073 newfig(subplot=311)
00074 subplot_vs_time(311,title='x',ylabel='[m]')
00075 plt.plot(t,x,'.')
00076 plt.axhline(meanval[0], color='r')
00077 subplot_vs_time(312,title='y',ylabel='[m]')
00078 plt.plot(t,y,'.')
00079 plt.axhline(meanval[1], color='r')
00080 subplot_vs_time(313,title='z',ylabel='[m]')
00081 plt.plot(t,z,'.')
00082 plt.axhline(meanval[2], color='r')
00083
00084 def fig_qxyzw():
00085 chars = 'xyzw'
00086 newfig(subplot=221)
00087 for i, char in zip(range(4),chars):
00088 subplot_vs_time(220+i,title='q'+char)
00089 plt.plot(t,globals()['q'+char],'.')
00090
00091 def fig_dt():
00092 newfig(title="dt[k] := t[k] - t[k-1]", ylabel='dt [s]', xlabel='t [s]')
00093 plt.plot(t[1:],dt)
00094
00095 def fig_xy():
00096 newfig(title="Top view (y vs. x)",xlabel="x [m]",ylabel="y [m]")
00097 plt.plot(x,y,'.')
00098 plt.axis('equal')
00099 plt.plot(meanval[0],meanval[1],'rx')
00100 add_circle(meanval[0],meanval[1],0.001)
00101 add_circle(meanval[0],meanval[1],0.01)
00102
00103 def fig_xz():
00104 newfig(title="Side view (z vs. x)",xlabel="x [m]",ylabel="z [m]")
00105 plt.plot(z,x,'.')
00106 plt.axis('equal')
00107 plt.plot(meanval[2],meanval[0],'rx')
00108 add_circle(meanval[2],meanval[0],0.001)
00109 add_circle(meanval[2],meanval[0],0.01)
00110
00111 def fig_yz():
00112 newfig(title="Side view (z vs. y)",xlabel="y [m]",ylabel="z [m]")
00113 plt.plot(z,y,'.')
00114 plt.axis('equal')
00115 plt.plot(meanval[2],meanval[1],'rx')
00116 add_circle(meanval[2],meanval[1],0.001)
00117 add_circle(meanval[2],meanval[1],0.01)
00118
00119 def load_bag(bagfile):
00120 bag = BagLoader(bagfile)
00121
00122 t = bag.vicon__recv__direct_output___header__time
00123
00124 (x, y, z) = (bag.vicon__recv__direct_output_transform_translation_x,
00125 bag.vicon__recv__direct_output_transform_translation_y,
00126 bag.vicon__recv__direct_output_transform_translation_z)
00127
00128 (qx, qy, qz, qw) = ( bag.vicon__recv__direct_output_transform_rotation_x,
00129 bag.vicon__recv__direct_output_transform_rotation_y,
00130 bag.vicon__recv__direct_output_transform_rotation_z,
00131 bag.vicon__recv__direct_output_transform_rotation_w)
00132
00133 return t, x, y, z, qx, qy, qz, qw
00134
00135 def load_npz(npzfile):
00136 npzfile = np.load(npzfile)
00137 return tuple(npzfile[foo] for foo in ('t','x','y','z','qx','qy','qz','qw'))
00138
00139 def save_npz(npzfile):
00140 np.savez(npzfile, **{'x':x,'y':y,'z':z,'t':t,'qx':qx,'qy':qy,'qz':qz,'qw':qw})
00141
00142 if __name__ == '__main__':
00143 fname = sys.argv[1]
00144
00145 if fname.endswith('.bag'):
00146 t,x,y,z, qx, qy, qz, qw = load_bag(fname)
00147 elif fname.endswith('.npz'):
00148 t,x,y,z, qx, qy, qz, qw = load_npz(fname)
00149
00150 meanval = np.array([np.mean(foo) for foo in (x,y,z)])
00151
00152 dt = t[1:]-t[:-1]
00153 fig_xyz()
00154 fig_dt()
00155 fig_xy()
00156 fig_xz()
00157 fig_yz()
00158 fig_qxyzw()
00159
00160 plt.show()