rxplot_main.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2009, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Revision $Id$
00034 
00035 from __future__ import print_function
00036 
00037 import sys
00038 
00039 WXVER = ['2.8', '2.9']
00040 import wxversion 
00041 if wxversion.checkInstalled(WXVER): 
00042     wxversion.select(WXVER) 
00043 else:
00044     sys.stderr.write("This application requires wxPython version %s\n"%(WXVER))
00045     sys.exit(1) 
00046 import wx
00047 
00048 import rospy
00049 import rosgraph.names
00050 
00051 import rxtools.rxplot
00052 
00053 # TODO: poll for rospy.is_shutdown()
00054 def rxplot_main():
00055     from optparse import OptionParser
00056     parser = OptionParser(usage="usage: rxplot [options] /topic/field1 [/topic/field2] [topic/field1:field2:field3]")
00057     parser.add_option("-l", "--legend", type="string",
00058                       dest="legend", default='',
00059                       help="set the legend")
00060     parser.add_option("-p", "--period", type="int",
00061                       dest="period", default=5,
00062                       help="set period displayed in window")
00063     parser.add_option("-m", "--marker", type="string",
00064                       dest="marker", default='None',
00065                       help="set the line marker, e.g. o, ^, .")
00066     parser.add_option("-t", "--title", type="string",
00067                       dest="title", default='RXPlot',
00068                       help="set the title")
00069     parser.add_option("--ymax", type="float",
00070                       dest="maxy", default=0,
00071                       help="set initial y-axis maximum")
00072     parser.add_option("--ymin", type="float",
00073                       dest="miny", default=0,
00074                       help="set initial y-axis minimum")
00075     parser.add_option("-b", "--buffer", type="int",
00076                       dest="buffer", default=-1,
00077                       help="set size of buffer in seconds (default of -1 keeps all data)")
00078     parser.add_option("-r", "--refresh_rate", type="float",
00079                       dest="refresh_rate", default=2,
00080                       help="set refresh rate (default 2hz)")
00081     parser.add_option("-P", "--pause", action="store_true",
00082                       dest="start_paused",
00083                       help="start in paused state")
00084     parser.add_option("-M", "--mode",
00085                       dest="mode", default="2d",
00086                       help="options: \"2d\", \"3d\", or \"scatter\" [default=%default].  "
00087                       "Use \"2d\" to plot all topics vs. time.  "
00088                       "Use \"3d\" or  \"scatter\" to plot in 3D using mplot3d.  "
00089                       "Both \"3d\" and \"scatter\" plot against time if 2 topics"
00090                       "are provided, and against third topic if 3 are provided.  "
00091                       )
00092     options, topics = parser.parse_args(rospy.myargv()[1:])
00093 
00094     if not topics:
00095         parser.error("Please specify a topic field")
00096     if options.mode not in ['2d', '3d', 'scatter']:
00097         parser.error("invalid mode: %s\nValid modes are 2d, 3d, and scatter"%(options.mode))
00098         
00099     topic_list = []
00100     for t in topics:
00101         # c_topics is the list of topics to plot together
00102         c_topics = []
00103         # compute combined topic list, t == '/foo/bar1,/baz/bar2'
00104         for sub_t in [x for x in t.split(',') if x]:
00105             # check for shorthand '/foo/field1:field2:field3'
00106             if ':' in sub_t:
00107                 base = sub_t[:sub_t.find(':')]
00108                 # the first prefix includes a field name, so save then strip it off
00109                 c_topics.append(base)
00110                 if not '/' in base:
00111                     parser.error("%s must contain a topic and field name"%sub_t)
00112                 base = base[:base.rfind('/')]
00113 
00114                 # compute the rest of the field names
00115                 fields = sub_t.split(':')[1:]
00116                 c_topics.extend(["%s/%s"%(base, f) for f in fields if f])
00117             else:
00118                 c_topics.append(sub_t)
00119 
00120         # #1053: resolve command-line topic names
00121         c_topics = [rosgraph.names.script_resolve_name('rxplot', n) for n in c_topics]
00122 
00123         topic_list.append(c_topics)
00124 
00125     #flatten for printing
00126     print_topic_list = []
00127     for l in topic_list:
00128         if type(l) == list:
00129             print_topic_list.extend(l)
00130         else:
00131             print_topic_list.append(l)
00132 
00133     # constrain the number of things we plot by len(COLORS). this is an artificial
00134     # performance hack.
00135     if len(print_topic_list) > len(rxtools.rxplot.COLORS):
00136         parser.error("maximum number of topics that can be plotted is %d"%len(rxtools.rxplot.COLORS))
00137 
00138     if options.mode in ['3d', 'scatter']:
00139         #TODO: need a better spec on what the 3d topic args are. It
00140         #would be nice if the 3d plot were as robust as the normal
00141         #rxplot with respect to having more plots.
00142         if len(print_topic_list) < 2 or len(print_topic_list) > 3:
00143             parser.error("You may only specific 2 or 3 topics with '3d' or 'scatter'.\nWhen 2 topics are provided, time is used as the third axis.")
00144         # for now, cannot multigraph, so unify representation going into plot call
00145         topic_list = [[x] for x in print_topic_list]
00146 
00147     print("plotting topics", ', '.join(print_topic_list))
00148 
00149     rxtools.rxplot.rxplot_app(topic_list, options)


rxtools
Author(s): Josh Faust, Rob Wheeler, Ken Conley
autogenerated on Mon Oct 6 2014 07:25:59