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

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
5

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 image aks  ( 2018-09-24 07:09:07 -0500 )edit

Glad I could be of help :)

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

This helps, Thank you

nihalsoans91 gravatar image nihalsoans91  ( 2020-05-11 17:14:19 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 10,014 times

Last updated: Sep 24 '18