Skip to content

Commit

Permalink
rostopic echo: Show stat when --noarr/nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Dec 30, 2015
1 parent 2c2e740 commit 5037d5b
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions tools/rostopic/src/rostopic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,16 @@ class CallbackEcho(object):
def __init__(self, topic, msg_eval, plot=False, filter_fn=None,
echo_clear=False, echo_all_topics=False,
offset_time=False, count=None,
field_filter_fn=None, fixed_numeric_width=None):
field_filter_fn=None, value_transform_fn=None,
fixed_numeric_width=None):
"""
:param plot: if ``True``, echo in plotting-friendly format, ``bool``
:param filter_fn: function that evaluates to ``True`` if message is to be echo'd, ``fn(topic, msg)``
:param echo_all_topics: (optional) if ``True``, echo all messages in bag, ``bool``
:param offset_time: (optional) if ``True``, display time as offset from current time, ``bool``
:param count: number of messages to echo, ``None`` for infinite, ``int``
:param field_filter_fn: filter the fields that are strified for Messages, ``fn(Message)->iter(str)``
:param value_transform_fn: transform the values of Messages, ``fn(Message)->Message``
:param fixed_numeric_width: fixed width for numeric values, ``None`` for automatic, ``int``
"""
if topic and topic[-1] == '/':
Expand Down Expand Up @@ -649,6 +651,7 @@ def __init__(self, topic, msg_eval, plot=False, filter_fn=None,
self.prefix = '\033[2J\033[;H'

self.field_filter=field_filter_fn
self.value_transform=value_transform_fn

# first tracks whether or not we've printed anything yet. Need this for printing plot fields.
self.first = True
Expand All @@ -657,11 +660,19 @@ def __init__(self, topic, msg_eval, plot=False, filter_fn=None,
self.last_topic = None
self.last_msg_eval = None

def custom_strify_message(self, val, indent='', time_offset=None, current_time=None, field_filter=None, type_information=None, fixed_numeric_width=None):
def custom_strify_message(self, val, indent='', time_offset=None, current_time=None, field_filter=None, value_transform=None,
type_information=None, fixed_numeric_width=None):
# ensure to print uint8[] as array of numbers instead of string
if type_information and type_information.startswith('uint8['):
val = [ord(x) for x in val]
return genpy.message.strify_message(val, indent=indent, time_offset=time_offset, current_time=current_time, field_filter=field_filter, fixed_numeric_width=fixed_numeric_width)
if value_transform:
val, value_untransform_fn = value_transform(val)
text = genpy.message.strify_message(val, indent=indent, time_offset=time_offset, current_time=current_time, field_filter=field_filter, fixed_numeric_width=fixed_numeric_width)
if value_transform:
# this operation is necessary because value_transform does change the type of message
# and that causes not strified values next time
val = value_untransform_fn(val)
return text

def callback(self, data, callback_args, current_time=None):
"""
Expand Down Expand Up @@ -714,12 +725,14 @@ def callback(self, data, callback_args, current_time=None):
if self.offset_time:
sys.stdout.write(self.prefix+\
self.str_fn(data, time_offset=rospy.get_rostime(),
current_time=current_time, field_filter=self.field_filter, type_information=type_information, fixed_numeric_width=self.fixed_numeric_width) + \
current_time=current_time, field_filter=self.field_filter, value_transform=self.value_transform,
type_information=type_information, fixed_numeric_width=self.fixed_numeric_width) + \
self.suffix + '\n')
else:
sys.stdout.write(self.prefix+\
self.str_fn(data,
current_time=current_time, field_filter=self.field_filter, type_information=type_information, fixed_numeric_width=self.fixed_numeric_width) + \
current_time=current_time, field_filter=self.field_filter, value_transform=self.value_transform,
type_information=type_information, fixed_numeric_width=self.fixed_numeric_width) + \
self.suffix + '\n')

# we have to flush in order before piping to work
Expand Down Expand Up @@ -1145,25 +1158,44 @@ def eval_fn(m):
parser.error("NUM_WIDTH must be an integer")

field_filter_fn = create_field_filter(options.nostr, options.noarr)
value_transform_fn = create_value_transform(options.nostr, options.noarr)
callback_echo = CallbackEcho(topic, None, plot=options.plot,
filter_fn=filter_fn,
echo_clear=options.clear, echo_all_topics=options.all_topics,
offset_time=options.offset_time, count=msg_count,
field_filter_fn=field_filter_fn, fixed_numeric_width=fixed_numeric_width)
field_filter_fn=field_filter_fn, value_transform_fn=value_transform_fn,
fixed_numeric_width=fixed_numeric_width)
try:
_rostopic_echo(topic, callback_echo, bag_file=options.bag)
except socket.error:
sys.stderr.write("Network communication failed. Most likely failed to communicate with master.\n")

def create_field_filter(echo_nostr, echo_noarr):
def field_filter(val):
def create_value_transform(echo_nostr, echo_noarr):
def value_transform(val):
fields = val.__slots__
field_types = val._slot_types
for f, t in zip(val.__slots__, val._slot_types):
transformed_arrays = []
for f, t in zip(fields, field_types):
if echo_noarr and '[' in t:
continue
val.__setattr__(f, '<array type: %s, length: %s>' %
(t.rstrip('[]'), len(val.__getattribute__(f))))
index = fields.index(f)
val._slot_types[index] = 'string'
transformed_arrays.append((index, t))
elif echo_nostr and 'string' in t:
continue
val.__setattr__(f, '<string length: %s>' % len(val.__getattribute__(f)))
def untransform_fn(val):
for index, type in transformed_arrays:
val._slot_types[index] = type
return val
return val, untransform_fn
return value_transform

def create_field_filter(echo_nostr, echo_noarr):
def field_filter(val):
fields = val.__slots__
field_types = val._slot_types
for f, t in zip(fields, field_types):
yield f
return field_filter

Expand Down

0 comments on commit 5037d5b

Please sign in to comment.