How to correctly set publishing rate to prevent lag?
I am experiencing lag when publishing markers in a callback and am unsure how to resolve this. When my node is started, it publishes at a similar rate to which it receives data from a subscribed topic in a callback. However, after a minute or two, the publishing rate drops dramatically and is out-of-sync with the data subscribed to. This has been shown using rospy.Time.now()
. The code I have is as follows:
class Example(object):
def __init__(self):
self.sensor_sub = rospy.Subscriber("sensor_data", Int16, self.sensor_callback, queue_size=1)
self.marker_publisher = rospy.Publisher('visualization_marker', Marker, queue_size=100, latch=True)
self.sensor_data = Int16(0)
self.pose = PoseStamped()
self.marker = Marker()
def sensor_callback(self, sensor_msg):
self.sensor_data = sensor_msg.data
self.get_pose()
self.pub_marker(self.pose)
def get_pose(self):
# SOME CODE TO GET POSE (self.pose) #
def pub_marker(self, pose):
# SOME CODE TO DEFINE MARKER #
self.marker_publisher.publish(self.marker)
if __name__ == '__main__':
try:
rospy.init_node('test', anonymous=True)
Example()
rospy.spin()
except rospy.ROSInterruptException:
pass
Any ideas how to solve this lag problem?