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

Revision history [back]

This really isn't a ROS question at all, this is a Python question. You'd have better luck looking on generic programming support sites (like stack overflow). The issue is that the first time the testAvoidance.update method gets called, the variable has never actually been defined. The only place in the code you posted that the variable gets defined is in the testAvoidance.laser_callback method, but almost certainly, the update method will get called first. You could easily solve this by adding a definition to main:

rospy.init_node('obstacle_avoidance_node')
global laser_array
laser_array = [0]*460
node = testAvoidance()

Other notes:

  • You should be careful about accessing elements [80:460] of the array. What happens if you get a published topic that has fewer than 460 ranges in it? This is why I defined the variable to be 460 zeros in the above snippet.
  • Would have probably been a good idea to include the information above the definition of your code. What imports do you have? Did you define any variables in the module's global namespace?
  • Be careful about calling the update function as fast as you can. Is this really necessary?
  • I'd replace the while(1): line with while not rospy.is_shutdown(): to help the node shutdown more reliably.