Python Script global variable [closed]

asked 2017-03-30 11:33:08 -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]
        ...

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 duplicate question by jarvisschultz
close date 2017-03-30 14:13:47.249783

Comments