ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

rospy function scheduler

asked 2012-06-01 00:40:00 -0600

Yianni gravatar image

Hello,

Is there a ropsy scheduler for calling a function after a period of time (or periodically calling it every n secs). The python sched module is kind of misbehaving, I guess it conflicts with ros based on the following example using the ps joystick with the PR2:

#!/usr/bin/env python
import roslib; roslib.load_manifest('d3')
import rospy
import sched, time
from sensor_msgs.msg import Joy

class Joystick(object):
    def __init__(self, topic_name='/joy'):
        self.topic_name = topic_name
        self.buttons_names = {'l1':10, 'l2':8}
        self.sub = rospy.Subscriber(topic_name, Joy, self.cb)
        self.blocked = False
        self.sched = sched.scheduler(time.time, time.sleep)
        print '>>> press L1 button'

    def cb(self, data):
        print '>> in callback:', data.buttons[self.buttons_names['l1']], data.buttons[self.buttons_names['l2']]
        if not self.blocked:
            self.foo(data)

    def foo(self, data):
        print '>> in foo:', data.buttons[self.buttons_names['l1']], data.buttons[self.buttons_names['l2']]
        if data.buttons[self.buttons_names['l1']]:
            self.blocked = True
            rospy.loginfo('%d', data.buttons[self.buttons_names['l1']])
            rospy.loginfo('L1 pressed')
            self.sched.enter(1, 1, self.bar, ())
        self.sched.run()

    def bar(self):
        rospy.loginfo('resuming control')
        self.blocked = False

if __name__=='__main__':
    rospy.init_node('foo', anonymous=False)
    joy = Joystick()
    rospy.spin()

Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2012-06-01 03:26:01 -0600

A Timer should do what you want.

edit flag offensive delete link more

Comments

Thanks. Works like a charm.

Yianni gravatar image Yianni  ( 2012-06-01 04:22:28 -0600 )edit

Question Tools

Stats

Asked: 2012-06-01 00:40:00 -0600

Seen: 1,075 times

Last updated: Jun 01 '12