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

Ros timing issue

asked 2015-01-13 03:06:53 -0500

robospace gravatar image

updated 2015-01-13 03:07:40 -0500

We did a simple code to subscribe to laser ranges and velocity of a robot in ros stage. But the order of execution of the functions seems to be different. Is this a problem in timing? The python code and output is given below.


 #!/usr/bin/env python

import rospy                           
import sensor_msgs.msg
from geometry_msgs.msg import Twist

ranges = []

def start():
    rospy.init_node('robot_values', anonymous = True)

def laser_scanner():
    rospy.Subscriber('base_scan', sensor_msgs.msg.LaserScan,laser_processor)

def laser_processor(data):
    global ranges
    ranges = data.ranges
    print "ranges[0] = ",ranges[0]
    print "ranges[1] = ",ranges[1]

def robot_mon():
    rospy.Subscriber('cmd_vel', Twist, robot_velocity)

def robot_velocity(data):
    all_velocities = data
    linear_velocity = all_velocities.linear.x
    angular_velocity = all_velocities.angular.z
    print "linear_velocity = ",linear_velocity 
    print "\n"

if __name__ == '__main__':


ranges[0] =  5.0
ranges[1] =  5.0
ranges[0] =  5.0
ranges[1] =  5.0
linear_velocity =  0.044

ranges[0] =  5.0
ranges[1] =  5.0
linear_velocity =  0.044

linear_velocity =  0.044

ranges[0] =  5.0
ranges[1] =  5.0
linear_velocity =  0.044

ranges[0] =  5.0
ranges[1] =  5.0
ranges[0] =  5.0
ranges[1] =  5.0
linear_velocity =  0.044
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-01-13 03:41:01 -0500

Depending on how the data you subscribe to is published, you might get some jitter in the order of how your callbacks are called. You should not rely on the order in which your callbacks are called too much, as ROS communication does not provide strict guarantees here. For instance, if you transmit data over WiFi a small message that originally got published after a bigger message might arrive at another node earlier due to packet loss or latency.

edit flag offensive delete link more


Thanks for the reply. Is there any way to correct it using Time in ROS. For example, by selecting a rate at which we subscribe to the different topics.

robospace gravatar image robospace  ( 2015-01-13 22:01:07 -0500 )edit

Most messages contain have a "stamp" field in the standardized header which can (and should) be used to do timestamp based operations. You cannot specify a "subscription rate", but you can republish to another topic at slower rate or do processing in your callback and throw away messages.

Stefan Kohlbrecher gravatar image Stefan Kohlbrecher  ( 2015-01-15 08:13:33 -0500 )edit

Is there any examples showing the use of the "stamp" field of the messages?

robospace gravatar image robospace  ( 2015-01-15 12:15:29 -0500 )edit

Question Tools

1 follower


Asked: 2015-01-13 03:06:53 -0500

Seen: 312 times

Last updated: Jan 13 '15