plot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the TU Darmstadt nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import argparse
34 
35 from python_qt_binding import QT_BINDING
36 from python_qt_binding.QtCore import qDebug
37 from rqt_gui_py.plugin import Plugin
38 
39 from rqt_py_common.ini_helper import pack, unpack
40 
41 from .plot_widget import PlotWidget
42 
43 from .data_plot import DataPlot
44 
45 
46 class Plot(Plugin):
47 
48  def __init__(self, context):
49  super(Plot, self).__init__(context)
50  self.setObjectName('Plot')
51 
52  self._context = context
53 
54  self._args = self._parse_args(context.argv())
55  self._widget = PlotWidget(
56  initial_topics=self._args.topics, start_paused=self._args.start_paused)
57  self._data_plot = DataPlot(self._widget)
58 
59  # disable autoscaling of X, and set a sane default range
60  self._data_plot.set_autoscale(x=False)
61  self._data_plot.set_autoscale(y=DataPlot.SCALE_EXTEND | DataPlot.SCALE_VISIBLE)
62  self._data_plot.set_xlim([0, 10.0])
63 
64  self._widget.switch_data_plot_widget(self._data_plot)
65  if context.serial_number() > 1:
66  self._widget.setWindowTitle(
67  self._widget.windowTitle() + (' (%d)' % context.serial_number()))
68  context.add_widget(self._widget)
69 
70  def _parse_args(self, argv):
71  parser = argparse.ArgumentParser(prog='rqt_plot', add_help=False)
72  Plot.add_arguments(parser)
73  args = parser.parse_args(argv)
74 
75  # convert topic arguments into topic names
76  topic_list = []
77  for t in args.topics:
78  # c_topics is the list of topics to plot
79  c_topics = []
80  # compute combined topic list, t == '/foo/bar1,/baz/bar2'
81  for sub_t in [x for x in t.split(',') if x]:
82  # check for shorthand '/foo/field1:field2:field3'
83  if ':' in sub_t:
84  base = sub_t[:sub_t.find(':')]
85  # the first prefix includes a field name, so save then strip it off
86  c_topics.append(base)
87  if not '/' in base:
88  parser.error("%s must contain a topic and field name" % sub_t)
89  base = base[:base.rfind('/')]
90 
91  # compute the rest of the field names
92  fields = sub_t.split(':')[1:]
93  c_topics.extend(["%s/%s" % (base, f) for f in fields if f])
94  else:
95  c_topics.append(sub_t)
96  # 1053: resolve command-line topic names
97  import rosgraph
98  c_topics = [rosgraph.names.script_resolve_name('rqt_plot', n) for n in c_topics]
99  if type(c_topics) == list:
100  topic_list.extend(c_topics)
101  else:
102  topic_list.append(c_topics)
103  args.topics = topic_list
104 
105  return args
106 
107  @staticmethod
108  def add_arguments(parser):
109  group = parser.add_argument_group('Options for rqt_plot plugin')
110  group.add_argument('-P', '--pause', action='store_true', dest='start_paused',
111  help='Start in paused state')
112  group.add_argument('-e', '--empty', action='store_true', dest='start_empty',
113  help='Start without restoring previous topics')
114  group.add_argument('topics', nargs='*', default=[], help='Topics to plot')
115 
116  def _update_title(self):
117  self._widget.setWindowTitle(self._data_plot.getTitle())
118  if self._context.serial_number() > 1:
119  self._widget.setWindowTitle(
120  self._widget.windowTitle() + (' (%d)' % self._context.serial_number()))
121 
122  def save_settings(self, plugin_settings, instance_settings):
123  self._data_plot.save_settings(plugin_settings, instance_settings)
124  instance_settings.set_value('autoscroll', self._widget.autoscroll_checkbox.isChecked())
125  instance_settings.set_value('topics', pack(self._widget._rosdata.keys()))
126 
127  def restore_settings(self, plugin_settings, instance_settings):
128  autoscroll = instance_settings.value('autoscroll', True) in [True, 'true']
129  self._widget.autoscroll_checkbox.setChecked(autoscroll)
130  self._data_plot.autoscroll(autoscroll)
131 
132  self._update_title()
133 
134  if len(self._widget._rosdata.keys()) == 0 and not self._args.start_empty:
135  topics = unpack(instance_settings.value('topics', []))
136  if topics:
137  for topic in topics:
138  self._widget.add_topic(topic)
139 
140  self._data_plot.restore_settings(plugin_settings, instance_settings)
141 
143  self._data_plot.doSettingsDialog()
144  self._update_title()
145 
146  def shutdown_plugin(self):
147  self._widget.clean_up_subscribers()
def __init__(self, context)
Definition: plot.py:48
def save_settings(self, plugin_settings, instance_settings)
Definition: plot.py:122
def _parse_args(self, argv)
Definition: plot.py:70
def shutdown_plugin(self)
Definition: plot.py:146
def _update_title(self)
Definition: plot.py:116
def add_arguments(parser)
Definition: plot.py:108
def restore_settings(self, plugin_settings, instance_settings)
Definition: plot.py:127
def trigger_configuration(self)
Definition: plot.py:142


rqt_plot
Author(s): Dorian Scholz
autogenerated on Sun Mar 17 2019 02:21:35