ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org
Ask Your Question
0

NameError: global name 'NAME' is not defined <SOLVED>

asked 2016-10-13 13:56:56 -0500

DannyMB gravatar image

updated 2016-10-18 22:46:11 -0500

Hi people! :) i am listening GPS's data and i want use them in another part of my code, for do Path Planing, but when I try to use them, it give me back this error

I defined variable global (global Lon, Lat) in many places like inside of def listener(), def backlatitud(): and def backlongitud() and it give back the same error.

Traceback (most recent call last):
  File "/home/pi/ros_catkin_ws/src/ceres_pkg/src/Controlador.py", line 25, in <module>
    listener()
  File "/home/pi/ros_catkin_ws/src/ceres_pkg/src/Controlador.py", line 20, in listener
    print Lat
NameError: global name 'Lat' is not defined

This is my code:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
global Lon, Lat

def backlatitud(data1):
    Lat = float(data1.data)
    #rospy.loginfo('Latitud %s', data1.data)
    return;

def backlongitud(data):
    Lon = float(data.data)
    #rospy.loginfo('longitud %s', Lon)
    return;

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("/latitud", String, backlatitud)
    rospy.Subscriber("/longitud", String, backlongitud)
    print Lat
    print Lon
    rospy.spin()

if __name__ == '__main__':
    listener()
edit retag flag offensive close merge delete

Comments

Please format code & console copy/pastes using the Preformatted Text button (the one with 101010 on it) in the future. That makes things much easier to read. Thanks.

gvdhoorn gravatar imagegvdhoorn ( 2016-10-13 14:29:47 -0500 )edit

Also: how is this related to NameError: global name 'NAME' is not defined?

gvdhoorn gravatar imagegvdhoorn ( 2016-10-13 14:30:25 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-10-13 16:57:03 -0500

Mani gravatar image

In your listener() function, Lat and Lon are being accessed prior to any initialisation. If you remove the two print statements before rospy.spin(), your code should work fine.

edit flag offensive delete link more

Comments

Yes, that work fine, but I need work with this variables (Lat and Lon) in another function, how do I call them?

for example def function(): LatNew = 2 * Lat LonNew = 2 * Lon Print LatNew Print LonNew

DannyMB gravatar imageDannyMB ( 2016-10-13 17:40:05 -0500 )edit

I'd recommend you use a class, instead of a flat file with a set of free functions. That would work-around all the issues you are having.

gvdhoorn gravatar imagegvdhoorn ( 2016-10-14 03:19:18 -0500 )edit

thanks @gvdhoom that's true, I did include a class, look at my final code C:\fakepath\Captura.JPG

DannyMB gravatar imageDannyMB ( 2016-10-18 22:39:18 -0500 )edit

Please don't use screenshots to post what is essentially text.

gvdhoorn gravatar imagegvdhoorn ( 2016-10-19 01:43:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-10-13 13:56:56 -0500

Seen: 6,763 times

Last updated: Oct 18 '16