Go to the documentation of this file.00001
00002 import rospy
00003 from std_msgs.msg import Float64
00004
00005
00006 class FilterMessages(object):
00007 def __init__(self):
00008 rospy.init_node('mean_filter', anonymous=True)
00009
00010 if rospy.has_param('~nb_occ'):
00011 nb_occurence = rospy.get_param('~nb_occ')
00012 else:
00013 nb_occurence = 5
00014 rospy.core.logwarn("N number of occurence provided, using 5 as default")
00015
00016 if rospy.has_param('~in_topic'):
00017 input_topic_param = rospy.get_param('~in_topic')
00018 self.subs = rospy.Subscriber(input_topic_param, Float64, self.callback)
00019 self.pub = rospy.Publisher(input_topic_param+"_filtered", Float64)
00020 else:
00021 rospy.core.logerr("No input topic provided, not starting filtering")
00022 return
00023
00024 self.queue = []
00025 self.queue_len = nb_occurence
00026 self.mean = Float64(0.0)
00027
00028 rospy.spin()
00029
00030 def compute_mean(self):
00031 mean = 0.0
00032 for val in range(self.queue_len):
00033 mean = mean+self.queue[val].data
00034
00035 return mean/self.queue_len
00036
00037
00038 def callback(self, data):
00039
00040 self.queue.append(data)
00041
00042 if len(self.queue) >= self.queue_len:
00043 self.mean = Float64(self.compute_mean())
00044 self.queue.pop(0)
00045
00046 self.pub.publish(self.mean)
00047
00048
00049 if __name__ == '__main__':
00050 FilterMessages()