Created
August 7, 2023 06:24
-
-
Save srathi-monarch/ed1576ab03dbac300b5945f42e0133cf to your computer and use it in GitHub Desktop.
Node to count the number of messages on topics.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
from sys import argv | |
import rospy | |
def cb(msg, cb_args): | |
counters, i = cb_args | |
counters[i] += 1 | |
def print_counts(ev): | |
topics = print_counts._topics | |
counters = print_counts._counters | |
print("{" + ', '.join((f"'{t}': {c}" for t, c in zip(topics, counters))), "}") | |
def main(): | |
rospy.init_node("wc", argv, anonymous=True) | |
if len(argv) <= 1: | |
rospy.logfatal(f"Usage:\n$ {argv[0]} /topic_name_1 _param:=value1 /topic2/name __ns:=a_ns") | |
topics = [] | |
counters = [] | |
subs = [] | |
for i, topic in enumerate(argv[1:]): | |
topics.append(topic) | |
counters.append(0) | |
subs.append(rospy.Subscriber(topic, rospy.AnyMsg, queue_size=1000, callback=cb, callback_args=(counters, i))) | |
print_counts._topics = topics | |
print_counts._counters = counters | |
logger = rospy.Timer(rospy.Duration(1, 0), print_counts) | |
try: | |
rospy.spin() | |
except Exception as e: | |
print(e) | |
finally: | |
print(zip(topics, counters)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment