00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 import sys
00012 import getopt
00013 import os
00014 
00015 import numpy
00016 import matplotlib.pyplot as pyplot
00017 import pylab
00018 
00019 class speedData:
00020     "ART speed data analysis."
00021 
00022     def __init__(self, name=None):
00023         "Constructor method."
00024 
00025         
00026         if name:
00027             self.name = name
00028         else:
00029             self.name = 'speed'
00030 
00031         
00032         self.clear()
00033 
00034         
00035         self.pr_poly = numpy.array([0.44371437, -3.84781813,  9.19754362])
00036         
00037         self.oct_poly = numpy.array([0.40480, -3.56259, 8.69659])
00038 
00039     def cd(self, path):
00040         "Change directory to path."
00041         os.chdir(os.path.expanduser(path))
00042 
00043     def clear(self):
00044         "Clear data arrays."
00045         self.bc_t = numpy.array([])
00046         self.bc_pos = numpy.array([])
00047         self.bs_t = numpy.array([])
00048         self.bs_pos = numpy.array([])
00049         self.od_t = numpy.array([])
00050         self.od_v = numpy.array([])
00051         self.pc_t = numpy.array([])
00052         self.pc_v = numpy.array([])
00053         self.tc_t = numpy.array([])
00054         self.tc_pos = numpy.array([])
00055         self.ts_t = numpy.array([])
00056         self.ts_pos = numpy.array([])
00057 
00058     
00059     
00060     
00061     
00062     
00063     
00064 
00065     def plot(self, save=False):
00066         """ Plot some interesting data from the speed test run.
00067 
00068         When save=True write the figures to PNG files, otherwise show
00069         them interactively.
00070         """
00071         self.plot_summary(save)
00072         self.plot_speed(save)
00073         self.plot_servo(save)
00074         
00075         
00076 
00077     def plot_brake(self, save=False):
00078         """ Plot brake requests and results from the speed test run.
00079 
00080         When save=True write the figure to a PNG file, otherwise show
00081         it interactively.
00082         """
00083         pyplot.hold(False)
00084         pyplot.plot(self.bs_t, self.bs_pos, 'r-', label="brake position")
00085         pyplot.hold(True)
00086         pyplot.plot(self.bc_t, self.bc_pos, 'bd', label="brake request")
00087         pyplot.xlabel("time (s)")
00088         pyplot.title(self.name + " brake")
00089         pyplot.legend(loc="best")
00090         pyplot.axis()
00091         pyplot.grid(True)
00092         self.render_plot(save, "-brake")
00093 
00094     def plot_servo(self, save=False):
00095         """ Plot servo controller requests and results from the speed test.
00096 
00097         When save=True write the figure to a PNG file, otherwise show
00098         it interactively.
00099         """
00100         pyplot.hold(False)
00101         pyplot.title(self.name + " servo")
00102         pyplot.plot(self.bs_t, -self.bs_pos, 'r-', label="-brake")
00103         pyplot.hold(True)
00104         pyplot.plot(self.bc_t, -self.bc_pos, 'rd', label="-request")
00105         pyplot.plot(self.ts_t, self.ts_pos, 'g-', label="throttle")
00106         pyplot.plot(self.tc_t, self.tc_pos, 'gd', label="request")
00107         pyplot.xlabel("time (s)")
00108         pyplot.ylabel("servo position")
00109         pyplot.legend(loc="best")
00110         pyplot.axis()
00111         pyplot.grid(True)
00112         self.render_plot(save, "-servo")
00113 
00114     def plot_speed(self, save=False):
00115         """ Plot speed requests and results from the speed test run.
00116 
00117         When save=True write the figure to a PNG file, otherwise show
00118         it interactively.
00119         """
00120         pyplot.hold(False)
00121         pyplot.title(self.name + " speed")
00122         pyplot.plot(self.od_t, self.od_v, 'b-', label="velocity")
00123         pyplot.hold(True)
00124         pyplot.plot(self.pc_t, self.pc_v, 'rd', label="request")
00125         pyplot.xlabel("time (s)")
00126         pyplot.ylabel("velocity (m/s)")
00127         pyplot.legend(loc="best")
00128         pyplot.axis()
00129         pyplot.grid(True)
00130         self.render_plot(save, "-speed")
00131 
00132     def plot_summary(self, save=False):
00133         """ Plot summary of speed test requests and results.
00134 
00135         When save=True write the figure to a PNG file, otherwise show
00136         it interactively.
00137         """
00138         pyplot.hold(False)
00139         pyplot.axis("off")              
00140         pyplot.title(self.name + " speed summary")
00141 
00142         
00143         pyplot.subplot(211)
00144         pyplot.plot(self.od_t, self.od_v, 'b-', label="velocity")
00145         pyplot.hold(True)
00146         pyplot.plot(self.pc_t, self.pc_v, 'rd', label="request")
00147         pyplot.xlabel("time (s)")
00148         pyplot.ylabel("velocity (m/s)")
00149         pyplot.legend(loc="best")
00150         
00151         pyplot.grid(True)
00152 
00153         
00154         pyplot.subplot(212)
00155         pyplot.hold(False)
00156         pyplot.plot(self.bs_t, -self.bs_pos, 'r-', label="-brake")
00157         pyplot.hold(True)
00158         pyplot.plot(self.bc_t, -self.bc_pos, 'rd', label="-request")
00159         pyplot.plot(self.ts_t, self.ts_pos, 'g-', label="throttle")
00160         pyplot.plot(self.tc_t, self.tc_pos, 'gd', label="request")
00161         pyplot.xlabel("time (s)")
00162         pyplot.ylabel("servo position")
00163         pyplot.legend(loc="best")
00164         
00165         pyplot.grid(True)
00166         self.render_plot(save, "-summary")
00167 
00168     def plot_throttle(self, save=False):
00169         """ Plot throttle requests and results from the speed test run.
00170 
00171         When save=True write the figure to a PNG file, otherwise show
00172         it interactively.
00173         """
00174         pyplot.hold(False)
00175         pyplot.plot(self.ts_t, self.ts_pos, 'g-', label="throttle position")
00176         pyplot.hold(True)
00177         pyplot.plot(self.tc_t, self.tc_pos, 'gd', label="throttle request")
00178         pyplot.xlabel("time (s)")
00179         pyplot.title(self.name + " throttle")
00180         pyplot.legend(loc="best")
00181         pyplot.axis()
00182         pyplot.grid(True)
00183         self.render_plot(save, "-throttle")
00184 
00185     def pwd(self):
00186         "Print current working directory."
00187         return os.getcwd()
00188 
00189     def render_plot(self, save=False, suffix=''):
00190         """Common plot rendering commands.
00191 
00192         When save=True write the figure to a PNG file appending suffix
00193         to the file name.  Otherwise show the plot interactively.
00194 
00195         """
00196         if save:
00197             pylab.savefig(self.name + suffix + ".png")
00198         else:
00199             pyplot.show()
00200 
00201     def set_brake_cmd(self, t, pos):
00202         "set brake command data fields."
00203         self.bc_t =   numpy.append(self.bc_t, t)
00204         self.bc_pos = numpy.append(self.bc_pos, pos)
00205 
00206     def set_brake_state(self, t, pos):
00207         "set brake state data fields."
00208         self.bs_t =   numpy.append(self.bs_t, t)
00209         self.bs_pos = numpy.append(self.bs_pos, pos)
00210 
00211     def set_odometry(self, t, v):
00212         "set odometry data fields."
00213         self.od_t = numpy.append(self.od_t, t)
00214         self.od_v = numpy.append(self.od_v, v)
00215 
00216     def set_pilot_cmd(self, t, v):
00217         "set pilot command data fields."
00218         self.pc_t = numpy.append(self.pc_t, t)
00219         self.pc_v = numpy.append(self.pc_v, v)
00220 
00221     def set_throttle_cmd(self, t, pos):
00222         "set throttle command data fields."
00223         self.tc_t =   numpy.append(self.tc_t, t)
00224         self.tc_pos = numpy.append(self.tc_pos, pos)
00225 
00226     def set_throttle_state(self, t, pos):
00227         "set throttle state data fields."
00228         self.ts_t =   numpy.append(self.ts_t, t)
00229         self.ts_pos = numpy.append(self.ts_pos, pos)
00230         
00231 
00232 def usage(progname):
00233     "Print usage message."
00234     print "\n", progname, """[-h] [ <file.bag> ]
00235 
00236  -h, --help        print this message
00237  -i, --interactive display plots to terminal (default: save as files)
00238 
00239 Run some unit tests of the ART speed data analysis package.
00240  """
00241 
00242 
00243 def main(argv=None):
00244     "Main program, called as a script or interactively."
00245 
00246     if argv is None:
00247         argv = sys.argv                 
00248 
00249     
00250     progname = os.path.basename(argv[0])
00251     if progname is "":
00252         progname = "plot_speed.py"
00253 
00254     
00255     try:
00256         opts, files = getopt.gnu_getopt(argv[1:], 'hi',
00257                                         ('help', 'interactive'))
00258     except getopt.error, msg:
00259         print msg
00260         print "for help use --help"
00261         return 9
00262 
00263     save = True
00264 
00265     for k,v in opts:
00266         if k in ("-h", "--help"):
00267             usage(progname)
00268             return 2
00269         if k in ("-i", "--interactive"):
00270             save = False
00271 
00272     
00273 
00274     return 0
00275 
00276 
00277 if __name__ == "__main__":
00278     
00279     sys.exit(main())