Re: [ros-users] Timers in rospy?

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: User discussions
Date:  
To: User discussions
Subject: Re: [ros-users] Timers in rospy?
Tim,

Thanks, I was in need of just the same thing today and glad that I
remembered this thread. One thing though, the 2nd line in the
Timer.run() method needs to be changed:

FROM:
        current_expected = rospy.Time.now()
TO:
        current_expected = rospy.Time.now() + self._duration


(the timer's actual operation isn't affected but the 'error' output
from the test program is wrong without the change)

Also I'm wondering why you use time.time() rather than
rospy.Time.now() in a couple places?

Cheers,
Pat

P.S. What's the etiquette on using someone's code from a mailing list
posting in one's own BSD-licensed project? :) Some form of attribution
of course, but is it okay to slap a BSD license header on a source
file containing said code? Is it okay if it says (c) 2011 UC Regents
just like all my other headers?

On Thu, Jan 20, 2011 at 5:24 PM, Tim Field <> wrote:
> Advait,
> Not that I know of.  Here's an example using a similar API to roscpp though:
>
> #!/usr/bin/env python
>
>
> import roslib; roslib.load_manifest('rospy')
> import rospy, threading, time
> class TimerEvent(object):
>     def __init__(self, last_expected, last_real, current_expected,
> current_real, last_duration):
>         self.last_expected    = last_expected
>         self.last_real        = last_real
>         self.current_expected = current_expected
>         self.current_real     = current_real
>         self.last_duration    = last_duration
> class Timer(threading.Thread):
>     def __init__(self, duration, callback, oneshot=False):
>         threading.Thread.__init__(self)
>         self._duration = duration
>         self._callback = callback
>         self._oneshot  = oneshot
>         self.setDaemon(True)
>         self.start()
>     def run(self):
>         r = rospy.Rate(1.0 / self._duration.to_sec())
>         current_expected = rospy.Time.now()
>         last_expected, last_real, last_duration = None, None, None
>         while not rospy.is_shutdown():
>             r.sleep()
>             current_real = rospy.Time.now()
>             start = time.time()
>             self._callback(TimerEvent(last_expected, last_real,
> current_expected, current_real, last_duration))
>             if self._oneshot:
>                 break
>             last_duration = time.time() - start
>             last_expected, last_real = current_expected, current_real
>             current_expected += self._duration
> if __name__ == '__main__':
>     import time
>     rospy.init_node('test')
>     def callback(event):
>         print 'last_expected:        ', event.last_expected
>         print 'last_real:            ', event.last_real
>         print 'current_expected:     ', event.current_expected
>         print 'current_real:         ', event.current_real
>         print 'current_error:        ', (event.current_real -
> event.current_expected).to_sec()
>         print 'profile.last_duration:', event.last_duration
>         if event.last_real:
>             print 'last_error:           ', (event.last_real -
> event.last_expected).to_sec(), 'secs'
>         print
>     Timer(rospy.Duration(2), callback)
>     time.sleep(5)
>
> On Thu, Jan 20, 2011 at 4:59 PM, Advait Jain <> wrote:
>>
>> Is there some similar to Timers
>> (http://www.ros.org/wiki/roscpp/Overview/Timers) in rospy?
>>
>> Thanks,
>> Advait
>> _______________________________________________
>> ros-users mailing list
>>
>> https://code.ros.org/mailman/listinfo/ros-users
>
>
> _______________________________________________
> ros-users mailing list
>
> https://code.ros.org/mailman/listinfo/ros-users
>
>