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

NameError: global name 'NAME' is not defined

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

DannyMB gravatar image

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

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 image mgruhler  ( 2016-10-12 09:17:25 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

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 image DannyMB  ( 2016-10-13 12:13:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-12 09:08:43 -0600

Seen: 3,899 times

Last updated: Oct 12 '16