clock_server.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 '''
00003 Created on Mar 16, 2015
00004  \todo Add license information here
00005 @author: dnad
00006 '''
00007 from __future__ import print_function
00008 
00009 import rospy
00010 from std_msgs.msg import Float64
00011 
00012 from functools import partial
00013            
00014 class ClockServer:
00015     def __init__(self):
00016         # Get the clock topics which should be tracked
00017         # Populate the subscribers
00018         topics = rospy.get_param("~track_clocks", "")
00019         self.create_subs(topics)
00020                         
00021         # Start the clock publisher
00022         self.ctime = rospy.Publisher("clock",Float64,queue_size=1)         
00023                 
00024     def create_subs(self, topics):
00025         self.diff_pubs = {}
00026         for topic in topics:
00027             self.diff_pubs[topic] = rospy.Publisher("clock_diff" + topic, Float64, queue_size=1)
00028             rospy.Subscriber(topic, Float64, partial(self.on_time, topic))
00029     
00030     def on_time(self, name, data):
00031         dt = Float64()
00032         dt.data = data.data - rospy.Time.now().to_sec()
00033         self.diff_pubs[name].publish(dt)
00034         
00035     def start(self):
00036         rate = rospy.Rate(1.0)
00037         msg = Float64()
00038         while not rospy.is_shutdown():    
00039             msg.data = rospy.Time.now().to_sec()
00040             self.ctime.publish(msg)
00041             rate.sleep()
00042 
00043 
00044 if __name__ == '__main__':
00045     rospy.init_node("clock_server")
00046     srv = ClockServer()
00047     srv.start()
00048    


snippets
Author(s): Gyula Nagy
autogenerated on Fri Aug 28 2015 11:22:33