timers.py
Go to the documentation of this file.
00001 # Based on Tim Field's code posted to ros-users. 
00002 # See: http://ros-users.122217.n3.nabble.com/Timers-in-rospy-td2298885.html
00003 
00004 # Software License Agreement (BSD License)
00005 #
00006 #  Copyright (c) 2011, Willow Garage
00007 #  All rights reserved.
00008 #
00009 #  Redistribution and use in source and binary forms, with or without
00010 #  modification, are permitted provided that the following conditions
00011 #  are met:
00012 #
00013 #   * Redistributions of source code must retain the above copyright
00014 #     notice, this list of conditions and the following disclaimer.
00015 #   * Redistributions in binary form must reproduce the above
00016 #     copyright notice, this list of conditions and the following
00017 #     disclaimer in the documentation and/or other materials provided
00018 #     with the distribution.
00019 #   * Neither the name of the University of California nor the names of its
00020 #     contributors may be used to endorse or promote products derived
00021 #     from this software without specific prior written permission.
00022 #
00023 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034 #  POSSIBILITY OF SUCH DAMAGE.
00035 
00036 import roslib; roslib.load_manifest('rospy')
00037 import rospy, threading, time
00038 
00039 class TimerEvent(object):
00040     def __init__(self, last_expected, last_real, current_expected, current_real, last_duration):
00041         self.last_expected    = last_expected
00042         self.last_real        = last_real
00043         self.current_expected = current_expected
00044         self.current_real     = current_real
00045         self.last_duration    = last_duration
00046 
00047 class Timer(threading.Thread):
00048     def __init__(self, duration, callback, oneshot=False):
00049         threading.Thread.__init__(self)
00050         self._duration = duration
00051         self._callback = callback
00052         self._oneshot  = oneshot
00053         self.setDaemon(True)
00054         self.start()
00055 
00056     def run(self):
00057         r = rospy.Rate(1.0 / self._duration.to_sec())
00058         current_expected = rospy.Time.now() + self._duration
00059         last_expected, last_real, last_duration = None, None, rospy.Duration(0)
00060         while not rospy.is_shutdown():
00061             r.sleep()
00062             current_real = rospy.Time.now()
00063             #start = time.time()
00064             start = rospy.Time.now()
00065             self._callback(TimerEvent(last_expected, last_real, current_expected, current_real, last_duration))
00066             if self._oneshot:
00067                 break
00068             #last_duration = time.time() - start
00069             last_duration = rospy.Time.now() - start
00070             last_expected, last_real = current_expected, current_real
00071             current_expected += self._duration
00072 
00073 if __name__ == '__main__':
00074     import time
00075     rospy.init_node('test')
00076     def callback(event):
00077         print 'last_expected:        ', event.last_expected
00078         print 'last_real:            ', event.last_real
00079         print 'current_expected:     ', event.current_expected
00080         print 'current_real:         ', event.current_real
00081         print 'current_error:        ', (event.current_real - event.current_expected).to_sec()
00082         print 'profile.last_duration:', event.last_duration.to_sec()
00083         if event.last_real:
00084             print 'last_error:           ', (event.last_real - event.last_expected).to_sec(), 'secs'
00085         print
00086 
00087     Timer(rospy.Duration(0.05), callback)
00088     time.sleep(15)


starmac_roslib
Author(s): Patrick Bouffard
autogenerated on Sun Jan 5 2014 11:38:14