Ros timing issue
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.
CODE:
#!/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__':
start()
laser_scanner()
robot_mon()
rospy.spin()
OUTPUT:
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