Ask Your Question
0

NameError: global name 'NAME' is not defined

asked 2016-10-12 09:08:43 -0500

DannyMB gravatar image

updated 2016-10-12 10:34:38 -0500

hello, I'm trying to publish data of my GPS but it appears this error

NameError: global name 'hora' is not defined

Anybody help me please :)

My code is this:

#!/usr/bin/env python
import rospy
import serial
import pynmea2
import roslib; roslib.load_manifest('ceres_pkg')
from std_msgs.msg import String
from geometry_msgs.msg import Point

serialStream = serial.Serial("/dev/ttyAMA0",9600,timeout=0.5)

 class GPS(object):
    def __init__(self):
        self.pub_ = rospy.Publisher("/dat_gps", Point, queue_size = 1)
        return
    def start(self):
        sentence = serialStream.readline()
        global hora,lat,lon
        if sentence.find('GGA') > 0:
            data = pynmea2.parse(sentence)
            hora = data.timestamp
            lat = data.latitude
            lon = data.longitude
        pos_msg = Point(hora, lat, lon)
        loop_rate = rospy.Rate(1)
        while not rospy.is_shutdown():
            self.pub_.publish(pos_msg);
            loop_rate.sleep()
        return

if __name__ == '__main__':
    rospy.init_node('GPS_py', log_level=rospy.INFO)
    rospy.loginfo("%s: starting GPS node", rospy.get_name())
    Gps = GPS()
    Gps.start()
edit retag flag offensive close merge delete

Comments

1

could you post the actual error output with a bit of context?

mgruhler gravatar imagemgruhler ( 2016-10-12 09:17:25 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-10-12 13:30:56 -0500

The error should occur in the case that sentence.find('GGA') > 0 evaluates to False. Then, although being declared, hora is not defined, i.e., didn't get assigned any value.

edit flag offensive delete link more

Comments

Thanks! that was the solution.

DannyMB gravatar imageDannyMB ( 2016-10-13 12:13:44 -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-12 09:08:43 -0500

Seen: 1,671 times

Last updated: Oct 12 '16