How to use timer callback in ROS?
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 ?