Created
November 23, 2016 10:53
-
-
Save spaghetti-/cfddb46c2873d96b475ae542c84406cd to your computer and use it in GitHub Desktop.
fit a random rostopic data to a distribution over time
This file contains hidden or 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
| import rospy | |
| import rostopic | |
| import sys | |
| import scipy | |
| import scipy.stats | |
| data = [] | |
| def cb(msg, field): | |
| field = field.strip().split('.') | |
| datum = msg | |
| for f in field: | |
| datum = getattr(datum, f) | |
| data.append(datum) | |
| def calculate(dist='norm'): | |
| _d = getattr(scipy.stats, dist) | |
| print "mu: %0.9f, stddev: %0.9f" % _d.fit(data) | |
| def usage(): | |
| print """ | |
| usage: ./stats topic_name field_name duration | |
| """ | |
| if __name__ == '__main__': | |
| if len(sys.argv) < 4: | |
| usage() | |
| sys.exit(1) | |
| rospy.init_node('parameter_estimator', anonymous=True) | |
| msg_class, real_topic, _ = rostopic.get_topic_class(sys.argv[1]) | |
| sub = rospy.Subscriber(real_topic, msg_class, lambda s: cb(s, sys.argv[2])) | |
| timer = rospy.Timer(rospy.Duration(int(sys.argv[3])), | |
| lambda _: (sub.unregister(), | |
| calculate(), | |
| rospy.signal_shutdown('done')), | |
| oneshot=True) | |
| rospy.spin() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment