GPS Python模块错误

时间:2012-12-22 20:51:04

标签: python gps raspberry-pi gpsd

我正在尝试在rpi上使用this script。如果gpsd正在运行并且我从链接的博客文章运行脚本,则会收到以下错误:

  File "/home/zzz/Timelapse/staticgps.py", line 29, in <module>
    gpsp = GpsPoller() # create the thread
  File "/home/zzz/Timelapse/staticgps.py", line 19, in __init__
    gpsd = gps.gps(mode=WATCH_ENABLE) #starting the stream of info
NameError: global name 'gps' is not defined

知道出了什么问题吗?谢谢!

编辑:这是我要求的脚本。它是链接的直接复制/粘贴。

#! /usr/bin/python
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0

import os
from gps import *
from time import *
import time
import threading

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

class GpsPoller(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        global gpsd #bring it in scope
        gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.current_value = None
        self.running = True #setting the thread running to true

    def run(self):
        global gpsd
        while gpsp.running:
            gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer

if __name__ == '__main__':
    gpsp = GpsPoller() # create the thread
    try:
        gpsp.start() # start it up
        while True:
            #It may take a second or two to get good data
            #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc

            os.system('clear')

            print
            print ' GPS reading'
            print '----------------------------------------'
            print 'latitude    ' , gpsd.fix.latitude
            print 'longitude   ' , gpsd.fix.longitude
            print 'time utc    ' , gpsd.utc,' + ', gpsd.fix.time
            print 'altitude (m)' , gpsd.fix.altitude
            print 'eps         ' , gpsd.fix.eps
            print 'epx         ' , gpsd.fix.epx
            print 'epv         ' , gpsd.fix.epv
            print 'ept         ' , gpsd.fix.ept
            print 'speed (m/s) ' , gpsd.fix.speed
            print 'climb       ' , gpsd.fix.climb
            print 'track       ' , gpsd.fix.track
            print 'mode        ' , gpsd.fix.mode
            print
            print 'sats        ' , gpsd.satellites

            time.sleep(5) #set to whatever

    except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
        print "\nKilling Thread..."
        gpsp.running = False
        gpsp.join() # wait for the thread to finish what it's doing
    print "Done.\nExiting."

4 个答案:

答案 0 :(得分:2)

您没有正确复制代码;链接页面有这一行:

gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info

请注意,它只是gps(),而不是gps.gps();在脚本的顶部,gps模块中的所有名称都被导入到当前名称空间中,使gps()成为本地名称。

确保执行在脚本顶部显示语句from gps import *,从错误消息中看出您没有正确导入它(NameError表示在您的脚本中导入了名为gps nothing

答案 1 :(得分:0)

将WATCH_ENABLE替换为1,它可以正常工作。 我不确定为什么不会暴露这个常量(新手) - 它在gps模块的client.py中声明

答案 2 :(得分:0)

如果有人偶然遇到同样的问题,那是因为您的脚本文件名为gps.py。只需将其重命名为其他名称,导入即可正常工作。

答案 3 :(得分:0)

将脚本命名为 gps.py 不仅会导致无法定义 gps,而且您也无法将其命名为 gps1.py。将其重命名为 mygps.py 之类的内容