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

Very slow update of global variables - python

asked 2015-12-04 09:30:12 -0500

Oevli gravatar image

Hi

I have made a node which takes input from several different sensors. I want to proces the sensor data and publish a on a new topic.

However when I move the data from the callback functions to my processing algorithm the using global variables, the variables are updated very slowly.

I have a very simplified node of what I have done...

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from std_msgs.msg import Float64

ranges = []

def callback(data):
     global ranges
     rospy.loginfo(data)
     ranges = data.ranges[200]
def listener():
     rospy.init_node('listener', anonymous=True)
     rospy.Subscriber("scan", LaserScan, callback)      
if __name__ == '__main__':
      while not rospy.is_shutdown():
           listener()
           rospy.Publisher('talk', Float64, queue_size=5).publish(ranges)
           rospy.sleep(0.01)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-12-04 11:37:42 -0500

gvdhoorn gravatar image

updated 2015-12-04 11:38:49 -0500

if __name__ == '__main__':
      while not rospy.is_shutdown():
           listener()
           rospy.Publisher('talk', Float64, queue_size=5).publish(ranges)
           rospy.sleep(0.01)

You are re-creating your subscriber every 0.01 seconds, which is probably not what you want.

However when I move the data from the callback functions to my processing algorithm the using global variables, the variables are updated very slowly.

I can imagine that the chances of ROS successfully registering your subscriber, receiving a message and delivering it to your callback in 0.01 sec are rather slim, and only succeeds every now and then. If that leads to most messages being missed, it would appear as if updates to ranges are very slow. In reality, they are probably very infrequent.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-12-04 09:30:12 -0500

Seen: 1,020 times

Last updated: Dec 04 '15