Printing data from a subscriber is lagging on several topics

asked 2016-09-13 08:08:22 -0600

Miguel Velez gravatar image

updated 2016-09-14 13:00:31 -0600

Hi,

I am subscribing to several topics to print data while running a TurtleBot simulator in Gazebo. I am running it inside of a VM. The topics are /odom, /gazebo/model_states,/ and /amcl_pose. The amcl_pose publishes at an average rate of 2Hz, whereas the others publish at about 800Hz to 1000Hz. When I subscribe to those topics and print the data (I sleep for .5 seconds after printing), the data lags on the /odom and /gazebo/model_states topics, both which publish very fast. This is not a problem in the /amcl_pose since it publishes 2 messages per second. Here is the code that I am using which is similar for all topics, except the data structure and the subscribed topic.

import rospy
import time

from nav_msgs.msg import Odometry

class robot_pose():

    def __init__(self):
        rospy.init_node("robot_odom_pose_monitor")      
            rospy.Subscriber("/odom", Odometry, self.PoseEventCallback, queue_size=1)
        rospy.spin();

    def PoseEventCallback(self,data):
        pose = data.pose.pose
        position = pose.position

        output = "time: " + time.strftime("%Y-%m-%d %H:%M:%S") + " | x: " + str(position.x) + " | y: " + str(position.y)

        rospy.loginfo(output)
        rospy.sleep(0.5)

if __name__ == '__main__':
    try:
        robot_pose()
    except rospy.ROSInterruptException:
        robot_pose.close()
        rospy.loginfo("exception")

As you can see, I am setting the queue_size to be 1, which would mean that new messages that are published should override the single message waiting on the queue when I process the data. Hence, I should be getting the latest message all the time. However, when I run this code, that is not the case. It seems as if I process all of the messages and the data lags from what is currently happening in the simulator.

Any ideas as to why I see this behavior?

Thanks!

edit retag flag offensive close merge delete

Comments

You can see if you're getting every message, and the message lag by printing the sequence number and the timestamp from the header.

ahendrix gravatar image ahendrix  ( 2016-09-13 21:20:48 -0600 )edit

You are right! I do not get consecutive sequence numbers. However, when I compare the sequence numbers of this program with the ones from rostopic echo, the ones I print are also lagging. e.g. echo is in seq: 289324 and I print messages with seq: 288147

Miguel Velez gravatar image Miguel Velez  ( 2016-09-14 13:07:14 -0600 )edit