plot_vicon.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 #  Copyright (c) 2011, UC Regents
00006 #  All rights reserved.
00007 #
00008 #  Redistribution and use in source and binary forms, with or without
00009 #  modification, are permitted provided that the following conditions
00010 #  are met:
00011 #
00012 #   * Redistributions of source code must retain the above copyright
00013 #     notice, this list of conditions and the following disclaimer.
00014 #   * Redistributions in binary form must reproduce the above
00015 #     copyright notice, this list of conditions and the following
00016 #     disclaimer in the documentation and/or other materials provided
00017 #     with the distribution.
00018 #   * Neither the name of the University of California nor the names of its
00019 #     contributors may be used to endorse or promote products derived
00020 #     from this software without specific prior written permission.
00021 #
00022 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 #  POSSIBILITY OF SUCH DAMAGE.
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()


starmac_vicon_testing
Author(s): Patrick Bouffard
autogenerated on Sun Jan 5 2014 11:39:10