Ask Your Question
0

How to correctly set publishing rate to prevent lag?

asked 2021-03-18 06:44:40 -0500

Py gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-03-18 07:58:58 -0500

Roberto Z. gravatar image

updated 2021-03-18 08:09:24 -0500

I don't know the details of your situation. However if you only care about the latest data measurement and you are fine if older, not yet sent values, get dropped, then try setting the publishers queue_size to 1. Like so:

self.marker_publisher = rospy.Publisher('visualization_marker', Marker, queue_size=1, latch=True)

With queue_size=100 you are avoiding having missing data and potentially accumulating visualization markers yet to be published.

Additionally, what is the rate of your incoming messages? You could also consider if reducing the rate at which sensor_data is being published is an valid option.

edit flag offensive delete link more

Comments

Thanks for this. It certainly improved results changing queue_size to 1. The rate for sensor_data has been reduced to 3, which also helped as it was previously at 100. However, a lag still builds up. Any other ideas how this can be managed?

Py gravatar image Py  ( 2021-03-18 10:18:01 -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

2 followers

Stats

Asked: 2021-03-18 06:44:40 -0500

Seen: 84 times

Last updated: Mar 18 '21