Ask Your Question
0

How to use timer callback in ROS?

asked 2018-09-24 04:57:35 -0500

aks gravatar image

updated 2018-09-24 07:38:42 -0500

I am working with ROS Kinetic. Like in ROS2, we can use a create_timer() function as described here.

Similarly I would want to use a similar functionality in rospy in ROS. Something that is mentioned here in point 3.

Can anyone give an example how to use it ?

I am trying something like this in my class constructor but it never enters the callback function :

class DemoNode():
    def __init__(self):
        print("entering constructor")
        #super().__init__('custom_talker')
        self.pub = rospy.Publisher('topic',customMessage, queue_size=10)
        self.counter = 1
        print("creating a publisher")
        self.timer = rospy.Timer(rospy.Duration(1), self.demo_callback)
        print("timer called")

    def demo_callback(self):
        print("entering callback")
        msg = DetectedObject()
        print("Reading the message correctly")
        msg.id = self.counter
        counter += 1
        rospy.loginfo("Publishing message {}".format(msg.id))
        pub.publish(msg)

if __name__ == '__main__':

    print("entering main")
    rospy.init_node('custom_talker')
    try:
        DemoNode()
        print("entering Try")
    except rospy.ROSInterruptException:
        print("exception thrown")
        pass

Is it correct ? The node is running but it never enters the callback function

Also, it is mentioned that this Timer is supported in ROS 1.5+ . What is exactly that ? Does Kinetic already fulfill this condition ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-09-24 07:04:31 -0500

Choco93 gravatar image
class DemoNode():
  def __init__(self):
    print("entering constructor")
    #super().__init__('custom_talker')
    self.pub = rospy.Publisher('topic',customMessage, queue_size=10)
    self.counter = 1
    print("creating a publisher")
    self.timer = rospy.Timer(rospy.Duration(1), self.demo_callback)
    print("timer called")

  def demo_callback(self, timer):
    print("entering callback")
    msg = DetectedObject()
    print("Reading the message correctly")
    msg.id = self.counter
    self.counter += 1
    rospy.loginfo("Publishing message {}".format(msg.id))
    self.pub.publish(msg)

if __name__ == '__main__':

  print("entering main")
  rospy.init_node('custom_talker')
  try:
    DemoNode()
    print("entering Try")
    rospy.spin()
  except rospy.ROSInterruptException:
    print("exception thrown")
    pass

This should work, you were missing rospy.spin() that was main issue, and alos timer in callback, as it needs 2 arguments(including self). Also self was missing at 2 places in demo_callback.

edit flag offensive delete link more

Comments

worked like a charm. Thankyou :)

aks gravatar imageaks ( 2018-09-24 07:09:07 -0500 )edit

Glad I could be of help :)

Choco93 gravatar imageChoco93 ( 2018-09-24 07:20:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-09-24 04:57:35 -0500

Seen: 645 times

Last updated: Sep 24 '18