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

Global variable python [closed]

asked 2017-03-30 11:42:22 -0500

mattMGN gravatar image

Hello,

I am facing an error with this script :

class testAvoidance:
    def __init__(self):
        self.sub = rospy.Subscriber('/RosAria/lms1xx_1_laserscan', LaserScan, self.laser_callback)
        rospy.loginfo('LaserScan Subscribed')

    def laser_callback(self, scan):
        global laser_array
        laser_array = scan.ranges

    def update(self):
        global laser_array
        laser_array_we = laser_array[80:460]
        ...

    if __name__ == '__main__':
        rospy.init_node('obstacle_avoidance_node')
        node = testAvoidance()
        global laser_array
        while(1):
            node.update()

And I get the following error :

  File "/home/seekurjr/catkin_ws/src/perso_package/src/obstacle_avoidance.py", line 112, in <module>
node.update()
  File "/home/seekurjr/catkin_ws/src/perso_package/src/obstacle_avoidance.py", line 32, in update
laser_array_we = laser_array[80:460]
NameError: global name 'laser_array' is not defined

I don't understand why 'laser_array' is not considered as global variable as defined in laser_callback. If someone could help me, It would be nice.

Regards,

Matthieu

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant. Please see http://wiki.ros.org/Support for more details. by tfoote
close date 2017-03-30 14:31:14.591425

Comments

1

This is general python programming please post to a general python help forum.

tfoote gravatar image tfoote  ( 2017-03-30 14:31:08 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-03-30 14:36:20 -0500

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.
edit flag offensive delete link more

Comments

Thank you very much Jarvis for this constructive answer.

  • First and fourth note : Yes better practices. Thank you for those advices
  • Third note : I should use something like rospy.Rate ? But how I know if I am not too slow/fast ? Sometimes I have inconsistent values, this could be the reason ?
mattMGN gravatar image mattMGN  ( 2017-03-30 15:49:36 -0500 )edit
  • 2nd note : The aim of this code is to detect and avoid obstacle. It is a really simple algorithm in order to improve my prog skills. I get distance values from lidar (via rosaria node) then I divide those values into several angular segments, in order to compute their respective averages.
mattMGN gravatar image mattMGN  ( 2017-03-30 15:52:39 -0500 )edit

rospy.Rate will do its best to make sure that you're running at a consistent rate. But, you should create a new question for these questions.

jayess gravatar image jayess  ( 2017-03-31 01:05:33 -0500 )edit

Ok, thank you

mattMGN gravatar image mattMGN  ( 2017-03-31 02:20:17 -0500 )edit
lingmaaki gravatar image lingmaaki  ( 2018-09-18 01:49:06 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2017-03-30 11:42:22 -0500

Seen: 4,329 times

Last updated: Mar 30 '17