Global variable python [closed]
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
This is general python programming please post to a general python help forum.